Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Therian13 · Jul 19, 2015 at 03:47 AM · shadershaderscullingdepthmaskzwrite

shader material showing over every object.

Hello Everyone,

Thank you for your time and assistance!

I have a shader that creates a fog like effect in my game. The issue I am having is that the material with the shader can be seen through any object such as walls or terrain.

alt text

I can turn off the ZTest, but then another shader (depth mask) I have that hides water inside my ship, will then also hide the fog shader... I suck at shaders, and they are a completely foreign thing to me... I can't seem to figure it out...

Would someone be kind enough to point me in the right direction or know how I can fix this?

Here is the "fog shader"

 Shader "Suimono2/effect_refractPlaneU5" {
 
 Properties {
     _MasterScale ("Master Scale", Float) = 1.0
     
     _BlurSpread ("Blur Spread", Range (0.0, 0.125)) = 0.001
     _BlurRamp ("Blur Ramp", 2D) = "" {}
 
     _underFogStart ("Underwater Fog Start", Range(0.0,1.0)) = 0.0
     _underFogStretch ("Underwater Fog Stretch", Range(0.0,0.02)) = 0.0
     
     //_underTex ("Underwater Fog Texture 2", 2D) = "" {}
     
     _TestHeight1 ("Test Height 1", 2D) = "" {}
 
     //refraction
     _RefrStrength ("Refraction Strength", Range(0.0,1.0)) = 0.0
     _RefrSpeed ("Refraction Speed", Float) = 0.5
     _AnimSpeed ("Animation Speed", Float) = 1.0
 
     _DepthAmt ("Depth Amount", Float) = 0.1
     _DiffuseColor ("Diffuse Color", Color) = (0.5, 0.5, 1.0, 1.0)
     
     _DepthColor ("Depth Over Tint", Color) = (0.25,0.25,0.5,1.0)
     _DepthColorR ("Depth Color 1(r)", Color) = (0.25,0.25,0.5,1.0)
     _DepthColorG ("Depth Color 2(g)", Color) = (0.25,0.25,0.5,1.0)
     _DepthColorB ("Depth Color 3(b)", Color) = (0.25,0.25,0.5,1.0)
 
     _Ramp2D ("BRDF Ramp", 2D) = "gray" {}
     _FalloffTex ("Falloff Texture", 2D) = "gray" {}
 
     _UnderReflDist ("Light Factor", Range(0.0,1.0)) = 1.0
 
     _suimonoHeight ("heightfactor", Float) = 1.0
     _transition ("transition", Range(0.0,1.0)) = 0.0
 }
 
 
 
 Subshader 
 { 
 //------------------------------
 //      UNDERWATER FOG
 //------------------------------
 //Tags {"Queue"= "Overlay+6"}
 Tags {"Queue"= "Overlay+21"}
 Cull Back
 Blend SrcAlpha OneMinusSrcAlpha
 ZWrite Off
 ZTest Always
 
 
 CGPROGRAM
 #pragma target 3.0
 #pragma surface surf BlinnPhongBRDF noambient alpha
 
 sampler2D _Ramp2D;
 sampler2D _CameraDepthTexture;
 sampler2D _FalloffTex;
 float4 _DepthColorB;
 float _SuimonoIsLinear;
 float camDepth;
 float _UnderReflDist;
 
 float hfac;
 
 struct Input {
     float3 viewDir;
     float4 pos;
     float4 screenPos;
     float2 uv_FalloffTex;
 };
 
 fixed4 LightingBlinnPhongBRDF (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
 {
     // modulate light function
     half4 inLight = _LightColor0;
     inLight = lerp(inLight*0.0,inLight,_UnderReflDist);
 
     fixed4 col;
 
     //testing
     //col.a = s.Alpha;
     col.rgb = (s.Albedo*atten*inLight.rgb)*s.Gloss;
     col.rgb += (s.Albedo * 1.4) * saturate(lerp(0.1,0.0,dot(viewDir,lightDir)));
     col.rgb *= _LightColor0.rgb;
     col.a = s.Alpha*saturate(lerp(-1.0,2.0,dot(viewDir,half3(0,1,0))+0.7+hfac));
     col.a = saturate(col.a);
 
     
     return col;
 }
 
 
 
 float4 _DepthColor;
 sampler2D _BlurRamp;
 float _DepthAmt;
 float _underFogStretch;
 float _underFogStart;
 float suimonoHeight;
 float suimonoCameraHeight;
 
 void surf (Input IN, inout SurfaceOutput o) {
 
     //CALCULATE DEPTH FOG
     float4 edgeBlendFactors = float4(0.0, 0.0, 0.0, 0.0);
     half dpth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, IN.screenPos));
     half depth = LinearEyeDepth(dpth);
     float4 DepthFade = float4(_underFogStretch,2.0,0.0,-0.25+_underFogStart);
     edgeBlendFactors = saturate(DepthFade * (depth-IN.screenPos.w));
     
     
     float depthAmt = tex2D(_BlurRamp, float2(0.0, 0.5)) - (DepthFade.w * float4(0.15, 0.08, 0.01, 0.0)).a;
     float4 falloff = tex2D(_FalloffTex, IN.uv_FalloffTex);
     depthAmt *= (edgeBlendFactors.y);
     depthAmt = edgeBlendFactors.x * depthAmt;
     float depthPos = depthAmt-DepthFade.w;
     half depthViz = 1.0-tex2D(_BlurRamp, float2(depthPos, 0.5)).r;
 
 
     o.Albedo = lerp(_DepthColorB.rgb*0.25, half3(1,0,0), 0.0);
     o.Albedo = lerp(half3(0,0,0), _DepthColorB.rgb*0.25, depthViz);
     o.Albedo = _DepthColorB.rgb;
 
     o.Gloss = falloff.r;
     
     hfac = (IN.screenPos.w/(suimonoCameraHeight)*pow(1.4,suimonoHeight-suimonoCameraHeight));
     
     o.Alpha = depthViz;
     camDepth = depthViz;
 
 
     
   }
 ENDCG          
 
 // -------------------------------------
 //   UNDERWATER REFRACTION and BLURRING 
 // -------------------------------------
 GrabPass {}
     //Tags {"Queue" = "Overlay+21"}
     //Name "BlurGrab"
 //}
 
 
 Tags {"Queue"= "Overlay+21"}
 Cull Back
 //Blend SrcAlpha OneMinusSrcAlpha
 ZWrite off
 ZTest Always
 
 
 
 CGPROGRAM
 #pragma target 3.0
 #pragma surface surf SuimonoNoLight noambient
 #pragma glsl
 
 
 struct Input {
     float4 screenPos;
     float2 uv_TestHeight1; 
     float2 uv_FalloffTex;
 };
 
 
 sampler2D _GrabTexture;
 float4 _GrabTexture_TexelSize;
 float4 _DepthColorB;
 float _BlurSpread;
 float _RefrStrength;
 sampler2D _TestHeight1;
 sampler2D _CameraDepthTexture;
 sampler2D _BlurRamp;
 float _DepthAmt;
 float _EdgeBlend;
 float _RefrShift;
 float _BumpStrength;
 float _MasterScale;
 float _RefrSpeed;
 sampler2D _FalloffTex;
 float _blurSamples;
 float _isForward;
 float _isMac;
 float _UVReversal;
 float _SuimonoIsLinear;
 float _isHDR;
 float _transition;
 
 fixed4 LightingSuimonoNoLight (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
 {
     fixed4 col;
     
     col.rgb = s.Albedo * lerp(fixed3(1,1,1),_DepthColorB.rgb,0.2);
     col.rgb *= s.Gloss;
     col.rgb = saturate(col.rgb);
     col.rgb *= (atten);
 
     col.a = 1.0;
 
     return col;
 }
 
 
 
 void surf (Input IN, inout SurfaceOutput o) {
 
     // calculate normals
     half4 d1 = tex2D(_TestHeight1,IN.uv_TestHeight1 * (_MasterScale*lerp(1.0,0.2,_transition)) + (_Time * (_RefrSpeed*0.1)));
     half3 useNormal = UnpackNormal(d1);
     useNormal = normalize(useNormal);
 
     //get falloff texture
     float4 falloff = tex2D(_FalloffTex, IN.uv_FalloffTex);
         
     //calculate texture and displace
     float4 uvs = IN.screenPos;
     
     if (_isForward == 1.0 && _isMac == 0.0){
         uvs.y = uvs.w - uvs.y;
     }
 
     if (_UVReversal == 1.0){
         if (_isForward == 1.0){
             uvs.y = IN.screenPos.y;
         } else {
             uvs.y = uvs.w - IN.screenPos.y;
         }
     }
     
     float4 uv1 = uvs;
     _RefrStrength *= (1.0+(4.0*_transition));
         uv1.y += (useNormal.y*(_RefrStrength*0.1))+(_RefrStrength*0.06);
         uv1.x -= (useNormal.y*(_RefrStrength*0.1))+(_RefrStrength*0.06);
     //calculate distorted color
     half4 oCol = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x, uv1.y, uv1.z,uv1.w))) * 1.4;
     oCol.a = 1.0;
 
     //calculated original depth
     half4 depth2 = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r;
 
     //calculate blur texture
     half blur = 1.0+(10*_transition);
     half4 xCol = half4(0,0,0,0);
     half4 xDepth = half4(0,0,0,0);
     half res = 1.0;
     int blursamples = 4+(4*_transition);
     int divsamples = blursamples-1;
 
         for(int i=1; i < 8 ; i++){
             res = 0.001 * i;
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x-res*blur, uv1.y, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x+res*blur, uv1.y, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x-res*blur, uv1.y, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x+res*blur, uv1.y, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
         }
         for(int i=1; i < 8 ; i++){
             res = 0.001 * i;
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x, uv1.y-res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x, uv1.y+res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x, uv1.y-res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x, uv1.y+res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
         }
         for(int i=1; i < 8 ; i++){
             res = 0.001 * i;
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x-res*blur, uv1.y-res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x+res*blur, uv1.y+res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x-res*blur, uv1.y-res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x+res*blur, uv1.y+res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
         }
         for(int i=1; i < 8 ; i++){
             res = 0.001 * i;
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x+res*blur, uv1.y-res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xCol += (tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(uv1.x-res*blur, uv1.y+res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x+res*blur, uv1.y-res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
             xDepth += (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(float4(uv1.x-res*blur, uv1.y+res*blur, uv1.z,uv1.w)))*(1.0/divsamples)*0.125);
         }
     
     o.Gloss = 0.0;
     o.Specular = 0.0;
 
     half useAlpha = 1.0;
     half3 useAlbedo = lerp(oCol.rgb,xCol.rgb,_BlurSpread*saturate(lerp(0.0,1.0,xDepth.r+(_transition*0.5))));
     useAlbedo = lerp(useAlbedo,xCol.rgb*((1.0-_transition)+0.25),saturate(_transition*2.0));
 
     o.Gloss = lerp(1.0,falloff.g-(_transition*0.15),_DepthColorB.a);
     o.Albedo = saturate(useAlbedo);
     o.Alpha = 1.0;
     
 }
 
 ENDCG
 
 
 
 }
 FallBack "Diffuse"
 }
 


And here is the shader for the DepthMask if it helps any.

 Shader "Suimono2/DepthMask" {
  
     SubShader {
         // Render the mask after regular geometry, but before masked geometry and
         // transparent things.
  
         Tags {"Queue" = "Geometry-2" }
  
         // Don't draw in the RGBA channels; just the depth buffer
 
         ColorMask 0
         ZWrite On
  
         // Do nothing specific in the pass:
  
         Pass {}
     }
 }


shader-culling2.png (461.5 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Therian13 · Jul 24, 2015 at 03:13 AM 0
Share

still an ongoing issue...

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rendering Depth Correctly With Multiple Cameras 0 Answers

Does anyone know why my rim shader effects water shader? 1 Answer

Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers

Drawing silhouette shader 0 Answers

How can I edit the built in shaders? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges