Adding Morph Targets
This article describes how to work with a 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 create morph targets for a surface of the mesh and set parameters to these targets to control the morph target animation. The following example shows how to create morph targets and set parameters from the code:
AppWorldLogic.h:
#ifndef __APP_WORLD_LOGIC_H__
#define __APP_WORLD_LOGIC_H__
#include <UnigineLogic.h>
#include <UnigineStreams.h>
#include <UnigineObjects.h>
#include <UnigineGame.h>
#include <UnigineMathLib.h>
using namespace Unigine;
class AppWorldLogic : public Unigine::WorldLogic
{
public:
AppWorldLogic();
~AppWorldLogic() override;
int init() override;
int update() override;
int postUpdate() override;
int updatePhysics() override;
int shutdown() override;
int save(const Unigine::StreamPtr &stream) override;
int restore(const Unigine::StreamPtr &stream) override;
private:
ObjectMeshSkinnedPtr mesh;
};
#endif // __APP_WORLD_LOGIC_H__
AppWorldLogic.cpp:
#include "AppWorldLogic.h"
#include <UnigineWorld.h>
int AppWorldLogic::init()
{
// get the node that refers to the exported FBX file and cast it to a skinned mesh
mesh = static_ptr_cast<ObjectMeshSkinned>(World::getNodeByName("spot_the_cow"));
// set the number of morph targets
mesh->setNumTargets(5, 0);
return 1;
}
int AppWorldLogic::update()
{
float time = Game::getTime() * 2.0f;
// calculate weights of targets
float k0 = sin(time * 3.0f) + 0.75f;
float k1 = cos(time * 3.0f) + 0.75f;
// set targets with parameters
mesh->setTarget(0, 1, 0, 1.0f, 0);
mesh->setTarget(1, 1, 0, -k0, 0);
mesh->setTarget(2, 1, 0, -k1, 0);
mesh->setTarget(3, 1, 1, k0, 0);
mesh->setTarget(4, 1, 2, k1, 0);
return 1;
}
int AppWorldLogic::shutdown()
{
mesh.clear();
return 1;
}
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 exported mesh was obtained by casting the node that refers to the FBX asset to ObjectMeshSkinned. The node is obtained from the World by its name.
- The setNumTargets() function sets the number of targets for the surface of the mesh. The exported mesh in the example given above has 2 targets (blend shapes).
-
By using the setTarget() function, all parameters for each created morph target are set in the update() function. Each target has its target weight. Weights have an influence on 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 + ... -
Since in the code given above, sin() and cos() functions are used for animation blending of different targets, five targets are created:
-
mesh->setTarget(0, 1, 0, 1.0f, 0);
This target is for the bind pose without any interpolation.
-
mesh->setTarget(1, 1, 0, -k0, 0); mesh->setTarget(2, 1, 0, -k1, 0);
These targets are used for interpolation of two animations blending.
-
mesh->setTarget(3, 1, 1, k0, 0);
This target is used for the first animation blending, which uses the sin() function for interpolation.
-
mesh->setTarget(4, 1, 2, k1, 0);
This target is used for the second animation blending, which uses the cos() function for interpolation.
-
After assigning the material to the mesh, the result looks as follows: