Unreal Engine - How to hide specific parts of a mesh using the vertex colors? Video:

I watched many tutorials on this subject and couldn’t yet find a solution for this.

I have one mesh with vertex painted with different tones of blue:
I need to hide parts of my mesh depending on the tone of blue selected.

The tones are going from 0.1 to 0.9.
So each different tone of blue is +0.1.
So dark blue is RGB 0, 0, 0.1
And light blue is RGB 0, 0, 0.9

I tried many things. All failed. I tried stepping. Using Hue. Threshold. Grayscale.
All those nodes dont give specific control over which specific parts should be hidden.

So the best i think is to do a Custom Node with a for loop (something i learned from @Supremative ) that iterates through the specific colors that need to be hidden.
Though im having trouble solving that puzzle, i dont understand how to refer to specific tones of blue in the for loop.

So basically something like this:
But this is not working:
Its just conceptual:

float Mask = VertexColors;
for(int i=0; i < NumsColorHide; i++) {
  if(i==0) Mask = saturate(Color1);
 if(i==1) Mask *= saturate(Color2);
 if(i==1) Mask *= saturate(Color3);
}
return Mask;

Video explaining in detail what im trying to do:

It won’t work with Masked:
image

You should use Translucent Material.

Try this:

My Products

2 Likes

Thank you so much.
Thats working.
Though i have another mesh that has 200 parts.
That will be a problem, right? Especially because its a translucent material.
They said in the unreal slackers that translucent material are more expensive.
Though this is great already.
I can still implement a for loop, and that way it will only run the necessary nodes when hiding less parts.

I see you duplicate identical code in the base material. It would be better to use a material instance and change one parameter only:

image

image

1 Like

I understand the reasoning behind using a new material instance. Would be simpler.
But the final goal, im using instanced static mesh component.

That means im limited to 1 material for all the Instances of the same mesh. And will then work with each instance using Per Instance Custom Data.

So perhaps, the other idea still stands, of using a for loop, with maybe a switch statement.
Here i made it like this:

float3 Hide;
for(int i=0; i < NumsColorHide; i++) {
  switch (i) {
    case 0:
      Hide = Color1;
      break;
    case 1:
      Hide *= Color2;
      break;
    case 2:
      Hide *= Color3;
      break;
    // add more cases for additional tones to hide.
  }
}
return Hide;

So im telling the loop i have 3 colors to hide. But could be 20. Would just need to put them all in the switch statement.
Then in the per instance custom data, i will tell in each mesh what colors to hide in each specific mesh, and how many.

Though if the possible colors to hide are 200. Then i need a switch with 200.
Although i assume that if im hiding 6 tones, the for loop will only run 6 times.

And if im not wrong a switch statement its quite efficient. So it finds its corresponding if fast, hopefully.
Or unless maybe i could get an array of the colors without having to check them all somehow.

Try this:

1 Like

That one seems to hide by selecting the incrementally. It could be useful in certain situations.
But it wont give control over selecting specific ones.
Like hiding 1 and 4 and 9.

I was just thinking. Maybe translucent is not necessary or even the mask.
Because it doesnt need to be hidden. It only needs to go away from the view.
So perhaps i can use the WPO to just move them below the floor in the Z.
I will try this new idea, and see if it could work.
Because i know if statements and translucent material can be quite taxing in the performance.

Wow so this could work too. This is amazing.
So perhaps the Translucent or masked is not necessary.
All i need is to move it below the floor.

Rather than moving it underground (which is technically fine) it’s also possible to move all of the unwanted verts to the same point in space, effectively shrinking them into nothingness.

For example, you can get the difference between the world position of the vertex and the object’s origin, and offset the verts to be hidden by this vector. This will move all of the chosen verts to origin, making them infinitely small. But it may also create a black hole so use at your own risk.

1 Like

Sounds interesting. But you would still need to select the vertices the way described above, right? Thats the hardest part. The hardest is to select specific vertices in a cheap manner.

Though i would like to see how you do that. Since i dont see how you get the exact vertice position in the materials and compare it with the World position.

It’s not difficult at all. Just use a bitmask node. This will allow you to select only the verts that have the specific vertex color.

You take a world position node which gives you the position of the vertex, and then the object position node which will give the origin in world space.
The difference between these points will be a vector pointing from the vertex to the origin of the mesh, with a magnitude equal to the distance between the points.
If you offset an object towards it’s origin, it will shrink. If you continue to offset it all the way to it’s origin, it will eventually have a scale of 0.