Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Need a method that is similar to CommandBuffer.DrawMesh, without camera settings

Discussion in 'Universal Render Pipeline' started by NarallandaKaratoga, May 7, 2024.

  1. NarallandaKaratoga

    NarallandaKaratoga

    Joined:
    Jul 11, 2017
    Posts:
    8
    I want to draw something to a RenderTexture, with materials and rectangle range provided.
    I originally think DrawMesh should considered the RenderTexture in normalized screen space, which is ranged from (0, 0) to (1, 1) or from (-1, -1) to (1, 1), and the matrix in parameter is for transforming mesh from local space to normalized space.

    Code (CSharp):
    1.  
    2. void OnEnable()
    3. {
    4.     RenderPipelineManager.beginContextRendering += this.OnRendering;
    5. }
    6.        
    7. void OnDisable()
    8. {
    9.     RenderPipelineManager.beginContextRendering -= this.OnRendering;
    10. }
    11.        
    12. void OnRendering(ScriptableRenderContext context, List<Camera> cameras)
    13. {
    14.     cmd = cmd ?? new CommandBuffer();
    15.     cmd.SetRenderTarget(renderTexture);
    16.     cmd.ClearRenderTarget(true, true, Color.red);
    17.     foreach(var entry in entries)
    18.     {
    19.         var trs = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
    20.         cmd.DrawMesh(ProtaUnityConstant.rectMeshOne, trs, entry.material, 0, 0);
    21.     }
    22.     context.ExecuteCommandBuffer(cmd);
    23.     cmd.Clear();
    24. }
    25.  
    However it doesn't work properly, I try to modify TRS matrix to fill the RenderTexture, and it needs to be 780 units width. I also found that the width required changes as my Scene window resizes, so I guess it uses sceneview camera's clip space.

    Is there any other way to draw a rect mesh on RenderTexture in normalized space? Or simply reset camera params?

    I had looked into CommandBuffer.Blit(). but I don't see any parameter for my TRS matrix to fit in.. It doesn't have an override where both material and scale&offset appears in parameter list.... kinda weird...
     
  2. NarallandaKaratoga

    NarallandaKaratoga

    Joined:
    Jul 11, 2017
    Posts:
    8
    Alright I found the solution...
    Code (CSharp):
    1.  
    2. cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
    3.