/// <summary>
        /// Create marching squares
        /// </summary>
        /// <param name="map"></param>
        /// <param name="wallSize"></param>
        /// <param name="is3D"></param>
        static void CreateSquareGrid(IMarchingMap map, MapGenParams parameters)
        {
            if (map.MapData == null)
                return;

            MSNode[] allMeshNodes = new MSNode[map.MapData.NodeCount];
            for (int row = 0; row < map.MapData.RowCount; row++)
                for (int col = 0; col < map.MapData.ColCount; col++)
                {
                    allMeshNodes[row * map.MapData.ColCount + col] = new MSNode(
                        map.MapData.GetNodePos(row, col) - Vector3.forward * parameters.wallSize,
                        map.MapData.GetNode(row, col).m_type == MapNodeType.WALL,
                        map.MapData.NodeSize
                        );
                }

            m_marchSquareGrid = new MarchingSquare[(map.MapData.RowCount - 1) * (map.MapData.ColCount - 1)];
            for (int sq = 0; sq < (map.MapData.RowCount - 1) * (map.MapData.ColCount - 1); sq++)
            {
                int row = sq / (map.MapData.ColCount - 1);
                int col = sq % (map.MapData.ColCount - 1);

                m_marchSquareGrid[sq] = new MarchingSquare(
                    allMeshNodes[(row + 1) * map.MapData.ColCount + col],
                    allMeshNodes[(row + 1) * map.MapData.ColCount + col + 1],
                    allMeshNodes[row * map.MapData.ColCount + col],
                    allMeshNodes[row * map.MapData.ColCount + col + 1]

                    );
            }
        }
        static void CreateWallMesh(MapGenParams parameters)
        {
            foreach (List<Vector3> edgePath in EdgePaths)
            {
                for (int i = 0; i < edgePath.Count - 1; i++)
                {
                    Vector3 topLeft = edgePath[i];
                    Vector3 topRight = edgePath[i + 1];
                    Vector3 botLeft = topLeft - Vector3.forward * parameters.wallSize;
                    Vector3 botRight = topRight - Vector3.forward * parameters.wallSize;

                    m_wallVertices.Add(topLeft);
                    m_wallVertices.Add(topRight);
                    m_wallVertices.Add(botLeft);
                    m_wallVertices.Add(botRight);

                    m_wallIndices.Add(i * 4);
                    m_wallIndices.Add(i * 4 + 2);
                    m_wallIndices.Add(i * 4 + 1);
                    m_wallIndices.Add(i * 4 + 2);
                    m_wallIndices.Add(i * 4 + 3);
                    m_wallIndices.Add(i * 4 + 1);
                }
            }

            WallMesh = new Mesh();
            WallMesh.vertices = m_wallVertices.ToArray();
            WallMesh.triangles = m_wallIndices.ToArray();
            WallMesh.RecalculateNormals();
        }
Exemple #3
0
        public static MapData GenerateMapData(MapGenParams mapGenParams)
        {
            MapData newMapData = new MapData(Vector3.zero, mapGenParams.m_rowCount, mapGenParams.m_colCount, mapGenParams.m_nodeSize);

            if (mapGenParams.DoEdges)
            {
                AddEdge(newMapData, mapGenParams.edgeSize);
            }
            if (mapGenParams.DoAddRandom)
            {
                AddRandom(newMapData, mapGenParams.fillChance);
            }
            if (mapGenParams.DoCellularAutomata)
            {
                CelularAutomata(newMapData, mapGenParams.cellAutomataIterCount);
            }

            return(newMapData);
        }
        public static void CreateMesh(IMarchingMap map, MapGenParams parameters)
        {
            if (map == null || map.MapData == null || parameters == null)
                return;

            Restart();

            CreateSquareGrid(map, parameters);
            MarchingSquaresToMeshes();

            CreateFloorMesh(map.MapData);
            CreateRoofMesh();

            map.RoofMesh.mesh = RoofMesh;
            map.FloorMesh.mesh = FloorMesh;


            CreateEdgePaths();
            if (parameters.Is3D)
            {
                CreateWallMesh(parameters);
                map.WallMesh.mesh = WallMesh;
            }
            else
            {
                map.PolygonCollider.pathCount = EdgePaths.Count;
                for (int i = 0; i < EdgePaths.Count; i++)
                {
                    List<Vector3> edges = EdgePaths[i];
                    Vector2[] vec2Edges = new Vector2[edges.Count];
                    for (int j = 0; j < edges.Count; j++)
                        vec2Edges[j] = edges[j];

                    map.PolygonCollider.SetPath(i, vec2Edges);
                }
            }
        }