Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What does lightposition.w mean?

Discussion in 'Shaders' started by prayshouse, Jan 17, 2018.

  1. prayshouse

    prayshouse

    Joined:
    Sep 11, 2017
    Posts:
    27
    Hello,
    In UnityCG.cginc, there exist many LightPosition.w. But I don't know the reason why to multiply LightPosition.w.

    For example, there is
    float3 toLight = unity_LightPosition.xyz - viewpos.xyz * unity_LightPosition.w;
    in the following code.

    Code (CSharp):
    1. float3 ShadeVertexLightsFull (float4 vertex, float3 normal, int lightCount, bool spotLight)
    2. {
    3.     float3 viewpos = UnityObjectToViewPos (vertex);
    4.     float3 viewN = normalize (mul ((float3x3)UNITY_MATRIX_IT_MV, normal));
    5.  
    6.     float3 lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz;
    7.     for (int i = 0; i < lightCount; i++) {
    8.         float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
    9.         float lengthSq = dot(toLight, toLight);
    10.  
    11.         // don't produce NaNs if some vertex position overlaps with the light
    12.         lengthSq = max(lengthSq, 0.000001);
    13.  
    14.         toLight *= rsqrt(lengthSq);
    15.  
    16.         float atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
    17.         if (spotLight)
    18.         {
    19.             float rho = max (0, dot(toLight, unity_SpotDirection[i].xyz));
    20.             float spotAtt = (rho - unity_LightAtten[i].x) * unity_LightAtten[i].y;
    21.             atten *= saturate(spotAtt);
    22.         }
    23.  
    24.         float diff = max (0, dot (viewN, toLight));
    25.         lightColor += unity_LightColor[i].rgb * (diff * atten);
    26.     }
    27.     return lightColor;
    28. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,991
    prayshouse likes this.
  3. prayshouse

    prayshouse

    Joined:
    Sep 11, 2017
    Posts:
    27