I’m trying to use the UNITY_LIGHT_ATTENUATION macro but it’s not working as (I) expected in the ForwardBase pass. In the ForwardAdd pass it works fine, with the third parameter affect the resulting light/shadow, while in the ForwardBase pass it seems to be ignored.
Am I doing something wrong? Is there anothger way to sample the shadows using world position?
I’m not using the URP, just the default one (SRP?). This is the stripped down shader:
Shader "Custom/Test"
{
Properties
{
_MainTex("Base (RGB)", 2D) = "white" {}
}
CGINCLUDE
#include "UnityStandardCore.cginc"
ENDCG
SubShader
{
Tags {"LightMode" = "ForwardBase"}
Pass {
Fog { Mode Off }
Lighting On
CGPROGRAM
#pragma multi_compile_instancing
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
float4 _MainTex_TexelSize;
struct appdata_t
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : POSITION;
float3 texcoord : TEXCOORD0;
float3 posWorld : TEXCOORD1;
LIGHTING_COORDS(4,5)
};
v2f vert (appdata_t v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.texcoord = float3((TRANSFORM_TEX(v.texcoord.xy, _MainTex)).xy,1);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag (v2f i) : COLOR
{
float2 uv = i.texcoord.xy;
float4 col = tex2D(_MainTex, uv);
UNITY_LIGHT_ATTENUATION(atten, i, 0) // worldPos parameter ignored, shadows work fine even when set to zero, seems to be using only _ShadowCoords
return atten;
}
ENDCG
}
Pass {
Tags {"LightMode" = "ForwardAdd" }
Fog { Mode Off }
Lighting On
ZWrite Off
//Blend Zero One // hide add
Blend One Zero // hide base
CGPROGRAM
#pragma target 3.0
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
float4 _MainTex_TexelSize;
struct appdata_t
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : POSITION;
float3 tex : TEXCOORD0;
fixed3 normalWorld : TEXCOORD1;
LIGHTING_COORDS(2,3)
float3 posWorld : TEXCOORD4;
};
v2f vert (appdata_t v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.tex = float3((TRANSFORM_TEX(v.uv.xy, _MainTex)).xy,1);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.normalWorld = mul( unity_ObjectToWorld, float4( v.normal, 0.0 ) ).xyz;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag (v2f i) : COLOR
{
float2 uv = i.tex.xy;
UNITY_LIGHT_ATTENUATION(atten, i, i.posWorld + ...) // changing posWorld paramenter affects lighting
return atten;
}
ENDCG
}
Pass
{
ZWrite On
Tags {"LightMode"="ShadowCaster"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
struct v2f {
V2F_SHADOW_CASTER;
};
v2f vert(appdata_base v)
{
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
return o;
}
float4 frag(v2f i) : SV_Target
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
}