UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
Программирование
Fundamentals
Setting Up Development Environment
Usage Examples
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
CIGI Client Plugin
Rendering-Related Classes

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 following article shows how to export the mesh with morph targets from Autodesk Maya and then add it to Unigine.

Requirements

  • It is supposed that you already have a 3D model with blend shapes ready to be exported.
  • It is supposed that you already have a world created.

See Also

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, you should do the following:

  1. In Autodesk Maya, select the mesh with blend shapes to be exported.

  2. On the main menu, click File -> Export Selection...

  3. 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.
  4. In the File Type Specific Options tab with export options, specify parameters to export the mesh.
  5. In the Deformed Models tab, check the Blend Shapes checkbox to export blend shapes.
  6. 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:

  1. Import the .fbx file with the Import Morph Targets option enabled.
  2. Add the imported file to the scene.
  3. 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:

Source code (C++)
#ifndef __APP_WORLD_LOGIC_H__
#define __APP_WORLD_LOGIC_H__

#include <UnigineLogic.h>
#include <UnigineStreams.h>
#include <UnigineObjects.h>
#include <UnigineEditor.h>
#include <UnigineGame.h>
#include <UnigineMathLib.h>

using namespace Unigine;

class AppWorldLogic : public Unigine::WorldLogic {
	
public:
	AppWorldLogic();
	virtual ~AppWorldLogic();
	
	virtual int init();
	
	virtual int update();
	virtual int render();
	virtual int flush();
	
	virtual int shutdown();
	virtual int destroy();
	
	virtual int save(const Unigine::StreamPtr &stream);
	virtual int restore(const Unigine::StreamPtr &stream);

private:
	ObjectMeshSkinnedPtr mesh;
};

#endif // __APP_WORLD_LOGIC_H__
AppWorldLogic.cpp:
Source code (C++)
#include "AppWorldLogic.h"

int AppWorldLogic::init() {
	
	// get the node that refers to the exported FBX file and cast it to a skinned mesh
	mesh = ObjectMeshSkinned::cast(Editor::get()->getNodeByName("sphere_0"));
	// set the number of morph targets 
	mesh->setNumTargets(5, 0);

	return 1;
}

int AppWorldLogic::update() {
	
	float time = Game::get()->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 Editor by the 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:
    • Source code (C++)
      mesh->setTarget(0,1,0,1.0f,0);
      This target is if for the bind pose without any interpolation.
    • Source code (C++)
      mesh->setTarget(1,1,0,-k0,0);
      mesh->setTarget(2,1,0,-k1,0);
      These targets are used for interpolation of two animations blending.
    • Source code (C++)
      mesh->setTarget(3,1,1,k0,0);
      This target is used for the first animation blending, which uses the sin() function for interpolation.
    • Source code (C++)
      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:

Last update: 04.06.2018