This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
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
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
Tutorials
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.

Dynamic Meshes

The article describes operations on dynamic meshes via code by using methods of the ObjectMeshDynamic class. This class is used to procedurally create dynamic meshes and modify them in runtime. You can also load an existing mesh as a dynamic one to modify it.

See Also#

Creating a Dynamic Mesh#

There are three methods to add a new dynamic mesh:

Creating a New Dynamic Mesh#

This chapter describes how to create a new dynamic mesh by allocating memory and adding vertices for a new mesh. You can allocate memory for a moderate number of triangles or for a big number of vertices.

Adding triangles#

You can create a dynamic mesh from scratch, by adding one triangle after another. This variant is suitable for creating meshes with a moderate number of triangles. To create a new dynamic mesh you should do the following:

  1. Reserve a necessary space in a vertex buffer for triangle vertices and add indices for them to the index buffer. This is done by calling one of the following methods:

    All these functions allocate memory for polygons of the corresponding form.

  2. Add the corresponding number of vertices via addVertex() method.
  3. After each call of the addVertex() function, you can set up properties of the last added vertex manually via calling:

    All these functions are optional, you can call them as needed.

  4. Calculate tangent vectors for all vertices via updateTangents().
  5. Calculate a bounding box and a bounding sphere for the created mesh by calling updateBounds().
  6. If necessary, you can remove duplicated vertices of the created mesh and optimize it for faster rendering. Use updateIndices() function, for example, if a quad polygon is created via addTriangles(2) (six vertices are created) instead of an optimized addTriangleQuads(1) (four vertices are created).
Usage Example

The following example shows how to create a quad polygon by using the addTriangleQuads() function.

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

#if UNIGINE_DOUBLE
	using Vec3 = Unigine.dvec3;
#else
	using Vec3 = Unigine.vec3;
#endif

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class DynamicMeshes : Component
{

	ObjectMeshDynamic mesh;

	private void Init()
	{

		// create a dynamic mesh
		mesh = new ObjectMeshDynamic();

		mesh.WorldTransform = MathLib.Translate(new Vec3(0.0f, 0.0f, 2.0f));

		// allocate space in a vertex buffer and create vertex indices
		mesh.AddTriangleQuads(1);
		
		// add vertices and assign texture coordinates, if necessary
		mesh.AddVertex(new vec3(-1.0f, -1.0f, 0.0f));
		mesh.AddTexCoord(new vec4(0, 0, 0, 0));

		mesh.AddVertex(new vec3(1.0f, -1.0f, 0.0f));
		mesh.AddTexCoord(new vec4(1, 0, 0, 0));

		mesh.AddVertex(new vec3(1.0f, 1.0f, 0.0f));
		mesh.AddTexCoord(new vec4(1, 1, 0, 0));

		mesh.AddVertex(new vec3(-1.0f, 1.0f, 0.0f));
		mesh.AddTexCoord(new vec4(0, 1, 0, 0));
		
		// calculate tangent vectors
		mesh.UpdateTangents();
		// optimize vertex and index buffers, if necessary
		//mesh.UpdateIndices(); 

		// calculate a mesh bounding box
		mesh.UpdateBounds();

	}

Adding a Large Number of Vertices#

To create dynamic meshes with a huge number of vertices, it is better to use the more optimized approach: instead of allocating memory in small chunks (which can be costly), all necessary memory is preallocated first and filled with data after that.

  1. Allocate space for triangle vertices in an index buffer using allocateIndices().
  2. Allocate space for vertices in a vertex buffer using allocateVertex().
  3. Add the corresponding number of vertices via addVertex() method.
  4. Add the corresponding number of indices via addIndex() method.
  5. Follow steps 3-6 described above.
Usage Example

The following example shows how to create a plane by adding vertex by vertex.

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

#if UNIGINE_DOUBLE
	using Vec3 = Unigine.dvec3;
#else
	using Vec3 = Unigine.vec3;
#endif

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class DynamicMeshes : Component
{

	ObjectMeshDynamic mesh;

	private void Init()
	{

		// create a dynamic mesh
		mesh = new ObjectMeshDynamic();

		mesh.WorldTransform = MathLib.Translate(new Vec3(0.0f,0.0f,2.0f));

		// allocate space in index and vertex buffers
		mesh.AllocateIndices(6);
		mesh.AllocateVertex(4);
		// add vertices
		mesh.AddVertex(new vec3(-1.0f,-1.0f,0.0f));
		mesh.AddVertex(new vec3(1.0f, -1.0f, 0.0f));
		mesh.AddVertex(new vec3(1.0f, 1.0f, 0.0f));
		mesh.AddVertex(new vec3(-1.0f, 1.0f, 0.0f));
		// add indices for created vertices
		mesh.AddIndex(0);
		mesh.AddIndex(1);
		mesh.AddIndex(2);
		mesh.AddIndex(0);
		mesh.AddIndex(2);
		mesh.AddIndex(3);

		// calculate tangent vectors
		mesh.UpdateTangents();
		// optimize vertex and index buffers, if necessary
		//mesh.UpdateIndices();

		// calculate a mesh bounding box
		mesh.UpdateBounds();
	}

Creating a Dynamic Mesh Based on the Existing Mesh#

There are some ways to create a dynamic mesh from the existing mesh:

Passing a Mesh to a New Dynamic Mesh#

To create a new ObjectMeshDynamic instance from this existing mesh, you should pass the existing Mesh class instance to the ObjectMeshDynamic class constructor.

Usage Example

The following example shows how to create a new dynamic mesh from the existing Mesh class instance. It is supposed that you already have the Mesh instance, but in this example we create a new instance of the Mesh class with the box surface.

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

#if UNIGINE_DOUBLE
	using Vec3 = Unigine.dvec3;
#else
	using Vec3 = Unigine.vec3;
#endif

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class DynamicMeshes : Component
{

	ObjectMeshDynamic dynamic_mesh;

	private void Init()
	{

		// create the mesh with the box surface
		Mesh mesh = new Mesh();
		mesh.AddBoxSurface("box", new vec3(1.0f));

		// create a new ObjectMeshDynamic instance by passing mesh as an argument
		dynamic_mesh = new ObjectMeshDynamic(mesh);
	}

Copy a Mesh to the Existing Dynamic Mesh#

To copy the mesh from the Mesh instance to an existing dynamic mesh, you should use the setMesh() function and pass the Mesh instance as an argument to it. This function copies a given mesh into the current dynamic mesh.

Usage Example

The following example shows how to copy the mesh from the Mesh instance to the ObjectMeshDynamic instance. It is supposed that you already have the Mesh instance, but in this example we create a new instance of the Mesh class with the box surface.

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

#if UNIGINE_DOUBLE
	using Vec3 = Unigine.dvec3;
#else
	using Vec3 = Unigine.vec3;
#endif

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class DynamicMeshes : Component
{

	ObjectMeshDynamic dynamic_mesh;

	private void Init()
	{

		// existing mesh with a box surface
		Mesh mesh = new Mesh();
		mesh.AddBoxSurface("box", new vec3(2.2f));

		// create a new ObjectMeshDynamic
		ObjectMeshDynamic dynamic_mesh = new ObjectMeshDynamic();

		// copy the existing mesh to the ObjectMeshDynamic
		dynamic_mesh.SetMesh(mesh);
	}

Adding Physics to Dynamic Meshes#

To add some physical properties to the dynamic mesh so that it could participate in interactions between objects or external physical forces, it should have a body. You can easily assign it from code or in UnigineEditor.

Usage Example

The following example shows how to add the rigid body with a shape to the dynamic mesh. In the example, the dynamic mesh is created from the existing mesh with the box surface.

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

#if UNIGINE_DOUBLE
	using Vec3 = Unigine.dvec3;
#else
	using Vec3 = Unigine.vec3;
#endif

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class DynamicMeshes : Component
{

	ObjectMeshDynamic dynamic_mesh;

	private void Init()
	{

		// create a mesh instance with a box surface
		Mesh mesh = new Mesh();
		mesh.AddBoxSurface("box", new vec3(0.2f));

		// create a new dynamic mesh from the Mesh instance and add it to editor
		dynamic_mesh = new ObjectMeshDynamic(mesh);

		dynamic_mesh.WorldTransform = MathLib.Translate(new Vec3(0.0f, 0.0f, 2.0f));

		// assign a body and a shape to the dynamic mesh
		BodyRigid body = new BodyRigid(dynamic_mesh);
		ShapeBox shape = new ShapeBox(body, new vec3(0.2f));
		
	}

See Also

Intersections with Dynamic Meshes#

To detect if the dynamic mesh intersects a line or bounds of the specified volume, you should use one of the following functions:

Read the Intersections article to know more about intersections.

Saving a Dynamic Mesh to a File#

To save the dynamic mesh into a file, you should copy it to the Mesh class instance. By using the save() function of the Mesh class, you can save the mesh with the specified name.

Usage Example

The following example shows how to save the dynamic mesh to the file. It is supposed that you have a dynamic mesh to save. You should pass a path as an argument to the save() function of the Mesh class. The path is relative to the data/ folder of your project.

Source code (C#)
// create a mesh instance
Mesh mesh_0 = new Mesh();
// copy the dynamic mesh to the Mesh class instance
dynamic_mesh.GetMesh(mesh_0);

// save the mesh to the data/meshes/dynamic_mesh.mesh file
mesh_0.Save("meshes/dynamic_mesh.mesh");
Last update: 2022-12-14
Build: ()