Search Unity

Shader with the fixed light direction (as the directional light should be supposed to work)

Discussion in 'Shaders' started by WereVarg, Feb 8, 2017.

  1. WereVarg

    WereVarg

    Joined:
    Apr 9, 2012
    Posts:
    20
    I've tried lot of shaders and for some reason Directional Light doesn't work with them as I'd wish. Looks like the specular depends of the distance to the light and direction. I've ended up using custom light from the examples.

    Code (CSharp):
    1. half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir) {
    2.         half3 h = normalize (half3(0,50,-15)+viewDir);
    3.         half diff = max (0, dot (s.Normal, lightDir));
    4.         float nh = max (0, dot (s.Normal, h));
    5.         float spec = pow (nh, 128.0);
    6.         half4 c;
    7.         c.rgb = s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec;
    8.        
    9.         c.a = s.Alpha;
    10.         return c;
    11.     }
    I've replaced the half3 h = normalize (lightDir+viewDir); with some constant vector
    It works fine but in "local" coordinates. How could I pass here the World Coordinates constant vector?
     
  2. WereVarg

    WereVarg

    Joined:
    Apr 9, 2012
    Posts:
    20
    The problem solved using for the viewDir camera.forward
    float3 viewDir1 = UNITY_MATRIX_IT_MV[2].xyz;

    p.s. and I wasn't calculating tangents when imporing
    upd. and the normal was wrong
     
    Last edited: Feb 12, 2017