Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why my simple shader doesn't get vertex Color?

Discussion in 'Editor & General Support' started by ksam2, Jan 3, 2015.

  1. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
    Hi there, sorry for the noob question but I've create a simple surface shader and put a texture and a normal map for it but can't add vertex Color to it! I want to change the main color but just don't know how.

    Here is my code
    Code (CSharp):
    1. Shader "My_Shaders/FirstShader" {
    2.  
    3. Properties {
    4.    _Color ("Main Color", Color) = (1,1,1,0.5)
    5.    _MainTex ("Texture", 2D) = "white" {}
    6.    _BumpMap ("Bumpmap", 2D) = "bump" {}
    7. }
    8.  
    9.  
    10. Subshader {
    11. Tags { "RenderType" = "Opaque" }
    12.  
    13.     CGPROGRAM
    14.      #pragma surface surf Lambert
    15.  
    16.     struct Input {
    17.  
    18.      float4 color : COLOR;
    19.      float2 uv_MainTex;
    20.      float2 uv_BumpMap;
    21.  
    22.     };
    23.  
    24.  
    25.     sampler2D _MainTex;
    26.     sampler2D _BumpMap;
    27.        
    28. void surf (Input IN, inout SurfaceOutput o) {
    29.      
    30.          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    31.          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    32.         // What should I add here to get it to work ?
    33.     }
    34.     ENDCG
    35.  
    36.  
    37.     }
    38.  
    39.  
    40.       Fallback "Diffuse"
    41.  
    42. }
    43.  
    44.  
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Line 18 is where you declare the vertex color. Now you certainly need to use it!

    Line 30 may e.g. be replace with something like this:
    Code (csharp):
    1. o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * IN.color.rgb;
     
    ksam2 likes this.
  5. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
    Still can't change the Main Color
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Warning: Untested

    Code (csharp):
    1. Shader "My_Shaders/FirstShader" {
    2.    Properties {
    3.      _Color ("Main Color", Color) = (1,1,1,0.5) // Tint
    4.      _MainTex ("Texture", 2D) = "white" {}
    5.      _BumpMap ("Bumpmap", 2D) = "bump" {}
    6.    }
    7.  
    8.    Subshader {
    9.      Tags { "RenderType" = "Opaque" }
    10.  
    11.      CGPROGRAM
    12.      #pragma surface surf Lambert
    13.  
    14.      struct Input {
    15.        float4 color : COLOR; // Vertex Color
    16.        float2 uv_MainTex;
    17.        float2 uv_BumpMap;
    18.      };
    19.  
    20.      float4 _Color; // Tint
    21.      sampler2D _MainTex;
    22.      sampler2D _BumpMap;
    23.  
    24.      void surf (Input IN, inout SurfaceOutput o) {
    25.        o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * IN.color.rgb * _Color; // Texture * Vertex Color * Tint
    26.        o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    27.      }
    28.      ENDCG
    29.  
    30.    }
    31.    Fallback "Diffuse"
    32. }
     
    ksam2 likes this.
  7. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
    Great, Worked
    Thanks a lot
    Thanks a lot :)