0
\$\begingroup\$

I have a Unity project with the builtin pipeline. I would like to save the shadowmap of the directional light source and visualize on screen as the primary renderTarget of the main camera. Unfortunately, I don't know, what I am doing wrong. I can't even find the ClearRenderTargetInstruction() in the FrameDebugger. Here is my code:

    using UnityEngine;
    using UnityEngine.Rendering;
    using System.Collections;
    
    public class ABR : MonoBehaviour
    {
        public string dirLightShadowMapName = "_DirLightShadowMap";
    
        RenderTargetIdentifier shadowMapRenderTextureIdentifier;
        private CommandBuffer sdwCmd;
        private Light lightComponent;
    
        void Start()
        {
            lightComponent = GetComponent<Light>();
            sdwCmd = new CommandBuffer();
            CacheShadowMap();

            CommandBuffer cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, true, Color.red);
            cmd.SetRenderTarget(shadowMapRenderTextureIdentifier);
            cmd.Clear();
            Camera.main.AddCommandBuffer(CameraEvent.AfterEverything,cmd);
            Graphics.ExecuteCommandBuffer(cmd);
        }
    
        void CacheShadowMap()
        {
            shadowMapRenderTextureIdentifier = BuiltinRenderTextureType.CurrentActive;
            sdwCmd.SetGlobalTexture(dirLightShadowMapName, shadowMapRenderTextureIdentifier);
            lightComponent.AddCommandBuffer(LightEvent.AfterShadowMap, sdwCmd);
        }
    
       
    }

Any tips are appreciated!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is that correct to add each Update another AddCommandBuffer to the camera? \$\endgroup\$
    – Zibelas
    Apr 10 at 13:54
  • \$\begingroup\$ I updated the code. Thanks! \$\endgroup\$
    – Fox1942
    Apr 10 at 14:16

0

You must log in to answer this question.

Browse other questions tagged .