I just signed up with Unity 3 Pro and am writing custom shaders for my game.
Unity shadows work for stock shaders: you just have to set all shadow casters and receivers in your scene and shadows magically work. But how about custom shaders? I would like to tap the shadow map myself to apply shadows to receivers, does anyone know how to do this? I tried sampling _ShadowMapTexture but this texture is always white.
For the sake of anyone else who is trying to write a fragment shader that receives shadows, I figured it out.
You must do these things:
‘#include “AutoLight.cginc”’
‘#include “Lighting.cginc”’
Add "Tags {“LightMode” = “ForwardBase”}
‘#pragma multi_compile_fwdbase’
Add the Unity macros to your VSOut struct, VS and PS: LIGHTING_COORDS, TRANSFER_VERTEX_TO_FRAGMENT, LIGHT_ATTENUATION.
None of this is documented in the Unity manual.
To access the primary directional light color, unity_LightColor[0] works as long as you don’t add “Tags {“LightMode” = “ForwardBase”}”. If you do add that line, then it doesn’t work: use _LightColor0.rgb instead. Why? Who knows… probably makes sense to someone with access to the Unity source code. Which means no one.
How do i detect if shadows are activated or compatible with current platform ?
I would like to use another sub-shader if it’s not the case. And can’t find a right way to do that.