#Writing a shadow pass for a custom lighting model

1 messages · Page 1 of 1 (latest)

tardy bough
#

So lately i've been working on a more efficient lighting model. And already got all the diffuse and etc. calculations need. But when I try to add shadows for additional lights. The pass won't work and i can't find any good references online. So i figured to ask for help here.

AdditionalLights Code:
{
void AdditionalLightsDiffuse(Varyings IN, out half3 finalColor)
{
uint pixelLightCount = GetAdditionalLightsCount();
half3 accumulatedColor = IN.mainLightColor;

            // For LIGHT_LOOP_BEGIN macro
            #if USE_CLUSTER_LIGHT_LOOP
                InputData inputData = (InputData)0;
                inputData.normalizedScreenSpaceUV = IN.screenPosition.xy / IN.screenPosition.w;
                inputData.positionWS = IN.positionWS;
            #endif

            LIGHT_LOOP_BEGIN(pixelLightCount)
                #if !USE_CLUSTER_LIGHT_LOOP
                    lightIndex = GetPerObjectLightIndex(lightIndex);
                #endif

                Light currentLight;

                #if _RECEIVE_SHADOWS
                    half4 shadowMask = CalculateShadowMask(inputData);
                    currentLight = GetAdditionalLight(lightIndex, IN.positionWS, shadowMask);
                #else
                currentLight = GetAdditionalLight(lightIndex, IN.positionWS);
                #endif

                if (currentLight.distanceAttenuation <= 0.0)
                    continue;
                // Diffuse calculation
                half NdotL = saturate(dot(IN.normalWS, currentLight.direction));
                half thisDiffuse = NdotL * (currentLight.distanceAttenuation * currentLight.shadowAttenuation);
                accumulatedColor += currentLight.color.rgb * thisDiffuse;
            LIGHT_LOOP_END
            finalColor = accumulatedColor;
        }

}

#

I couldn't fit the entire shadow pass so here it is:
{
Pass
{
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }

        // -------------------------------------
        // Render State Commands
        ZWrite On
        ZTest LEqual
        ColorMask 0
        Cull[_Cull]

        HLSLPROGRAM

        // -------------------------------------
        // Shader Stages
        #pragma vertex ShadowPassVertex
        #pragma fragment ShadowPassFragment

        //--------------------------------------
        // GPU Instancing
        #pragma multi_compile_instancing
        #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"

        // -------------------------------------
        // Universal Pipeline keywords
        
        // -------------------------------------
        // Unity defined keywords
        #pragma multi_compile _ LOD_FADE_CROSSFADE

        // This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
        #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW

        // -------------------------------------
        // Includes
        #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
        ENDHLSL
    }

}

vestal root
#

You should first use the URP/Lit.shader to confirm shadows are enabled in the project. (If not check URP Asset). Then use a combination of materials to check if the issue is with casting or receiving (or both)

Can check casting by using the custom shader/material on some object above a plane with the regular URP/Lit material. If you see the shadow it tells you the ShadowCaster pass is working.

And vice versa to check receiving shadows, use the URP/Lit.shader on some object above a plane with custom shader. If you see the shadow, that tells you the AdditionalLights function is working.