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

_WorldSpaceLightPos0 giving no position information for point lights.

Discussion in 'Shaders' started by Eliotz, Jul 20, 2020.

  1. Eliotz

    Eliotz

    Joined:
    Jul 18, 2015
    Posts:
    81
    Hey, I have a question about shader writing in the Cg language.

    I'm a little confused as to how this variable(_WorldSpaceLightPos0) works. In the docs, it says that it should return the direction when the light is set to directional, but otherwise, it should return a position.

    And the direction works flawlessly. But when I switch my only light in the scene to a point light. I get all 0's as values.

    To debug this I simply return the _WorldSpaceLightPos0 in my fragment function. If it's a directional light, I get colors, representing the direction. If it's a point light, I get just black, no matter where I move the light.

    I'm lost and have no idea how to fix this. Maybe I'm missing a line of code or something.

    Here's the full code for proof.

    Code (CSharp):
    1. Shader "ShaderTest/LitBlinnPhong"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _SurfaceColor("Surface Color", Color) = (1,1,1,1)
    7.         _Ambient("Ambient Light", Color) = (0,0,0,0)
    8.         _Gloss("Glossiness", Range(1, 10)) = 1
    9.         _RimPower("Rim Light", Range(0,1)) = 1
    10.         _Point("Point", Vector) = (0,0,0,0)
    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.  
    24.                 #include "UnityCG.cginc"
    25.                 #include "UnityLightingCommon.cginc"
    26.  
    27.                 float _Gloss;
    28.                 float _RimPower;
    29.                 float4 _SurfaceColor;
    30.                 float4 _Ambient;
    31.                 float4 _Point;
    32.  
    33.                 struct appdata
    34.                 {
    35.                     float4 vertex : POSITION;
    36.                     float3 normal : NORMAL;
    37.                     float2 uv : TEXCOORD0;
    38.                 };
    39.  
    40.                 struct v2f
    41.                 {
    42.                     float4 vertex : SV_POSITION;
    43.                     float3 worldNormal : COLOR0;
    44.                     float3 viewDir : COLOR1;
    45.                     float2 uv : TEXCOORD0;
    46.                 };
    47.  
    48.                 v2f vert(appdata v)
    49.                 {
    50.                     v2f o;
    51.  
    52.                     o.vertex = UnityObjectToClipPos(v.vertex);
    53.                     o.worldNormal = UnityObjectToWorldNormal(v.normal);
    54.                     o.viewDir = WorldSpaceViewDir(v.vertex);
    55.                     return o;
    56.                 }
    57.  
    58.                 fixed4 frag(v2f i) : SV_Target
    59.                 {
    60.  
    61.                     float4 lightCol = _LightColor0;
    62.  
    63.                     float3 n = normalize(i.worldNormal);
    64.                     float3 v = normalize(i.viewDir);
    65.                     float3 l = _WorldSpaceLightPos0.xyz;
    66.                     float3 r = reflect(-l, n);
    67.                     float3 h = normalize(l + v);
    68.  
    69.                     float3 lambert = float3(0, 0, 0);
    70.                     float3 blinn = float3(0, 0, 0);
    71.                     float3 fresnel = float3(0, 0, 0);
    72.  
    73.                     float lightDist = 1 - distance(_Point, i.worldNormal);
    74.                     float atten = 1.0;
    75.  
    76.                     float gloss = _Gloss * _Gloss;
    77.                     float roughness = _Gloss / 10;
    78.  
    79.                     if (_WorldSpaceLightPos0.w == 0)
    80.                     {
    81.                         lambert = clamp(dot(n, l), 0, 1);
    82.                         blinn = pow(max(0, dot(h, n)), gloss) * lambert;
    83.                         fresnel = clamp(1 - pow(dot(n, v), 0.5), 0, 1) * lambert;
    84.                     }
    85.                     else
    86.                     {
    87.                         //lightDist = distance(n, l);
    88.                         //atten = 1 / lightDist;
    89.  
    90.                         //lambert = atten;
    91.                     }
    92.  
    93.                     float4 lambertLight = float4(lambert, 1) * _SurfaceColor;
    94.                     float4 blinnLight = float4(blinn, 1);
    95.                     float4 fresnelLight = float4(fresnel, 1) * _RimPower * roughness;
    96.  
    97.                     //float4 light = (blinnLight + lambertLight + fresnelLight) * lightCol;
    98.                     float4 light = float4(lambert, 1);
    99.  
    100.                     float4 color = float4(1, 1, 1, 1);
    101.  
    102.                     float4 col = color * lightDist;
    103.                     return _WorldSpaceLightPos0;
    104.                 }
    105.  
    106.                 ENDCG
    107.             }
    108.         }
    109. }
    110.  
     
    pasxoshorp13 likes this.
  2. pasxoshorp13

    pasxoshorp13

    Joined:
    Dec 11, 2018
    Posts:
    2
    did you solve the bug?
     
  3. sewy

    sewy

    Joined:
    Oct 11, 2015
    Posts:
    150
    In this thread bgolus writes

    Also if "LightMode"="ForwardBase" is true then the _WorldSpaceLightPos0 is always for a directional light, even if none exists in the scene. Only "LightMode"="ForwardAdd" has support for passing the position of point lights.
     
  4. DaveA_VR

    DaveA_VR

    Joined:
    May 26, 2022
    Posts:
    33
    Is your light baked?