Search Unity

Question SHADOW_ATTENUATION doesn't seem to affect my geometry shader

Discussion in 'Shaders' started by minjoppe, Oct 9, 2020.

  1. minjoppe

    minjoppe

    Joined:
    Jul 21, 2020
    Posts:
    1


    Applying Pragma Forwardbase, Inserting Shadowcoords in the struct and Transfershadow in the geometry output don't seem to affect the Shadow Attenuation result of the fragment pass applied to the geometry.

    I wonder if my placement of certain code is wrong or my include files are incomplete though I can't see the problem myself
    I was wondering if more experienced shaderlab programmers could see what I'm doing wrong here

    my current shader is only focussed on receiving shadows using directional lighting.

    I added comment lines to highlight the Shadow implementation.



    Code (CSharp):
    1.  
    2. CGINCLUDE
    3.  
    4.  
    5. #include "UnityCG.cginc"
    6. #include "Lighting.cginc"
    7. #include "Autolight.cginc"
    8. #include "CustomTesselation.cginc"
    9. #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    10.  
    11.        float _CardSize;
    12.        float _CardRemovalVal;
    13.        float _RenderDistance;
    14.  
    15.        sampler2D _WindDistortionMap;
    16.        float4 _WindDistortionMap_ST;
    17.  
    18.        float2 _WindSpeed;
    19.        float _WindStrength;
    20.        
    21.        float _OffsetAmount;
    22.        
    23.        float _DisplacementStrength;
    24.  
    25.        struct geometryOutput
    26.        {
    27.            float4 pos : SV_POSITION;
    28.            float2 uv : TEXCOORD0;
    29.            SHADOW_COORDS(1)//----------------------------------------------
    30.        };
    31.  
    32.        geometryOutput VertexOutput(float3 pos, float2 uv)
    33.        {
    34.            geometryOutput o;
    35.            o.pos = UnityObjectToClipPos(pos);
    36.            TRANSFER_SHADOW(o)//----------------------------------------------
    37.            o.uv = uv;
    38.            return o;
    39.        }
    40.  
    41.        float3x3 AngleAxis3x3(float angle, float3 axis)
    42.        {
    43.            float c, s;
    44.            sincos(angle, s, c);
    45.  
    46.            float t = 1 - c;
    47.            float x = axis.x;
    48.            float y = axis.y;
    49.            float z = axis.z;
    50.  
    51.            return float3x3(
    52.                t * x * x + c, t * x * y - s * z, t * x * z + s * y,
    53.                t * x * y + s * z, t * y * y + c, t * y * z - s * x,
    54.                t * x * z - s * y, t * y * z + s * x, t * z * z + c
    55.                );
    56.        }
    57.  
    58.        float rand(float3 co)
    59.        {
    60.            return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 53.539))) * 43758.5453123);
    61.        }
    62.  
    63.        [maxvertexcount(8)]
    64.        void geo(triangle vertexOutput IN[3] : SV_POSITION, inout TriangleStream<geometryOutput> triStream)
    65.        {
    66.            geometryOutput o;
    67.            float3 pos = IN[0].vertex;
    68.            float3 wpos = IN[0].wpos;
    69.            float2 wposuv = IN[0].wposuv;
    70.            float3 vertNormal = IN[0].normal;
    71.            float4 vertTangent = IN[0].tangent;
    72.            float3 color = IN[0].color;
    73.            float3 randomOffset = (rand(wpos) * 2 - 1) * _OffsetAmount;
    74.            randomOffset.y = 0;
    75.  
    76.  
    77.            float viewDistance = distance(wpos, _WorldSpaceCameraPos);
    78.  
    79.            if (_CardSize * color.r < _CardRemovalVal || viewDistance > _RenderDistance)
    80.                return;
    81.  
    82.            float3 vertCross = cross(vertNormal, vertTangent) * vertTangent.w;
    83.  
    84.            float3x3 tangentToLocal = float3x3(
    85.                vertTangent.x, vertCross.x, vertNormal.x,
    86.                vertTangent.y, vertCross.y, vertNormal.y,
    87.                vertTangent.z, vertCross.z, vertNormal.z
    88.                );
    89.  
    90.            float3x3 facingRotationMatrix = AngleAxis3x3(rand(pos) * UNITY_TWO_PI, float3(0, 0, 1));
    91.  
    92.            float2 uv = pos.xz * _WindDistortionMap_ST.xy + _WindDistortionMap_ST.zw + _WindSpeed * _Time;
    93.            float2 windSample = (tex2Dlod(_WindDistortionMap, float4(uv, 0, 0)).xy * 2 - 1) * _WindStrength;
    94.  
    95.            float3 wind = float3(windSample.x, 0, windSample.y);
    96.  
    97.            float2 wposUV_tex = tex2Dlod(_RenderTexture, float4(wposuv.xy, 0,0));
    98.            float3 renderDisplacement = (float3(wposUV_tex.x -0.25, 0, wposUV_tex.y -0.25)) * _DisplacementStrength;
    99.  
    100.            float3x3 transformationMatrix = mul(tangentToLocal, facingRotationMatrix);
    101.            pos.xz += vertTangent * randomOffset.xz;
    102.  
    103.            //first quad
    104.            triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3((-_CardSize / 2) * color.r, 0, 0)), float2(0, 0)));
    105.            triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3((_CardSize / 2) * color.r, 0, 0)), float2(1, 0)));
    106.            triStream.Append(VertexOutput(pos + renderDisplacement + wind * (color.r * _CardSize) + mul(transformationMatrix, float3((-_CardSize / 2) * color.r, 0, _CardSize * color.r)), float2(0,1)));
    107.            triStream.Append(VertexOutput(pos + renderDisplacement + wind * (color.r * _CardSize) + mul(transformationMatrix, float3((_CardSize / 2) * color.r, 0, _CardSize * color.r)), float2(1, 1)));
    108.            
    109.            //start second quad
    110.            triStream.RestartStrip();
    111.            triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3(0, (-_CardSize / 2) * color.r, 0)), float2(0, 0)));
    112.            triStream.Append(VertexOutput(pos + mul(transformationMatrix, float3(0, (_CardSize / 2) * color.r, 0)), float2(1, 0)));
    113.            triStream.Append(VertexOutput(pos + renderDisplacement + wind * (color.r * _CardSize) + mul(transformationMatrix, float3(0, (-_CardSize / 2) * color.r, _CardSize * color.r)), float2(0, 1)));
    114.            triStream.Append(VertexOutput(pos + renderDisplacement + wind * (color.r * _CardSize) + mul(transformationMatrix, float3(0, (_CardSize / 2) * color.r, _CardSize * color.r)), float2(1, 1)));
    115.        }
    116.    
    117.    ENDCG
    118.  
    119.    SubShader
    120.    {
    121.        Cull Off
    122.        Tags
    123.            {
    124.                "Queue" = "Transparent"
    125.                "RenderType" = "Transparent"
    126.            }
    127.        Pass
    128.        {
    129.            Tags
    130.            {
    131.                "LightMode" = "ForwardBase"
    132.            }
    133.            ZWrite On
    134.            AlphaToMask On
    135.  
    136.            CGPROGRAM
    137.  
    138.            #pragma vertex vert
    139.            #pragma fragment frag
    140.            #pragma geometry geo
    141.            #pragma hull hull
    142.            #pragma domain domain
    143.  
    144.            #pragma target 4.6
    145.            #pragma multi_compile_fwdbase
    146.  
    147.            #include "UnityCG.cginc"
    148.            #include "AutoLight.cginc"
    149.            #include "Lighting.cginc"
    150.  
    151.            sampler2D _MainTex;
    152.            float4 _TopColor;
    153.            float4 _BottomColor;
    154.            float _ShadowStrength;
    155.  
    156.            float4 frag(geometryOutput i, fixed facing : VFACE) : SV_Target
    157.            {
    158.                half atten = SHADOW_ATTENUATION(i);//-----------------------------
    159.  
    160.                float4 tex = tex2D(_MainTex, i.uv);
    161.                float4 col = lerp(_BottomColor, _TopColor, i.uv.y);
    162.                col *= atten;
    163.                tex *= col;
    164.  
    165.                return tex;
    166.            }
    167.            ENDCG
    168.        }
    169.    }
    170. }
    171.