Mesh 类是对象网格几何体的基本脚本接口。该类使用数组来表示三角形、顶点位置、法线和纹理坐标,还提供了许多其他有用的属性和函数来辅助网格的生成。
通常使用网格过滤器 (Mesh Filter) 组件将网格数据附加到对象(该对象还需要 MeshRenderer 以使几何体可见)。使用熟悉的 GetComponent 函数访问此组件:
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
MeshFilter mf;
void Start()
{
//如果此 gameObject 具有 MeshFilter,则 mf 将引用该组件
mf = GetComponent<MeshFilter>();
}
}
Mesh 对象具有顶点及其关联数据(法线和 UV 坐标)的属性以及三角形数据的属性。顶点可按任何顺序提供,但是法线和 UV 的数组必须经过排序,使索引全部对应于顶点(即,法线数组的元素 0 提供顶点 0 的法线,依此类推)。顶点为 Vector3,表示对象局部空间中的点。法线为经过标准化的 Vector3,表示方向,同样位于局部坐标中。UV 指定为 Vector2,但由于 Vector2 类型没有名为 U 和 V 的字段,因此必须在脑海中将它们分别转换为 X 和 Y。
三角形指定为整数三元组,作为顶点数组的索引。该数组只是一个简单的整数索引列表,并不会使用特殊的类来表示三角形。它们针对每个三角形以三个为一组,因此前三个元素定义第一个三角形,接下来的三个定义第二个三角形,依此类推。三角形的一个重要细节是角顶点的排序问题。它们的排列应符合以下要求:当向下观看三角形的可见外表面时,角应顺时针转动,但从哪个角开始并不重要。
Mesh 类还具有更低级别的高级 API,使您可以 使用原始网格顶点和索引缓冲区数据。在需要最高性能或最低内存分配的情况下,这很有用。
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.