Search Unity

Question How do I get ObjectIDs in HDRP?

Discussion in 'High Definition Render Pipeline' started by fct509, May 13, 2021.

  1. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    Does anyone know how to get pull things like the Object ID, Primitive ID, or Instance ID by using a Custom Pass System in HDRP 7.5. I came across a post 8 months ago from someone that claimed they did it, but they didn't actually say how (and I can't seem to find the post anymore).

    I think it's possible on a technical level, but I'm a bit lost on what to do. The official documentation for HDRP isn't really much help in this.
     
  2. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    764
    Hey, let me give you some pointers on how to achieve that.
    You will need 3 things;

    First, you need a script that assign a color to each renderer in your scene. Here's an example that assign a random color each one and put that color in a new property called "ObjectColor". However, you can pick a color based on the material instance ID or the shader ID for example :
    Code (CSharp):
    1. var RendererList = Resources.FindObjectsOfTypeAll(typeof(Renderer));
    2.  
    3.         System.Random rand = new System.Random(3);
    4.  
    5.         foreach (Renderer renderer in RendererList)
    6.         {
    7.             MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
    8.             float hue = (float)rand.NextDouble();
    9.             propertyBlock.SetColor("ObjectColor",  Color.HSVToRGB(hue, 0.7f, 1.0f));
    10.             renderer.SetPropertyBlock(propertyBlock);
    11.         }
    Then you will need a new shader graph that will retrieve that color and assign it to the base color of the object. You can create a very simple unlit and link that to the base color. Then create a material from this by right clicking on the graph and select "Create > Material"

    upload_2021-5-14_10-18-3.png

    Lastly, you need to setup your custom pass volume and output the target color buffer to the camera to be able to see something happening.

    upload_2021-5-14_10-22-48.png

    If you do not want to display that on the screen but export them as image to be used later you can set it to custom and use a script to write them to disk for example using this API.

    Hope that helps.
     
    Last edited: May 14, 2021
  3. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    Thanks, I'm going to have to give this a try over weekend to see if I can get it to work.

    But, as for the API, that's not actually useful for Unity 2019.4 LTS because Unity 2019.4 doesn't have that API. The AOV API is present for Unity 2020.3 LTS, even if the documentation sucks. Yet, the project I'm working on, is built on Unity 2019.4 and HDRP 7.X. Newer features that are in HDRP 10.X aren't applicable, this is why I mentioned the general versions of Unity and HDRP that I'm using.

    But, as for the idea of trying to use the Renderer to set the color, thank you. I am going to have to try that.
     
  4. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    So, I took a closer look, and it turns out that the AOV API wasn't added to Unity with 2020.2. It's even part or HDRP 7.4 and Unity 2019.4. It was only added to the manual with Unity 2020.2 and some ability to work with Custom Passes was added to the API.

    To, @chap-unity, sorry for saying that the AOV API was not applicable. And, sorry for the much, much meaner things I was thinking.
     
    chap-unity likes this.
  5. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    So could anybody actually get this to work? And does anybody have an actual step by step on how to achive this? Not just snippets of code and not saying how it all ties together?

    For example, I'm on 2021.2.0b16 with HDRP 12 and Raytracing. I need to get a pass like this out with Recorder, or any other way that is native to Unity

    upload_2021-10-18_10-16-36.png

    It's such a common pass in compositing and VFX, Architectural work, that I'm surprised something like that does just simply ship already made. Why do we have to keep re-inventing the wheel with Unity?!?

    Apologies for my tone. Deadline approaching and frustration setting in. Any time saved moving from traditional DCC rendering, to realtime, has been lost due to not being able to do the simplest thing by default, but by having to work around various workarounds, re-inventing the wheel. etc.

    If there's a good samaratin out there that could lend a hand on exactly how to get a pass like the one above, or at least show me how to do a mask for a single group of object instances, or a mask for a single material instance, I would greatly appreciate it!!!
    (taking into account we can put basic code together, but we're artists, not a coders)
     
    fct509 likes this.
  6. Matjio

    Matjio

    Unity Technologies

    Joined:
    Dec 1, 2014
    Posts:
    108
    newguy123 likes this.
  7. vladala

    vladala

    Unity Technologies

    Joined:
    Mar 3, 2017
    Posts:
    189
    Hello,

    Unfortunately I cannot do better than the previous posts suggesting code snippets.
    Supporting a new AOV pass in the recorder is not straight forward enough to fit on a forum post.
    I have added it to our recorder backlog and will do my best to prioritize it.

    Sorry that I was not able to be more helpful.
     
    Ruchir and HIBIKI_entertainment like this.
  8. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595
    Good to know this at least, and good luck moving forward
     
  9. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
  10. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    I managed to get a Custom Pass ObjectID out! Super Cool!

    However, I have raytracing enabled and so using deferred. There's an error about Forward only.
    upload_2021-10-20_0-11-44.png
     
  11. Matjio

    Matjio

    Unity Technologies

    Joined:
    Dec 1, 2014
    Posts:
    108
  12. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    Any Idea why objects spawned with Vegetation Studio Pro are not affected by Custom Pass?
    I checked layers of the VSPro object, I checked the original layers of the tree prefabs used in spawned, but not seeing why it would not be afffected...

    upload_2021-10-20_18-11-52.png
     
  13. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    764
    Shot in the dark: could that be because they are spawned inside a layer that is unnamed by any chance?
     
  14. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    no idea, how can I check that?
    also in my custom pass, I said to use "Everything", so theoretically that should use all layers, including unnamed ones isnt it?
     
  15. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    No I dont think that's it. In its settings it says everything is on default layer:
    upload_2021-10-20_18-53-11.png
     
    chap-unity likes this.
  16. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    Maybe because they're instanced or indirect instanced?
     
  17. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    Bingo!
    If I set them to normal, then it works as expected. Why is that I wonder....
    upload_2021-10-21_1-0-56.png
     
  18. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    because life is hell :)
    Instanced and indirect instancing also doesn't get motion vectors, so TAA turns into a ghost town...
    And you basically NEED instancing for vegetation.
     
  19. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    LOL :)

    I've actually been very happy with how DLSS is working for me. No more TAA for me!
     
  20. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    Ideally everyone would have RTX and use DLSS :(, but if your making a game and it's intended for the masses, TAA would be the best choice available
     
    fct509 likes this.
  21. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    Just circling back to this.

    Isnt that then an issue with the Custom Pass? ie it Doesnt detect or render Instanced or Instanced Indirect objects?
    Surely it should be able to deliver a custom pass to anything and everything?

    Then tying this back in with my situation, setting VSPro to normal mode, somehow makes the custom pass work. However VSPro normal mode uses Graphics.DrawMesh.
    Raytracing limitations says that Graphics.DrawMesh is NOT supported. But then, how on earth does it work fine in my case, especially with custom pass, and the other methods does not?!?
     
    Last edited: Oct 21, 2021
    daneobyrd and PutridEx like this.