- Home /
How could I access light direction in a surface shader?
Hi, everyone! I'm trying to write a shader that adds a second Diffuse texture depending on where the lights hit the mesh. Something like this:
However, I can't for the life of me figure out how to access the "Light Direction" in the "surf" function of the shader. Is it possible?
Answer by tanoshimi · Apr 25, 2014 at 07:17 PM
You don't access light direction in surf(), but you can access it in a lighting model. Define a custom struct that contains both your diffuse textures:
struct CustomSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half3 Diff1;
half3 Diff2;
};
Pass this struct to a custom lighting model, e.g.:
#pragma surface surf CustomLighting
half4 LightingCustomLighting (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) { ... }
and, in the model itself, set the RGB components of the return value based on NdotL as desired. Something like:
if(NDotL > 0) {
c.rgb = s.Diff1;
}
else {
c.rgb = s.Diff2;
}
More info and examples at http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderLightingExamples.html