Exemplo n.º 1
0
    public void Update(Vector3 center, float radius, NoiseData noise, float scale, Material terrainMaterial, Material waterMaterial)
    {
        if (generated)
        {
            // We load the materials to prevent null references.
            if (terrainMaterial == null)
            {
                //terrainMaterial = Resources.Load<Material>("Materials/StandardTerrain");
                terrainMaterial = new Material(Resources.Load <Material>("Materials/StandardTerrain"));
                terrainMaterial.SetColor("_Sand", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f));
                terrainMaterial.SetColor("_Grass", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f));
                terrainMaterial.SetColor("_DarkGrass", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f));
                terrainMaterial.SetColor("_Rocks", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f));
                terrainMaterial.SetColor("_DarkRocks", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f));
                terrainMaterial.SetColor("_Snow", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f));
            }
            if (waterMaterial == null)
            {
                waterMaterial = Resources.Load <Material>("Materials/StandardWater");
            }
            // We make sure the noise never get a null reference.
            if (noise == null)
            {
                noise = Resources.Load <NoiseData>("NoiseDatas/Standard");
            }

            // We make sure the planet is unique.
            noise.RecalculateOffsets();

            // Generate the GameObjects.
            // Terrain.
            planet      = new GameObject();
            planet.name = "Planet";
            planet.transform.position = center;
            planet.AddComponent <MeshRenderer>().material = terrainMaterial;
            // Water.
            GameObject water = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            water.transform.parent = planet.transform;
            water.GetComponent <MeshRenderer>().material = waterMaterial;
            water.transform.localScale *= radius * 2;

            // Generate Mesh.
            Mesh mesh = new Mesh();
            mesh.Clear();
            // We apply the noise to each point of the icosphere.
            mesh.vertices  = ArrayConvertionManager.Vector3ToVector3Height(ico.GetPoints(), radius, noise);;
            mesh.triangles = ico.GetTriangles();
            // We make sure to recalculate everything to good mesh behaviour.
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();

            // Apply mesh to the terrain.
            planet.AddComponent <MeshFilter>().mesh         = mesh;
            planet.AddComponent <MeshCollider>().sharedMesh = mesh;
            planet.transform.localScale *= scale;
            generated = false;
        }
    }