有关网格顶点的单个 VertexAttribute 的信息。
Mesh 顶点数据包含不同的顶点属性。例如,顶点可以包括位置、法线、TexCoord0 和颜色。
网格通常使用已知格式的数据布局,例如,位置通常是 3 分量浮点矢量 (Vector3),
不过也可以为网格指定非标准数据格式及其布局。
可以使用 VertexAttributeDescriptor
在 Mesh.SetVertexBufferParams 中指定自定义网格数据布局。
顶点数据使用单独的“流”进行布局(每个流进入底层图形 API 中单独的顶点缓冲区)。
虽然 Unity 支持多达 4 个顶点流,但大多数网格仅使用一个。当不需要处理某些顶点属性时,
单独的流最为有用,例如,蒙皮网格通常使用两个顶点流(一个流包含所有蒙皮数据:
位置、法线、切线;另一个流包含所有非蒙皮数据:颜色和纹理坐标)。
在每个流中,顶点属性按一个接一个的方式布局,顺序如下:VertexAttribute.Position、
VertexAttribute.Normal、VertexAttribute.Tangent、VertexAttribute.Color、VertexAttribute.TexCoord0、
...、VertexAttribute.TexCoord7、VertexAttribute.BlendWeight、VertexAttribute.BlendIndices。
并非所有 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.