Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mobile shader that ignores light color

Discussion in 'Shaders' started by jlanisdev, Aug 11, 2018.

  1. jlanisdev

    jlanisdev

    Joined:
    Jan 18, 2016
    Posts:
    76
    I am looking for a basic mobile shader (similar to Mobile/VertexLit or Mobile/Diffuse) which is affected by light and shadows, but just not the color of the light (including any color from ambient environment lighting). How I would I do this?

    I have the following shader, which turns the directional light color to grayscale, but I can't seem to get it to work correctly with ambient environment colors.

    Code (CSharp):
    1. Shader "Example/Diffuse Texture" {
    2.         Properties {
    3.             _MainTex ("Texture", 2D) = "white" {}
    4.         }
    5.         SubShader {
    6.         Tags { "RenderType" = "Opaque" }
    7.         CGPROGRAM
    8.           #pragma surface surf SimpleLambert
    9.  
    10.  
    11.           half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
    12.               half NdotL = dot (s.Normal, lightDir);
    13.               half4 c;
    14.               c.rgb = s.Albedo * dot( _LightColor0.rgb * (NdotL * atten), float3(0.22, 0.707, 0.071) );
    15.               c.a = s.Alpha;
    16.               return c;
    17.           }
    18.  
    19.         struct Input {
    20.             float2 uv_MainTex;
    21.         };
    22.      
    23.         sampler2D _MainTex;
    24.      
    25.         void surf (Input IN, inout SurfaceOutput o) {
    26.             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    27.         }
    28.         ENDCG
    29.         }
    30.         Fallback "Diffuse"
    31.     }
     
    Last edited: Aug 11, 2018
  2. jlanisdev

    jlanisdev

    Joined:
    Jan 18, 2016
    Posts:
    76
    I figured it out myself, here is the solution for anyone who needs something similar (ignores only the color of the light - in other words, converts light colors to greyscale):

    Code (CSharp):
    1. Shader "Example/Diffuse Texture" {
    2.         Properties {
    3.             _Color("Color",COLOR)=(0.5,0.5,0.5,1.0)
    4.             _MainTex ("Texture", 2D) = "white" {}
    5.         }
    6.         SubShader {
    7.         Tags { "RenderType" = "Opaque" }
    8.         CGPROGRAM
    9.           #pragma surface surf SimpleLambert lightModel noambient
    10.  
    11.            sampler2D _MainTex;
    12.            fixed4 _Color;
    13.  
    14.  
    15.           half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
    16.               half NdotL = dot (s.Normal, lightDir);
    17.               half4 c;
    18.               c.rgb = _Color * (dot(s.Albedo * UNITY_LIGHTMODEL_AMBIENT, float3(0.22, 0.707, 0.071)) + dot( s.Albedo * _LightColor0.rgb * (NdotL * atten), float3(0.22, 0.707, 0.071) ));
    19.               c.a = s.Alpha;
    20.               return c;
    21.           }
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.         };
    26.      
    27.      
    28.         void surf (Input IN, inout SurfaceOutput o) {
    29.             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    30.         }
    31.         ENDCG
    32.         }
    33.         Fallback "Diffuse"
    34.     }
     
    Last edited: Aug 11, 2018