Download

How to get mesh's section/sub-mesh position in material?

Hi,

We have a humanoid skeleton mesh, and we’re making a material for the “eye” section(or called sub-mesh?), in which we need to get the eye’s world position to achieve a specific effect.

But the ActorPositionWS returns the skeleton mesh actor’s position, and the ObjectPositionWS returns the whole skeleton’s primitives’ bounds center instead of the eye itself (including body, head, etc).

I know there’s a walkaround by using blueprint to get the eye’s bone position and set the param on material instance or use custom primitive data.

But is there a way to get or calculate the eye’s position directly by using material itself?

You’ve already found the best & most performant solution; sending the eye position to the material is a perfect use case for parameters (I’m sure this is how Epic would do it).

Nonetheless, I’m gonna give an alternative just to answer your question. This is more expensive, limited, and inaccurate (due to computer precision) than just using a parameter, so I would recommend not doing this.

Alternative

If the eye mesh is a perfect sphere, and you know the radius of that sphere, you can use math to find the position of the eye. The position of the eye would be pixelPosition - pixelNormal * eyeRadius.


Due to precision issues, as well as the mesh not being perfectly smooth, the result will be both inaccurate & inconsistent between different pixels and viewpoints.

Edit: A better alternative is just to separate the eyes from the rest of the mesh. That way, the object position will return the position of the eye.

1 Like

Thanks for your reply and your solution!

Unfortunately our eye is made in a Toon style, it’s just two round flat meshes.

But as you suggested, we will use material parameter to achieve this.
In fact we passed the whole transform matrix into the material, to transform multiple positions into eye’s local space.

The only downside is that our animator needs to make sure the bone’s transform is relatively in the right position and direction, but that’s not a big problem. (That’s why we want to calculate it in the first time instead of using bone’s transform.)