Ben
Ben

Reputation: 4110

Hide parts of mesh overlapping another mesh in Unity

I have thoses two meshes:

two meshes

In my game, I put the hat on the hair at runtime:

hat on hairs

As you can see, as expected, the hair is visible outise the hat part.

How can I achieve this in Unity (what kind of mask shader should I use?):

enter image description here

I've tryed to make a depth mask but it hides every meshes in my scene. I just want to hide the hair, not others meshes. And what if I have two player having the same case? Would player mask hide player 2 hair? How can I avoid that?

Upvotes: 1

Views: 1332

Answers (1)

Daniel
Daniel

Reputation: 7724

What I would do:

  1. write a C# code that gets the pivot position (bottom part of the hat) and its up vector every frame.

  2. build a plane with these values. The up vector would be the normal vector of the plane and a plane can be defined by a point and a normal vector.

  3. I would pass the equation of the plane to the shader (via Material.SetFloat or Material.SetVector) and evaluate if the world positions of the hair vertices are in the correct or in the wrong side of the plane.

Upvotes: 2

Related Questions