【问题标题】:How to calculate UVs of a flat poligon mesh如何计算平面多边形网格的 UV
【发布时间】:2017-05-12 18:29:55
【问题描述】:

我的游戏生成了一个平面(建筑物的地板)。如图所示,它是一个扁平的 poligon 网格:

poligon 是程序生成的,每次都会不同。

我需要映射 UV 坐标,以便正确显示标准的方形纹理,例如砖砌的地板。

为每个顶点分配正确的 UV 坐标的最佳方法是什么?

【问题讨论】:

    标签: unity3d unity5 mesh uv-mapping


    【解决方案1】:

    对于不规则的形状,您可能希望在网格上“粘贴”纹理(想象在网格上粘贴一个矩形贴纸并剪掉那些落在网格形状之外的贴纸)。

    对于这种类型的映射,您可能想要使用Mesh.bounds,它会在本地坐标中为您提供meshbounding box,这是您要去的区域“粘贴”你的纹理。

    Mesh mesh = GetComponent<MeshFilter>();
    Bounds bounds = mesh.bounds;
    

    获取网格的顶点:

    Vector3[] vertices = mesh.vertices;
    

    现在进行映射:

    Vector2[] uvs = new Vector2[vertices.Length];
    for(int i = 0; i < vertices.Length; i++)
    {
      uvs[i] = new Vector2(vertices[i].x / bounds.size.x, vertices[i].z / bounds.size.z);
    }
    mesh.uv = uvs;
    

    【讨论】:

    • 效果很好,谢谢!我试图找出网格周围的边界正方形,然后将顶点坐标映射到所述正方形,但我在某个地方搞砸了。使用给定的边界要容易得多:)
    猜你喜欢
    • 2012-02-16
    • 2021-05-25
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多