0

I am learning a basic lambert lighting model shader, the shader scripts:

Shader "Unlit/NewUnlitShader"
{

    Properties{
        _MainColor("MainColor",Color)=(1,1,1,1)
    }

    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "UnityLightingCommon.cginc"

            struct v2f
            {
                float4 pos:SV_POSITION;
                fixed4 dif:COLOR0;
                
            };

            fixed4 _MainColor;

            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                //normal vector
                float3 n = UnityObjectToWorldNormal(v.normal);
                n=normalize(n);

                //lighting direction vector
                fixed3 l = normalize(_WorldSpaceLightPos0.xyz);

                //calculate lambert
                fixed ndotl = dot(n,l);
                o.dif = _LightColor0*_MainColor*saturate(ndotl);

                return  o;

                
            }

            fixed4 frag(v2f i):SV_Target
            {
                return  i.dif;
            }


            
            ENDCG
        }
    }
}

Something strange happen: When I create a material from this shader and assign the material to a sphere, the sphere always show black:

the left is urp/lit and the right is my shader,the shader previews is black

When I create a new project and install urp, I copy my shader to this new project, things works correct:

performs correct

So I guess I may have made some wrong settings in the old project,could anyone help me figure that out? Thanks!

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • The second one looks completely different regarding lighting .. seems to have some ambient lights the first one doesn't have e.g. ? You are using `_LightColor0` which might be different in the projects? – derHugo Jun 02 '23 at 10:19
  • you are right, when I modified `o.dif = _LightColor0*_MainColor*saturate(ndotl);` to `o.dif = _LightColor0;` the first one still showes black, and the second one's color is consistent with directional light color,which is white. But I also checked the directional light color of the first project, which was also white. – ConnonsWonderland Jun 02 '23 at 13:11

0 Answers0