#Water Mask
1 messages · Page 1 of 1 (latest)
Made a thread to not clutter to much the channel.
So, I understand it isn't as straight forward as it seems.
I'll explain the setup steps in the following messages.
- You want the water and mask object to ONLY render with the renderer features, else they are rendered both by the regular loop, and the renderer features.
You can do this by removing their layers from the Opaque and Transparent layer mask in the "filtering" options of your renderer asset :
- I was sure it was possible to force the material to not write into the scene color (HDRP can do this), but no. So for the boat mask to write only to stencil, and not the scene color, you need a dedicated shader with "ColorMask 0". Here is one :
Shader "Unlit/UnlitStencil"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
ColorMask 0
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return 1;
}
ENDCG
}
}
}
- The inside mask of the boat must be a closed volume, with normals pointing outside. Here I made a mesh that is exactly mathing the inside faces, bit it can be a bit bigger to avoid any zfight
- Set the Render Object feature for the mask to use the dedicated stencil only shader, with a stencil value different than 0, and "Pass" set to "Replace" (so it will write to the stencil bit)
- Set the Render Object feature for the water to test for "Not Equal" on the same bit as before, and event needs to be one after the boat mask.
You should now have a floating boat with no water inside 🙂
You might also probably want to disable shadow casting for the water and inside mesh.