Search Unity

Help needed on CustomPass pixel picking to RenderTexture

Discussion in 'High Definition Render Pipeline' started by andrea_i, Mar 2, 2020.

  1. andrea_i

    andrea_i

    Joined:
    Nov 18, 2012
    Posts:
    32
    Hello! I could really use some help, I've been stuck on this for a few days now.

    (marginally important context, I'm porting this project over to HDRP: https://andrea-intg.itch.io/clayxels ).

    My aim is to render some flat colors on a texture and read them back at specific coords, this is to do pixel-picking on my proceduraly drawn primitives.
    I had this working on the legacy pipeline using OnRenderImage, but on HDRP I'm really struggling to understand what I should be doing.

    EDIT:
    I first tried with a custompass, did not work but then someone suggested I should be using commandbuffers for this.
    All I'd need to know is if there's something obviously wrong about this piece of code.

    EDIT again:
    I thought it worked, but I was just reading some other randomly active rendertexture.
    After properly setting RenderTexture.active = pickingRenderTexture, I get only black pixels.

    Code (CSharp):
    1.  
    2. Texture2D pickingTextureResult = new Texture2D(1, 1, TextureFormat.ARGB32, false);
    3. Rect rectReadPicture = new Rect(this.mousePosX, this.mousePosY, 1, 1);
    4.  
    5. RenderTexture pickingRenderTexture = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32);
    6.  
    7. RenderTargetIdentifier rtID = new RenderTargetIdentifier(this.pickingRenderTexture);
    8.  
    9. CommandBuffer commandBuffer = new CommandBuffer();
    10. commandBuffer.SetRenderTarget(rtID);
    11. commandBuffer.ClearRenderTarget(true, true, Color.clear, 1f);
    12.  
    13. commandBuffer.DrawProceduralIndirect(new Matrix4x4(), myMat, 0, MeshTopology.Triangles, myIndirectDrawArgsBuffer);
    14.  
    15. Graphics.ExecuteCommandBuffer(commandBuffer);
    16.  
    17. RenderTexture.active = this.pickingTextureResult;
    18. this.pickingTextureResult.ReadPixels(this.rectReadPicture, 0, 0);
    19. this.pickingTextureResult.Apply();
    20.  
    21. Color32 pickCol = this.pickingTextureResult.GetPixel(0, 0);
    22. Debug.Log(pickCol);
    23.  
    24. RenderTexture.ReleaseTemporary(this.pickingRenderTexture);
    25.  
     
    Last edited: Mar 3, 2020
  2. andybak

    andybak

    Joined:
    Jan 14, 2017
    Posts:
    569
    I'm personally very much looking forward to the HDRP compatible release of Clayxels so I'll second this request!

    @Remy_Unity @SebLagarde or anyone?
     
    andrea_i likes this.
  3. andrea_i

    andrea_i

    Joined:
    Nov 18, 2012
    Posts:
    32
    The current status is as follows:
    I get meaningful values from the rendertarget if I use CommandBuffer.DrawRenderer on some geometry, but not with CommandBuffer.DrawProcedural.
    I've stripped down my shader to just draw a triangle, there's no indirect args involved, no structuredBuffer, to make sure everything checks out I'm also drawing the same shader from the same material with Graphics.DrawProcedural.
     
  4. andrea_i

    andrea_i

    Joined:
    Nov 18, 2012
    Posts:
    32