Search Unity

SkyBox doesn't clear with Greater ZTest effect

Discussion in 'Shaders' started by obviousjim, Jan 16, 2011.

  1. obviousjim

    obviousjim

    Joined:
    Oct 11, 2009
    Posts:
    29
    Hello,

    I'm trying to do a bit of at trick to get silhouette reflections on arbitrary shaped mesh (for the ocean shader project)

    inspired by this tutorial:
    http://www.burgzergarcade.com/tutorials/game-engines/unity3d/unity-ios-shaderlab-tutorial-9-ztest

    The strategy is to render a reflection pass with all the objects flipped and only showing the bits that are underneath the water, which will create a nice reflection texture to apply to the water's surface.

    I have an inverted (flipped upside down) instance of my model rendered for this pass, that is occluded by the ocean layer using ZTest Greater so that only the vertices underneath the ocean show up,

    right now i'm testing with a plane, but will replace it with a dynamic mesh. When i view the scene camera it looks fine, but when i view it with the in-game camera any areas covered by the plane are not refreshed and get color smearing as the model animates.

    if I turn off the skybox and use a clear color instead it works. What is different about how the skybox renders as opposed to solid color? is there a way to replicate the way the skybox clears?

    I've attached the shaders i'm using and some scene screensots below. Thanks for any help, I understand this is a bit of a weird one.

    Material applied to the ocean:
    Code (csharp):
    1.  
    2. Shader "Masked/Reflection Pass Ocean" {
    3.    
    4.     SubShader {
    5.         // Render the mask after regular geometry, but before masked geometry and
    6.         // transparent things.
    7.        
    8.         Tags { "Queue" = "Geometry" }
    9.        
    10.  
    11.        
    12.         // Turn off lighting, because it's expensive and the thing is supposed to be
    13.         // invisible anyway.
    14.        
    15.         Lighting Off
    16.  
    17.         // Draw into the depth buffer in the usual way.  This is probably the default,
    18.         // but it doesn't hurt to be explicit.
    19.  
    20.        ZTest LEqual
    21.        
    22.        ZWrite On
    23.        
    24.         // Don't draw anything into the RGBA channels. This is an undocumented
    25.         // argument to ColorMask which lets us avoid writing to anything except
    26.         // the depth buffer.
    27.  
    28.         ColorMask 0
    29.        
    30.         // Do nothing specific in the pass:
    31.  
    32.         Pass {}
    33.     }
    34. }
    35.  
    Material applied to the objects in the scene

    Code (csharp):
    1.  
    2. Shader "Masked/Reflection Pass Object " {  
    3.  
    4.     Properties {
    5.         _Color ("Main Color", COLOR) = (1,1,1,1)
    6.     }
    7.  
    8.  
    9.     SubShader {
    10.         // Render the mask after regular geometry, but before masked geometry and
    11.         // transparent things.
    12.        
    13.         Tags { "Queue" = "Geometry+10" }
    14.        
    15.  
    16.         Pass {
    17.             Color [_Color]
    18.  
    19.            Cull Front            
    20.            ZTest Greater
    21.                ZWrite Off
    22.        
    23.         }
    24.     }
    25. }
    26.  
    and some screen shots below
     

    Attached Files: