Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Transparency sorting issue with custom shader

Discussion in 'Shaders' started by OddViz, May 6, 2021.

  1. OddViz

    OddViz

    Joined:
    Dec 17, 2019
    Posts:
    9
    Hi!
    Disclaimer: I'm a complete noob at shaders, please be patient with me! :oops:

    I've been using Keijiro's Point Cloud Renderer on Github to render point clouds, and it's working well.
    However, these points (using both the disk and point shaders) do not interact correctly with other, transparent objects in the scene. Even just a simple box with the standard shader set to Transparent rendering mode looks wrong - the points are always in front of the object, even though they are on the same layer.


    Can you tell which points are inside or behind the box?

    I realize this is likely to do with how the shader is written but I have no idea where to start.
    I tried messing around with the SubShader's Queue and RenderType tags to no avail.
    Please help me figure out what I should be doing!

    The shaders can be found here, I'm using the Disk one.

    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,363
    Not actually a problem with shaders at all. It's a problem with how Keijiro is rendering the point cloud. That project renders the point cloud using
    OnRenderObject()
    , which renders after the rest of the scene has rendered. This means it'll render on top of any transparent objects since for transparency sorting to work properly opaque objects need to render before the transparent ones, and the transparent objects need to be sorted prior to rendering.

    To fix this you'd need to modify the c# code to use a command buffer that uses
    CameraEvent.AfterForwardOpaque
    instead of
    Graphics.DrawProceduralNow()
    .
     
    OddViz likes this.
  3. OddViz

    OddViz

    Joined:
    Dec 17, 2019
    Posts:
    9
    THANK YOU! That pointed me in the right direction, I didn't even think the issue was with the renderer rather than the shader itself. I've changed the the code to use the CommandBuffers API and it works!
    Interesting thing is, it's still using OnRenderObject. I've seen examples using OnWillRenderObject for this pipeline, but that never gets called in my case. Still, it works, so I've got no complaints. Thanks again!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,363
    For a command buffer with static data, you really just need to add it to the camera once in an OnEnable() and you're done. The only reason to do anything more than that is you want to stop rendering it, in which case you'd want to remove it from the camera.

    If you're calling
    camera.AddCommandBuffer()
    every
    OnRenderObject()
    you're going to have a bad time.
     
  5. OddViz

    OddViz

    Joined:
    Dec 17, 2019
    Posts:
    9
    You're definitely right, but unfortunately my data isn't static and needs to be updated every frame, so I have to remove and re-add the command buffer on each
    OnRenderObject()
    call.
     
  6. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429

    Hi,

    I'd be interested in the PointCloudRenderer script you've modified, I can't get it to render transparency properly still...

    Thanks,

    Jean