Create a mask to hide haircuts ( cards ) under a cap or a hat

Hello :saluting_face:

I would like to create a mask to hide haircuts under a cap.

Please note, this is a haircut using haircards (not the new MetaHuman / Unreal5 technique)

I started looking Material custom stencils or BoxMask-3D, but I would like some advice.

Thanks in advance

To create a mask that hides haircuts under a cap using haircards in Unreal Engine (not the MetaHuman/Unreal Engine 5 hair system), here’s some advice:

1. Material Custom Stencils

  • This is a viable option and allows fine control over masking.
  • Steps:
    1. Use a unique Custom Depth Stencil value for the cap.
    2. In the hair material, create a masking logic using the SceneTexture: Custom Depth node to detect the cap’s stencil and mask the hair beneath it.
    3. Blend this mask with your existing hair material’s opacity input.
  • Challenges:
    • May require tweaking to ensure the stencil mask aligns perfectly with the cap.
    • Handling transparency and avoiding hard edges at the mask boundary might need additional blending logic.

2. BoxMask-3D

  • This works well for objects like caps where the masking volume is geometrically simple.
  • Steps:
    1. In the hair material, use a BoxMask-3D node to define a 3D area corresponding to the cap’s position.
    2. Set the origin and bounds of the mask dynamically, either through blueprint or manual tuning, to align it with the cap.
  • Challenges:
    • Requires precise alignment between the BoxMask and the cap.
    • If the cap moves, you’ll need to dynamically update the mask’s position, which can be achieved via blueprints.

3. Dynamic Masking via Cap Proxy Geometry

  • Use a simplified version of the cap as a masking proxy.
  • Steps:
    1. Create a low-poly version of the cap with minimal geometry.
    2. Assign a specific material to this proxy that outputs only depth or a mask color.
    3. Integrate this proxy with the hair shader to subtract or mask hair beneath it.
  • Advantages:
    • Offers more natural integration with the cap.
    • Easier to debug and adjust compared to custom stencil or box masks.

4. Blueprint-Driven Approach

  • You can dynamically set parameters in the hair material based on the cap’s position, size, and orientation.
  • Use the cap’s bounding box to define mask coordinates.

Additional Tips:

  • Testing and Debugging: Use visualization nodes in Unreal to debug the mask (e.g., SceneColor to preview opacity).
  • Soft Transitions: Add falloff to your mask using interpolation nodes to avoid hard edges, especially when using BoxMask-3D.
  • Performance Considerations: Keep material instructions low; avoid excessive complexity in the mask logic.

If you need a more tailored solution, share your current setup or any blockers, and I can provide more specific advice!

If you need to build something that is universal, you probably want to use hair flow maps and distance to nearest surface in the hair card shader.

Its farily expensive to do this rendering wise. And extremely error prone. Lets call this the “id1ot with a free rendering engine and way too much time” way.
Essentially, it’ll never work or be used by anyone except that person, and if you show it to other devs they’ll rightly laugh at you.

What you really should do is just make mesh merged versions of the hairdo with the hat and swap out the hair do to the custom one.
The AAA way, instead of the other stuff.

You can use runtime merge and a custom algorythm to chop the extra hair (some mentioned above).
Lets call this the A way.
Its performant. But error prone because procedural.

There is no AA middle road way here. Generally because people are smart enough to just do the AAA way and not have any issue with anything at all.