有关网格顶点的单个 VertexAttribute 的信息。
Mesh 顶点数据包含不同的顶点属性。例如,顶点可以包括位置、法线、TexCoord0 和颜色。
网格通常使用已知格式的数据布局,例如,位置通常是 3 分量浮点矢量 (Vector3),
不过也可以为网格指定非标准数据格式及其布局。
可以使用 VertexAttributeDescriptor
在 Mesh.SetVertexBufferParams 中指定自定义网格数据布局。
Vertex data is laid out in separate "streams" (each stream goes into a separate vertex buffer in the underlying graphics API).
Unity supports up to four vertex streams, but you usually use only one stream. Separate streams are most useful when some vertex attributes don't need to be processed, or you need to give the vertex attributes a specific data layout.
在每个流中,顶点属性按一个接一个的方式布局,顺序如下:VertexAttribute.Position、
VertexAttribute.Normal、VertexAttribute.Tangent、VertexAttribute.Color、VertexAttribute.TexCoord0、
...、VertexAttribute.TexCoord7、VertexAttribute.BlendWeight、VertexAttribute.BlendIndices。
If you include BlendWeight
or BlendIndices
attributes in your vertex data, use Unity's default stream layout so Unity doesn't reorder your vertex attributes or incorrectly render your vertices in a SkinnedMeshRenderer.
In the first stream, add VertexAttribute.Position, VertexAttribute.Normal and VertexAttribute.Tangent.
In the second stream, add VertexAttribute.Color, and VertexAttribute.TexCoord0 to VertexAttribute.TexCoord7.
In the third stream, add VertexAttribute.BlendWeight and VertexAttribute.BlendIndices.
All the attributes in the second stream are optional. If you don't include any Color
or TexCoord
attributes, add BlendWeight
and BlendIndices
to the second stream instead.
并非所有 format 和 dimension 组合都有效。具体来说,顶点属性的数据大小
必须是 4 字节的倍数。例如,尺寸为 3
的 VertexAttributeFormat.Float16 格式
是无效的。另请参阅:SystemInfo.SupportsVertexAttributeFormat。
var mesh = new Mesh(); // 使用以下内容指定顶点布局: // - 浮点位置, // - 具有两个分量的半精度 (FP16) 法线, // - 低精度 (UNorm8) 切线 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);
匹配此顶点布局的 C# 结构(用于 Mesh.SetVertexBufferData)可能如下所示:
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] struct ExampleVertex { public Vector3 pos; public ushort normalX, normalY; public Color32 tangent; }
VertexAttributeDescriptor | 创建一个 VertexAttributeDescriptor 结构。 |