Search Unity

Question Per-particle sorting?

Discussion in 'Visual Effect Graph' started by Quasimodem, Oct 11, 2022.

  1. Quasimodem

    Quasimodem

    Joined:
    May 21, 2013
    Posts:
    52
    Hi everyone, hoping to get some help with transparency sorting in our VFX system.

    We're trying to address a longstanding issue with how our particles layer with our sprites in our top-down 2D game. Our VFX system relies on instantiating single particle systems (now Visual Effect components) and telling them to emit single particles at specific positions when needed.

    The issue we're having is that all particles from a given emitter are either entirely behind or entirely in front of all sprites on the same sorting layer. Our sprites are sorted along the z-axis, as configured in the 2D renderer component, and we'd like our particles to play by the same rules. This would, for example, allow a bomb blast that goes off in a patch of grass to be interleaved correctly between the clumps of grass - behind some, in front of others.

    Is this possible with either the legacy system or the new visual graph system using this single-emitter strategy? Or are we consigned to pay the overhead of having a whole new emitter instance at the correct position, just to emit a single correctly-layered particle? The concern there is obviously that we may need many 100s of emitter instances, which seems like it's going to quickly blow through our budgets.

    Thanks in advance -
     
  2. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    Morning.
    You got a bunch of Sorting options when using VFXGraph.
    On the "per-particle" level you can find the options in the Inspector when selecting the "Output" of your System.
    upload_2022-10-12_9-33-2.png

    By default it's using the Distance to camera, and as it said the Particles are sorted within the same system based on the Distance to the camera.
    Distance_to_cam.gif

    But you can also use the other options like, based on Lifetime with "Youngest in Front", or the "Custom" mode to sort the Particles as you like.
    When in "Custom" mode , a new Float input can be used in the "Output Context", allowing you to Set the Sorting.
    You can use the "ParticlesID", or make some math based on the Position, or use any custom particle Attribute. It really depend on what you need.
    upload_2022-10-12_9-46-11.png
     
    lilacsky824 likes this.
  3. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    Now, Each system can have multiple "Output Context".
    But there's no notion of Camera distance per Output as the Bound are handle globally per System.
    So you can have sorting issues like these:
    Unity_Z3UlBNWE7a.gif

    You still can "force" the Output Sorting, in the Inspector by changing the "Sort Priority" Option:
    upload_2022-10-12_9-54-14.png
     

    Attached Files:

    lilacsky824 likes this.
  4. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    Each VFXGraph should be properly Sorted based on their Bounding Box. Unity_wef5oOjECQ.gif

    On the example below you can see that I've changed the bounding Box, position resulting in a an incorrect Sorting between VFXGraph components.
    Unity_APKpSLK0AJ.gif

    To finish on the Sorting Options , two other options can be leveraged:
    The "Output Render Order" of a VFXGraph asset , where you can reorder the Sorting of the different Outputs:

    upload_2022-10-12_10-43-51.png
     
    lilacsky824 likes this.
  5. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    And the "Sorting Layer" and "Sorting Priorities" in the Layer.
    upload_2022-10-12_10-47-13.png

    I'm hoping that this overview of the Sorting parameters will help you in your Sorting Quest.
    Sadly I'm still lacking experience with URP and 2D renderer so I hope my answer won't be too generic.

    If you still have issues, don't hesitate to post again and I will poke someone with more experience on this subject.
    Have a great day
     
    XieJiJun and lilacsky824 like this.
  6. Quasimodem

    Quasimodem

    Joined:
    May 21, 2013
    Posts:
    52
    Hi OrsenFavrel, thank you for the extremely detailed response! Easily the most detail I've ever gotten on these forums.

    These ordering features seem to be in a newer version of Visual Graph than I'm on, but they're great to know about.

    As I dug further into the problem, I hit a wall I could not get over through traditional means. Our layering landscape is too complex to be handled by any of the default Distance to Camera/Ortho/Perspective/Custom Axis/etc sorting options and we have no alternative but to continue sorting manually by using the sortingOrder property. This has worked great for us but makes the problem of layering in particles even more tricky.

    However, with some creative use of the depth buffer, I think I've solved the problem. Our in-game entities move along the X and Z axis, but if I add a Y element proportional to their Z value (e.g. lower Y value towards the top of the screen) and make them write to the depth buffer, I can then turn on LEqual for my particle depth buffer test (also being sure to emit particles according to this same Y-axis rule) and viola! Particles now appear to sort correctly among a complex group of sprites, each with a different sortingOrder.

    This solution seems to have checked all the boxes; explicit layering control via sortingOrder, and the ability to emit single particles where needed from a single emitter and have them layer correctly with everything else. Our game gets really bombastic and I know we will be unable to afford a huge Visual Effect instance count on low-end systems, so this was key.

    Thanks again for the response
     
    OrsonFavrel likes this.
  7. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    Well I'm glad to hear that you found a solution to your issue. If by any chance at some point in time, you expose in detail your problem and how you solved it to the world (Blog Post, Twitter thread etc...) I'll be happy to read/watch it.

    Have a great day.