Search Unity

Character visible through other objects

Discussion in 'Shaders' started by NeonTanto, Jun 5, 2014.

  1. NeonTanto

    NeonTanto

    Joined:
    Jun 19, 2013
    Posts:
    156
    Ok, so the thing I'm trying to make is making unit visible through solid objects.
    I write simple shader (I remove all unnecessary):
    Code (CSharp):
    1.  
    2. SubShader {
    3.                 Tags { "RenderType"="Opaque" "Queue" = "Geometry+1"}
    4.                 LOD 200
    5.                
    6.                 Cull Back
    7.                 ZWrite Off
    8.                 ZTest Always
    9.  
    10.                 CGPROGRAM
    11.                 #pragma surface surf Overlap decal:blend
    12.                 #pragma target 3.0
    13.                
    14.                 sampler2D _OverlapText;
    15.                 half4 _OverlapColor;
    16.  
    17.                 struct Input {
    18.                         float2 uv_MainTex;
    19.                 };
    20.                
    21.                 half4 LightingOverlap(SurfaceOutput o, half3 lightDir, half3 viewDir, half atten)
    22.                 {              
    23.                         half3 returnColor = o.Albedo;                  
    24.                         return half4(returnColor, o.Alpha);                    
    25.                 }
    26.                
    27.                 half4 LightingOverlap_PrePass(SurfaceOutput o, half4 light)
    28.                 {
    29.                         fixed4 c;
    30.                         c.rgb = o.Albedo;
    31.                         c.a = o.Alpha;                
    32.                         return c;      
    33.                 }
    34.  
    35.                 void surf (Input IN, inout SurfaceOutput o) {
    36.                         half4 c = tex2D (_OverlapText, IN.uv_MainTex);
    37.                         o.Albedo =  c.rgb * _OverlapColor.rgb;
    38.                         o.Alpha = c.a * _OverlapColor.a;
    39.                 }
    40.                 ENDCG                  
    41.                        
    42.        
    43.                 Cull Off
    44.                 ZWrite on
    45.                 ZTest LEqual
    46.  
    47.                 CGPROGRAM
    48.                 #pragma surface surf Lambert
    49.                 #pragma target 3.0
    50.                
    51.                 struct Input {
    52.                         half2 uv_MainTex;
    53.                         half3 viewDir;
    54.                 };
    55.  
    56.                 void surf (Input IN, inout SurfaceOutput o) {
    57.                        
    58.                         half4 c = tex2D (_MainTex, IN.uv_MainTex);                    
    59.                         o.Albedo = c.rgb * _Color.rgb;
    60.                         o.Alpha = _Color.a * c.a;
    61.                 }
    62.                 ENDCG          
    63.         }
    64.  
    That's what the goal looks like in forward renderer:

    Thats fine! (Cubes queue is set to "Geometry")
    Than I add another character:

    Thats fine too! Spidys don't show through each other.
    But when using deferred renderer shader is behaving unpredictably.
    For example I set to Cubes are same shader ("Geometry+1"). And I see Spider Man number one throw Spider Man number two (it's wrong), but i dont see them throw cubes:

    It looks to act completely random. What I'am doing wrong?

    P.S. There is a chance that problem is due to the faulty Spidy model, since none of standart cubes or spheres were shown through each other with this shader.
     
  2. glacius3000

    glacius3000

    Joined:
    Oct 5, 2012
    Posts:
    69
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It's not completely random, but the issue is probably that you haven't defined the order very well. I think you want to do an initial pass with ZTest Greater and then a regular pass with ZTest LEqual. These should be in Geometry+1 and Geometry+2, respectively.
     
  4. NeonTanto

    NeonTanto

    Joined:
    Jun 19, 2013
    Posts:
    156
    Yep. I agree with you... but I don't know how make two different pass in one Subshader with different queue... Or maybe how render object (with more than one submesh) with two materials without additional camera.