Shader "Custom/URP/RoofSlopeProjection_Shadowed_URP2021" { Properties { _BaseMap ("Texture", 2D) = "white" {} _TileScale ("Density", Float) = 1 _ShadowMin ("Shadow Min", Range(0,1)) = 0.35 } SubShader { Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Opaque" "Queue"="Geometry" } // ========================================================= // ForwardOnly (works even when the URP Renderer is Deferred) // Receives Main Light shadows (including screen-space in Deferred) // ========================================================= Pass { Name "UniversalForwardOnly" Tags { "LightMode"="UniversalForwardOnly" } HLSLPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_instancing // IMPORTANT for Deferred: include screen-space main light shadows #pragma multi_compile_fragment _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN #pragma multi_compile_fragment _ _SHADOWS_SOFT #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl" #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl" TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); CBUFFER_START(UnityPerMaterial) float _TileScale; float _ShadowMin; CBUFFER_END struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct Varyings { float4 positionHCS : SV_POSITION; float3 positionWS : TEXCOORD0; float3 normalWS : TEXCOORD1; float4 shadowCoord : TEXCOORD2; UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO }; Varyings vert (Attributes v) { Varyings o; UNITY_SETUP_INSTANCE_ID(v); UNITY_TRANSFER_INSTANCE_ID(v, o); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); VertexPositionInputs posInputs = GetVertexPositionInputs(v.positionOS.xyz); o.positionHCS = posInputs.positionCS; o.positionWS = posInputs.positionWS; o.normalWS = normalize(TransformObjectToWorldNormal(v.normalOS)); // Works for both shadowmap and screen-space shadows o.shadowCoord = GetShadowCoord(posInputs); return o; } half4 frag (Varyings i) : SV_Target { UNITY_SETUP_INSTANCE_ID(i); float3 n = normalize(i.normalWS); // Roof slope projection (your logic) float3 worldDown = float3(0, -1, 0); float3 slopeDir = normalize(worldDown - n * dot(worldDown, n)); float3 sideDir = normalize(cross(slopeDir, n)); float2 uv; uv.x = dot(i.positionWS, sideDir); uv.y = -dot(i.positionWS, slopeDir); uv *= _TileScale; half3 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv).rgb; // Reliable main-light shadow attenuation (forward + deferred screen-space) float shadowAtten = MainLightRealtimeShadow(i.shadowCoord); // Prevent pitch-black shadows shadowAtten = lerp(_ShadowMin, 1.0, shadowAtten); albedo *= shadowAtten; return half4(albedo, 1.0); } ENDHLSL } // ========================================================= // DepthNormalsOnly (for SSAO / effects needing normals in Deferred) // ========================================================= Pass { Name "DepthNormalsOnly" Tags { "LightMode"="DepthNormalsOnly" } ZWrite On ZTest LEqual HLSLPROGRAM #pragma vertex vertDN #pragma fragment fragDN #pragma multi_compile_instancing #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl" #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl" struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct Varyings { float4 positionHCS : SV_POSITION; float3 normalWS : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO }; Varyings vertDN (Attributes v) { Varyings o; UNITY_SETUP_INSTANCE_ID(v); UNITY_TRANSFER_INSTANCE_ID(v, o); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); VertexPositionInputs pos = GetVertexPositionInputs(v.positionOS.xyz); o.positionHCS = pos.positionCS; o.normalWS = normalize(TransformObjectToWorldNormal(v.normalOS)); return o; } half4 fragDN (Varyings i) : SV_Target { UNITY_SETUP_INSTANCE_ID(i); return half4(PackNormalOctRectEncode(normalize(i.normalWS)).rg, 0, 0); } ENDHLSL } // ========================================================= // ShadowCaster (casts shadows) // ========================================================= Pass { Name "ShadowCaster" Tags { "LightMode"="ShadowCaster" } ZWrite On ZTest LEqual ColorMask 0 HLSLPROGRAM #pragma vertex vertShadow #pragma fragment fragShadow #pragma multi_compile_instancing #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl" #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl" struct Attributes { float4 positionOS : POSITION; }; struct Varyings { float4 positionHCS : SV_POSITION; float4 shadowCoords : TEXCOORD3; }; Varyings vertShadow (Attributes IN) { Varyings OUT; OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz); // Get the VertexPositionInputs for the vertex position VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS.xyz); // Convert the vertex position to a position on the shadow map float4 shadowCoordinates = GetShadowCoord(positions); // Pass the shadow coordinates to the fragment shader OUT.shadowCoords = shadowCoordinates; return OUT; } half4 fragShadow (Varyings IN) : SV_Target { // Get the value from the shadow map at the shadow coordinates half shadowAmount = MainLightRealtimeShadow(IN.shadowCoords); // Set the fragment color to the shadow value return shadowAmount; } ENDHLSL } } FallBack Off }