Hello
I’m currently working on a transparent surface shader, that should render shadows based on provided textures. These are the provided textures:
The first two textures are shadows of a scene after the sphere has been inserted, and the last two are the scene before the sphere has been inserted
I want to add the first two textures together(let’s call the combined texture for after), and the last two textures together(let’s call the combined texture for prev). With the texture combined, I first added 0.01 to both textures(to avoid division by 0), then multiplied both textures by newLa(which is_La/9). Lastly, I divide the two textures with each other. This should result in a texture containing only the shadows of the inserted sphere, where the shadow colour is determined by the _La value. The idea is further described in these papers:
- https://www.scitepress.org/Papers/2020/89430/89430.pdf
- https://vbn.aau.dk/ws/portalfiles/portal/4968814/icpr06.pdf
But the shadows’ colour is not determined by the _La value, but instead, unity adds colour to the shadows themselves or so I assume. This I don’t understand why because I don’t use any of the functions in the AutoLight.cginc file and I don’t obtain the light colour of the scene.
Any help in making the shadow take colour from the _La variable is much welcome
This is the code I’m running
Shader "Custom/ShadowReceiver"
{
Properties
{
_La("Ambient light from hemisphere", Vector) = (0, 0, 0, 0)
_ShadowMask ("Sun texture", 2D) = "white" {}
_AmbientMask ("Ambient texture", 2D) = "white" {}
_AllHemiShadowMask ("After ambient texture", 2D) = "white" {}
_AllSunShadowMask ("After ambient texture", 2D) = "white" {}
}
SubShader
{
Tags { "Queue" = "Geometry" }
Pass
{
Cull Back
ZTest LEqual
ZWrite Off
Blend dstColor Zero
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform vector _La;
sampler2D _ShadowMask;
float4 _ShadowMask_ST;
sampler2D _AllSunShadowMask;
float4 _AllSunShadowMask_ST;
sampler2D _AmbientMask;
sampler2D _AllHemiShadowMask;
float4 _AmbientMask_ST;
float4 _AllHemiShadowMask_ST;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 wPos: TEXCOORD1;
float4 viewPos: TEXCOORD2;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.wPos = mul(unity_ObjectToWorld, v.vertex);
o.viewPos = ComputeScreenPos(o.pos);
o.uv = v.texcoord;
return o;
}
fixed4 frag(v2f input) : COLOR
{
float2 textureCoordinateAll = input.viewPos.xy / input.viewPos.w;
textureCoordinateAll = TRANSFORM_TEX(textureCoordinateAll, _AllSunShadowMask);
fixed4 afterSun = tex2D(_AllSunShadowMask,textureCoordinateAll);
float2 textureCoordinate = input.viewPos.xy / input.viewPos.w;
textureCoordinate = TRANSFORM_TEX(textureCoordinate, _ShadowMask);
fixed4 prevSun = tex2D(_ShadowMask, textureCoordinate);
float2 textureCoordinateHemi = input.viewPos.xy / input.viewPos.w;
float2 textureCoordinateHemiAll = input.viewPos.xy / input.viewPos.w;
textureCoordinateHemi = TRANSFORM_TEX(textureCoordinateHemi, _AmbientMask);
textureCoordinateHemiAll = TRANSFORM_TEX(textureCoordinateHemiAll, _AllHemiShadowMask);
float4 prevHemi = tex2D(_AmbientMask, textureCoordinateHemi);
float4 afterHemi = tex2D(_AllHemiShadowMask, textureCoordinateHemiAll);
fixed4 after = (afterSun + afterHemi) - 1;
fixed4 prev = prevHemi + prevSun - 1;
fixed4 newLa = _La/9;
return ((after+0.01) * newLa)/((prev+0.01) * newLa);
}
ENDCG
}
}
FallBack "Vertex"
}