SetVertexBufferData
Sets the data of the vertex buffer of the Mesh.
Read time 6 minutesLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
SetVertexBufferData<T>(NativeArray<T>, int, int, int, int, MeshUpdateFlags)
Sets the data of the vertex buffer of the Mesh.
public void SetVertexBufferData<T>(NativeArray<T> data, int dataStart, int meshBufferStart, int count, int stream = 0, MeshUpdateFlags flags = MeshUpdateFlags.Default) where T : struct
Parameters
Vertex data array.
The first element in the data to copy from.
The first element in mesh vertex buffer to receive the data.
Number of vertices to copy.
Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3.
Flags controlling the function behavior, see MeshUpdateFlags.
Remarks
Simple usage of Mesh scripting API involves using functions like Mesh.vertices, Mesh.normals to setup vertex data.
For advanced use cases that require maximum performance, you can use the advanced API, which has functions like Mesh.SetSubMesh, Mesh.SetIndexBufferParams, Mesh.SetIndexBufferData, and Mesh.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 Mesh.SetVertexBufferParams, Mesh.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: Mesh.SetVertexBufferParams, Mesh.SetIndexBufferParams, Mesh.SetIndexBufferData, Mesh.SetSubMesh, MeshUpdateFlags, Mesh.AcquireReadOnlyMeshData.
SetVertexBufferData<T>(`<>[], int, int, int, int, MeshUpdateFlags)
Sets the data of the vertex buffer of the Mesh.
public void SetVertexBufferData<T>(T[] data, int dataStart, int meshBufferStart, int count, int stream = 0, MeshUpdateFlags flags = MeshUpdateFlags.Default) where T : struct
Parameters
Vertex data array.
The first element in the data to copy from.
The first element in mesh vertex buffer to receive the data.
Number of vertices to copy.
Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3.
Flags controlling the function behavior, see MeshUpdateFlags.
Remarks
Simple usage of Mesh scripting API involves using functions like Mesh.vertices, Mesh.normals to setup vertex data.
For advanced use cases that require maximum performance, you can use the advanced API, which has functions like Mesh.SetSubMesh, Mesh.SetIndexBufferParams, Mesh.SetIndexBufferData, and Mesh.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 Mesh.SetVertexBufferParams, Mesh.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: Mesh.SetVertexBufferParams, Mesh.SetIndexBufferParams, Mesh.SetIndexBufferData, Mesh.SetSubMesh, MeshUpdateFlags, Mesh.AcquireReadOnlyMeshData.
SetVertexBufferData<T>(List<T>, int, int, int, int, MeshUpdateFlags)
Sets the data of the vertex buffer of the Mesh.
public void SetVertexBufferData<T>(List<T> data, int dataStart, int meshBufferStart, int count, int stream = 0, MeshUpdateFlags flags = MeshUpdateFlags.Default) where T : struct
Parameters
Vertex data array.
The first element in the data to copy from.
The first element in mesh vertex buffer to receive the data.
Number of vertices to copy.
Vertex buffer stream to set data for (default 0). Value must be within range 0 to 3.
Flags controlling the function behavior, see MeshUpdateFlags.
Remarks
Simple usage of Mesh scripting API involves using functions like Mesh.vertices, Mesh.normals to setup vertex data.
For advanced use cases that require maximum performance, you can use the advanced API, which has functions like Mesh.SetSubMesh, Mesh.SetIndexBufferParams, Mesh.SetIndexBufferData, and Mesh.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 Mesh.SetVertexBufferParams, Mesh.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: Mesh.SetVertexBufferParams, Mesh.SetIndexBufferParams, Mesh.SetIndexBufferData, Mesh.SetSubMesh, MeshUpdateFlags, Mesh.AcquireReadOnlyMeshData.