Search Unity

Change light direction in SS

Discussion in 'Shaders' started by Airship, May 25, 2013.

  1. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    260
    I am trying to make a Surface shader that uses a directional light with it's lightDir angle xyz properties defined from the shader properties as a Vector property rather than using the main directional light in the scene. The reason for this is I want the shadows to derive from the scene's main directional light, but the actual light applied to be coming from a different custom angle. Is this possible? What I am having initial difficulty on is figuring out how to convert my customLightAngle vector into the correct object/ world space so I can pass it into the surface shader. Thanks for any pointers.

    Code (csharp):
    1. vec ("customLightAngle", Vector) = (45,45,45,1)
    2.  
    3.  
    4. fixed4 CustomLight (SurfaceOutputCustom s, fixed3 lightDir, fixed atten)
    5. {  
    6.     lightDir = myCustomVector;
    7.     fixed diff = max (0, dot (s.Normal, lightDir.xyz ));
    8. }
     
  2. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    260
    Here is what I have so far. Still can't figure out how to correctly turn a property vector into a directional light with rotation defined by the vector's xyz.


    Code (csharp):
    1.   Shader "CustomVectorLight" {
    2.     Properties {
    3.       lightDirVec ("light vec", Vector) = (45,45,45,1)      
    4.     }
    5.     SubShader {
    6.      Tags { "RenderType" = "Opaque" }
    7.    
    8.       CGPROGRAM
    9. #pragma surface surf CustomLight vertex:vert target 2.0  
    10.     struct SurfaceOutputCustom {
    11.     fixed3 Albedo;
    12.     fixed3 Normal;
    13.     fixed3 Emission;
    14.     half Specular;
    15.     fixed Gloss;
    16.     fixed Alpha;
    17.     fixed3 CustomLightDir;
    18. };
    19.     float3 lightDirVec;
    20. fixed4 LightingCustomLight(SurfaceOutputCustom s, fixed3 lightDir, fixed atten)
    21. {
    22.     fixed4 c;
    23.     c.rgb = max (0, dot (s.Normal, s.CustomLightDir.xyz ));
    24.     c.a = 1;
    25.     return c;
    26. }
    27.    
    28.       struct Input {
    29.       float2 uv_Tex1;
    30.       float3 lightDirVec;  
    31.       };
    32.      
    33. void vert (inout appdata_full v, out Input o) {
    34. float3 objSpaceLightPos = mul(_World2Object, float4 (lightDirVec,1)).xyz;
    35. objSpaceLightPos = objSpaceLightPos.xyz * unity_Scale.w - v.vertex.xyz;
    36. TANGENT_SPACE_ROTATION;
    37. o.lightDirVec = mul (rotation, objSpaceLightPos);
    38.       }
    39.  
    40. void surf (Input IN, inout SurfaceOutputCustom o) {
    41.       o.CustomLightDir = IN.lightDirVec.xyz;
    42.       o.Albedo = .5f;    
    43.       }
    44.       ENDCG
    45.     }  
    46.   }
     
    Last edited: May 25, 2013