Search Unity

Shader inverting light direction

Discussion in 'Shaders' started by thx2013, Aug 8, 2015.

  1. thx2013

    thx2013

    Joined:
    Dec 25, 2012
    Posts:
    8
    I`ve been following this tutorial
    , but the result turned out not as It had been excepted: the lighting on geometry with the shader applied looked inverted.
    http://snag.gy/PIqia.jpg
    I found the cause pretty quickly though I cannot figure out why it behaves that way, here take a look(pay attention to lines 37-40):

    Code (CSharp):
    1.  
    2. Shader "1_Lambdert" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1.0,1.0,1.0,1.0)
    5.     }
    6.     SubShader {
    7.         Pass {
    8.             Tags { "LightMode" = "ForwardBase"}
    9.             CGPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.        
    13.             //user defined variables
    14.             uniform float4 _Color;
    15.        
    16.             //unity defined variables
    17.             uniform float4 _LightColor0;
    18.        
    19.             //base input structs
    20.             struct vertexInput{
    21.                 float4 vertex : POSITION;
    22.                 float3 normal : NORMAL;
    23.             };      
    24.             struct vertexOutput{
    25.                 float4 pos : SV_POSITION;
    26.                 float4 col : COLOR;
    27.             };
    28.        
    29.             //vertex functions
    30.             vertexOutput vert(vertexInput v){
    31.                 vertexOutput o;
    32.            
    33.                 float3 normalDirection = normalize( mul( float4(v.normal,0.0), _World2Object ).xyz );
    34.                 float lightDirection;
    35.                 float atten = 1.0;
    36.            
    37.                 lightDirection = normalize( _WorldSpaceLightPos0.xyz );          
    38.                 float3 diffuseReflection = max( 0.0, dot(normalDirection, lightDirection));          
    39.                 //replacing to upper lines with these fixes the error      
    40.                 //float3 diffuseReflection = max( 0.0, dot(normalDirection, normalize( _WorldSpaceLightPos0.xyz )));
    41.            
    42.            
    43.                 o.col = float4(diffuseReflection, 1.0);
    44.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    45.                 return o;
    46.             }
    47.        
    48.             //fragment function
    49.             float4 frag(vertexOutput i) : COLOR
    50.             {
    51.                 return i.col;
    52.             }
    53.        
    54.             ENDCG
    55.         }
    56.     }
    57. }
    58.  
    Does anyone has any ideas what`s wrong with that assignment?
     
  2. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    You're assigning a vector of 3 floats to just a single float, but you then use it as a vector again. So instead of the .xyz components being used, the components .xxx form the vector, because a float can only store one number. Unity unfortunately does not throw a warning when this happens, so you'll have to watch out for this yourself.

    So, "float lightDirection" should be "float3 lightDirection" instead.
     
  3. thx2013

    thx2013

    Joined:
    Dec 25, 2012
    Posts:
    8
    Thank you, that was obvious, should have figured out myself