Search Unity

Sorting transparent objects against sea plane - Response to twitter

Discussion in 'General Graphics' started by WikkidEdd1, Nov 25, 2017.

  1. WikkidEdd1

    WikkidEdd1

    Joined:
    Nov 17, 2015
    Posts:
    10
    @afender7 posted a problem on twitter. https://twitter.com/afender7/status/931284975181271041 and it was much easier to respond here.

    This is the original question.

    "Is there _any_ way to do/fake doing per-pixel overlapping of multiple translucent objects in Unity? E.g. in the video, both the water and the ghost-outline are translucent, but because the ghost is nearer to the camera, all of it displays as in front."

    There is a video in the original tweet, but I've made a gif which highlights the problem
    Problem.gif

    The main issue is that you've got a big semi-transparent water plane and semi-transparent objects intersecting with the water. Given the sorting for transparent objects is done on a per object basis you'll get one or the other drawing over each other which doesn't give the desired look.

    It sounded like the @afender7 was after a simple answer so I've knocked together what I think is the simplest approach which works for his use case. The fact that the water is just a plane helps a lot.

    The general idea is to draw the under water part of the objects first, then draw the water, then the above part of the object. I've written a shader which clips pixels depending on whether they are above or below the water's world Y value. I set the work

    I'm controlling render order by setting the Render Queue values on the materials

    2000 - Draw Opaque
    3000 - Draw Transparent Object - Discarding all pixels which are ABOVE water's Y value
    3001 - Draw Water
    3002 - Draw Transparent Object - Discarding all pixels which are BELOW water's Y value

    This is the end result:

    FakeBetterSorting.gif

    I've attached the unity package with the solution in. Hope this helps.
     

    Attached Files:

    richardkettlewell likes this.