public void generate(bool surroundings = true)
    {
        if (surroundings)          /*
                                    * checkandGenerate (new Vector3 (-10000, 0, 10000), -1,1);
                                    * checkandGenerate (new Vector3 (-10000, 0, 0),-1,0);
                                    * checkandGenerate (new Vector3 (-1000, 0, -10000),-1,-1);
                                    * checkandGenerate (new Vector3 (0, 0, 10000),0,1);
                                    *
                                    * checkandGenerate (new Vector3 (0, 0, -10000),0,-1);
                                    * checkandGenerate (new Vector3 (10000, 0, 10000),1,1);
                                    * checkandGenerate (new Vector3 (10000, 0, 0),1,0);
                                    * checkandGenerate (new Vector3 (10000, 0, -10000),1,-1);*/
        {
        }
        //if (transform.childCount == 0)
        topologyOptions options = new topologyOptions()
        {
            seed          = seed,
            elevationHigh = 0.05f,
            elevationLow  = 0.00f,
            centerPoint   = 0.5f,
            coarseNoise   = 800,
            midNoise      = 1500,
            flatNoise     = 8000,
            seaLevel      = 0.549f,
            island        = false
        };

        Instantiate(
            terrains.getTerrain(trees, foliages, grass, textures, normals, transform.parent.GetComponent <worldConstants> ().noiseMatrix, baseTerrain, options, null)
            ).transform.SetParent(transform, false);
    }
Example #2
0
        public static GameObject getTerrain(
            GameObject[] trees,
            GameObject[] foliage,
            Texture2D[] grass,

            Texture2D[] textures,
            Texture2D[] normals,
            float[,] noise,
            GameObject origin,
            topologyOptions options,
            Texture2D elevation = null

            )
        {
            GameObject ret = origin;

            List <int[, ]> detailmap = new List <int[, ]> ();


            TerrainData terr = new TerrainData()
            {
                size = new Vector3(50, 3200, 50), heightmapResolution = 2048, alphamapResolution = 2048
            };

            terr.SetDetailResolution(2048, 8);

            terr = createPrototypes(terr, trees, foliage, grass, textures, normals);
            for (int i = 0; i < terr.detailPrototypes.Length; i++)
            {
                detailmap.Add(terr.GetDetailLayer(0, 0, terr.detailWidth, terr.detailHeight, i));
            }

            List <float[, ]> mapData = topologyCreator.makeTerrain(elevation, noise, options);

            terr.SetHeights(0, 0, mapData[0]);

            terr.SetAlphamaps(0, 0, drawSplatMaps(mapData[1], 2048, textures.Length));

            detailmap = drawDetails(mapData[2], detailmap);
            for (int i = 0; i < detailmap.Count; i++)
            {
                terr.SetDetailLayer(0, 0, i, detailmap [i]);
            }

            ret.GetComponent <Terrain>().terrainData          = terr;
            ret.GetComponent <TerrainCollider> ().terrainData = terr;

            int y = 0;

            while (y < mapData [3].GetLength(0))
            {
                int x = 0;
                while (x < mapData [3].GetLength(1))
                {
                    if (mapData [3] [x, y] > 0)
                    {
                        Vector3 treepos   = new Vector3((float)x / 2048.0f, mapData [3][x, y], (float)y / 2048.0f);
                        float   randomize = (float)Random.Range(1, 18) / 10;
                        if (terr.GetHeight(x, y) > (terr.size.y * options.elevationHigh) && terr.GetHeight(x, y) < (terr.size.y * options.elevationHigh + terr.size.y * 0.0025))
                        {
                            ret.GetComponent <Terrain>().AddTreeInstance(new TreeInstance()
                            {
                                heightScale    = 0.2f + randomize, widthScale = 0.2f + randomize,
                                position       = treepos,
                                color          = Color.white,
                                lightmapColor  = new Color(0.5f, 0.5f, 0.5f),
                                prototypeIndex = Random.Range(0, terr.treePrototypes.Length)
                            });
                        }
                    }
                    x++;
                }
                y++;
            }
            return(ret);
        }