Search Unity

Bug Graphics.RenderMesh doesn't work in URP

Discussion in 'Universal Render Pipeline' started by arkano22, May 20, 2023.

  1. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    What the title says. Graphics.RenderMesh does absolutely nothing in URP/HDRP, however it works fine in the Built-in pipeline.

    Graphics.DrawMesh however, seems to work fine in all pipelines.

    Unity 2021.3.4f1, URP 12.1.7. Has anyone encountered this?
     
    Last edited: May 20, 2023
    mariluu and bart_artanim like this.
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    I think I found the culprit, here's the solution for those that hit the same pitfall:

    The default values for the RenderParams struct (that Graphics.RenderMesh takes as input) work for the built-in pipeline but don't work at all for SRPs.

    If you create a RenderParams instance using its constructor, the value of RenderParams.renderingLayerMask is initialized to a value other than 0. However if you create a RenderParams instance using the default constructor (which uses the default value of each type to initialize the struct's members), renderingLayerMask gets the default value for unsigned ints (zero) so bitwise '&' with any layer will always yield 0, ensuring nothing gets rendered.

    So what if you want to create a RenderParams struct without passing a material to its constructor? then you need to explicitly set the value of renderingLayerMask to GraphicsSettings.defaultRenderingLayerMask, otherwise RenderMesh won't render anything.

    I'm reporting this as a bug since:
    - It behaves differently depending on the render pipeline being used.
    - The default values don't work at all in SRPs, it stands to reason than assigning a mesh and a material should be all that's necessary for this to work.
    - The need for using a specific constructor or populate the struct yourself with values other than the defaults isn't mentioned anywhere in the documentation for either RenderMesh or RenderParams, should at least be mentioned in RenderParam's constructor whose only documentation as of writing this is "Constructor." - duh.

    Stumbled upon the solution by mere chance, was ready to mark "RenderMesh" as broken and move on to DrawMesh instead.
     
    Last edited: May 20, 2023
  3. bart_artanim

    bart_artanim

    Joined:
    Jul 18, 2018
    Posts:
    19
    Okay, I was going to tell you "thank you ever so much because I ran into this exact issue with an asset I'm using" ... but you would know, because you are developing the asset. Anyway, enjoy my emails pointing you to your own post :D
     
    Sluggy likes this.