class in UnityEngine
/
Inherits from:Object
/
Implemented in:UnityEngine.CoreModule
Suggest a change Success!Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Close Submission failedFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close Your name Your email Suggestion* Switch to Manual DescriptionA class that allows you to create or modify meshes.
Meshes contain vertices and multiple triangle arrays.
Conceptually, all vertex data is stored in separate arrays of the same size. For example, if you have a mesh of 100 Vertices, and want to have a position, normal and two texture coordinates for each vertex, then the mesh should have vertices, normals, uv and uv2 arrays, each being 100 in size. Data for i-th vertex is at index "i" in each array.
For every vertex there can be a vertex position, normal, tangent, color and up to 8 texture coordinates. Texture coordinates most often are 2D data (Vector2), but it is possible to make them Vector3 or Vector4 if needed. This is most often used for holding arbitrary data in mesh vertices, for special effects used in shaders. For skinned meshes, the vertex data can also contain boneWeights.
The mesh face data, i.e. the triangles it is made of, is simply three vertex indices for each triangle. For example, if the mesh has 10 triangles, then the triangles array should be 30 numbers, with each number indicating which vertex to use. The first three elements in the triangles array are the indices for the vertices that make up that triangle; the second three elements make up another triangle and so on.
Note that while triangle meshes are the most common use case, Unity also supports other mesh topology types, for example Line or Point meshes. For line meshes, each line is composed of two vertex indices and so on. See SetIndices and MeshTopology.
Simple vs Advanced Mesh API
The Mesh class has two sets of methods for assigning data to a Mesh from script. The "simple" set of methods provide a basis for setting the indices, triangle, normals, tangents, etc. These methods include validation checks, for example to ensure that you are not passing in data that would include out-of-bounds indices. They represent the standard way to assign Mesh data from script in Unity.
The "simple" methods are: SetColors, SetIndices, SetNormals, SetTangents, SetTriangles, SetUVs, SetVertices, SetBoneWeights.
There is also an "advanced" set of methods, which allow you to directly write to the mesh data with control over whether any checks or validation should be performed. These methods are intended for advanced use cases which require maximum performance. They are faster, but allow you to skip the checks on the data you supply. If you use these methods you must make sure that you are not supplying invalid data, because Unity will not check for you.
The "advanced" methods are: SetVertexBufferParams, SetVertexBufferData, SetIndexBufferParams, SetIndexBufferData, SetSubMesh, and you can use the MeshUpdateFlags to control which checks or validation are performed or omitted. Use AcquireReadOnlyMeshData to take a read-only snapshot of Mesh data that you can use with C# Jobs and Burst, and AllocateWritableMeshData with ApplyAndDisposeWritableMeshData to create Meshes from C# Jobs and Burst.
Manipulating meshes from a script
There are three common tasks that might want to use the Mesh API for:
1. Building a mesh from scratch: should always be done in the following order:
using UnityEngine;public class Example : MonoBehaviour { Vector3[] newVertices; Vector2[] newUV; int[] newTriangles;
void Start() { Mesh mesh = new Mesh(); GetComponent<MeshFilter>().mesh = mesh; mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
2. Modifying vertex attributes every frame:
a) Get vertices
b) Modify them
c) Assign them back to the mesh.
3. Continously changing the mesh triangles and vertices:
a) Call Clear to start fresh
b) Assign vertices and other attributes
c) Assign triangle indices.
It is important to call Clear before assigning new vertices or triangles. Unity always checks the supplied triangle indices whether they don't reference out of bounds vertices. Calling Clear then assigning vertices then triangles makes sure you never have out of bounds data.
using UnityEngine;Properties Property Description bindposeCount The number of bind poses in the Mesh. bindposes The bind poses. The bind pose at each index refers to the bone with the same index. blendShapeCount Returns BlendShape count on this mesh. boneWeights The BoneWeight for each vertex in the Mesh, which represents 4 bones per vertex. bounds The bounding volume of the Mesh. colors Vertex colors of the Mesh. colors32 Vertex colors of the Mesh. indexBufferTarget The intended target usage of the Mesh GPU index buffer. indexFormat Format of the mesh index buffer data. isReadable Returns true if the Mesh is read/write enabled, or false if it is not. normals The normals of the Mesh. skinWeightBufferLayout The dimension of data in the bone weight buffer. subMeshCount The number of sub-meshes inside the Mesh object. tangents The tangents of the Mesh. triangles An array containing all triangles in the Mesh. uv The texture coordinates (UVs) in the first channel. uv2 The texture coordinates (UVs) in the second channel. uv3 The texture coordinates (UVs) in the third channel. uv4 The texture coordinates (UVs) in the fourth channel. uv5 The texture coordinates (UVs) in the fifth channel. uv6 The texture coordinates (UVs) in the sixth channel. uv7 The texture coordinates (UVs) in the seventh channel. uv8 The texture coordinates (UVs) in the eighth channel. vertexAttributeCount Returns the number of vertex attributes that the mesh has. (Read Only) vertexBufferCount Gets the number of vertex buffers present in the Mesh. (Read Only) vertexBufferTarget The intended target usage of the Mesh GPU vertex buffer. vertexCount Returns the number of vertices in the Mesh (Read Only). vertices Returns a copy of the vertex positions or assigns a new vertex positions array. Constructors Constructor Description Mesh Creates an empty Mesh. Inherited Members Properties Property Description hideFlags Should the object be hidden, saved with the Scene or modifiable by the user? name The name of the object. Public Methods Method Description GetInstanceID Gets the instance ID of the object. ToString Returns the name of the object. Static Methods Method Description Destroy Removes a GameObject, component or asset. DestroyImmediate Destroys the object obj immediately. You are strongly recommended to use Destroy instead. DontDestroyOnLoad Do not destroy the target Object when loading a new Scene. FindAnyObjectByType Retrieves any active loaded object of Type type. FindFirstObjectByType Retrieves the first active loaded object of Type type. FindObjectsByType Retrieves a list of all loaded objects of Type type. Instantiate Clones the object original and returns the clone. InstantiateAsync Captures a snapshot of the original object (that must be related to some GameObject) and returns the AsyncInstantiateOperation. Operators Operator Description bool Does the object exist? operator != Compares if two objects refer to a different object. operator == Compares two object references to see if they refer to the same object.public class ExampleClass : MonoBehaviour { Vector3[] newVertices; Vector2[] newUV; int[] newTriangles;
void Start() { Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
// Do some calculations... mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4