Mesh

class in UnityEngine

/

Hereda de:Object

Cambiar al Manual

Descripción

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; } }

Variables

bindposesThe bind poses. The bind pose at each index refers to the bone with the same index.
blendShapeCountReturns BlendShape count on this mesh.
boneWeightsThe bone weights for each vertex in the Mesh, up to a maximum of 4.
boundsThe bounding volume of the Mesh.
colorsVertex colors of the Mesh.
colors32Vertex colors of the Mesh.
indexFormatFormat of the mesh index buffer data.
isReadableReturns true if the Mesh is read/write enabled, or false if it is not.
normalsThe normals of the Mesh.
subMeshCountThe number of sub-meshes inside the Mesh object.
tangentsThe tangents of the Mesh.
trianglesAn array containing all triangles in the Mesh.
uvThe base texture coordinates of the Mesh.
uv2The second texture coordinate set of the mesh, if present.
uv3The third texture coordinate set of the mesh, if present.
uv4The fourth texture coordinate set of the mesh, if present.
uv5The fifth texture coordinate set of the mesh, if present.
uv6The sixth texture coordinate set of the mesh, if present.
uv7The seventh texture coordinate set of the mesh, if present.
uv8The eighth texture coordinate set of the mesh, if present.
vertexAttributeCountReturns the number of vertex attributes that the mesh has. (Read Only)
vertexBufferCountGets the number of vertex buffers present in the Mesh. (Read Only)
vertexCountReturns the number of vertices in the Mesh (Read Only).
verticesReturns a copy of the vertex positions or assigns a new vertex positions array.

Constructores

MeshCreates an empty Mesh.

Funciones Públicas

AddBlendShapeFrameAdds a new blend shape frame.
ClearLimpia todos los datos de los índices de los vertices y los triángulos.
ClearBlendShapesClears all blend shapes from Mesh.
CombineMeshesCombines several Meshes into this Mesh.
GetAllBoneWeightsGets the bone weights for the Mesh.
GetBaseVertexGets the base vertex index of the given sub-mesh.
GetBindposesGets the bind poses of the Mesh.
GetBlendShapeFrameCountReturns the frame count for a blend shape.
GetBlendShapeFrameVerticesRetreives deltaVertices, deltaNormals and deltaTangents of a blend shape frame.
GetBlendShapeFrameWeightReturns the weight of a blend shape frame.
GetBlendShapeIndexReturns index of BlendShape by given name.
GetBlendShapeNameReturns name of BlendShape by given index.
GetBonesPerVertexThe number of non-zero bone weights for each vertex.
GetBoneWeightsGets the bone weights for the Mesh.
GetColorsGets the vertex colors of the Mesh.
GetIndexCountGets the index count of the given sub-mesh.
GetIndexStartGets the starting index location within the Mesh's index buffer, for the given sub-mesh.
GetIndicesFetches the index list for the specified sub-mesh.
GetNativeIndexBufferPtrRetrieves a native (underlying graphics API) pointer to the index buffer.
GetNativeVertexBufferPtrRetrieves a native (underlying graphics API) pointer to the vertex buffer.
GetNormalsGets the vertex normals of the Mesh.
GetSubMeshGet information about a sub-mesh of the Mesh.
GetTangentsGets the tangents of the Mesh.
GetTopologyGets the topology of a sub-mesh.
GetTrianglesFetches the triangle list for the specified sub-mesh on this object.
GetUVDistributionMetricThe UV distribution metric can be used to calculate the desired mipmap level based on the position of the camera.
GetUVsGets the UVs of the Mesh.
GetVertexAttributeReturns information about a vertex attribute based on its index.
GetVertexAttributeDimensionGet dimension of a specific vertex data attribute on this Mesh.
GetVertexAttributeFormatGet format of a specific vertex data attribute on this Mesh.
GetVertexAttributesGet information about vertex attributes of a Mesh.
GetVerticesGets the vertex positions of the Mesh.
HasVertexAttributeChecks if a specific vertex data attribute exists on this Mesh.
MarkDynamicOptimize mesh for frequent updates.
MarkModifiedNotify Renderer components of mesh geometry change.
OptimizeOptimizes the Mesh data to improve rendering performance.
OptimizeIndexBuffersOptimizes the geometry of the Mesh to improve rendering performance.
OptimizeReorderVertexBufferOptimizes the vertices of the Mesh to improve rendering performance.
RecalculateBoundsRecalculate the bounding volume of the Mesh from the vertices.
RecalculateNormalsRecalculates the normals of the Mesh from the triangles and vertices.
RecalculateTangentsRecalculates the tangents of the Mesh from the normals and texture coordinates.
SetBoneWeightsSets the bone weights for the Mesh.
SetColorsSet the per-vertex colors of the Mesh.
SetIndexBufferDataSets the data of the index buffer of the Mesh.
SetIndexBufferParamsSets the index buffer size and format.
SetIndicesSets the index buffer for the sub-mesh.
SetNormalsSet the normals of the Mesh.
SetSubMeshSets the information about a sub-mesh of the Mesh.
SetTangentsSet the tangents of the Mesh.
SetTrianglesSets the triangle list for the sub-mesh.
SetUVsSets the UVs of the Mesh.
SetVertexBufferDataSets the data of the vertex buffer of the Mesh.
SetVertexBufferParamsSets the vertex buffer size and layout.
SetVerticesAssigns a new vertex positions array.
UploadMeshDataUpload previously done Mesh modifications to the graphics API.

Funciones Estáticas

AcquireReadOnlyMeshDataGets a snapshot of Mesh data for read-only access.
AllocateWritableMeshDataAllocates data structures for Mesh creation using C# Jobs.
ApplyAndDisposeWritableMeshDataApplies data defined in MeshData structs to Mesh objects.

Miembros heredados

Variables

hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameEl nombre del objeto.

Funciones Públicas

GetInstanceIDDevuelve el id de la instancia del objeto.
ToStringReturns the name of the object.

Funciones Estáticas

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindObjectOfTypeDevuelve el primer objeto activo cargado de tipo type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
InstantiateClona el objeto original y devuelve el clon.

Operadores

bool¿Existe el objeto?
operator !=Compare si dos objetos se refieren a un objeto diferente.
operator ==Compara dos referencias de objeto para ver si se refieren al mismo objeto.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961