• 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
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 something like dirt, rock and grass).

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

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

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

Comment
tingham

People who like this

1 Show 0
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
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, which 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. (This 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, which 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 this 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
burnumd
DanjelRicci

People who like this

2 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

Thank you very much for the exhaustive reply. Unfortunately I just noticed that C4D, wich I use with Unity, simply doesn't have any vertex painting function... This is the first time I'm looking for a vertex painting tool on C4D, and since my previous software (3DS Max) had it, I really didn't expect C4D to not have vertex paint. I'm on a Mac, so I really can't use 3DS now.

The shader seems to work on iPod Touch 4 tough, but I can't try its vertex paint features. Sorry. :( It would be perfect if some one else could try it and let us know.

For now I'm going the painted-diffuse way. Result is visually great, but since textures are 2048x2048 each, I will end up to heve my App to be about 50 extra Megabytes... Hopefully this won't be a big problem.

Thanks again!

avatar image Foxis · Apr 19, 2012 at 09:19 AM 0
Share

Sorry for the ignorance, but would this run under the vertexlit rendering path...?

avatar image tingham · Feb 28, 2013 at 04:34 AM 0
Share

This shader is great but it doesn't support lighting at all. Can we get an update that includes a lighting pass? (Per vertex and ambient would be great.)

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

Creating a Cube Projector 2 Answers

How do I make the same texture look the same on terrain or objects 0 Answers

Custom Terrain Textures 0 Answers

Terrain texture low quality 0 Answers

Graphics.DrawTexture with Shader not working on iOS? 1 Answer


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