- Home /
Accessing shadow texture in shader
Hey, I need to know what unity names its shadow texture in CG shaders, and how can I access it when doing vert/frag shaders instead of surface shaders?
the macros (LIGHTING_COORDS, LIGHT_ATTENUATION, TRANSFER_VERTEX_TO_FRAGMENT) do not seem to work, I get solid white as my shadow texture (have tried with multiple different compiler pragmas)
Anyone ever do this? Or am I up shit creek and have to go generate my own shadow texture with a 2nd camera?
Answer by whydoidoit · Apr 21, 2013 at 08:55 PM
Add SHADOW_COORDS(x) to your vertex to fragment structure where X is a free TEXCOORD
Use TRANSFER_SHADOW(output) in your vertex shader
Multiply the colour by SHADOW_ATTENUATION(input) in your fragment shader
Post your shader code - that works in all of my shaders.
i figured it out, it requires "Light$$anonymous$$ode" = "ForwardBase"
#pragma multi_compile_fwdbase
AND requires forward rendering (not ideal)
so i'm just gonna generate my own shadow map ins$$anonymous$$d
$$anonymous$$uch better performance than deferred though...
Answer by karmakun · Jan 13, 2014 at 07:19 PM
here the example, actually you are very close
about shader: ignore de uv:TEXCOORD, no need for shadow pass
Shader "Custom/shadows" { Properties {
}
SubShader
{
Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
pass{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord: TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
LIGHTING_COORDS(2,3)
};
v2f vert (a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord;
o.normal = v.normal;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
float4 frag (v2f i) : COLOR
{
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float atten = LIGHT_ATTENUATION(i) ;
float3 lambert = float(max(0.0,dot(i.normal,lightDirection)));;
return float4(lambert *atten,1);
}
ENDCG
}
}
FallBack "Diffuse"
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100752im_/https://answers.unity.com/themes/thub/images/avi.jpg)