Version: 2023.2

Mesh

class in UnityEngine

/

继承自:Object

切换到手册

描述

A class that allows you to create or modify meshes.

Meshes contain vertices and multiple triangle arrays.

从概念上讲,所有顶点数据都存储在大小相同的单独数组中。例如,如果 一个网格有 100 个顶点,并且希望每个顶点都具有位置、法线和两个 纹理坐标,则该网格应具有 verticesnormalsuvuv2 数组,每个数组的大小都是 100。第 i 个顶点的数据在每个数组中处于索引“i”处。

对于每个顶点,可以具有顶点位置、法线、切线、颜色和最多 8 个纹理坐标。 纹理坐标通常是 2D 数据 (Vector2),但是在需要时可以 将它们设为 Vector3Vector4。这通常用于保存网格顶点中的任意 数据,以及用于着色器中使用的特殊效果。对于蒙皮网格,顶点数据还可以 包含 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.

请注意,虽然三角形网格是最常见的用例,不过 Unity 也支持其他 网格拓扑类型,例如线网格或点网格。对于线网格,每条线 由两个顶点索引组成,依此类推。请参阅 SetIndicesMeshTopology

**简单与高级网格 API**

Mesh 类具有两组方法,可用于从脚本向网格分配数据。“简单”的一组方法为设置索引、三角形、法线、切线等提供基础。这些方法包括验证检查,例如用于确保不会传入包含越界索引的数据。这组方法代表了在 Unity 中从脚本分配网格数据的标准方式。

The "simple" methods are: SetColors, SetIndices, SetNormals, SetTangents, SetTriangles, SetUVs, SetVertices, SetBoneWeights.

还有一组“高级”方法,通过这些方法可以直接写入网格数据,并控制是否应执行任何检查或验证。这些方法旨在用于需要最大性能的高级用例。它们速度更快,不过允许跳过对提供的数据进行的检查。如果使用这些方法,则必须确保未提供无效数据,因为 Unity 不会进行检查。

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.



**从脚本操作网格**

有三种常见任务可能需要使用网格 API:

**1.从头开始构建网格**: 应始终按以下顺序进行:
a) 分配 vertices
b) 分配 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.每一帧都修改顶点属性**:
a) 获取顶点
b) 修改它们
c) 将它们分配回网格。

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.连续更改网格三角形和顶点**:
a) 调用 Clear 以开始刷新
b) 分配顶点和其他属性
c) 分配三角形索引。

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

变量

bindposeCountThe number of bind poses in the Mesh.
bindposes绑定姿势。每个索引处的绑定姿势引用索引相同的骨骼。
blendShapeCount返回此网格上的 BlendShape 计数。
boneWeightsThe BoneWeight for each vertex in the Mesh, which represents 4 bones per vertex.
bounds网格的包围体。
colors网格的顶点颜色。
colors32网格的顶点颜色。
indexBufferTargetThe intended target usage of the Mesh GPU index buffer.
indexFormat网格索引缓冲区数据的格式。
isReadable如果网格支持读/写,则返回 true,否则返回 false。
normals网格的法线。
skinWeightBufferLayoutThe dimension of data in the bone weight buffer.
subMeshCountMesh 对象中的子网格数。
tangents网格的切线。
triangles包含网格中所有三角形的数组。
uvThe texture coordinates (UVs) in the first channel.
uv2The texture coordinates (UVs) in the second channel.
uv3The texture coordinates (UVs) in the third channel.
uv4The texture coordinates (UVs) in the fourth channel.
uv5The texture coordinates (UVs) in the fifth channel.
uv6The texture coordinates (UVs) in the sixth channel.
uv7The texture coordinates (UVs) in the seventh channel.
uv8The texture coordinates (UVs) in the eighth channel.
vertexAttributeCount返回网格所具有的顶点属性数。(只读)
vertexBufferCount获取网格中存在的顶点缓冲区数。(只读)
vertexBufferTargetThe intended target usage of the Mesh GPU vertex buffer.
vertexCount返回网格中的顶点数(只读)。
vertices返回顶点位置的副本或分配新顶点位置数组。

构造函数

Mesh创建空网格。

公共函数

AddBlendShapeFrame添加新的混合形状帧。
Clear清除所有顶点数据和所有三角形索引。
ClearBlendShapes从网格中清除所有混合形状。
CombineMeshes将多个网格合并到此网格中。
GetAllBoneWeights获取网格的骨骼权重。
GetBaseVertex获取给定 sub-mesh 的基顶点索引。
GetBindposes获取网格的绑定姿势。
GetBlendShapeBufferRetrieves a GraphicsBuffer that provides direct read and write access to GPU blend shape vertex data.
GetBlendShapeBufferRangeGet the location of blend shape vertex data for a given blend shape.
GetBlendShapeFrameCount返回混合形状的帧计数。
GetBlendShapeFrameVertices检索混合形状帧的 deltaNormals、deltaNormals 和 /deltaTangents/。
GetBlendShapeFrameWeight返回混合形状帧的权重。
GetBlendShapeIndex按给定名称返回 BlendShape 的索引。
GetBlendShapeName按给定索引返回 BlendShape 的名称。
GetBonesPerVertex每个顶点的非零骨骼权重数量。
GetBoneWeightBufferRetrieves a GraphicsBuffer that provides direct read and write access to GPU bone weight data.
GetBoneWeights获取网格的骨骼权重。
GetColors获取网格的顶点颜色。
GetIndexBufferRetrieves a GraphicsBuffer to the GPU index buffer.
GetIndexCount获取给定 sub-mesh 的索引计数。
GetIndexStart对于给定 /sub-mesh/,获取网格索引缓冲区中的起始索引位置。
GetIndices获取指定子网格的索引列表。
GetNativeIndexBufferPtr检索指向索引缓冲区的原生(底层图形 API)指针。
GetNativeVertexBufferPtr检索指向顶点缓冲区的原生(底层图形 API)指针。
GetNormals获取网格的顶点法线。
GetSubMesh获取有关网格的子网格的信息。
GetTangents获取网格的切线。
GetTopology获取子网格的拓扑。
GetTriangles获取此对象上指定子网格的切线列表。
GetUVDistributionMetricUV 分布指标可用于根据摄像机的位置计算所需的 Mipmap 级别。
GetUVsGets the texture coordinates (UVs) stored in a given channel.
GetVertexAttribute基于索引返回有关顶点属性的信息。
GetVertexAttributeDimension获取此网格上特定顶点数据属性的尺寸。
GetVertexAttributeFormat获取此网格上特定顶点数据属性的格式。
GetVertexAttributeOffsetGet offset within a vertex buffer stream of a specific vertex data attribute on this Mesh.
GetVertexAttributes获取有关网格的顶点属性的信息。
GetVertexAttributeStreamGets the vertex buffer stream index of a specific vertex data attribute on this Mesh.
GetVertexBufferRetrieves a GraphicsBuffer that provides direct acces to the GPU vertex buffer.
GetVertexBufferStrideGet vertex buffer stream stride in bytes.
GetVertices获取网格的顶点位置。
HasVertexAttribute检查特定顶点数据属性是否在此网格上存在。
MarkDynamic优化网格以便频繁更新。
MarkModified向 Renderer 组件通知网格几何体更改。
Optimize优化网格数据以提高渲染性能。
OptimizeIndexBuffers优化网格几何体以提高渲染性能。
OptimizeReorderVertexBuffer优化网格顶点以提高渲染性能。
RecalculateBoundsRecalculate the bounding volume of the Mesh and all of its sub-meshes with the vertex data.
RecalculateNormals从三角形和顶点重新计算网格的法线。
RecalculateTangents从法线和纹理坐标重新计算网格的切线。
RecalculateUVDistributionMetricRecalculates the UV distribution metric of the Mesh from the vertices and uv coordinates.
RecalculateUVDistributionMetricsRecalculates the UV distribution metrics of the Mesh from the vertices and uv coordinates.
SetBindposesSets the bind poses of the Mesh.
SetBoneWeights设置网格的骨骼权重。
SetColors设置网格的每顶点颜色。
SetIndexBufferData设置网格索引缓冲区的数据。
SetIndexBufferParams设置索引缓冲区大小和格式。
SetIndices为子网格设置索引缓冲区。
SetNormals设置网格的法线。
SetSubMesh设置有关网格的子网格的信息。
SetSubMeshesSets information defining all sub-meshes in this Mesh, replacing any existing sub-meshes.
SetTangents设置网格的切线。
SetTriangles为子网格设置三角形列表。
SetUVsSets the texture coordinates (UVs) stored in a given channel.
SetVertexBufferData设置网格顶点缓冲区的数据。
SetVertexBufferParams设置顶点缓冲区大小和布局。
SetVertices分配新的顶点位置数组。
UploadMeshData将以前进行的网格修改上传到图形 API。

静态函数

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.

继承的成员

变量

hideFlags该对象应该隐藏、随场景一起保存还是由用户修改?
name对象的名称。

公共函数

GetInstanceIDGets the instance ID of the object.
ToString返回对象的名称。

静态函数

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoad在加载新的 Scene 时,请勿销毁 Object。
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
Instantiate克隆 original 对象并返回克隆对象。

运算符

bool该对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用,判断它们是否引用同一个对象。