class in UnityEngine
/
Inherits from:Object
/
Implemented in:UnityEngine.CoreModule
A class that allows creating or modifying meshes from scripts.
Meshes contain vertices and multiple triangle arrays.
The triangle arrays are simply indices into the vertex arrays; three indices for each triangle.
For every vertex there can be a normal, two texture coordinates, color and tangent. These are optional though and can be removed at will. All vertex information is stored in separate arrays of the same size, so if your mesh has 10 vertices, you would also have 10-size arrays for normals and other attributes.
There are probably 3 things you might want to use the modifyable mesh interface for:
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:
a) Assign vertices
b) Assign triangles.
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.
using UnityEngine;
public class Example : MonoBehaviour { void Update() { Mesh mesh = GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices; Vector3[] normals = mesh.normals;
for (var i = 0; i < vertices.Length; i++) { vertices[i] += normals[i] * Mathf.Sin(Time.time); }
mesh.vertices = vertices; } }
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;
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; } }
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. |
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. |
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 base texture coordinates of the Mesh. |
uv2 | The second texture coordinate set of the mesh, if present. |
uv3 | The third texture coordinate set of the mesh, if present. |
uv4 | The fourth texture coordinate set of the mesh, if present. |
uv5 | The fifth texture coordinate set of the mesh, if present. |
uv6 | The sixth texture coordinate set of the mesh, if present. |
uv7 | The seventh texture coordinate set of the mesh, if present. |
uv8 | The eighth texture coordinate set of the mesh, if present. |
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) |
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. |
Mesh | Creates an empty Mesh. |
AddBlendShapeFrame | Adds a new blend shape frame. |
Clear | Clears all vertex data and all triangle indices. |
ClearBlendShapes | Clears all blend shapes from Mesh. |
CombineMeshes | Combines several Meshes into this Mesh. |
GetAllBoneWeights | Gets the bone weights for the Mesh. |
GetBaseVertex | Gets the base vertex index of the given sub-mesh. |
GetBindposes | Gets the bind poses of the Mesh. |
GetBlendShapeFrameCount | Returns the frame count for a blend shape. |
GetBlendShapeFrameVertices | Retreives deltaVertices, deltaNormals and deltaTangents of a blend shape frame. |
GetBlendShapeFrameWeight | Returns the weight of a blend shape frame. |
GetBlendShapeIndex | Returns index of BlendShape by given name. |
GetBlendShapeName | Returns name of BlendShape by given index. |
GetBonesPerVertex | The number of non-zero bone weights for each vertex. |
GetBoneWeights | Gets the bone weights for the Mesh. |
GetColors | Gets the vertex colors of the Mesh. |
GetIndexCount | Gets the index count of the given sub-mesh. |
GetIndexStart | Gets the starting index location within the Mesh's index buffer, for the given sub-mesh. |
GetIndices | Fetches the index list for the specified sub-mesh. |
GetNativeIndexBufferPtr | Retrieves a native (underlying graphics API) pointer to the index buffer. |
GetNativeVertexBufferPtr | Retrieves a native (underlying graphics API) pointer to the vertex buffer. |
GetNormals | Gets the vertex normals of the Mesh. |
GetSubMesh | Get information about a sub-mesh of the Mesh. |
GetTangents | Gets the tangents of the Mesh. |
GetTopology | Gets the topology of a sub-mesh. |
GetTriangles | Fetches the triangle list for the specified sub-mesh on this object. |
GetUVDistributionMetric | The UV distribution metric can be used to calculate the desired mipmap level based on the position of the camera. |
GetUVs | Gets the UVs of the Mesh. |
GetVertexAttribute | Returns information about a vertex attribute based on its index. |
GetVertexAttributeDimension | Get dimension of a specific vertex data attribute on this Mesh. |
GetVertexAttributeFormat | Get format of a specific vertex data attribute on this Mesh. |
GetVertexAttributes | Get information about vertex attributes of a Mesh. |
GetVertices | Gets the vertex positions of the Mesh. |
HasVertexAttribute | Checks if a specific vertex data attribute exists on this Mesh. |
MarkDynamic | Optimize mesh for frequent updates. |
MarkModified | Notify Renderer components of mesh geometry change. |
Optimize | Optimizes the Mesh data to improve rendering performance. |
OptimizeIndexBuffers | Optimizes the geometry of the Mesh to improve rendering performance. |
OptimizeReorderVertexBuffer | Optimizes the vertices of the Mesh to improve rendering performance. |
RecalculateBounds | Recalculate the bounding volume of the Mesh from the vertices. |
RecalculateNormals | Recalculates the normals of the Mesh from the triangles and vertices. |
RecalculateTangents | Recalculates the tangents of the Mesh from the normals and texture coordinates. |
RecalculateUVDistributionMetric | Recalculates the UV distribution metric of the Mesh from the vertices and uv coordinates. |
RecalculateUVDistributionMetrics | Recalculates the UV distribution metrics of the Mesh from the vertices and uv coordinates. |
SetBoneWeights | Sets the bone weights for the Mesh. |
SetColors | Set the per-vertex colors of the Mesh. |
SetIndexBufferData | Sets the data of the index buffer of the Mesh. |
SetIndexBufferParams | Sets the index buffer size and format. |
SetIndices | Sets the index buffer for the sub-mesh. |
SetNormals | Set the normals of the Mesh. |
SetSubMesh | Sets the information about a sub-mesh of the Mesh. |
SetSubMeshes | Sets information defining all sub-meshes in this Mesh, replacing any existing sub-meshes. |
SetTangents | Set the tangents of the Mesh. |
SetTriangles | Sets the triangle list for the sub-mesh. |
SetUVs | Sets the UVs of the Mesh. |
SetVertexBufferData | Sets the data of the vertex buffer of the Mesh. |
SetVertexBufferParams | Sets the vertex buffer size and layout. |
SetVertices | Assigns a new vertex positions array. |
UploadMeshData | Upload previously done Mesh modifications to the graphics API. |
AcquireReadOnlyMeshData | Gets a snapshot of Mesh data for read-only access. |
AllocateWritableMeshData | Allocates data structures for Mesh creation using C# Jobs. |
ApplyAndDisposeWritableMeshData | Applies data defined in MeshData structs to Mesh objects. |
hideFlags | Should the object be hidden, saved with the Scene or modifiable by the user? |
name | The name of the object. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the object. |
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. |
FindObjectOfType | Returns the first active loaded object of Type type. |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | Clones the object original and returns the clone. |
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. |