Main Content

Build Actor from Mesh Data and Apply Texture Using Simulink

This example shows how to build an actor from mesh data and apply texture using Simulink®. 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 MATLAB®, see Build Actor from Mesh Data and Apply Texture Using MATLAB.

This example uses the mesh data of a cube to build a cube actor. You can use Simulation 3D Actor block and Simulation 3D Scene Configuration block to create a 3D environment with an actor. You can build the appearance of the actor from mesh data, apply texture, and set a view in the scene to view the actor in the Simulation 3D Viewer window.

Open Model

Open the Simulink model.

open_system("CreateMeshActorAndApplyTexture");

Simulink model with Simulation 3D Actor block named cube and Simulation 3D Scene Configuration block.

Explore Model Components

The model includes a Simulation 3D Actor block and a Simulation 3D Scene Configuration block. The Simulation 3D Scene Configuration block implements a 3D simulation environment. Double-click the Simulation 3D Scene Configuration block to open the Block Parameters dialog box. Set a view in the scene with the Scene view parameter. You can also set a custom viewpoint with this parameter. You must include the configuration block when building Simulink models with Simulation 3D Actor blocks.

Block parameter dialog box of Simulation 3D Scene Configuration block.

The Simulation 3D Actor block adds an actor to the 3D environment. Double-click the Simulation 3D Actor block to open the Block Parameters dialog box. To create an actor before simulation starts, on the Main tab, set the Operation parameter to Create at setup. The block first creates an empty actor with the name specified in the Actor name parameter. You can use any name for the actor. Then, the block loads the source file, if any is present, and runs the Initialization script. For more details, see Operating Modes.

The block parameter dialog box of the Simulation 3D Actor block named cube shows parameters and the initialization script

The Initialization script builds an actor using the createMesh function with mesh data, vertices (V), normals (N), and faces (F). 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. 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. 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. For this example, C is an empty matrix. On the Transform tab, you can also set the Initial position, Initial rotation and Actor scale of the actor.

Simulate Model

Simulate the model and view the cube actor with texture in the Simulation 3D Viewer.

sim("CreateMeshActorAndApplyTexture");

Cube actor with texture in the virtual world scene.

Close Model

Close the Simulink model.

close_system("CreateMeshActorAndApplyTexture");

See Also

|

Related Topics