I’ve been doing some research of how to not have water inside my ship. I saw a tutorial on using a depth mask shader but it didn’t quite work.
Can anybody recommend a URP compatible way to do it?
I’ve been doing some research of how to not have water inside my ship. I saw a tutorial on using a depth mask shader but it didn’t quite work.
Can anybody recommend a URP compatible way to do it?
Stencil buffers are often used for this. You would add renderobjects renderer features with stencils set.
depth mask is useful.Creata a shell from your ship,then ZWrite On and ColorMask 0 in shell shader,set shell render queue in front of your water(water 3002 and shell 3001 in my case).
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
ZWrite On
ColorMask 0
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (v2f i) : SV_Target
{
// sample the texture
float4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDHLSL
}
}
Good luck.
Thanks Dujiangyue. I’m not a coder and I got some compile errors when pasting your code into a new shader:
Shader "Custom/NewSurfaceShader 1"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
ZWrite On
ColorMask 0
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (v2f i) : SV_Target
{
// sample the texture
float4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDHLSL
}
}
complete code here:
Shader "Unlit/WaterMaskForCofferdam"
{
Properties
{
_MainTex("Main Tex",2D)="black"{}
}
SubShader
{
Tags { "RenderType"="Opaque" }
ZWrite On
ColorMask 0
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float4 _MainTex_ST;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (v2f i) : SV_Target
{
// sample the texture
float4 col = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex, i.uv);
return col;
}
ENDHLSL
}
}
}
Thanks I will take a look.
https://web.archive.org/web/20210831213650/http://wiki.unity3d.com:80/index.php/DepthMask
Add the following shader on a plane over your water It will only cut through transparent and Cutout materials
Shader "Masked/Mask" {
SubShader {
// Render the mask after regular geometry, but before masked geometry and
// transparent things.
Tags {"Queue" = "Geometry+10" }
// Don't draw in the RGBA channels; just the depth buffer
ColorMask 0
ZWrite On
// Do nothing specific in the pass:
Pass {}
}
}