示例#1
0
        //Awake
        private void Awake()
        {
            terrainGenerator = new TerrainGenerator(seed, new NoiseGeneratorSettings(TerrainOctaves, TerrainPersistence, TerrainFrequency, TerrainAmplitude, TerrainHeightPerLevel, TerrainNumLevels),
                                                    new NoiseGeneratorSettings(IronOctaves, IronPersistence, IronFrequency, IronAmplitude),
                                                    new NoiseGeneratorSettings(IceOctaves, IcePersistence, IceFrequency, IceAmplitude),
                                                    new NoiseGeneratorSettings(HeliumOctaves, HeliumPersistence, HeliumFrequency, HeliumAmplitude),
                                                    new NoiseGeneratorSettings(RegolithOctaves, RegolithPersistence, RegolithFrequency, RegolithAmplitude));

            terrainManager = new GameObject().AddComponent <TerrainManager>().Initialize(this, terrainGenerator, chunkSize, chunkMapWidth, chunkMaterial);
            terrainManager.transform.parent = transform;

            buildingManager = new GameObject().AddComponent <BuildingManager>().Initialize(this);
            buildingManager.transform.parent = transform;

            workTaskManager = new GameObject().AddComponent <WorkTaskManager>().Initialize(this);
            workTaskManager.transform.parent = transform;

            colonistManager = new GameObject().AddComponent <ColonistManager>().Initialize(this, workTaskManager);
            colonistManager.transform.parent = transform;


            AstarPath.active.Scan();
        }
示例#2
0
        public TerrainChunk Initialize(TerrainManager terrainManager, int cellsPerChunk, ChunkLocation location, TerrainGenerator terrainGenerator, Material chunkMaterial)
        {
            this.terrainManager = terrainManager;

            transform.position = new Vector3(location.GetX() * cellsPerChunk, 0, location.GetZ() * cellsPerChunk);
            transform.name     = "Chunk";
            gameObject.layer   = 8;

            this.location      = location;
            this.cellsPerChunk = cellsPerChunk;

            cellHeights = new float[cellsPerChunk + 1, cellsPerChunk + 1];
            for (int i = 0; i < cellsPerChunk + 1; i++)
            {
                for (int j = 0; j < cellsPerChunk + 1; j++)
                {
                    cellHeights[i, j] = terrainGenerator.GetTerrainHeight(i + location.GetAbsX(), j + location.GetAbsZ());
                }
            }

            chunkCells = new TerrainCell[cellsPerChunk, cellsPerChunk];
            for (int i = 0; i < cellsPerChunk; i++)
            {
                for (int j = 0; j < cellsPerChunk; j++)
                {
                    CellLocation loc = new CellLocation(i, j, location);

                    chunkCells[i, j] = new TerrainCell(loc, terrainGenerator);
                }
            }

            m_material = chunkMaterial;
            GetComponent <MeshRenderer>().material = m_material;

            return(this);
        }
示例#3
0
    public void GenerateMesh(Simulation.TerrainCell[,] cellHeights, Simulation.TerrainManager terMan, Material MeshMaterial)
    {
        Vector3 tp = transform.position;

        int numVerts     = (cellHeights.GetLength(0) + 1) * (cellHeights.GetLength(1) + 1) * 6;
        int numTriangles = (cellHeights.GetLength(0) + 1) * (cellHeights.GetLength(1) + 1) * 6;

        Vector3[] verts     = new Vector3[numVerts];
        int[]     triangles = new int[numTriangles];
        Vector3[] normals   = new Vector3[numVerts];
        Vector2[] uv        = new Vector2[numVerts];

        int counter = 0;

        for (int i = 0; i < cellHeights.GetLength(0); i++)
        {
            for (int j = 0; j < cellHeights.GetLength(1); j++)
            {
                float cellHeight = 0;                // cellHeights[i, j].GetHeight();

                float downCell = 0, upCell = 0, rightCell = 0, leftCell = 0;
                float downRightCell = 0, downLeftCell = 0, upRightCell = 0, upLeftCell = 0;

                if (i == 0 || i == cellHeights.GetLength(0) - 1 || j == 0 || j == cellHeights.GetLength(1) - 1)
                {
                    //downCell = terMan.GetHeightAtCell(i + 1 + tp.x, j + tp.z);
                    //upCell = terMan.GetHeightAtCell(i - 1 + tp.x, j + tp.z);
                    //rightCell = terMan.GetHeightAtCell(i + tp.x, j + 1 + tp.z);
                    //leftCell = terMan.GetHeightAtCell(i + tp.x, j - 1 + tp.z);
                    //
                    //downRightCell = terMan.GetHeightAtCell(i + 1 + tp.x, j + 1 + tp.z);
                    //downLeftCell = terMan.GetHeightAtCell(i + 1 + tp.x, j - 1 + tp.z);
                    //upRightCell = terMan.GetHeightAtCell(i - 1 + tp.x, j + 1 + tp.z);
                    //upLeftCell = terMan.GetHeightAtCell(i - 1 + tp.x, j - 1 + tp.z);
                }
                else
                {
                    //downCell = cellHeights[i + 1, j].GetHeight();
                    //upCell = cellHeights[i - 1, j].GetHeight();
                    //rightCell = cellHeights[i, j + 1].GetHeight();
                    //leftCell = cellHeights[i, j - 1].GetHeight();
                    //
                    //downRightCell = cellHeights[i + 1, j + 1].GetHeight();
                    //downLeftCell = cellHeights[i + 1, j - 1].GetHeight();
                    //upRightCell = cellHeights[i - 1, j + 1].GetHeight();
                    //upLeftCell = cellHeights[i - 1, j - 1].GetHeight();
                }
                float topRightCorner    = Mathf.Max(cellHeight, downCell, rightCell, downRightCell);;
                float topLeftCorner     = Mathf.Max(cellHeight, downCell, leftCell, downLeftCell);
                float bottomRightCorner = Mathf.Max(cellHeight, upCell, rightCell, upRightCell);;
                float bottomLeftCorner  = Mathf.Max(cellHeight, upCell, leftCell, upLeftCell);;


                if (topRightCorner == bottomLeftCorner)
                {
                    verts[counter]     = new Vector3(i, bottomLeftCorner, j);
                    verts[counter + 1] = new Vector3(i, bottomRightCorner, j + 1);
                    verts[counter + 2] = new Vector3(i + 1, topRightCorner, j + 1);

                    verts[counter + 3] = new Vector3(i, bottomLeftCorner, j);
                    verts[counter + 4] = new Vector3(i + 1, topRightCorner, j + 1);
                    verts[counter + 5] = new Vector3(i + 1, topLeftCorner, j);

                    Vector3 bL = new Vector3(i, bottomLeftCorner, j);
                    Vector3 bR = new Vector3(i, bottomRightCorner, j + 1);
                    Vector3 tL = new Vector3(i + 1, topLeftCorner, j);
                    Vector3 tR = new Vector3(i + 1, topRightCorner, j + 1);

                    Vector3 upperCross = Vector3.Cross(tR - bR, bL - bR).normalized;
                    Vector3 lowerCross = Vector3.Cross(bL - tL, tR - tL).normalized;

                    normals[counter]     = normals[counter + 1] = normals[counter + 2] = upperCross;
                    normals[counter + 3] = normals[counter + 4] = normals[counter + 5] = lowerCross;

                    uv[counter]     = new Vector2(i, j);
                    uv[counter + 1] = new Vector2(i, j + 1);
                    uv[counter + 2] = new Vector2(i + 1, j + 1);

                    uv[counter + 3] = new Vector2(i, j);
                    uv[counter + 4] = new Vector2(i + 1, j + 1);
                    uv[counter + 5] = new Vector2(i + 1, j);
                }
                else
                {
                    verts[counter]     = new Vector3(i, bottomLeftCorner, j);
                    verts[counter + 1] = new Vector3(i, bottomRightCorner, j + 1);
                    verts[counter + 2] = new Vector3(i + 1, topLeftCorner, j);

                    verts[counter + 3] = new Vector3(i, bottomRightCorner, j + 1);
                    verts[counter + 4] = new Vector3(i + 1, topRightCorner, j + 1);
                    verts[counter + 5] = new Vector3(i + 1, topLeftCorner, j);

                    Vector3 bL = new Vector3(i, bottomLeftCorner, j);
                    Vector3 bR = new Vector3(i, bottomRightCorner, j + 1);
                    Vector3 tL = new Vector3(i + 1, topLeftCorner, j);
                    Vector3 tR = new Vector3(i + 1, topRightCorner, j + 1);

                    Vector3 upperCross = Vector3.Cross(tL - tR, bR - tR).normalized;
                    Vector3 lowerCross = Vector3.Cross(bR - bL, tL - bL).normalized;

                    normals[counter]     = normals[counter + 1] = normals[counter + 2] = lowerCross;
                    normals[counter + 3] = normals[counter + 4] = normals[counter + 5] = upperCross;

                    uv[counter]     = new Vector2(i, j);
                    uv[counter + 1] = new Vector2(i, j + 1);
                    uv[counter + 2] = new Vector2(i + 1, j);

                    uv[counter + 3] = new Vector2(i, j + 1);
                    uv[counter + 4] = new Vector2(i + 1, j + 1);
                    uv[counter + 5] = new Vector2(i + 1, j);
                }
                counter += 6;
            }
        }

        for (int i = 0; i < (cellHeights.GetLength(0) + 1) * (cellHeights.GetLength(1) + 1) * 6; i++)
        {
            triangles[i] = i;             //no vertex saving, each triangle gets its own three vertices, in order
        }

        mesh           = new Mesh();
        mesh.name      = "Chunk Mesh";
        mesh.vertices  = verts;
        mesh.triangles = triangles;
        mesh.normals   = normals;
        mesh.uv        = uv;

        GetComponent <MeshFilter>().mesh         = mesh;
        GetComponent <MeshRenderer>().material   = MeshMaterial;
        GetComponent <MeshCollider>().sharedMesh = mesh;


        //OptemizeMesh(mesh, MeshMaterial);
    }