Apply Morph Target Deformer to Mesh

Apply Morph Target Deformer to Mesh

A morph target deformer blends one or more pre-defined target shapes onto a mesh, controlled by a weight per target. This is the standard mechanism for effects like facial expressions or shape key animation.

The MorphTargetDeformer holds one or more MorphTargetChannels. Each channel has a list of target shapes — Shape instances built from the same vertex layout as the base mesh — and a list of weights, one per target.

Adding a Morph Target

This code example creates a box mesh and adds a single morph target whose positions are scaled by two, blended at weight 0.7:

// Create a scene with a box mesh
Scene scene = new Scene();
Mesh mesh = new Box(1, 1, 1).toMesh();

// Morph target: same vertex layout with positions scaled by 2, built as a Shape
Vector3[] controlPoints = new Vector3[mesh.getControlPoints().size()];
for (int i = 0; i < mesh.getControlPoints().size(); i++) {
    Vector4 cp = mesh.getControlPoints().get(i);
    controlPoints[i] = new Vector3(cp.x * 2, cp.y * 2, cp.z * 2);
}
Shape target = Shape.fromControlPoints(controlPoints);

// Create the morph target deformer with one channel
MorphTargetDeformer deformer = new MorphTargetDeformer("morph");
MorphTargetChannel channel = new MorphTargetChannel("squeeze");
channel.getTargets().add(target);
channel.getWeights().add(0.7);
deformer.getChannels().add(channel);

// Attach the deformer to the mesh and add the mesh to the scene
mesh.getDeformers().add(deformer);
scene.getRootNode().createChildNode("morph-box", mesh);

// Save to FBX, the morph target is written as a BlendShape deformer
scene.save("morph.fbx", FileFormat.FBX7400ASCII);

Notes on File Formats

Morph targets roundtrip through FBX, where the deformer is written as a BlendShape with a BlendShapeChannel per channel.