# Take out water from inside of the boat with Custom Stencil  - logic puzzle

**URL:** <https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545>\
**Category:** Rendering\
**Tags:** stencil, custom-depth, unreal-engine, UE4, Blueprint, question\
**Created:** [July 29, 2016, 11:36am UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545 "2016-07-29T11:36:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chishabk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Chishabk](https://forums.unrealengine.com/u/Chishabk)\
**Post date:** [July 29, 2016, 11:36am UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/1 "2016-07-29T11:36:10Z")

</div>

Hi guys,

I thought I have a simple task to resolve but i was wrong.

Basically I want take the water out of my boat:  
Since I have an opaque ocean , i choose to use Custom Stencil as masking solution. For that I build 2 meshes to play with as it follows:

**1**. The actual **mesh** of the boat that i’ll use **for render**

**2**. A **“mask” mesh** to hide the inside of my rendered mesh hull.

**3**. … and of course the **ocean surface**.

Now, I assigned, **3 different stencil values** to each of my meshes + the ocean  
render mesh - \> **( mask = 150 , Render mesh = 10, ocean = 200)**

My problem is that whatever substractions or adding operations I do with this stencils in my PP material blueprint  
i’m not able to get an intersection , to isolate **only the water from inside** the boat… For sure my math is poor, and i’m missing something:

Help, I’m losing it…

 ![100657-boat.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/9/9/399dc576d045f0e3f0f10b907ece6082349fd40b.jpeg)

the mesh to render :

 ![100658-hull.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/4/c/54ccc959f37c145528f89b3e7077fcdd6d8bf185.jpeg)

the mesh for mask:

 ![100659-mask.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/e/d/8ed6802f2d3cf149d2ee559d46ef2008999d66c7.jpeg)

viZualisation of my stencil values:

 ![100660-stencilbuffer.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/9/b/49b5c986bc989be84b895e257b50d2e24fdc6fd0.jpeg)

My basic Blueprint in PPDepthShader

 ![100671-material_bluprint.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/2/7/7270ce2d1ffde18830aa64803ff657668d49e967.jpeg)

---

<div class="post-metadata">

**Author:** ![Chishabk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Chishabk](https://forums.unrealengine.com/u/Chishabk)\
**Post date:** [July 29, 2016, 11:50am UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/2 "2016-07-29T11:50:43Z")

</div>

an example for one of my combinations of stencile masks

Here for example i masked the boat out of my ocean,  
but if i zoom inside the boat, under the “roof” of my mask volume  
the water is still inside, ( second image in attachment) and i don’t know what fantastic math equation.( part from add and subtract), i should use to cut it out.

does this make any sens to you, or I’m talking gibberish?

 ![100673-exampleforstencilcombinations.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/c/e/7cea6390d65644215b8ddafc869b49f317092670.jpeg)

 ![100674-exampleforstencilcombinations2.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/0/f/b0f68cfb72e588e764cad0e396807e37d367ce36.jpeg)

---

<div class="post-metadata">

**Author:** ![Chishabk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Chishabk](https://forums.unrealengine.com/u/Chishabk)\
**Post date:** [July 29, 2016, 7:57pm UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/3 "2016-07-29T19:57:49Z")

</div>

I’ve change my method and I’ve done, it. Differently. Using DistanceToNearestSurface to mask a hole in my ocean  
in the shape of the boat.  
here is the blueprint:

 ![100709-disttosurface.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/b/a/7ba42bcc4bf67780c65322d7899f80da8e2f8d55.jpeg)

 ![100710-disttonearestsurf2.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/8/3/383eacbcc4a396b7cc2a5b005f6f162beb73916c.jpeg)

][2]

Custom stencil method, at least for this problem (and for me) is an dead end.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a0945a49](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a0945a49](https://forums.unrealengine.com/u/anonymous_user_a0945a49)\
**Post date:** [August 15, 2016, 7:06pm UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/4 "2016-08-15T19:06:50Z")

</div>

I know this is old, but I’m trying to do something similar. Could you elaborate on what exactly the working setup does? I need something similar, not the exact same, and I’m unsure what to take away from yours.

---

<div class="post-metadata">

**Author:** ![Burnrate](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Burnrate](https://forums.unrealengine.com/u/Burnrate)\
**Post date:** [August 23, 2016, 5:40pm UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/5 "2016-08-23T17:40:50Z")

</div>

Can you elaborate more on your solution? About where and how you applied the distance to nearest surface? I might be missing something obvious.

Thanks!

---

<div class="post-metadata">

**Author:** ![ConceptualTrap](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ConceptualTrap](https://forums.unrealengine.com/u/ConceptualTrap)\
**Post date:** [November 11, 2016, 4:14pm UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/6 "2016-11-11T16:14:03Z")

</div>

This is a consistently difficult thing to do for many people. Would you mind explaining your customdepth technique in a bit more detail?

Also, what are you using for the ocean system? Is it the Community Ocean Shader? Also, would your customdepth technique work with transparent materials on the ocean plane mesh?

---

<div class="post-metadata">

**Author:** ![DannyManPc](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dannymanpc/32/1192235_2.png) [@DannyManPc](https://forums.unrealengine.com/u/DannyManPc)\
**Post date:** [June 18, 2022, 3:49pm UTC](https://forums.unrealengine.com/t/take-out-water-from-inside-of-the-boat-with-custom-stencil-logic-puzzle/365545/7 "2022-06-18T15:49:53Z")

</div>

Also tried out so so much… the stencils were a no go for me either… then I also tried the DistanceToNearestSurface method OP described, and oh boy! It worked!

Just plug this into the water material of your ocean or water.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/2/3/a23f543f9711bf1c4765a74f74afcda0e5a79e32.png)

Then make a low poly shape of your boat in 3D, slightly bigger then the hull, import that mesh into the same BP/group as your boat mesh (_named DistanceField in the example image_), enable Generate Distance Fields on the low poly SM, but change the resolution to like 0.1 instead of 1 (tweak where needed). Then on the low poly mesh disable everything visual related and shadow related, like render in main pass and use as occluder.

A BP would look like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/d/f4d85d8acdce771d6bb7b1f574a40fb64eeabcc8.png)

And then you should be done… just leaving this here for future generations :D…

PS: Make sure your project has Distance Fields enabled in the Project Settings, otherwise it isnt working at all.
