How to get the morphed mesh from MorphTargetManager?

Hello,
I am using the MorphTargetManager to morph a mesh to targets with sliders like here : https://www.babylonjs-playground.com/#HPV2TZ#2

It works perfectly. However, I would like to get the morphed mesh (the one displayed), in order to export it in a .stl file or to measure it size.

If possible, how can I do this ?

It is not possible easily as the interpolation is done in the vertex shader and you can’t get the output of it.

What you can do is replicate the calculation in javascript. They are not very complicated as it is only linear interpolations. From the shader code:

#ifdef MORPHTARGETS
	positionUpdated += (position{X} - position) * morphTargetInfluences[{X}];
	
	#ifdef MORPHTARGETS_NORMAL
	normalUpdated += (normal{X} - normal) * morphTargetInfluences[{X}];
	#endif

	#ifdef MORPHTARGETS_TANGENT
	tangentUpdated.xyz += (tangent{X} - tangent.xyz) * morphTargetInfluences[{X}];
	#endif

    #ifdef MORPHTARGETS_UV
	uvUpdated += (uv_{X} - uv) * morphTargetInfluences[{X}];
	#endif
#endif

position is the unmodified position of the mesh (the one you get by doing mesh.getVerticesData("position")). position{X} are the positions for the target 0, 1, …

For eg, if there are two targets, the computation will be:

positionUpdated += (position0 - position) * morphTargetInfluences[0];
positionUpdated += (position1 - position) * morphTargetInfluences[1];

Same thing for the normal, tangent, uv.

…and welcome to the community!

1 Like