Search Unity

Camera Culling Mask help!

Discussion in 'Editor & General Support' started by Khyrid, Jul 28, 2012.

  1. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    I am using three different cameras for my first person controller.

    Main Camera: Culls Weapon Layer and UnderWater Layer, depth 0

    Weapon Camera: Cull all except Weapon Layer, depth 1

    UnderWater Camera: Cull all except UnderWater Layer, depth 2

    -I have tried every combination of settings I can and the Underwater layer makes the Weapons layer disappear entirely.

    -The only object with the UnderWater tag is this plane with an underwater effect shader on it that fills the camera's view. This plane's mesh renderer is off by default and set to on when entering under water.

    -Before the plan'es renderer is set to on the UnderWater Camera does not effect the visibility of the Weapon Layer.

    -As soon as it turns on the Underwater effect looks nice and the Main Camera renders everything fine, but the Weapon Camera doesn't render anything.

    -Even after leaving water and the plane has the renderer turned off, the Weapon layer still wont show the weapons or anything.

    -I set the Weapon layer to depth 4 to test, it showed the weapon and everything else black, some water shader effect was on the gun.

    -I turned off the Weapon Camera and nothing from any camera rendered, all was black.

    -I set the weapon Camera to cull all layers and the two other camera rendered fine. So if the Weapon camera doesn't render anything, than the rest works fine, but if i turn it off, everything is black??? WTH?

    Is Unity just buggy and messed up or am I doing something wrong? Could it be the fault of this shader I'm using? All the problems seems associated with when the under water shader turns on.
     
    Last edited: Jul 28, 2012
  2. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Bumped for great justice!

    Here is the Code for the under water shader I'm using:

    Code (csharp):
    1. // Upgrade NOTE: replaced 'glstate.matrix.modelview[0]' with 'UNITY_MATRIX_MV'
    2. // Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
    3.  
    4. Shader "UnderWater" {
    5.     Properties {
    6.         _NoiseTex ("Noise Texture (RG)", 2D) = "white" {}
    7.         strength("strength", Range(0.01, 0.1)) = 0.2
    8.         transparency("transparency", Range(0.01, 0.1)) = 0.05
    9.     }
    10.      
    11.     Category {
    12.         Tags { "Queue" = "Transparent+10" }
    13.         SubShader {
    14.             GrabPass {
    15.                 Name "BASE"
    16.                 Tags { "LightMode" = "Always" }
    17.             }
    18.            
    19.             Pass {
    20.                 Name "BASE"
    21.                 Tags { "LightMode" = "Always" }
    22.                 Lighting Off
    23.                 Cull Off
    24.                 ZWrite On
    25.                 ZTest LEqual
    26.                 Blend SrcAlpha OneMinusSrcAlpha
    27.                 AlphaTest Greater 0
    28.              
    29.              
    30.     CGPROGRAM
    31. // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members distortion)
    32. #pragma exclude_renderers xbox360
    33.     #pragma vertex vert
    34.     #pragma fragment frag
    35.     #pragma fragmentoption ARB_precision_hint_fastest
    36.     #pragma fragmentoption ARB_fog_exp2
    37.     #include "UnityCG.cginc"
    38.      
    39.     sampler2D _GrabTexture : register(s0);
    40.     float4 _NoiseTex_ST;
    41.     sampler2D _NoiseTex;
    42.     float strength;
    43.     float transparency;
    44.      
    45.     struct data {
    46.         float4 vertex : POSITION;
    47.         float3 normal : NORMAL;
    48.         float4 texcoord : TEXCOORD0;
    49.     };
    50.      
    51.     struct v2f {
    52.         float4 position : POSITION;
    53.         float4 screenPos : TEXCOORD0;
    54.         float2 uvmain : TEXCOORD2;
    55.         float distortion;
    56.     };
    57.      
    58.     v2f vert(data i){
    59.         v2f o;
    60.         o.position = mul(UNITY_MATRIX_MVP, i.vertex);      // compute transformed vertex position
    61.         o.uvmain = TRANSFORM_TEX(i.texcoord, _NoiseTex);   // compute the texcoords of the noise
    62.         float viewAngle = dot(normalize(ObjSpaceViewDir(i.vertex)),
    63.                              i.normal);
    64.         o.distortion = viewAngle * viewAngle;   // square viewAngle to make the effect fall off stronger
    65.         float depth = -mul( UNITY_MATRIX_MV, i.vertex ).z;  // compute vertex depth
    66.         o.distortion /= 1+depth;        // scale effect with vertex depth
    67.         o.distortion *= strength;   // multiply with user controlled strength
    68.         o.screenPos = o.position;   // pass the position to the pixel shader
    69.         return o;
    70.     }
    71.      
    72.     half4 frag( v2f i ) : COLOR
    73.     {  
    74.         // compute the texture coordinates
    75.         float2 screenPos = i.screenPos.xy / i.screenPos.w;   // screenpos ranges from -1 to 1
    76.         screenPos.x = (screenPos.x + 1) * 0.5;   // I need 0 to 1
    77.         screenPos.y = (screenPos.y + 1) * 0.5;   // I need 0 to 1
    78.      
    79.         // check if anti aliasing is used
    80.         if (_ProjectionParams.x < 0)
    81.             screenPos.y = 1 - screenPos.y;
    82.        
    83.         // get two offset values by looking up the noise texture shifted in different directions
    84.         half4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz);
    85.         half4 offsetColor2 = tex2D(_NoiseTex, i.uvmain - _Time.yx);
    86.        
    87.         // use the r values from the noise texture lookups and combine them for x offset
    88.         // use the g values from the noise texture lookups and combine them for y offset
    89.         // use minus one to shift the texture back to the center
    90.         // scale with distortion amount
    91.         screenPos.x += ((offsetColor1.r + offsetColor2.r) - 1) * i.distortion;
    92.         screenPos.y += ((offsetColor1.g + offsetColor2.g) - 1) * i.distortion;
    93.        
    94.         half4 col = tex2D( _GrabTexture, screenPos );
    95.         col.a = i.distortion/transparency;
    96.         return col;
    97.     }
    98.      
    99.     ENDCG
    100.             }
    101.         }
    102.     }
    103.      
    104.     }
     
    Last edited: Jul 29, 2012
  3. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Please
     
  4. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Please, this is a project killing issue.
     
  5. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Anybody.
     
  6. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Should I put the shader in a script on the camera instead of putting it on a plane as a child object of the camera?
     
  7. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    Maybe someone would be more likely to assist if this was in the ShaderLab section of the forum?
     
  8. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Yeah, I didn't realize it was a shader problem at first. Maybe if a mod happens along they can move my post there, otherwise I'll start a new one later on.