Tech Breakdown: Hiding Water in Boat Interiors

The Problem. Someone get a bucket!
  • Writing a shader for the ‘mask’ mesh
  • Modifying the water shader to use the mask

The Mask Mesh

First we need a mesh for our boat that we can use to mask out the water surface. I hand-author a mesh for each boat, depending on your situation you may be able to use a single generic mesh.

The water mask mesh

The Mask Shader

Now that we have the mask mesh, we need a shader and material that will hide the water surface wherever the mask is drawn. At first, we’ll accomplish this purely with the depth buffer.

The shader for the water mask mesh
Toggling the water mask mesh on and off

The Stencil Buffer

If you’re unfamiliar with the Stencil Buffer, you can think of it as basically another screen you can draw to that contains just integers instead of colors. Shaders can specify what value to write to the stencil buffer, and also can specify a comparison operation that prevents the shader from drawing unless the stencil buffer matches the referenced value. It’s like an auxiliary depth buffer that you can choose any value to write to.

Stencil - This is just signifying that we’ll be doing a stencil operation in this shader pass Ref 1 - The stencil value we'll be referencing is 1Comp always - When we look at the current stencil value, our shader should always draw its pixel regardless of the stencil value.Pass replace - When we draw a pixel, we should replace the current stencil value with our value, aka '1'. 

Masking in the Water Shader

Water shaders can be pretty complicated, so I’m going to leave out everything related to that except for the relevant stencil part. Presumably whatever you’re using for your water has some kind of custom shader associated with it, so you’ll just need to edit that shader and insert this Stencil block.

Adding the stencil block to the water shader.
Water water everywhere but not a drop in our boat!
Artifact is ‘fixed’



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store