private float yIntensity = 0.3F; // Intensity of randomization on the Y axis #endregion Fields #region Methods /** * This function generates the heightmap and makes the vertices. * Normals are calculated by the MakeVertsUnique() function. * Calling this function is required, so there's no need to be save about executing the * RecalculateNormals() function */ public void GenerateHeightmap() { noise = new ex.FractalNoise(); noise.SetNoise( offsetX, offsetY, seed ); mesh = GetComponent<MeshFilter>().mesh; if( !mesh ) { Debug.Log( "Can't find mesh!" ); return; } if( landMaterial ) renderer.material = landMaterial; else renderer.material.color = Color.magenta; Vector3[] vertices = new Vector3[Height * Width]; Vector2[] uv = new Vector2[Height * Width]; Vector2 uvScale = new Vector2( 1.0f / ( Width - 1 ), 1.0f / ( Height - 1 ) ); Vector3 sizeScale = new Vector3( Size.x / ( Width - 1 ), Size.y, Size.z / ( Height - 1 ) ); int y = 0; int x = 0; for( y = 0; y < Height; y++ ) { for( x = 0; x < Width; x++ ) { Vector3 vertex = new Vector3( x, noise.GenerateFractal( amplitude, frequency, octaves, (float) x, (float) y, persistence ), y ); vertex.y += noise.GeneratePerlin( (float) x, (float) y ) * yIntensity; vertices[y*Width + x] = Vector3.Scale( sizeScale, vertex ); // Sets the vertex position on the grid uv[y * Width + x] = Vector2.Scale( new Vector2( x, y ), uvScale ); if( y < Height - 1 && y > 0 && x < Width - 1 && x > 0 ) { // Don't randomize outer bounds of node because of (future) tiling vertices[y * Width + x].x += Random.Range( -1.0f, 1.0f ) * yIntensity; vertices[y * Width + x].z += Random.Range( -1.0f, 1.0f ) * yIntensity; } } } // Build triangle indices: 3 indices into vertex array for each triangle int[] triangles = new int[(Height - 1) * (Width - 1) * 6]; int index = 0; for( y = 0; y < Height-1; y++ ) { for( x = 0; x < Width-1; x++ ) { // For each grid cell output two triangles triangles[index++] = (y * Width) + x; triangles[index++] = ((y+1) * Width) + x; triangles[index++] = (y * Width) + x + 1; triangles[index++] = ((y+1) * Width) + x; triangles[index++] = ((y+1) * Width) + x + 1; triangles[index++] = (y * Width) + x + 1; } } mesh.Clear(); mesh.vertices = vertices; mesh.uv = uv; mesh.triangles = triangles; MakeVertsUnique(); }
private ex.FractalNoise noise; // Object of FractalNoise class, namespaced in ex /** * This function generates the heightmap and makes the vertices. * Normals are calculated by the MakeVertsUnique() function. * Calling this function is required, so there's no need to be save about executing the * RecalculateNormals() function */ public void GenerateHeightmap() { noise = new ex.FractalNoise(); noise.SetNoise(offsetX, offsetY, seed); mesh = GetComponent <MeshFilter>().mesh; if (!mesh) { Debug.Log("Can't find mesh!"); return; } if (landMaterial) { renderer.material = landMaterial; } else { renderer.material.color = Color.magenta; } Vector3[] vertices = new Vector3[Height * Width]; Vector2[] uv = new Vector2[Height * Width]; Vector2 uvScale = new Vector2(1.0f / (Width - 1), 1.0f / (Height - 1)); Vector3 sizeScale = new Vector3(Size.x / (Width - 1), Size.y, Size.z / (Height - 1)); int y = 0; int x = 0; for (y = 0; y < Height; y++) { for (x = 0; x < Width; x++) { Vector3 vertex = new Vector3(x, noise.GenerateFractal(amplitude, frequency, octaves, (float)x, (float)y, persistence), y); vertex.y += noise.GeneratePerlin((float)x, (float)y) * yIntensity; vertices[y * Width + x] = Vector3.Scale(sizeScale, vertex); // Sets the vertex position on the grid uv[y * Width + x] = Vector2.Scale(new Vector2(x, y), uvScale); if (y < Height - 1 && y > 0 && x < Width - 1 && x > 0) // Don't randomize outer bounds of node because of (future) tiling { vertices[y * Width + x].x += Random.Range(-1.0f, 1.0f) * yIntensity; vertices[y * Width + x].z += Random.Range(-1.0f, 1.0f) * yIntensity; } } } // Build triangle indices: 3 indices into vertex array for each triangle int[] triangles = new int[(Height - 1) * (Width - 1) * 6]; int index = 0; for (y = 0; y < Height - 1; y++) { for (x = 0; x < Width - 1; x++) { // For each grid cell output two triangles triangles[index++] = (y * Width) + x; triangles[index++] = ((y + 1) * Width) + x; triangles[index++] = (y * Width) + x + 1; triangles[index++] = ((y + 1) * Width) + x; triangles[index++] = ((y + 1) * Width) + x + 1; triangles[index++] = (y * Width) + x + 1; } } mesh.Clear(); mesh.vertices = vertices; mesh.uv = uv; mesh.triangles = triangles; MakeVertsUnique(); }