Mesh.SetVertexBufferData

Switch to Manual
public void SetVertexBufferData (NativeArray<T> data, int dataStart, int meshBufferStart, int count, int stream, Rendering.MeshUpdateFlags flags);
public void SetVertexBufferData (T[] data, int dataStart, int meshBufferStart, int count, int stream, Rendering.MeshUpdateFlags flags);
public void SetVertexBufferData (List<T> data, int dataStart, int meshBufferStart, int count, int stream, Rendering.MeshUpdateFlags flags);

Parameters

dataVertex data array.
dataStartThe first element in the data to copy from.
meshBufferStartThe first element in mesh vertex buffer to receive the data.
countNumber of vertices to copy.
streamVertex buffer stream to set data for (default 0).
flagsFlags controlling the function behavior, see MeshUpdateFlags.

Description

Sets the data of the vertex buffer of the Mesh.

Simple usage of Mesh scripting API involves using functions like vertices, normals to setup vertex data.

For advanced use cases that require maximum performance, you can use the advanced API, which has functions like SetSubMesh, SetIndexBufferParams, SetIndexBufferData, and SetVertexBufferParams. This advanced API gives access to the underlying mesh data structures that primarily work on raw index buffers, vertex buffers and mesh subset data.

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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961