Пример #1
0
    private void GenerateMesh()
    {
        if (!showMesh)
        {
            meshFilter.mesh = null;
            return;
        }

        if (m_finalMaps == null)
        {
            return;
        }

        List <Vector3> vertices       = new List <Vector3>();
        List <int>     triangles      = new List <int>();
        MarchingSquare marchingSquare = new MarchingSquare();
        int            count          = 0;

        for (int x = 0; x < mapSize.x - 1; x++)
        {
            for (int y = 0; y < mapSize.y - 1; y++)
            {
                marchingSquare.SetCenter(new Vector3(-mapSize.x * 0.5f + x + cubeSize, -mapSize.y * 0.5f + y + cubeSize, 0));
                marchingSquare.SetNodes(
                    m_finalMaps[x, y + 1] == 1,
                    m_finalMaps[x + 1, y + 1] == 1,
                    m_finalMaps[x + 1, y] == 1,
                    m_finalMaps[x, y] == 1);

                vertices.AddRange(marchingSquare.vertices);
                foreach (int triangle in marchingSquare.triangles)
                {
                    triangles.Add(triangle + count);
                }

                count += 8;
            }
        }

        Mesh mesh = new Mesh();

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.RecalculateNormals();
        meshFilter.mesh = mesh;
    }
Пример #2
0
    private void OnToggleValueChanged(bool enabled)
    {
        if (m_marchingSquare == null)
        {
            m_marchingSquare = new MarchingSquare(Vector3.zero, squareSize, topLeftToggle.isOn, topRightToggle.isOn, bottomRightToggle.isOn, bottomLeftToggle.isOn);
        }
        else
        {
            m_marchingSquare.SetNodes(topLeftToggle.isOn, topRightToggle.isOn, bottomRightToggle.isOn, bottomLeftToggle.isOn);
        }

        Mesh mesh = new Mesh();

        mesh.vertices  = m_marchingSquare.vertices;
        mesh.triangles = m_marchingSquare.triangles.ToArray();
        mesh.RecalculateNormals();
        meshFilter.mesh = mesh;
    }