• 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
0
Question by tpennetta · Mar 03, 2014 at 06:39 PM · shadertexturesurfaceshadersplatmaps

Vertex Colors as Splat Map for Terrain Texture colors blown out

Hi All,

I am writing a custom surface shader to use the RGB channels of a mesh's Vertex Colors to be used as a splat map for a terrain mesh. So far the shader "works" except where this is a color blended in 2 or more color channels.

I have attached the code, the image of the result inside of Unity, and the result inside of Blender using Vertex Colors as the splat map. As you can see, where there is both a R, and a G channel in the vertex colors, the texture blending gets blown out, yellow and not the grass texture" of a pure green vertex color.

Any help is greatly appreciated:

 Shader "Vertex Color Splat Surf Shader" {
     Properties {
         _Splat1 ("Base (RGB)", 2D) = "white" {}
         _Splat2 ("Base (RGB)", 2D) = "white" {}
         _Splat3 ("Base (RGB)", 2D) = "white" {}
     }
     SubShader {
         Tags { "RenderType"="Transparent" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf BlinnPhong
         
         sampler2D _Splat1;
         sampler2D _Splat2;
         sampler2D _Splat3;
  
         struct Input {
             float2 uv_Splat1;
             float2 uv_Splat2;
             float2 uv_Splat3;
             float4 color: Color; // Vertex color
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             half4 splat1 = tex2D (_Splat1, IN.uv_Splat1);
             half4 splat2 = tex2D (_Splat2, IN.uv_Splat2);
             half4 splat3 = tex2D (_Splat3, IN.uv_Splat3);
             half4 vc = normalize(IN.color);
             o.Albedo = splat1.rgb * vc.r; // vertex RGB
             o.Albedo += splat2.rgb * vc.g;
             o.Albedo += splat3.rgb * vc.b;
             o.Albedo = saturate(o.Albedo);
             o.Alpha = vc.a;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }


Unity Blown Out Colors Unity Blown Out Colors

Blender Correct Version Blender Correct Colors

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
0
Best Answer

Answer by tpennetta · Mar 03, 2014 at 07:33 PM

Anyone wondering about a fix for this problem, it was the blending equation. Blending based on multiplication caused the blow out. Instead, I had to lerp between each channel in the vertex color map.

The code is below:

 Shader "Vertex Color Splat Surf Shader" {
     Properties {
         _Splat1 ("Base (RGB)", 2D) = "white" {}
         _Splat2 ("Base (RGB)", 2D) = "white" {}
         _Splat3 ("Base (RGB)", 2D) = "white" {}
     }
     SubShader {
         Tags { "RenderType"="Transparent" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf BlinnPhong
         
         sampler2D _Splat1;
         sampler2D _Splat2;
         sampler2D _Splat3;
  
         struct Input {
             float2 uv_Splat1;
             float2 uv_Splat2;
             float2 uv_Splat3;
             float4 color: Color; // Vertex color
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             half4 splat1 = tex2D (_Splat1, IN.uv_Splat1);
             half4 splat2 = tex2D (_Splat2, IN.uv_Splat2);
             half4 splat3 = tex2D (_Splat3, IN.uv_Splat3);
             fixed3 albedo = lerp(splat1.rgb, splat2.rgb, IN.color.g);
             albedo = lerp(albedo, splat3.rgb, IN.color.b);
             
             o.Albedo = albedo;
 
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }

Now there is a much nicer look and feel here: alt text

Comment
Add comment · Show 1 · 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 ThreeDeeNut · Sep 13, 2014 at 10:55 PM 0
Share

Hey tpennetta! I was really excited to find this. I am working on getting a custom mesh from Blender into Unity to use as my terrain and I am trying to solve how I am going to make a splat map that I can use in Unity. I really appreciate you posting the shader code but I was hoping you could elaborate more on the workflow you used to go from painting vertex colors on your model in Blender, to getting different textures to appear on the model in Blender from the vertex color information, to getting the model in Unity and getting the splat map setup using your shader. Thanks in advance for your time and help. Bryson

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Visible seams on borders when tiling texture 1 Answer

Help with adding a normalmap to a shader 1 Answer

Two Textures Dissolve Shader (clip & lerp) 1 Answer

Texture2D to Texture3D 2 Answers

Unity Terrain shader on non-terrain objects 0 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