具有均匀面的程序圆网格

时间:2018-11-21 06:42:48

标签: javascript c# unity3d mesh

我正在尝试通过程序创建一个具有均匀面孔的2d圆,如下所示。

enter image description here

通常情况下,我会create it with a triangle fan structure,但我需要使面孔大致相同。我在寻找示例,但是我只能找到“立方体到球形”的示例。妥协可能类似于以下内容:

enter image description here

您能帮我找到一种绘制此结构的方法吗?我想用C#来做,但是js甚至是伪代码都可以!

非常感谢

1 个答案:

答案 0 :(得分:5)

您让我对您的问题感兴趣,我想我已经找到了您所需要的解决方案。这是我们如何创建所需的拓扑的方法:

1)我们从六角形开始。为什么是六角形而不是其他形状?因为六角形是唯一半径等于边长的魔术形状。我们将此半径称为 R 。现在,我们将尝试创建一个类似于圆形的形状,该形状由边长约为 R 的三角形组成。

2)现在,想象一些同心圆,半径为 R,2R,3R ,依此类推-分辨率越高,分辨率越高。

3)1号圆的半径为 R 。现在,我们用半径为 R 的六角形替换该圆。

Example 1

4)现在,我们将在第二个圆上添加更多节点以扩展六角形。圆号 N 的周长是多少?它是 2PiRN 。现在,我们将其划分为长度为 R X 个边。因此, X = 2PiN ,大​​约为 6N 。因此,我们将第一个圆划分为6个边(六边形),第二个圆划分为12个边,然后划分为18个,24个,依此类推。

5)现在,我们有许多圆被划分为边缘。现在,我们需要将边连接成三角形。如何在圆 N (外部)和 N-1 (内部)之间建立三角形?外圈比内圈多6个边。如果它们具有相同数量的顶点,我们可以将它们与四边形相连。但是他们没有。因此,我们仍将尝试构建四边形,但是对于构建的每N个四边形,我们将需要添加1个三角形。每个四边形使用来自内部圆的2个顶点和来自外部圆的2个顶点。每个三角形从外部圆使用2个顶点,从内部圆使用1个顶点,从而补偿了多余的顶点。

Example 2

6)现在,终于有一些经过测试的示例代码可以满足您的需求。它将生成一个具有统一拓扑的圆,其中心点位于原点,半径为1,分为*分辨率子圆。它可以使用一些次要的性能优化(目前已超出范围),但总的来说应该可以完成这项工作。

using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class UniformCirclePlane : MonoBehaviour {

    public int resolution = 4;

    // Use this for initialization
    void Start() {
        GetComponent<MeshFilter>().mesh = GenerateCircle(resolution);
    }

    // Update is called once per frame
    void Update() {

    }

    // Get the index of point number 'x' in circle number 'c'
    static int GetPointIndex(int c, int x) {
        if (c < 0) return 0; // In case of center point
        x = x % ((c + 1) * 6); // Make the point index circular
                               // Explanation: index = number of points in previous circles + central point + x
                               // hence: (0+1+2+...+c)*6+x+1 = ((c/2)*(c+1))*6+x+1 = 3*c*(c+1)+x+1

        return (3 * c * (c + 1) + x + 1);
    }

    public static Mesh GenerateCircle(int res) {

        float d = 1f / res;

        var vtc = new List<Vector3>();
        vtc.Add(Vector3.zero); // Start with only center point
        var tris = new List<int>();

        // First pass => build vertices
        for (int circ = 0; circ < res; ++circ) {
            float angleStep = (Mathf.PI * 2f) / ((circ + 1) * 6);
            for (int point = 0; point < (circ + 1) * 6; ++point) {
                vtc.Add(new Vector2(
                    Mathf.Cos(angleStep * point),
                    Mathf.Sin(angleStep * point)) * d * (circ + 1));
            }
        }

        // Second pass => connect vertices into triangles
        for (int circ = 0; circ < res; ++circ) {
            for (int point = 0, other = 0; point < (circ + 1) * 6; ++point) {
                if (point % (circ + 1) != 0) {
                    // Create 2 triangles
                    tris.Add(GetPointIndex(circ - 1, other + 1));
                    tris.Add(GetPointIndex(circ - 1, other));
                    tris.Add(GetPointIndex(circ, point));
                    tris.Add(GetPointIndex(circ, point));
                    tris.Add(GetPointIndex(circ, point + 1));
                    tris.Add(GetPointIndex(circ - 1, other + 1));
                    ++other;
                } else {
                    // Create 1 inverse triange
                    tris.Add(GetPointIndex(circ, point));
                    tris.Add(GetPointIndex(circ, point + 1));
                    tris.Add(GetPointIndex(circ - 1, other));
                    // Do not move to the next point in the smaller circle
                }
            }
        }

        // Create the mesh
        var m = new Mesh();
        m.SetVertices(vtc);
        m.SetTriangles(tris, 0);
        m.RecalculateNormals();
        m.UploadMeshData(true);

        return m;

    }
}

最终结果:

enter image description here

相关问题