data | 顶点数据数组。 |
dataStart | 要从中进行复制的数据中的第一个元素。 |
meshBufferStart | 用于接收数据的网格顶点缓冲区中的第一个元素。 |
count | 要复制的顶点数。 |
stream | Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3. |
flags | 控制函数行为的标志,请参阅 MeshUpdateFlags。 |
设置网格顶点缓冲区的数据。
Mesh 脚本 API 的简单用法涉及使用 vertices、normals 等函数设置顶点数据。
对于需要最大性能的高级用例,可以使用具有 SetSubMesh、SetIndexBufferParams、SetIndexBufferData 和
SetVertexBufferParams 等函数的高级 API。通过此高级
API 可以访问主要适用于原始索引缓冲区、顶点缓冲区和网格子集数据的基础网格数据结构。
You can use SetVertexBufferData
to set vertex data directly, without using format conversions for each vertex attribute.
The supplied data layout has to match the vertex data layout of the mesh (see SetVertexBufferParams,
GetVertexAttributes). Partial updates of the data are also possible, via dataStart
, meshBufferStart
, count
parameters.
using UnityEngine; using UnityEngine.Rendering; using Unity.Collections;
public class Example : MonoBehaviour { // Vertex with FP32 position, FP16 2D normal and a 4-byte tangent. // In some cases StructLayout attribute needs // to be used, to get the data layout match exactly what it needs to be. [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] struct ExampleVertex { public Vector3 pos; public ushort normalX, normalY; public Color32 tangent; }
void Start() { var mesh = new Mesh(); // specify vertex count and layout var layout = new[] { new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3), new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float16, 2), new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.UNorm8, 4), }; var vertexCount = 10; mesh.SetVertexBufferParams(vertexCount, layout);
// set vertex data var verts = new NativeArray<ExampleVertex>(vertexCount, Allocator.Temp);
// ... fill in vertex array data here...
mesh.SetVertexBufferData(verts, 0, 0, vertexCount); } }
Additional resources: SetVertexBufferParams, SetIndexBufferParams, SetIndexBufferData, SetSubMesh, MeshUpdateFlags, AcquireReadOnlyMeshData.