Custom Shader - Shadow distance changes color (Solved)

Hello, Im having an issue with my custom shader changing the color of the object when inside and outside of the Quality’s shadow distance setting.

In this picture I set one chunk of grass to the default shader provided by unity and it works fine. If anyone may know why this is happening, I’m all ears :slight_smile:

Shader

Shader "Custom/Cubes" {
   
     Properties {
         _Color("Colour (RGBA)", Color) = (0,0,0,1)
         _Strength("Strength",Range(0, 0.5))=0.01
         _Darken("Darken", Range(0,1))=0
         _RenderDistance("Render Detail Distance", Float)=35
     }

     SubShader {
         Tags {
             "RenderType"="Opaque"
         }

         LOD 200
         Cull Back

         CGPROGRAM
         #pragma surface surf Standard
         float4 _Color;
         float _Strength;
         float _Darken;
         float _RenderDistance;

         struct Input {
             float3 worldPos;
         };
        
         float rand(float3 myVector)  {
             return frac(sin(dot(myVector, float3(12.9898,78.233,45.5432))) * 43758.5453);
         }
        
         void surf (Input IN, inout SurfaceOutputStandard o) {
            float3 vWPos = IN.worldPos;
             float dist = distance(_WorldSpaceCameraPos, vWPos);

             if(dist <= _RenderDistance) {
                 float randNoise = rand(round(vWPos * 2)) * _Strength;
                 float3 noise = float3(randNoise, randNoise, randNoise);
                 o.Albedo = (_Darken == 0 ? noise : -noise) + _Color.xyz;
             } else {
                 o.Albedo = _Color.xyz;
             }

             o.Alpha = 1;
             o.Smoothness = 0.25f;
         }

         ENDCG
     }

     FallBack "Diffuse"
}
1 Like

Solved it. If anyone is curious to the solution.

Shader "Custom/Cubes" {
   
    Properties {
       _Color("Colour (RGBA)", Color) = (0,0,0,1)
       _MainTex ("Albedo (RGB)", 2D) = "white" {}
       _Strength("Strength",Range(0, 0.5))=0.01
       _Darken("Darken", Range(0,1))=0
       _RenderDistance("Render Detail Distance", Float)=35
    }

    SubShader {
        Tags {
            "RenderType"="Opaque"
        }

        Lighting On
        LOD 200
        Cull Back

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows
        #pragma target 3.0

        float4 _Color;
        sampler2D _MainTex;
        float _Strength;
        float _Darken;
        float _RenderDistance;

        struct Input {
            float2 uv_MainTex;
            float3 worldPos;
        };
        
        float rand(float3 myVector)  {
            return frac(sin(dot(myVector, float3(12.9898,78.233,45.5432))) * 43758.5453);
        }
        
        void surf (Input IN, inout SurfaceOutputStandard o) {
           float3 vWPos = IN.worldPos;
           float dist = distance(_WorldSpaceCameraPos, vWPos);

           fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;

            if(dist <= _RenderDistance) {
               float randNoise = rand(round(vWPos * 2)) * _Strength;
               float3 noise = float3(randNoise, randNoise, randNoise);
               o.Albedo = (_Darken == 0 ? noise : -noise) + o.Albedo;
            }

            o.Alpha = 1;
            o.Smoothness = 0.25f;
        }

        ENDCG
    }

    FallBack "Diffuse"
}
1 Like