private void GenWorld() { // Create rotation for noise (so they are less likely to be integers) Quaternion rot = Quaternion.Euler(45, 45, 45); // Create new grid grid = new float[width, height]; /*Vector3 initialArgs = * new Vector3((transform.position.x / transform.lossyScale.x) * (float)width, * (transform.position.y / transform.lossyScale.y) * (float)height, * seed); * * Vector3 diff = new Vector3(1, 1, 0);*/ Vector3 initialArgs = transform.position; initialArgs.z = Seed; Vector3 diff = new Vector3(transform.lossyScale.x / (float)width, transform.lossyScale.y / (float)height, 0); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { // Initialise perlin arguments //Vector3 perlinArgs = // new Vector3(x / (float)width, y / (float)height, seed); /*Vector3 perlinArgs = * new Vector3(x, y, seed); * * perlinArgs.x += (transform.position.x / transform.lossyScale.x) * (float)width; * perlinArgs.y += (transform.position.y / transform.lossyScale.y) * (float)height;*/ Vector3 perlinArgs = initialArgs + new Vector3(diff.x * x, diff.y * y, 0); perlinArgs.Scale(noiseScale); // Rotate perlin args so they're unlikely to be integers perlinArgs = rot * perlinArgs; // Generate perlin noise float perlin = ImprovedPerlin.Noise(perlinArgs.x, perlinArgs.y, perlinArgs.z); // Set grid values to noise grid[x, y] = perlin; } } }
// Update is called once per frame void Update() { currentNoiseOffset += Time.deltaTime * noiseSpeed; for (int x = 0; x < resolution; x++) { for (int y = 0; y < resolution; y++) { cacheHeights[x, y] = perlin.Noise(x * noiseFrequency, y * noiseFrequency, currentNoiseOffset); } } terrainData.SetHeights(0, 0, cacheHeights); }
void Update() { currentNoiseOffset += Time.deltaTime * noiseSpeed; for (int i = 0, y = 0; y <= sizeY; y++) { for (int x = 0; x <= sizeX; x++, i++) { vertices[i].z = noiseAmplitude * perlin.Noise(x * noiseFrequency, y * noiseFrequency, currentNoiseOffset); } } mesh.vertices = vertices; mesh.RecalculateNormals(); }
// Update is called once per frame void Update() { currentNoiseOffset += Time.deltaTime * noiseSpeed; int i = 0; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { cacheVertices[i].y = perlin.Noise(x * noiseFrequency, y * noiseFrequency, currentNoiseOffset) * noiseAmplitude; i++; } } // replace the old vertices with the new ones planeMesh.vertices = cacheVertices; // generate new normals so that the lighting works correctly planeMesh.RecalculateNormals(); }