createMesh
Description
Examples
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);
Delete World
Delete the world object.
delete(world)
Input Arguments
actor
— Actor class where mesh is being created
sim3d.Actor
object
Actor class where mesh is created, specified as a sim3d.Actor
object.
vertices
— Vertex positions
N-by-3 matrix
Vertex positions, specified as an N-by-3 matrix. This matrix includes the coordinates of all vertex positions to be used for the mesh geometry. Each vertex has a vertex ID equal to its row number in the matrix. N specifies the number of vertices.
Example: vertices = [-1 -1 0; 1 -1 0; 1 1 0; -1 1 0; -1 -1 0; 1 -1 0;1 1 0;-1 1
0]
Data Types: double
faces
— Vertices of each triangular face
M-by-3 matrix
Vertices of each triangular face, specified as an M-by-3 matrix. This matrix defines how each triangle of the mesh is drawn. The matrix specifies the vertex IDs that define each triangular face of the mesh. M is the number of triangular faces in the mesh.
Example: faces = [0 3 2; 0 2 1; 4 6 7;
4 5 6]
Data Types: double
normals
— Normal vectors of each vertex
N-by-3 matrix
Normal vectors of each vertex, specified as an N-by-3 matrix. Each
row of the matrix specifies the normal vector for a vertex. This matrix must be the same
size as vertices
matrix.
Example: normals = [0 0 1; 0 0 1; 0 0 1; 0 0 1; 0 0 -1; 0 0 -1; 0 0 -1; 0 0
-1]
Data Types: double
vcolor
— Vertex colors
N-by-3 matrix
Vertex colors, specified as an N-by-3 matrix. This matrix specifies the
color value of each vertex in the [R G B]
form. This matrix must be the
same length as vertices
.
Note
To display vcolor
along with the actor base color, set the
VertexBlend
property to a value greater than 0. If
VertexBlend
is 1, then actor displays only the
vcolor
.
Example: vcolor = [1 0 0; 0 1 0; 0 0 1; 1 0 1; 1 1 0; 0 1 1; 1 1 0; 0 1
0]
Data Types: double
tcoords
— Texture coordinates
N-by-2 matrix
Texture coordinates for each vertex, specified as an N-by-2 matrix. This
matrix defines which point on the texture file maps to each of
the vertices. This matrix must be the same length as
vertices
.
Example: tcoords = [0 0; 0 1; 1 1; 1 0; 0 0; 0 1; 1 1; 1 0]
Data Types: double
Version History
Introduced in R2022b
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)