Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Camera CustomRender conversion to Command Buffer

Discussion in 'High Definition Render Pipeline' started by maxxa05, Apr 4, 2023.

  1. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    In our game, we have a Camera with a custom render to render the trail of our character on a render texture, which does the following:

    Code (CSharp):
    1.         private void HandleCustomRender(ScriptableRenderContext context, HDCamera camera)
    2.         {
    3.             CommandBuffer cameraBuffer = new CommandBuffer
    4.             {
    5.                 name = "*** Trail Camera"
    6.             };
    7.  
    8.             cameraBuffer.BeginSample("*** Trail Camera");
    9.             context.SetupCameraProperties(rendererCamera);
    10.  
    11.             var cullingParameters = GetCullingParameters();
    12.             CullingResults cullingResults = context.Cull(ref cullingParameters);
    13.  
    14.             DrawingSettings drawingSettings = new DrawingSettings();
    15.             drawingSettings.SetShaderPassName(0, new ShaderTagId("DepthOnly"));
    16.             drawingSettings.SetShaderPassName(1, new ShaderTagId("SRPDefaultUnlit"));
    17.             drawingSettings.SetShaderPassName(2, new ShaderTagId("Vertex"));
    18.  
    19.             drawingSettings.overrideMaterial = depthMaterial;
    20.             depthMaterial.SetFloat("_ModelScale", modelScale);
    21.  
    22.             FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.opaque);
    23.  
    24.             // Render objects that can draw a snow trail
    25.             context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
    26.  
    27.             // Need to clear the depth buffer to avoid drawing glitch when intersecting previous trail
    28.             cameraBuffer.ClearRenderTarget(true, false, Color.white, 1);
    29.  
    30.             cameraBuffer.EndSample("*** Trail Camera");
    31.             context.ExecuteCommandBuffer(cameraBuffer);
    32.             cameraBuffer.Clear();
    33.             context.Submit();
    34.         }
    Now, This only renders a few meshes and I would like to convert it to a command buffer for better CPU performance. This was done by a third party, I understand most of it, but I'm not sure of everything. The depthMaterial shader is a pretty standard unlit material that renders black with a geometry shader.

    Now, I tried to render a simple sphere at the same spot as the previous shader with this command buffer code. To simplify things, I use the same Camera for the matrix, but I deactivate it.

    Code (CSharp):
    1. var trsMatrix = Matrix4x4.TRS(playerPosition, quaternion.identity, Vector3.one);
    2. var vpMatrix = camera.projectionMatrix * camera.worldToCameraMatrix;
    3. CommandBuffer cmd = new CommandBuffer();
    4. cmd.SetRenderTarget(renderTexture);
    5. cmd.DrawMesh(sphereMesh, vpMatrix * trsMatrix, depthMaterial);
    6. cmd.ClearRenderTarget(true, false, Color.clear, 1);
    7. Graphics.ExecuteCommandBuffer(cmd);
    And this does not really work at all. It seems to render some black pixels, just not nearly similar to how it should be.

    So I have 2 theories:
    1. Something's wrong with my matrix
    2. The calls to drawingSettings.SetShaderPassName() (which I do not understand) are doing some stuff I need to find a way to replicate using the CommandBuffer.
    Any idea how I could do this?
     
    Last edited: Apr 4, 2023
  2. NergethicFW

    NergethicFW

    Joined:
    Nov 21, 2022
    Posts:
    5
    I've read in some other post that you were able to resolve your issue. Would you be willing to share the solution? :)
     
    Gasimo likes this.