Search Unity

How to add Albedo(color setting) to Mobile-VertexLit

Discussion in 'Shaders' started by twinmatrix, Jul 6, 2018.

  1. twinmatrix

    twinmatrix

    Joined:
    Sep 6, 2012
    Posts:
    38
    I'm trying to edit the Mobile/VertexLit shader so that I can set the albedo (main color) of the shader. However, I'm failing miserably. It seems albedo has to be set from a surface shader and when I try to combine that, I get all sorts of errors.

    Can someone please do this simple edit and show me how it should be?

    Here is the Mobile-VertexLit code:

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. // Simplified VertexLit shader. Differences from regular VertexLit one:
    4. // - no per-material color
    5. // - no specular
    6. // - no emission
    7.  
    8. Shader "Mobile/VertexLit" {
    9. Properties {
    10.     _MainTex ("Base (RGB)", 2D) = "white" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags { "RenderType"="Opaque" }
    15.     LOD 80
    16.  
    17.     // Non-lightmapped
    18.     Pass {
    19.         Tags { "LightMode" = "Vertex" }
    20.  
    21.         Material {
    22.             Diffuse (1,1,1,1)
    23.             Ambient (1,1,1,1)
    24.         }
    25.         Lighting On
    26.         SetTexture [_MainTex] {
    27.             constantColor (1,1,1,1)
    28.             Combine texture * primary DOUBLE, constant // UNITY_OPAQUE_ALPHA_FFP
    29.         }
    30.     }
    31.  
    32.     // Lightmapped
    33.     Pass
    34.     {
    35.         Tags{ "LIGHTMODE" = "VertexLM" "RenderType" = "Opaque" }
    36.  
    37.         CGPROGRAM
    38.  
    39.         #pragma vertex vert
    40.         #pragma fragment frag
    41.         #pragma target 2.0
    42.         #include "UnityCG.cginc"
    43.         #pragma multi_compile_fog
    44.         #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
    45.  
    46.         float4 _MainTex_ST;
    47.  
    48.         struct appdata
    49.         {
    50.             float3 pos : POSITION;
    51.             float3 uv1 : TEXCOORD1;
    52.             float3 uv0 : TEXCOORD0;
    53.             UNITY_VERTEX_INPUT_INSTANCE_ID
    54.         };
    55.  
    56.         struct v2f
    57.         {
    58.             float2 uv0 : TEXCOORD0;
    59.             float2 uv1 : TEXCOORD1;
    60.         #if USING_FOG
    61.             fixed fog : TEXCOORD2;
    62.         #endif
    63.             float4 pos : SV_POSITION;
    64.  
    65.             UNITY_VERTEX_OUTPUT_STEREO
    66.         };
    67.  
    68.         v2f vert(appdata IN)
    69.         {
    70.             v2f o;
    71.             UNITY_SETUP_INSTANCE_ID(IN);
    72.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    73.  
    74.             o.uv0 = IN.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    75.             o.uv1 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    76.  
    77.         #if USING_FOG
    78.             float3 eyePos = UnityObjectToViewPos(IN.pos);
    79.             float fogCoord = length(eyePos.xyz);
    80.             UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
    81.             o.fog = saturate(unityFogFactor);
    82.         #endif
    83.  
    84.             o.pos = UnityObjectToClipPos(IN.pos);
    85.             return o;
    86.         }
    87.  
    88.         sampler2D _MainTex;
    89.  
    90.         fixed4 frag(v2f IN) : SV_Target
    91.         {
    92.             fixed4 col;
    93.             fixed4 tex = UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.uv0.xy);
    94.             half3 bakedColor = DecodeLightmap(tex);
    95.  
    96.             tex = tex2D(_MainTex, IN.uv1.xy);
    97.             col.rgb = tex.rgb * bakedColor;
    98.             col.a = 1.0f;
    99.  
    100.             #if USING_FOG
    101.             col.rgb = lerp(unity_FogColor.rgb, col.rgb, IN.fog);
    102.             #endif
    103.  
    104.             return col;
    105.         }
    106.  
    107.         ENDCG
    108.     }
    109.  
    110.     // Pass to render object as a shadow caster
    111.     Pass
    112.     {
    113.         Name "ShadowCaster"
    114.         Tags { "LightMode" = "ShadowCaster" }
    115.  
    116.         ZWrite On ZTest LEqual Cull Off
    117.  
    118.         CGPROGRAM
    119.         #pragma vertex vert
    120.         #pragma fragment frag
    121.         #pragma target 2.0
    122.         #pragma multi_compile_shadowcaster
    123.         #include "UnityCG.cginc"
    124.  
    125.         struct v2f {
    126.             V2F_SHADOW_CASTER;
    127.             UNITY_VERTEX_OUTPUT_STEREO
    128.         };
    129.  
    130.         v2f vert( appdata_base v )
    131.         {
    132.             v2f o;
    133.             UNITY_SETUP_INSTANCE_ID(v);
    134.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    135.             TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    136.             return o;
    137.         }
    138.  
    139.         float4 frag( v2f i ) : SV_Target
    140.         {
    141.             SHADOW_CASTER_FRAGMENT(i)
    142.         }
    143.         ENDCG
    144.     }
    145. }
    146. }
    147.  
     
  2. roseportalgames

    roseportalgames

    Joined:
    Aug 9, 2017
    Posts:
    173
    Anyone? Or just advice on how to do it is fine. Then I'll try again to write it. :)
     
  3. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    You can try adding

    Code (csharp):
    1. _Color ("Tint", Color) = (1,1,1,1)
    below _MainTex ("Base (RGB)", 2D) = "white" {}

    Then, right below sampler2D _MainTex; you add

    Code (csharp):
    1. fixed4 _Color;
    Finally, you replace return col; with

    Code (csharp):
    1. return col * _Color;
    Let me know how it goes.
     
  4. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    88
    Did you ever get this to work? I'm trying to figure this out myself!
     
  5. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    88
    The above suggestions don't wind up changing the color so something must be missing or I'm doing something wrong. Here is my shader code:

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. // Simplified VertexLit shader. Differences from regular VertexLit one:
    4. // - no per-material color
    5. // - no specular
    6. // - no emission
    7.  
    8. Shader "Mobile/VertexLit-Color" {
    9. Properties {
    10.     _MainTex ("Base (RGB)", 2D) = "white" {}
    11.     _Color("Tint", Color) = (1,1,1,1)
    12. }
    13.  
    14. SubShader {
    15.     Tags { "RenderType"="Opaque" }
    16.     LOD 80
    17.  
    18.     // Non-lightmapped
    19.     Pass {
    20.         Tags { "LightMode" = "Vertex" }
    21.  
    22.         Material {
    23.             Diffuse (1,1,1,1)
    24.             Ambient (1,1,1,1)
    25.         }
    26.         Lighting On
    27.         SetTexture [_MainTex] {
    28.             constantColor (1,1,1,1)
    29.             Combine texture * primary DOUBLE, constant // UNITY_OPAQUE_ALPHA_FFP
    30.         }
    31.     }
    32.  
    33.     // Lightmapped
    34.     Pass
    35.     {
    36.         Tags{ "LIGHTMODE" = "VertexLM" "RenderType" = "Opaque" }
    37.  
    38.         CGPROGRAM
    39.  
    40.         #pragma vertex vert
    41.         #pragma fragment frag
    42.         #pragma target 2.0
    43.         #include "UnityCG.cginc"
    44.         #pragma multi_compile_fog
    45.         #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
    46.  
    47.         float4 _MainTex_ST;
    48.  
    49.         struct appdata
    50.         {
    51.             float3 pos : POSITION;
    52.             float3 uv1 : TEXCOORD1;
    53.             float3 uv0 : TEXCOORD0;
    54.             UNITY_VERTEX_INPUT_INSTANCE_ID
    55.         };
    56.  
    57.         struct v2f
    58.         {
    59.             float2 uv0 : TEXCOORD0;
    60.             float2 uv1 : TEXCOORD1;
    61.         #if USING_FOG
    62.             fixed fog : TEXCOORD2;
    63.         #endif
    64.             float4 pos : SV_POSITION;
    65.  
    66.             UNITY_VERTEX_OUTPUT_STEREO
    67.         };
    68.  
    69.         v2f vert(appdata IN)
    70.         {
    71.             v2f o;
    72.             UNITY_SETUP_INSTANCE_ID(IN);
    73.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    74.  
    75.             o.uv0 = IN.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    76.             o.uv1 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    77.  
    78.         #if USING_FOG
    79.             float3 eyePos = UnityObjectToViewPos(IN.pos);
    80.             float fogCoord = length(eyePos.xyz);
    81.             UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
    82.             o.fog = saturate(unityFogFactor);
    83.         #endif
    84.  
    85.             o.pos = UnityObjectToClipPos(IN.pos);
    86.             return o;
    87.         }
    88.  
    89.         sampler2D _MainTex;
    90.         fixed4 _Color;
    91.  
    92.         fixed4 frag(v2f IN) : SV_Target
    93.         {
    94.             fixed4 col;
    95.            
    96.             //is this next line right? It's not working!
    97.             col *= _Color;
    98.            
    99.             fixed4 tex = UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.uv0.xy);
    100.             half3 bakedColor = DecodeLightmap(tex);
    101.  
    102.             tex = tex2D(_MainTex, IN.uv1.xy);
    103.             col.rgb = tex.rgb * bakedColor;
    104.             col.a = 1.0f;
    105.            
    106.             #if USING_FOG
    107.             col.rgb = lerp(unity_FogColor.rgb, col.rgb , IN.fog);
    108.             #endif
    109.  
    110.             return col;
    111.         }
    112.  
    113.         ENDCG
    114.     }
    115.  
    116.     // Pass to render object as a shadow caster
    117.     Pass
    118.     {
    119.         Name "ShadowCaster"
    120.         Tags { "LightMode" = "ShadowCaster" }
    121.  
    122.         ZWrite On ZTest LEqual Cull Off
    123.  
    124.         CGPROGRAM
    125.         #pragma vertex vert
    126.         #pragma fragment frag
    127.         #pragma target 2.0
    128.         #pragma multi_compile_shadowcaster
    129.         #include "UnityCG.cginc"
    130.  
    131.         struct v2f {
    132.             V2F_SHADOW_CASTER;
    133.             UNITY_VERTEX_OUTPUT_STEREO
    134.         };
    135.  
    136.         v2f vert( appdata_base v )
    137.         {
    138.             v2f o;
    139.             UNITY_SETUP_INSTANCE_ID(v);
    140.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    141.             TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    142.             return o;
    143.         }
    144.  
    145.         float4 frag( v2f i ) : SV_Target
    146.         {
    147.             SHADOW_CASTER_FRAGMENT(i)
    148.         }
    149.         ENDCG
    150.     }
    151. }
    152. }
    153.  
     
  6. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    88
    return col * _Color; doesn't seem to do anything.
     
  7. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You are making a few mistakes here I think - instead just c+p things, look for a few moments what's happening there; you are setting the color value and a few lines below you overwrite the change. Instead, make it something like this.

    Code (CSharp):
    1. tex = tex2D(_MainTex, IN.uv1.xy);
    2.             col.rgb = tex.rgb * bakedColor;
    3.             col *= _Color;
    4.             col.a = 1.0f;
    I'm not familiar with this shader, but looks like there's // Lightmapped comment, so I just tried to flip "Static" on in the object's Inspector panel and color changes just fine. I assume that pass only works for lightmapped objects.
     
  8. ZolnierGames

    ZolnierGames

    Joined:
    Feb 19, 2018
    Posts:
    88
    Yeah, adding that line is something I tried and it's still not updating the color of the texture. I feel like this shouldn't be so difficult, but nothing I've tried is working. What is it about that mobile vertex-lit shader?
     
  9. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Did you set the object "Static" in the inspector panel? If you did, it should have worked. I tried it myself and it worked just fine in 2018.x.
     
  10. Blixem-Media

    Blixem-Media

    Joined:
    Mar 4, 2015
    Posts:
    6
    I also wasn't able to get this to work without a light map. But you can set the Ambient to the color property's value as an alternative to tint your models. This is an alternative solution can be used in some use cases.

    Add a color property.
    _Color ("Tint", Color) = (1,1,1,1)


    And set the Ambient to the color value in Material.

    Material {
    Diffuse (1,1,1,1)
    Ambient [_Color]
    }