Search Unity

Difference between float4 _LightColor0 and float4 _WorldSpaceLightPos0 ?

Discussion in 'Shaders' started by frankfringe, Sep 10, 2019.

  1. frankfringe

    frankfringe

    Joined:
    Feb 9, 2019
    Posts:
    106
    Hi, I have the following code in my shader:

    Code (CSharp):
    1.             float4 _LightColor0;
    2.             //float4 _WorldSpaceLightPos0;
    3.  
    It works this way. As soon as I comment out
     _LightColor0;
    the Shader won't compile anymore (it gives me "undeclared identifier _LightColor0"). I can use
    _WorldSpaceLightPos0;
    without declaring it first. Even more, as soon as I uncomment
    //float4 _WorldSpaceLightPos0;
    in the above, I get "redefinition of
    _WorldSpaceLightPos0
    ".

    What is the difference between these two global Unity Variables? Why do I have to declare one of them before using it while I am not allowed to declare the other one before using it?

    For completeness, here is the complete shader I am using

    Code (CSharp):
    1. Shader "Unlit/Displacement/3. Specular"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _UOffset("U Offset", float) = 0
    7.         _UMultiplicator("U Multiplikator", float) = 0.1
    8.         _NoiseAmplitude("Noise Amplitude", float) = 1
    9.         _WaterColor("Water Color", Color) = (0.2, 0.2, 0.35, 1)
    10.         _Shininess("Shininess", float) = 1
    11.     }
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 100
    16.  
    17.         Pass
    18.         {
    19.             Tags {"LightMode" = "ForwardBase"}
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             // make fog work
    24.             #pragma multi_compile_fog
    25.  
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.                 float3 normal : NORMAL;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float2 uv : TEXCOORD0;
    38.                 UNITY_FOG_COORDS(1)
    39.                 float4 vertex : SV_POSITION;
    40.                 float4 col : COLOR;
    41.             };
    42.            
    43.             float4 _LightColor0;
    44.             //float4 _WorldSpaceLightPos0;
    45.  
    46.             sampler2D _MainTex;
    47.             float4 _MainTex_ST;
    48.             float _UMultiplicator;
    49.             float _UOffset;
    50.             float _NoiseAmplitude;
    51.             float4 _WaterColor;
    52.             float Shininess;
    53.  
    54.             v2f vert (appdata v)
    55.             {
    56.                 v2f o;
    57.                 //noise addieren
    58.                 fixed4 noise = tex2Dlod(_MainTex, float4(v.uv.x*_UMultiplicator, v.uv.y, 0, 0));
    59.                 v.vertex.xyz += noise.rgb;
    60.                
    61.                 float3 normalDirection = UnityObjectToWorldNormal(v.normal);
    62.                 //directional lighting
    63.                 float atten = 1.0;
    64.                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    65.                 float3 diffuseReflection = atten * _LightColor0.xyz * max(0.0, dot(normalDirection, lightDirection));
    66.                 //add ambient lighting
    67.                 float3 lightFinal = diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
    68.                
    69.                 o.col = float4(lightFinal * _WaterColor, 1.0);
    70.                 //normales positionen veraendern
    71.                 o.vertex = UnityObjectToClipPos(v.vertex);
    72.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    73.                 UNITY_TRANSFER_FOG(o,o.vertex);
    74.                 return o;
    75.             }
    76.  
    77.             fixed4 frag (v2f i) : SV_Target
    78.             {
    79.                 // sample the texture
    80.                 fixed4 col = i.col;
    81.                 // apply fog
    82.                 UNITY_APPLY_FOG(i.fogCoord, col);
    83.                 return col;
    84.             }
    85.             ENDCG
    86.         }
    87.     }
    88. }
    89.  
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    "_WorldSpaceLightPos0" included from "UnityShaderVariables.cginc" through "UnityCG.cginc".
     
  3. frankfringe

    frankfringe

    Joined:
    Feb 9, 2019
    Posts:
    106
    Ah ok, after checking https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html again I saw that
    _LightPos0
    is actually declared in
    Lighting.cginc
    , which I did not include.

    Do I understand this right: If I want to use one of the globals here https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html, I can either declare it myself or include the file in which it is declared?
    Is there any runtime-cost associated with declaring globals but not using them (if I include
    Lighthing.cginc
    , but don't use much of it, will Unity still set all the globals in there every frame for the shader?)