【问题标题】:Unity - Determining UVs for a circular plane mesh generated by codeUnity - 确定代码生成的圆形平面网格的 UV
【发布时间】:2022-05-03 15:30:07
【问题描述】:

我正在尝试生成一个由三角形组成的圆形网格,该三角形的中心位于圆心。

网格已正确生成,但 UV 未正确生成,我在理解如何添加它们时遇到了一些麻烦。 我以为我只会复制顶点的模式,但没有成功。 这是函数:

private void _MakeMesh(int sides, float radius = 0.5f)
{
m_LiquidMesh.Clear();
float angleStep = 360.0f / (float) sides;
List<Vector3> vertexes      = new List<Vector3>();
List<int>       triangles   = new List<int>();
List<Vector2>   uvs         = new List<Vector2>();
Quaternion      rotation  = Quaternion.Euler(0.0f, angleStep, 0.0f);

// Make first triangle.
vertexes.Add(new Vector3(0.0f, 0.0f, 0.0f));  
vertexes.Add(new Vector3(radius, 0.0f, 0.0f));    
vertexes.Add(rotation * vertexes[1]);        

// First UV ??
uvs.Add(new Vector2(0, 0));
uvs.Add(new Vector2(1, 0));
uvs.Add(rotation * uvs[1]);

// Add triangle indices.
triangles.Add(0);
triangles.Add(1);
triangles.Add(2);
for (int i = 0; i < sides - 1; i++)
{
    triangles.Add(0); 
    triangles.Add(vertexes.Count - 1);
    triangles.Add(vertexes.Count);

    // UV ??



    vertexes.Add(rotation * vertexes[vertexes.Count - 1]);
}
m_LiquidMesh.vertices   = vertexes.ToArray();
m_LiquidMesh.triangles  = triangles.ToArray();
m_LiquidMesh.uv         = uvs.ToArray();

m_LiquidMesh.RecalculateNormals();
m_LiquidMesh.RecalculateBounds();

Debug.Log("<color=yellow>Liquid mesh created</color>");
}

How does mapping UV work in a case like this?

编辑:我正在尝试使用这个圆圈作为从中心向外流动的东西的效果(想想:酿造罐的液体网)

【问题讨论】:

  • UV 映射应该是这样工作的,但是我有一种感觉,使用 vector2 重复旋转可能是它横向的地方。一种一直对我有用的方法是只复制生成的顶点,省略 z 坐标(如果你的网格需要垂直于 z 轴)
  • @zambari 感谢您的洞察力。我会试试看会发生什么。

标签: c# unity3d uv-mapping


【解决方案1】:

这是一篇旧帖子,但也许其他人会从我的解决方案中受益。

所以基本上我给我的中心点指定了 uv 的中心 (0.5, 0.5),然后使用使用的圆形公式为每个其他点指定了 uv 坐标。但当然,我必须将 cos 和 sin 结果从 -1..1 重新映射到 0..1,一切正常。

    Vector2[] uv = new Vector2[vertices.Length];
    uv[uv.Length - 1] = new Vector2(0.5f, 0.5f);

    for (int i = 0; i < uv.Length - 1; i++)
    {
        float radians = (float) i / (uv.Length - 1) * 2 * Mathf.PI;
        uv[i] = new Vector2(Mathf.Cos(radians).Remap(-1f, 1f, 0f, 1f), Mathf.Sin(radians).Remap(-1f, 1f, 0f, 1f));
    }
        
    mesh.uv = uv;

重映射是这样的扩展,它基本上采用一个范围内的值并将其重新映射到另一个范围(在本例中是从 -1..1 到 0..1):

public static float Remap(this float value, float from1, float to1, float from2, float to2) {
    return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 2017-05-12
    • 2015-05-08
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多