有关网格顶点的单个 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 结构。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.