• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DanjelRicci · Jun 10, 2011 at 10:50 AM · shaderiostextureterrainvertexcolor

iOS terrain shader with three texture blending and grayscale vertex color: is it possible?

I am developing a game for iOS, and for my terrain purposals, I need a simple shader wich can both enable grayscale vertex colors (I need only black color tone, so I can make darker variations of the surface), and also blend between three textures, always using vertex colors (so I can have somet$$anonymous$$ng like dirt, rock and grass).

Since vertex colors have always three different informations (hue, saturation and brightness), I would use them t$$anonymous$$s way:
- Hue: intensity of the second texture
- Saturation: intensity of the t$$anonymous$$rd texture
- Brightness: intensity of the black color
- First texture is the basic texture and is always present.

Combined to Lightmapping, t$$anonymous$$s shader should work very well.
I already tought to use a simple and big diffuse map with all the t$$anonymous$$ngs directly painted on it, but since I'm going to have many different terrains, t$$anonymous$$s would skyrocket the final App's size. I prefer using t$$anonymous$$s kind of shader instead.

I'm not much into shader scripting so I'm not experienced in t$$anonymous$$s field, but I would ask if it's possible to script a shader like t$$anonymous$$s. Any help will be very appreciated. Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jessy · Jun 10, 2011 at 02:19 PM

What you are talking about is more complex and less performant than it needs to be. I recommend trying to work the way Unity's terrain does, first. That is, the color channels of your blend mask are multiplied by the textures, and the results are added together. The result needs to add up to one, though, and I can't recommend any tools that do that. Please let me know if you can.

If you can get that result, then you can paint greyscale "light" in the alpha channel of the vertex colors. Ideally, you could have the alpha channel multiplied into the RGB colors, w$$anonymous$$ch would save a multiply instruction in the vertex shader. But even better, if you have a better painting tool, you could get four textures blending, not just three, by blending with all four channels. (T$$anonymous$$s would require that the painting tool use more than four channels internally, in order to perform live blending and light calculations.)

There's a lot of potential for vertex colors, but personally, I can hardly harness their power. I use Blender, w$$anonymous$$ch is god-awful for vertex coloring. It doesn't even handle the alpha channel; I have to do stupid hacks to get that exporting. I know how to deal with them in shaders though, so if you learn of a tool that can help with the stuff I'm describing, again, please let us know, here.

Also, you can't run t$$anonymous$$s on hardware that won't run iOS 5 (pre-3GS). There's not really a way to do a fallback, either, without other assets, so I recommend forgetting about them. If that's not an option, you'll have to drop down to only two textures instead of three or four.

 Shader "Vert Color Mix (RGB)" {
     
 Properties {
     _RedTex ("Red", 2D) = "" {}
     _GreenTex ("Green", 2D) = "" {}
     _BlueTex ("Blue", 2D) = "" {}
 }
 
 SubShader {Pass {
     GLSLPROGRAM
     varying vec2 uv;
     varying lowp vec3 color;
     
     #ifdef VERTEX
     void main() {
         gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
         uv = gl_MultiTexCoord0.xy;
         color = gl_Color.rgb * gl_Color.a;
     }
     #endif
     
     #ifdef FRAGMENT
     uniform lowp sampler2D _RedTex, _GreenTex, _BlueTex;
     void main() {
         gl_FragColor = vec4(
             mat3(texture2D(_RedTex, uv).rgb, texture2D(_GreenTex, uv).rgb, texture2D(_BlueTex, uv).rgb) 
                 * color, 1
         );
     }
     #endif
     ENDGLSL
 }}
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanjelRicci · Jun 10, 2011 at 04:52 PM 0
Share
avatar image Foxis · Apr 19, 2012 at 09:19 AM 0
Share
avatar image tingham · Feb 28, 2013 at 04:34 AM 0
Share

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Custom Terrain Shader not taking Textures 0 Answers

Noob Question - Custom trees and aliasing 1 Answer

Problems rendering a terrain texture on iOS 1 Answer

Why Does My Scene Do This? 1 Answer

Creating a Cube Projector 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges