Version: 2023.1

VertexAttributeDescriptor

struct in UnityEngine.Rendering

切换到手册

描述

有关网格顶点的单个 VertexAttribute 的信息。

Mesh 顶点数据包含不同的顶点属性。例如,顶点可以包括位置、法线、TexCoord0 和颜色。 网格通常使用已知格式的数据布局,例如,位置通常是 3 分量浮点矢量 (Vector3), 不过也可以为网格指定非标准数据格式及其布局。

可以使用 VertexAttributeDescriptorMesh.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.PositionVertexAttribute.NormalVertexAttribute.TangentVertexAttribute.ColorVertexAttribute.TexCoord0、 ...、VertexAttribute.TexCoord7VertexAttribute.BlendWeightVertexAttribute.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.

并非所有 formatdimension 组合都有效。具体来说,顶点属性的数据大小 必须是 4 字节的倍数。例如,尺寸为 3VertexAttributeFormat.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;
}

变量

attribute顶点属性。
dimension顶点属性的维度。
format顶点属性的格式。
stream属性应位于此顶点缓冲区流中。

构造函数

VertexAttributeDescriptor创建一个 VertexAttributeDescriptor 结构。