Search Unity

Question Custom Shader Misplacement

Discussion in 'General Graphics' started by MisterSixtyFour, Jan 23, 2023.

  1. MisterSixtyFour

    MisterSixtyFour

    Joined:
    Jun 28, 2021
    Posts:
    60
    I'm working on this laser for a video game, and it has a ring and the beam part.
    Both the ring and beam use the same custom shader: https://github.com/greggman/hsva-unity

    However, how do I make the rings go around the beam instead of behind or in front of it using the same shader?
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,924
    Short answer: you can't, at least not with your current object setup.

    Long answer: transparent objects using alpha blending won't give correct results if they intersect, because the alpha blending operation is not commutative: objects need to be sorted from back to front based on their distance to the camera, so that objects that are "behind" other transparent objects get drawn first. This is called the "painter's algorithm".

    However, if two objects intersect (as is the case with your beam and rings), it's not possible to tell which one is closer to the camera since some parts of the ring are closer than the beam, and some parts are further away that the beam.

    If the camera is not going to move around too much (eg. if this is a 2D game), you can split your rings in two separate objects: the part that goes behind the beam, and the part that goes in front. Then, you can use their material's render priority so that they're drawn in this specific order: "ring_behind", "beam", "ring_front". That will give correct results.
     
    c0d3_m0nk3y likes this.
  3. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    arkano already gave a great answer. Just want to add, there are some engines which support Order Independent Transparency (OIT) but Unity isn't one of them. That's because it comes with caveats, usually.

    You could try to use additive blending instead of alpha blending since this is supposed to be light. Additive blending is commutative.

    You might have to change the art a bit to make it look good, though. Maybe like this.
     
    Last edited: Jan 25, 2023
    arkano22 likes this.