Esempio n. 1
0
 public FoliageSpawner(NewMeshGenerator _meshGenerator, GameObject _treeSpawner, float _treeDensity, GameObject _grassPrefab, float _grassDensity, Transform _parent)
 {
     this.meshGenerator = _meshGenerator;
     this.treeSpawner   = _treeSpawner;
     this.treeDensity   = _treeDensity;
     this.grassPrefab   = _grassPrefab;
     this.grassDensity  = _grassDensity;
     this.parent        = _parent;
 }
        void Generate()
        {
            if (useSeed)
            {
                seed = Seed;
            }
            else
            {
                seed = System.DateTime.Now.Ticks.ToString();
            }

            if (this.transform.childCount > 0)
            {
                foreach (Transform child in this.transform)
                {
                    GameObject.Destroy(child.gameObject);
                }
            }

            gen = new NewAutomataGenerator(seed.GetHashCode());

            gen.Generate(width, height, highThreshold, midThreshold, lowThreshold, smoothIterations, minimumRegionSize, borderThickness);

            NewMeshGenerator.SimplexArguments simplexArguments = new NewMeshGenerator.SimplexArguments(octaves, multiplier, amplitude, lacunarity, persistence, seed);

            meshGenerator = new NewMeshGenerator(width, height, gen.MapRegions, cellSize, wallHeight, simplexArguments);
            meshGenerator.GenerateMeshes();

            Texture2D splatMap = new Texture2D(meshGenerator.heightMap.GetLength(0), meshGenerator.heightMap.GetLength(1));

            for (int x = 0; x < splatMap.width; x++)
            {
                for (int y = 0; y < splatMap.height; y++)
                {
                    splatMap.SetPixel(x, y, new Color(0, 0, 1.0f, 0));

                    if (meshGenerator.heightMap[x, y] < 0.7)
                    {
                        splatMap.SetPixel(x, y, new Color(0, 1.0f, 0, 0));
                    }

                    if (meshGenerator.heightMap[x, y] < 0.55)
                    {
                        splatMap.SetPixel(x, y, new Color(1.0f, 0, 0, 0));
                    }
                }
            }

            splatMap.Apply();

            foreach (Mesh mesh in meshGenerator.GroundMeshes)
            {
                GameObject obj = new GameObject("Ground Mesh");
                obj.transform.parent = this.transform;
                groundMaterial.SetTexture("_Control", splatMap);
                obj.AddComponent <MeshRenderer>().material = groundMaterial;
                obj.AddComponent <MeshFilter>().mesh       = mesh;

                obj.AddComponent <MeshCollider>();
            }

            foreach (Mesh mesh in meshGenerator.WallMeshes)
            {
                GameObject obj = new GameObject("Wall Mesh");
                obj.transform.parent = this.transform;
                obj.AddComponent <MeshRenderer>().material = wallMaterial;
                obj.AddComponent <MeshFilter>().mesh       = mesh;

                obj.AddComponent <MeshCollider>();
            }

            FoliageSpawner foliageSpawner = new FoliageSpawner(meshGenerator, treeSpawner, treeDensity, grassPrefab, grassDensity, this.transform);

            foliageSpawner.Generate();
        }