Search Unity

Outer glow using particles

Discussion in 'General Graphics' started by Nicksaccount, Dec 14, 2018.

  1. Nicksaccount

    Nicksaccount

    Joined:
    Jan 10, 2013
    Posts:
    24
    Hi,

    I'm trying to create an outer glow using particles but can't work out how to keep the object from being hidden by the particles.

    I've tried using the particle sort hack option but it doesn't seem to affect another object. I want to always keep the glow behind the object, but the object can rotate and move freely in space, in front of, and behind other objects so I can't really just offset the particles a little behind the object.

    Is there a way to always sort the object above the particles, or perhaps only generate particles from vertices that aren't seen by the camera?

    Currently I've got:
    Untitled-2.png

    And I want to create (photoshopped):
    Untitled-1.png

    Thanks,

    Nick
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    That only affects how it sorts with other particle systems, not anything else, and only if those particles are transparent.

    You can change the sorting by modifying the materials' render queue. Specifically you'd need to change the sword's material to have a queue higher than the particles, or vice versa. But that's not necessarily what you really want to do here. Changing the sword material's queue to be part of the transparency queue range means it won't receive shadows anymore, and changing the particle's queue to the opaque range means that the particles will be hidden by the skybox.

    It would also be possible to only spawn particles from the back side of the sword, but that also won't actually fix the issue 100% here. To do it would require rebuilding a "back face" mesh in script every update, so it'd be expensive, and if the sword is rotated faster than the lifetime of the particles you're still going to have particles intersecting the sword or outright covering it.

    So ...

    Actually, you totally can do this. There are a few ways to go about it. The simplest method is to use the Particle System's Renderer > Pivot setting. Set the Z to a negative value.
    upload_2018-12-14_9-28-15.png

    This pushes the particles away from the camera, and thus behind the object. It'll also push it behind other things in the scene, like the character and the world if the Z value is too high, or other things are too close. It can also still be visible when the object is rotated such that the distance being pushed back doesn't clear the whole mesh (see Ethan's neck above). Larger push values also "shrink" the particles since they are just being moved away from the camera and will get smaller due to perspective.

    The next method would be to abuse the soft particle values. This is easiest to do when using the Particles/Standard Unlit shader, just increase the far fade to a relatively large value, but it can be done with the older particle shaders too (use a smaller Soft Particles Factor). This does require soft particles are enabled for your project though which you may, or may not want to do. It does has the same problem as offsetting using the particle's renderer settings in terms of also intersecting with other objects in the world, and still showing up when the distance isn't great enough, but at least it's a softer transition when it does intersect.


    Beyond this there are more complicated shader based approaches. You can push the particles back using a shader, which can be done in a way that they don't shrink, but the same intersection issues will persist. There's also the option of using stencils or destination alpha.

    To use stencils you'd need your sword to write to the stencil buffer, and you'd need the particles to test against the stencil buffer and not render anywhere the stencil has been written to by the sword. This means custom shaders on both the sword and particles, and requires you use the forward rendering path. However this approach means the location of the particles doesn't matter, you can even use the Pivot Z to push them towards the camera so they draw above other objects more readily, and the sword will still mask them.
     
  3. Nicksaccount

    Nicksaccount

    Joined:
    Jan 10, 2013
    Posts:
    24
    That's awesome, thanks bgolus!

    Just changing the pivot value works well for my use case so thankfully that's all I need but good to know there are other options.
     
    richardkettlewell likes this.