Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How To Occlude Post Effect Objects ?

Discussion in 'Image Effects' started by TreasureMap, Jun 13, 2016.

  1. TreasureMap

    TreasureMap

    Joined:
    Apr 23, 2014
    Posts:
    14
    I have a glow post effect that is being applied to some weapons in my scene.
    Unfortunately, the glow does not get occluded by foreground and background scene objects.
    I was wondering if anyone knows of a way to fix this ?

    PostGlow_No_Z_Occlusion.png PostGlow_No_Z_Occlusion_2.png

    I have some foreground and background objects (floor, 3 capsules, a wall) set to layer Default
    I have 2 Weapons Set To Layer "Glow". The weapons are sticking half way into the floor.

    I have 3 Scene cameras:
    Depth 0: Main Camera - Sees Default Layer - Clear Solid Color (Floor, capsule, wall)
    Depth 1: Glow Camera - Sees Nothing - Don't Clear (Renders Weapon Glow to RT, uses command buffer)
    Depth 2: Weapon Camera - Sees Glow Layer - Don't Clear (Renders Weapons on top of glow)


    Every thing looks fine except the glow part which is not getting properly occluded.
    I have included some sample pics for reference.
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    Does your shader here do z testing? Take a look at the frame debugger to see the state of rendering at this step.
     
  3. TreasureMap

    TreasureMap

    Joined:
    Apr 23, 2014
    Posts:
    14
    The camera's in my scene render a depth texture.
    The objects in my scene were using Unlit/Color and Unlit/Texture.

    For testing, I had my glow post effect shader do a depth pass only.
    I have some occlusion objects (cylinders) in my scene that I put in front of my weapons.
    Unfortunately there was still no occlusion.
    Then I changed the cylinder's shader to "Legacy Shaders/VertexLit" and finally got occlusion!
    I could finally see the cylinder pass in front of my weapons in the Depth pass.

    For some reason, the shader "Unlit/Color" does not seem to write to the depth buffer (Even with ZWrite On!)
    But the shader "Legacy Shaders/VertexLit" does. I got depth and occlusion with that shader.
    It seems to have something to do with the ShadowCaster pass.
    If I include a ShadowCaster pass to the Unlit shaders (color/texture), my effect works.

    My question is, why did I have to add the shadow caster pass to Unlit/Color and Unlit/Texture to get depth and occlusion to work properly ?
     

    Attached Files:

  4. TreasureMap

    TreasureMap

    Joined:
    Apr 23, 2014
    Posts:
    14
    Now I have a new problem. In the QualitySettings, if I set Anti Aliasing to Disabled, the camera that renders the weapon on top of the glow stops working and glitches badly. I can only see the glow part.
    The glow effect only seems to work properly when Anti Aliasing is on.
    Seems to have to do with the Camera AA Resolve - GrabRenderTexture op.
    Camera_AA_Resolve_GrabRenderTexture.png

    I need a way to get it to work without Anti Aliasing on. Appreciate any feedback you can give me.

    Info:
    MainCamera - Depth 0 - Clear Solid Color -
    Sees Everything But Weapon Layer Objects. Renders Weapon Glow Effect.

    WeaponCamera - Depth 1 - Don't Clear -
    Sees Only Weapon Layer Objects. Renders Weapon On Top Of Glow.


    EDIT**
    Workaround (Effect is working, even with Anti Alias off)

    PreClear Camera - Depth 0, Clear Solid Color, Culling Mask - Nothing

    Main Camera - Depth 1, Don't Clear, Culling Mask - All but Glow Layer Weapons.
    Renders Weapon Glow To RT, composite with Blit(source, destination, compositeMaterial)

    Weapon Camera - Depth 2, Don't Clear, Culling Mask - Glow Layer Weapons
    Renders Weapon on top of Glow.
     
    Last edited: Jun 16, 2016
  5. TreasureMap

    TreasureMap

    Joined:
    Apr 23, 2014
    Posts:
    14
    After some more research, I discovered something unusual about Unity shaders
    and that I DIDN'T need to add a ShadowCaster pass to the Unlit/Color, Unlit/Texture shaders to get the occlusion.

    All I had to do to get the glow effect to properly occlude with scene objects was to add Fallback "Diffuse" to the Unlit shaders (Color,Texture).
    And voila, I got proper occlusion using the Unlit shaders. I don't know why it worked, but it did.

    So, If you are trying to make a glow post effect, and can't get occlusion on front and back scene objects,
    try adding Fallback "Diffuse" to the scene object shaders.