Main Content

Build Actor from Mesh Data and Apply Texture Using MATLAB

This example shows how to build an actor from mesh data and apply texture using MATLAB®. You can build an appearance for the actor using the createMesh function and apply texture using the Texture property of the sim3d.Actor object. A mesh is a 3D build of a model consisting of polygons and is defined by vertices, normals, and faces. To build an actor from mesh data and apply texture using Simulink®, see Build Actor from Mesh Data and Apply Texture Using Simulink.

This example uses the mesh data of a cube to build a cube actor. First, you create a 3D environment using sim3d.World object and an actor using sim3d.Actor object. Next, input the mesh data to build the appearance of the actor and apply a texture. Then, you add the actor to the world and set a view in the scene. Finally, you view the actor in the Simulation 3D Viewer window.

Create 3D Environment

Create a world object.

world = sim3d.World();

Build Actor from Mesh Data

Instantiate an actor object named cube. You can use any name for the actor.

Actor = sim3d.Actor('ActorName', 'cube');

To build an actor with createMesh function, specify the input arguments vertices (V), normals (N), and faces (F) of the mesh. This example uses the mesh data for a cube of size [1 1 1]. The mesh data contains 24 vertices, 24 normals for each of the vertices, and 12 triangular faces. Each face of the cube is made with two triangular faces, and each triangular face is made with three vertices. If you can have the vertices, normals, and faces of an actor, you can build the actor with createMesh function.

V = [-0.5 -0.5 -0.5; 0.5 -0.5 -0.5; 0.5 0.5 -0.5; -0.5 0.5 -0.5; ...
     -0.5 -0.5 0.5; 0.5 -0.5 0.5; 0.5 0.5 0.5; -0.5 0.5 0.5; ...
     -0.5 -0.5 -0.5; 0.5 -0.5 -0.5; 0.5 0.5 -0.5; -0.5 0.5 -0.5; ...
     -0.5 -0.5 0.5; 0.5 -0.5 0.5; 0.5 0.5 0.5; -0.5 0.5 0.5; ...
     -0.5 -0.5 -0.5; 0.5 -0.5 -0.5; 0.5 0.5 -0.5; -0.5 0.5 -0.5; ...
     -0.5 -0.5 0.5; 0.5 -0.5 0.5; 0.5 0.5 0.5; -0.5 0.5 0.5];

N = [0 0 -1; 0 0 -1; 0 0 -1; 0 0 -1; 0 0 1; 0 0 1; ...
     0 0 1; 0 0 1; -1 0 0; 1 0 0; 1 0 0; -1 0 0; ...
     -1 0 0; 1 0 0; 1 0 0; -1 0 0; 0 -1 0; 0 -1 0; ...
     0 1 0; 0 1 0; 0 -1 0; 0 -1 0; 0 1 0; 0 1 0];

F = [0 2 3; 0 1 2; 16 20 21; 21 17 16; 9 13 10; 10 13 14; ...
     18 22 19; 19 22 23; 11 15 8; 8 15 12; 4 7 5; 5 7 6];

Apply Texture

The input argument Texture coordinates, T, maps the texture file on to each vertices of the actor. T defines which point on the texture file maps to each of the vertices.

T = [0 0; 1 0; 1 1; 0 1; 0 1; 1 1; 1 0; 0 0; 1 1; 0 1; 1 1; 0 1; ...
     1 0; 0 0; 1 0; 0 0; 0 1; 1 1; 0 1; 1 1; 0 0; 1 0; 0 0; 1 0];

The input argument Vertex Colors, C, specifies color for each vertex in [R G B] components. To display the vertex colors, you must set the VertexBlend property of sim3d.Actor object to a value greater than 0 and less than 1. C is optional to build an actor from mesh data and for this example, C is an empty matrix.

C = [];

Create a mesh using the actor object and mesh data. Use the image file to set the texture. Add the actor object to the world.

createMesh(Actor, V, N, F, T, C);
Actor.Texture = fullfile(pwd,"image.png");
add(world,Actor);

Set Actor Transformation

Use the actor object translation, rotation, and scale properties to orient the actor relative to the world origin.

Actor.Translation = [0 0 0];
Actor.Rotation = [0 0 0];
Actor.Scale = [1 1 1];

Set Viewer Window Point of View

If you do not create a viewport, then the default view is set and you can use the keyboard shortcuts and mouse controls to navigate in the Simulation 3D Viewer window.

For this example, use the createViewport function to create a viewport.

viewport = createViewport(world,Translation=[-6 0 1]);

Run Animation

Run a simulation set for 10 seconds with a sample time of 0.02 seconds.

sampletime = 0.02;
stoptime = 10;
run(world,sampletime,stoptime);

Cube actor with texture in the virtual world scene.

Delete World

Delete the world object.

delete(world)

See Also

| | | |

Related Topics