Ejemplo n.º 1
0
    /*
     * void Start() {
     *      RegenerateMesh ();
     * }
     */

    void RegenerateMesh()
    {
        if (prevTilesX == 0 || prevTilesZ == 0 ||
            prevTilesX != tilesX || prevTilesZ != tilesZ)
        {
            prevTilesX = tilesX;
            prevTilesZ = tilesZ;

            mesh = GetComponent <FlatMeshGeneratorV4_Working> ().GenerateMesh(mesh);
        }

        if (noise == null || prevSeed != seed)
        {
            prevSeed = seed;
            noise    = new PerlinNoise(seed);
        }

        Vector3[] curVerts = mesh.vertices;

        for (int z = 0; z < (tilesZ + 1); z++)
        {
            for (int x = 0; x < (tilesX + 1); x++)
            {
                Vector3 curVert = curVerts [z * (tilesX + 1) + x];
                curVert.y = noise.FractalNoise(x, z, octave, frequency, amplitude);
                curVerts [z * (tilesX + 1) + x] = curVert;
            }
        }

        mesh.vertices = curVerts;

        mesh.Optimize();
        mesh.RecalculateNormals();
    }
        public float GetNoise(float x, float z)
        {
            if (perlin == null || prevSeed != seed)
            {
                prevSeed = seed;
                perlin   = new PerlinNoise(seed);
            }

            return(perlin.FractalNoise(x, z, octave, frequency, amplitude));
        }
    public void GenerateTerrainNoise(GameObject terrainTile, float offsetX, float offsetZ)
    {
        noise = new PerlinNoise(seed);

        Mesh mesh = terrainTile.GetComponent <MeshFilter> ().sharedMesh;

        Vector3[] curVerts = mesh.vertices;

        for (int i = 0; i < curVerts.Length; i++)
        {
            Vector3 curVert = curVerts [i];

            curVert.y    = noise.FractalNoise(curVert.x + offsetX, curVert.z + offsetZ, octave, frequency, amplitude);
            curVerts [i] = curVert;
        }

        mesh.vertices = curVerts;

        mesh.Optimize();
        mesh.RecalculateNormals();

        terrainTile.GetComponent <MeshFilter> ().sharedMesh = mesh;
    }