This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Version Control
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
Rebuilding the Engine 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
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

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#

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:

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

  2. In 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 <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:

Source code (C++)
#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:

    • Source code (C++)
      mesh->setTarget(0, 1, 0, 1.0f, 0);

      This target is 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: 2023-06-23
Build: ()