Пример #1
0
        public static IEnumerable <IEnumerable <Vector2> > GenerateWallPolygon(bool[,] layout)
        {
            var marching = new MarchingSquare();
            var islands  = LevelDecomposer.DecomposeLevel(layout);

            if (islands.Count() == 0)
            {
                throw new Exception("Downlinked Planet could not be parsed");
            }

            // List of Lists, where each list holds the points of an 'island'.
            // An island is a collision body in the level.
            var finalPoints = new List <IEnumerable <Vector2> >();

            var wholeShape = GetExteriorWall(layout, islands);

            finalPoints.Add(AddIsland(marching, wholeShape, false));

            foreach (var island in islands.Skip(1))
            {
                var shape = MapTilesToArray(layout, island);

                finalPoints.Add(AddIsland(marching, shape));
            }

            return(finalPoints);
        }
Пример #2
0
        public SquareGrid(int[,] level, float size)
        {
            int   nodeCountX = level.GetLength(0);
            int   nodeCountY = level.GetLength(1);
            float mapWidth   = nodeCountX * size;
            float mapHeight  = nodeCountY * size;

            ControlNode[,] controlNodes = new ControlNode[nodeCountX, nodeCountY];

            for (int x = 0; x < nodeCountX; x++)
            {
                for (int y = 0; y < nodeCountY; y++)
                {
                    Vector3 pos = new Vector3(-mapWidth / 2 + x * size + size / 2, 0, -mapHeight / 2 + y * size + size / 2);
                    controlNodes[x, y] = new ControlNode(pos, (level[x, y] == 1), size);
                }
            }

            squares = new MarchingSquare[nodeCountX - 1, nodeCountY - 1];

            for (int x = 0; x < nodeCountX - 1; x++)
            {
                for (int y = 0; y < nodeCountY - 1; y++)
                {
                    squares[x, y] = new MarchingSquare(controlNodes[x, y + 1], controlNodes[x + 1, y + 1], controlNodes[x + 1, y], controlNodes[x, y]);
                }
            }
        }
Пример #3
0
        private void AddIsland(MarchingSquare marching, bool[,] wholeShape, bool entryType = true)
        {
            var points = marching.DoMarch(wholeShape, entryType);

            var verts = CreateCollisionObject(points);

            vertices.AddRange(verts.Select(p => p * 100)); // Debug
        }
    public override void OnInspectorGUI()
    {
        MarchingSquare ms = target as MarchingSquare;

        if (GUI.Button(EditorGUILayout.GetControlRect(), "prefab to cell"))
        {
            ms.PrefabsToCells();
        }
        if (GUI.Button(EditorGUILayout.GetControlRect(), "populate from selected folder"))
        {
            ms.FromFolder();
            ms.PrefabsToCells();
        }
        DrawDefaultInspector();
    }
Пример #5
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;
    }
Пример #6
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;
    }
Пример #7
0
        public void GenerateWallPolygon(bool[,] layout)
        {
            // Debug shit
            //vertices.Clear();

            var marching = new MarchingSquare();
            var islands  = LevelDecomposer.DecomposeLevel(layout);

            if (islands.Count() == 0)
            {
                throw new Exception("Downlinked Planet could not be parsed");
            }

            var wholeShape = GetExteriorWall(layout, islands);

            AddIsland(marching, wholeShape, false); // false means wall

            foreach (var island in islands.Skip(1))
            {
                var shape = MapTilesToArray(layout, island);

                AddIsland(marching, shape);
            }
        }
Пример #8
0
 private static IEnumerable <Vector2> AddIsland(MarchingSquare marching, bool[,] wholeShape, bool entryType = true)
 {
     return(marching.DoMarch(wholeShape, entryType));
 }
Пример #9
0
    void TriangulateSquare(MarchingSquare square) // oh boy here we go
    {
        switch (square.configuration)
        {
        case 0:
            break;

        // 1 point cases
        case 1:
            MeshFromPoints(false, square.centreLeft, square.centreBottom, square.bottomLeft);
            solidEdgeVertices.Add(square.bottomLeft.vertexIndex);
            break;

        case 2:
            MeshFromPoints(false, square.bottomRight, square.centreBottom, square.centreRight);
            solidEdgeVertices.Add(square.bottomRight.vertexIndex);
            break;

        case 4:
            MeshFromPoints(false, square.topRight, square.centreRight, square.centreTop);
            solidEdgeVertices.Add(square.topRight.vertexIndex);
            break;

        case 8:
            MeshFromPoints(false, square.topLeft, square.centreTop, square.centreLeft);
            solidEdgeVertices.Add(square.topLeft.vertexIndex);
            break;

        // 2 point cases

        case 3:
            MeshFromPoints(false, square.centreRight, square.bottomRight, square.bottomLeft, square.centreLeft);
            solidEdgeVertices.Add(square.bottomLeft.vertexIndex);
            solidEdgeVertices.Add(square.bottomRight.vertexIndex);
            break;

        case 6:
            MeshFromPoints(false, square.centreTop, square.topRight, square.bottomRight, square.centreBottom);
            solidEdgeVertices.Add(square.topRight.vertexIndex);
            solidEdgeVertices.Add(square.bottomRight.vertexIndex);
            break;

        case 9:
            MeshFromPoints(false, square.topLeft, square.centreTop, square.centreBottom, square.bottomLeft);
            solidEdgeVertices.Add(square.topLeft.vertexIndex);
            solidEdgeVertices.Add(square.bottomLeft.vertexIndex);
            break;

        case 12:
            MeshFromPoints(false, square.topLeft, square.topRight, square.centreRight, square.centreLeft);
            solidEdgeVertices.Add(square.topRight.vertexIndex);
            solidEdgeVertices.Add(square.topLeft.vertexIndex);
            break;

        case 5:
            MeshFromPoints(false, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft, square.centreLeft, square.centreTop);
            break;

        case 10:
            MeshFromPoints(false, square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.centreBottom, square.centreLeft);
            break;

        // 3 point cases

        case 7:
            MeshFromPoints(false, square.topRight, square.bottomRight, square.bottomLeft, square.centreLeft, square.centreTop);
            solidEdgeVertices.Add(square.topRight.vertexIndex);
            solidEdgeVertices.Add(square.bottomLeft.vertexIndex);
            break;

        case 11:
            MeshFromPoints(false, square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.bottomLeft);
            solidEdgeVertices.Add(square.topLeft.vertexIndex);
            solidEdgeVertices.Add(square.bottomRight.vertexIndex);
            break;

        case 13:
            MeshFromPoints(false, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft, square.topLeft);
            solidEdgeVertices.Add(square.bottomLeft.vertexIndex);
            solidEdgeVertices.Add(square.topRight.vertexIndex);
            break;

        case 14:
            MeshFromPoints(false, square.topLeft, square.topRight, square.bottomRight, square.centreBottom, square.centreLeft);
            solidEdgeVertices.Add(square.topLeft.vertexIndex);
            solidEdgeVertices.Add(square.bottomRight.vertexIndex);

            break;

        //4 Point case

        case 15:
            MeshFromPoints(true, square.topLeft, square.topRight, square.bottomRight, square.bottomLeft);
            checkedVerts.Add(square.topLeft.vertexIndex);
            checkedVerts.Add(square.bottomLeft.vertexIndex);
            checkedVerts.Add(square.topRight.vertexIndex);
            checkedVerts.Add(square.bottomRight.vertexIndex);
            solidVertices.Add(square.topLeft.vertexIndex);
            solidVertices.Add(square.bottomLeft.vertexIndex);
            solidVertices.Add(square.topRight.vertexIndex);
            solidVertices.Add(square.bottomRight.vertexIndex);
            break;
        }
    }