- Home /
How do I sample a shadowmap in a custom shader?
Hi guys,
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.
Has anyone written a custom shader in Unity that receives shadows?
Surely someone must have..
Answer by Santokes · Nov 04, 2011 at 11:01 PM
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.
Good luck!
-Peter
Shader "Custom/PeterShader2" {
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
CGINCLUDE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
uniform sampler2D _MainTex;
ENDCG
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
Lighting On
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
struct VSOut
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD1;
LIGHTING_COORDS(3,4)
};
VSOut vert(appdata_tan v)
{
VSOut o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
float4 frag(VSOut i) : COLOR
{
float3 lightColor = _LightColor0.rgb;
float3 lightDir = _WorldSpaceLightPos0;
float4 colorTex = tex2D(_MainTex, i.uv.xy * float2(25.0f));
float atten = LIGHT_ATTENUATION(i);
float3 N = float3(0.0f, 1.0f, 0.0f);
float NL = saturate(dot(N, lightDir));
float3 color = colorTex.rgb * lightColor * NL * atten;
return float4(color, colorTex.a);
}
ENDCG
}
}
FallBack "Diffuse"
}
$$anonymous$$A$$anonymous$$E NOTE that you can't do this if Tags {"Queue"="Transparent"} !
This works but it seems to shadow also all pixels that are not facing the lightsource, additionally darkening those areas. Did you find a solution for this?
Hi temptest123
You can use N*L to deter$$anonymous$$e which faces are front-facing and just reduce shadows this way.
float NL = saturate(dot(N, lightDir));
float shadow = LIGHT_ATTENUATION(i) * NL;
Now you have a term, shadow, which is equally dark for back faces and shadowed front faces, no double darkening.
$$anonymous$$A$$anonymous$$E NOTE that you can't do this if Tags {"Queue"="Transparent"} !
You can, if you add
#pragma multi_compile_fwdadd_fullshadows
- equally undocumented :)
Answer by mikkel.gjoel · Nov 14, 2013 at 01:11 PM
MAKE NOTE that you can't do this if Tags {"Queue"="Transparent"} !
You can, if you add
#pragma multi_compile_fwdadd_fullshadows
- equally undocumented :)
Thanks! I just hit this magic handling of Transparent within a week of your comment. Good ti$$anonymous$$g. :)
Answer by Kushulain · Nov 22, 2013 at 06:26 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613022859im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to correctly sample a shadow map from a render texture? 1 Answer
Custom shadow subshader 1 Answer
Getting depth value from Shadow map in shader 0 Answers
What's in unity_LightShadowBias? 0 Answers
Shadows being received by non-shadow collectors that are depth culled by a shadow collector. 1 Answer