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

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

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

      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-12-13
Build: ()