Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Unity adding unwanted shadow attenuation to transparent surface shader

Discussion in 'Shaders' started by reptinias, May 10, 2024.

  1. reptinias

    reptinias

    Joined:
    Nov 29, 2018
    Posts:
    3
    Hello

    I'm currently working on a transparent surface shader, that should render shadows based on provided textures. These are the provided textures:
    AllHemiShadows.png AllSunShadows.png AmbientOcclsionTex.png ShadowTex.png

    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:

    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
    Code (CSharp):
    1. Shader "Custom/ShadowReceiver"
    2. {
    3.     Properties
    4.     {
    5.         _La("Ambient light from hemisphere", Vector) = (0, 0, 0, 0)
    6.         _ShadowMask ("Sun texture", 2D) = "white" {}
    7.         _AmbientMask ("Ambient texture", 2D) = "white" {}
    8.         _AllHemiShadowMask ("After ambient texture", 2D) = "white" {}
    9.         _AllSunShadowMask ("After ambient texture", 2D) = "white" {}
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "Queue" = "Geometry" }
    14.        
    15.         Pass
    16.         {
    17.             Cull Back
    18.             ZTest LEqual
    19.             ZWrite Off
    20.             Blend dstColor Zero
    21.             CGPROGRAM
    22.          
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.             #include "UnityCG.cginc"
    26.            
    27.             uniform vector _La;
    28.  
    29.             sampler2D _ShadowMask;
    30.             float4 _ShadowMask_ST;
    31.            
    32.             sampler2D _AllSunShadowMask;
    33.             float4 _AllSunShadowMask_ST;
    34.  
    35.             sampler2D _AmbientMask;
    36.             sampler2D _AllHemiShadowMask;
    37.            
    38.             float4 _AmbientMask_ST;
    39.             float4 _AllHemiShadowMask_ST;
    40.            
    41.             struct v2f
    42.             {
    43.                 float4 pos : SV_POSITION;
    44.                 float2 uv : TEXCOORD0;
    45.                 float4 wPos: TEXCOORD1;
    46.                 float4 viewPos: TEXCOORD2;
    47.             };
    48.             v2f vert(appdata_base v)
    49.             {
    50.                 v2f o;
    51.                 o.pos = UnityObjectToClipPos (v.vertex);
    52.                 o.wPos = mul(unity_ObjectToWorld, v.vertex);
    53.                 o.viewPos = ComputeScreenPos(o.pos);
    54.                 o.uv = v.texcoord;
    55.          
    56.                 return o;
    57.             }
    58.             fixed4 frag(v2f input) : COLOR
    59.             {
    60.                 float2 textureCoordinateAll = input.viewPos.xy / input.viewPos.w;
    61.                 textureCoordinateAll = TRANSFORM_TEX(textureCoordinateAll, _AllSunShadowMask);
    62.                 fixed4 afterSun = tex2D(_AllSunShadowMask,textureCoordinateAll);
    63.                
    64.                 float2 textureCoordinate = input.viewPos.xy / input.viewPos.w;
    65.                 textureCoordinate = TRANSFORM_TEX(textureCoordinate, _ShadowMask);
    66.                 fixed4 prevSun = tex2D(_ShadowMask, textureCoordinate);
    67.  
    68.                 float2 textureCoordinateHemi = input.viewPos.xy / input.viewPos.w;
    69.                 float2 textureCoordinateHemiAll = input.viewPos.xy / input.viewPos.w;
    70.                
    71.                 textureCoordinateHemi = TRANSFORM_TEX(textureCoordinateHemi, _AmbientMask);
    72.                 textureCoordinateHemiAll = TRANSFORM_TEX(textureCoordinateHemiAll, _AllHemiShadowMask);
    73.                
    74.                 float4 prevHemi = tex2D(_AmbientMask, textureCoordinateHemi);
    75.                 float4 afterHemi = tex2D(_AllHemiShadowMask, textureCoordinateHemiAll);
    76.  
    77.                 fixed4 after = (afterSun + afterHemi) - 1;
    78.                 fixed4 prev = prevHemi + prevSun - 1;
    79.  
    80.                 fixed4 newLa = _La/9;
    81.                
    82.                 return ((after+0.01) * newLa)/((prev+0.01) * newLa);
    83.                
    84.             }
    85.             ENDCG
    86.         }
    87.     }
    88.     FallBack "Vertex"
    89. }