Search Unity

Shadowmap Generation Clip Space Issues

Discussion in 'General Graphics' started by Dragnipurake97, Sep 28, 2019.

  1. Dragnipurake97

    Dragnipurake97

    Joined:
    Sep 28, 2019
    Posts:
    40
    I am having an error when generating a shadow map using this tutorial: https://catlikecoding.com/unity/tutorials/scriptable-render-pipeline/spotlight-shadows/

    For some reason the calculated clip space has a very small z component and I can't figure out why. If I set the clip.z manually it generates the shadow map so the error has something to do with the matrices.


    SHADOW RENDER METHOD

    Code (CSharp):
    1.  void RenderShadowMap(ScriptableRenderContext context)
    2.     {
    3.         // shadow map texture format
    4.         shadowMap = RenderTexture.GetTemporary(shadowMapSize, shadowMapSize, 16, RenderTextureFormat.Shadowmap);
    5.         shadowMap.filterMode = FilterMode.Bilinear;
    6.         shadowMap.wrapMode = TextureWrapMode.Clamp;
    7.        
    8.         // Set depth map to render
    9.         CoreUtils.SetRenderTarget(shadowBuffer, shadowMap, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.Depth);
    10.         shadowBuffer.BeginSample("Render Shadow Map");
    11.         context.ExecuteCommandBuffer(shadowBuffer);
    12.         shadowBuffer.Clear();
    13.  
    14.         // Retrive VP matricies
    15.         Matrix4x4 view, projection;
    16.         ShadowSplitData splitData;
    17.         cullResults.ComputeSpotShadowMatricesAndCullingPrimitives(0, out view, out projection, out splitData);
    18.  
    19.         shadowBuffer.SetViewProjectionMatrices(view, projection);
    20.         context.ExecuteCommandBuffer(shadowBuffer);
    21.         shadowBuffer.Clear();
    22.  
    23.         // Draw
    24.         ShadowDrawingSettings shadowSettings = new ShadowDrawingSettings(cullResults, 0);
    25.         context.DrawShadows(ref shadowSettings);
    26.  
    27.         shadowBuffer.SetGlobalTexture(shadowMapId, shadowMap);
    28.  
    29.         shadowBuffer.EndSample("Render Shadow Map");
    30.         context.ExecuteCommandBuffer(shadowBuffer);
    31.         shadowBuffer.Clear();
    32.     }

    SHADER CODE

    Code (CSharp):
    1. #ifndef MYRP_SHADOWCASTER_INCLUDED
    2. #define MYRP_SHADOWCASTER_INCLUDED
    3.  
    4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    5.  
    6. CBUFFER_START(UnityPerFrame)
    7. float4x4 unity_MatrixVP;
    8. CBUFFER_END
    9.  
    10. CBUFFER_START(UnityPerDraw)
    11. float4x4 unity_ObjectToWorld;
    12. CBUFFER_END
    13.  
    14. #define UNITY_MATRIX_M unity_ObjectToWorld
    15. #define UNITY_MATRIX_VP unity_MatrixVP
    16.  
    17. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
    18.  
    19. struct VertexInput
    20. {
    21.     float4 pos : POSITION;
    22.     UNITY_VERTEX_INPUT_INSTANCE_ID
    23. };
    24.  
    25. struct VertexOutput
    26. {
    27.     float4 clipPos : SV_POSITION;
    28. };
    29.  
    30. VertexOutput ShadowCasterPassVertex(VertexInput input)
    31. {
    32.     VertexOutput output;
    33.     UNITY_SETUP_INSTANCE_ID(input);
    34.     float4 worldPos = mul(UNITY_MATRIX_M, float4(input.pos.xyz, 1.0));
    35.     output.clipPos = mul(UNITY_MATRIX_VP, worldPos);
    36.     return output;
    37. }
    38.  
    39. float4 ShadowCasterPassFragment(VertexOutput input) : SV_TARGET
    40. {
    41.     return 0;
    42. }
    43.  
    44. #endif // MYRP_SHADOWCASTER_INCLUDED