Adding Morph Targets
This article describes how to work with the morph target animation also known as blend shapes in UNIGINE. Usually, morph targets are used to change facial expressions of characters.
The article shows how to export the mesh with morph targets from Autodesk Maya and then add it to UNIGINE.
The model used in this tutorial is Spot by Keenan Crane distributed under CC0 1.0 Universal.
Requirements#
- It is supposed that you already have a 3D model with blend shapes ready to be exported, and this model is within the limitations set in UNIGINE.
- It is supposed that you already have a world created.
See Also#
- Description of the ObjectMeshSkinned class functions
Step 1. Export a Mesh with Blend Shapes from Maya#
This section shows the way of exporting meshes with blend shapes in the FBX format from Autodesk Maya. It contains an example mesh of a calf, which has 2 blend shapes (morph targets).
To export the mesh with blend shapes, do the following:
-
In Autodesk Maya, select the mesh with blend shapes to be exported.
In the main menu, click File -> Export Selection...
- In the Export Selection window, choose a folder to save the mesh and specify a name for the FBX file. In the Files of type drop-down list, choose FBX export.
- In the File Type Specific Options tab with export options, specify parameters to export the mesh.
- In the Deformed Models tab, check the Blend Shapes checkbox to export blend shapes.
- Click Export Selection.
Now you have the mesh in the FBX format that can be easily added to your project.
Step 2. Add a Mesh into the World#
This section shows how to add the exported mesh to the world and set up morph target animation.
To add the exported mesh to the world:
-
Import the *.fbx file with the Import Morph Targets option enabled.
- Add the imported file to the scene.
- Save the world.
Each blend shape of the mesh is a target. You need to enable morph targets for the mesh surface and set parameters to these targets to control the morph target animation. The following example shows how to create morph targets and set parameters via code:
using System.Collections;
using System.Collections.Generic;
using Unigine;
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Morph : Component
{
ObjectMeshSkinned mesh;
void Init()
{
// get the node and cast it to a skinned mesh
mesh = node as ObjectMeshSkinned;
// enable targets of the surface
mesh.SetSurfaceTargetEnabled(0, 1, true);
mesh.SetSurfaceTargetEnabled(0, 2, true);
}
void Update()
{
float time = Game.Time * 2.0f;
// calculate weights of targets
float k0 = MathLib.Sin(time * 3.0f) + 0.75f;
float k1 = MathLib.Cos(time * 3.0f) + 0.75f;
// set targets with parameters
mesh.SetSurfaceTargetWeight(0, 0, 1.0f - k0 - k1);
mesh.SetSurfaceTargetWeight(0, 1, k0);
mesh.SetSurfaceTargetWeight(0, 2, k1);
}
}
The code given above gets the node that refers to the FBX file and sets parameters to morph targets. Let's clarify the essential things:
-
The mesh was obtained by casting the node that refers to the FBX asset to ObjectMeshSkinned. The node to be cast is the one to which the component is assigned. Prior to casting this node, you may also want to add a check if it is ObjectMeshSkinned:
if (node.Type != Node.TYPE.OBJECT_MESH_SKINNED) return; // get the node and cast it to a skinned mesh mesh = node as ObjectMeshSkinned;
- The SetSurfaceTargetEnabled() function sets the target for the specified mesh surface.
-
By using SetSurfaceTargetWeight(), weights for morph targets are set. Each target has its target weight. Weights affect coordinates of the mesh: coordinates are multiplied by their weights. Thus, all enabled targets are multiplied by their weights and the new mesh is created:
final_xyz = target_0_xyz * weight_0 + target_1_xyz * weight_1 + ... -
Three targets are enabled for the object and sin() and cos() functions are used for animation blending of these targets in the following way:
-
Subtract the impact of the other two morph targets from the bind pose to receive a normalized weight for blending.
mesh.SetSurfaceTargetWeight(0, 0, 1.0f - k0 - k1);
-
The first target weight is modified using the sin() function.
mesh.SetSurfaceTargetWeight(0, 1, k0);
-
The second target weight is modified using the cos() function.
mesh.SetSurfaceTargetWeight(0, 2, k1);
-
After assigning the component to the mesh, the result looks as follows: