This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Basics
Rendering
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
Animations-Related Classes
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
VR-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

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#

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 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:

Notice
The code is implemented as a component that should be assigned to the node via UnigineEditor. Check the article on Component System for more detail.
Morph.h
#pragma once
#include <UnigineComponentSystem.h>
#include <UnigineWorld.h>
#include <UnigineGame.h>

class Morph :
	public Unigine::ComponentBase
{
public:
	// declare constructor and destructor for the Morph class
	COMPONENT_DEFINE(Morph, Unigine::ComponentBase);

	// declare methods to be called at the corresponding stages of the execution sequence
	COMPONENT_INIT(init);
	COMPONENT_UPDATE(update);

private:
	Unigine::ObjectMeshSkinnedPtr mesh;

protected:
	// world main loop
	void init();
	void update();
};
Morph.cpp
#include "Morph.h"

REGISTER_COMPONENT(Morph);		// macro for component registration by the Component System
using namespace Unigine;
using namespace Math;

void Morph::init()
{
	// get the node and cast it to a skinned mesh
	mesh = static_ptr_cast<ObjectMeshSkinned>(node);
	
	// enable targets of the surface
	mesh->setSurfaceTargetEnabled(0, 1, true);
	mesh->setSurfaceTargetEnabled(0, 2, true);
}

void Morph::update()
{
	float time = Game::getTime() * 2.0f;
	// calculate weights of targets
	float k0 = Unigine::Math::sin(time * 3.0f) + 0.75f;
	float k1 = Unigine::Math::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:

    Source code (C++)
    //check if the node to which the component is assigned is MeshSkinned
    if (node->getType() != Node::OBJECT_MESH_SKINNED)
    	return;
    // get the node and cast it to a skinned mesh
    mesh = static_ptr_cast<ObjectMeshSkinned>(node);
  • 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.

      Source code (C++)
      mesh->setSurfaceTargetWeight(0, 0, 1.0f - k0 - k1);
    • The first target weight is modified using the sin() function.

      Source code (C++)
      mesh->setSurfaceTargetWeight(0, 1, k0);
    • The second target weight is modified using the cos() function.

      Source code (C++)
      mesh->setSurfaceTargetWeight(0, 2, k1);

After assigning the component to the mesh, the result looks as follows:

Last update: 2024-09-18
Build: ()