Version: 2020.2

Mesh

class in UnityEngine

/

继承自:Object

切换到手册

描述

用于通过脚本创建或修改网格的类。

Meshes contain vertices and multiple triangle arrays.

三角形数组是顶点数组的简单索引;每个三角形三个索引。

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.

您可能需要将可修改的网格接口用于 3 种用途:

有三种常见任务可能需要使用网格 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; } }

变量

bindposes绑定姿势。每个索引处的绑定姿势引用索引相同的骨骼。
blendShapeCount返回此网格上的 BlendShape 计数。
boneWeightsThe BoneWeight for each vertex in the Mesh, which represents 4 bones per vertex.
bounds网格的包围体。
colors网格的顶点颜色。
colors32网格的顶点颜色。
indexFormat网格索引缓冲区数据的格式。
isReadable如果网格支持读/写,则返回 true,否则返回 false。
normals网格的法线。
subMeshCountMesh 对象中的子网格数。
tangents网格的切线。
triangles包含网格中所有三角形的数组。
uv网格的基础纹理坐标。
uv2网格的第二个纹理坐标集(如果存在)。
uv3网格的第三个纹理坐标集(如果存在)。
uv4网格的第四个纹理坐标集(如果存在)。
uv5网格的第五个纹理坐标集(如果存在)。
uv6网格的第六个纹理坐标集(如果存在)。
uv7网格的第七个纹理坐标集(如果存在)。
uv8网格的第八个纹理坐标集(如果存在)。
vertexAttributeCount返回网格所具有的顶点属性数。(只读)
vertexBufferCount获取网格中存在的顶点缓冲区数。(只读)
vertexCount返回网格中的顶点数(只读)。
vertices返回顶点位置的副本或分配新顶点位置数组。

构造函数

Mesh创建空网格。

公共函数

AddBlendShapeFrame添加新的混合形状帧。
Clear清除所有顶点数据和所有三角形索引。
ClearBlendShapes从网格中清除所有混合形状。
CombineMeshes将多个网格合并到此网格中。
GetAllBoneWeights获取网格的骨骼权重。
GetBaseVertex获取给定 sub-mesh 的基顶点索引。
GetBindposes获取网格的绑定姿势。
GetBlendShapeFrameCount返回混合形状的帧计数。
GetBlendShapeFrameVertices检索混合形状帧的 deltaNormals、deltaNormals 和 /deltaTangents/。
GetBlendShapeFrameWeight返回混合形状帧的权重。
GetBlendShapeIndex按给定名称返回 BlendShape 的索引。
GetBlendShapeName按给定索引返回 BlendShape 的名称。
GetBonesPerVertex每个顶点的非零骨骼权重数量。
GetBoneWeights获取网格的骨骼权重。
GetColors获取网格的顶点颜色。
GetIndexCount获取给定 sub-mesh 的索引计数。
GetIndexStart对于给定 /sub-mesh/,获取网格索引缓冲区中的起始索引位置。
GetIndices获取指定子网格的索引列表。
GetNativeIndexBufferPtr检索指向索引缓冲区的原生(底层图形 API)指针。
GetNativeVertexBufferPtr检索指向顶点缓冲区的原生(底层图形 API)指针。
GetNormals获取网格的顶点法线。
GetSubMesh获取有关网格的子网格的信息。
GetTangents获取网格的切线。
GetTopology获取子网格的拓扑。
GetTriangles获取此对象上指定子网格的切线列表。
GetUVDistributionMetricUV 分布指标可用于根据摄像机的位置计算所需的 Mipmap 级别。
GetUVs获取网格的 UV。
GetVertexAttribute基于索引返回有关顶点属性的信息。
GetVertexAttributeDimension获取此网格上特定顶点数据属性的尺寸。
GetVertexAttributeFormat获取此网格上特定顶点数据属性的格式。
GetVertexAttributes获取有关网格的顶点属性的信息。
GetVertices获取网格的顶点位置。
HasVertexAttribute检查特定顶点数据属性是否在此网格上存在。
MarkDynamic优化网格以便频繁更新。
MarkModified向 Renderer 组件通知网格几何体更改。
Optimize优化网格数据以提高渲染性能。
OptimizeIndexBuffers优化网格几何体以提高渲染性能。
OptimizeReorderVertexBuffer优化网格顶点以提高渲染性能。
RecalculateBounds从顶点重新计算网格的包围体。
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.
SetBoneWeights设置网格的骨骼权重。
SetColors设置网格的每顶点颜色。
SetIndexBufferData设置网格索引缓冲区的数据。
SetIndexBufferParams设置索引缓冲区大小和格式。
SetIndices为子网格设置索引缓冲区。
SetNormals设置网格的法线。
SetSubMesh设置有关网格的子网格的信息。
SetSubMeshesSets information defining all sub-meshes in this Mesh, replacing any existing sub-meshes.
SetTangents设置网格的切线。
SetTriangles为子网格设置三角形列表。
SetUVs设置网格的 UV。
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对象的名称。

公共函数

GetInstanceID返回对象的实例 ID。
ToString返回对象的名称。

静态函数

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoad在加载新的 Scene 时,请勿销毁 Object。
FindObjectOfType返回第一个类型为 type 的已加载的激活对象。
FindObjectsOfTypeGets a list of all loaded objects of Type type.
Instantiate克隆 original 对象并返回克隆对象。

运算符

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