private void GenerateMapInternal(SimplexNoiseGenerationData generationData, CancellationToken token) { m_Noise = new SimplexNoise(generationData.Seed); m_Map = new float[generationData.Width, generationData.Height]; for (var x = 0; x < generationData.Width; x++) { for (var y = 0; y < generationData.Height; y++) { var elevation = 0d; var noiseMod = 1d; var nx = (double)x / generationData.Width - 0.5d; var ny = (double)y / generationData.Height - 0.5d; for (var i = 0; i < generationData.Octaves; i++) { elevation += noiseMod * m_Noise.Evaluate(nx * (1 / noiseMod) * generationData.Frequency, ny * (1 / noiseMod) * generationData.Frequency); noiseMod /= 2; } var clampedElevation = Math.Clamp(elevation, -1, 1); var normalizedElevation = (clampedElevation + 1) / 2; var redistributedElevation = (float)Math.Pow(normalizedElevation, generationData.Redistribution); m_Map[x, y] = redistributedElevation; token.ThrowIfCancellationRequested(); } token.ThrowIfCancellationRequested(); } }
public void Execute(int id) { // position of current vertex in world space Vector3 offsetPos = Vector3.one * chunkSize / 2; Vector3 position = center - offsetPos + ToVector3(id) * vertSpacing; // Generate noise float density = 0; float frequency = scale / 100; float amplitude = 1; float octaveWeight = 1; for (int i = 0; i < numberOfOctaves; i++) { float val = SimplexNoise.Evaluate(position * frequency + octaveOffsetBuffer[i] + offset); val = 1 - Mathf.Abs(val); val *= val; val *= octaveWeight; density += val * amplitude; octaveWeight = Mathf.Max(Mathf.Min(val * weightMultiplier, 1), 0); amplitude *= persistence; frequency *= lacunarity; } // Generate surface plane and add noise density = -(position.y + surfaceOffset) + density * weight; // Generate shelves/terraces if (terracing.z > 0) { density += (position.y % terracing.x) * terracing.y; } // Add bedrock layer if (position.y < bedrockHeight) { density += bedrockWeight; } // Close mesh edges if (solidifyEdges) { Vector3 edgeOffset = new Vector3(Mathf.Abs(position.x), Mathf.Abs(position.y), Mathf.Abs(position.z)) - mapSize / 2; // if current vertex is at chunk boundary, aka non-negative edge offset // then turn into air if (edgeOffset.x >= 0 || edgeOffset.y >= 0 || edgeOffset.z >= 0) { density = -100; } } vertexBuffer[id] = position; densityBuffer[id] = density; }
void Update3D() { SimplexNoise noise = new SimplexNoise(seed); float[] noiseMap = new float[res * res]; float maxVal = float.MinValue; float minVal = float.MaxValue; for (int x = 0; x < res; x++) { float px = x / (res - 1f); for (int y = 0; y < res; y++) { float py = (y + offset.x) / (res - 1f); float angle = py * Mathf.PI * 2; // circumference should = 1 (length of cylinder) // 2*pi*r = 1 therefore r = 1/(2pi) float radius = 1 / (2 * Mathf.PI); float circleX = Mathf.Cos(angle) * radius; float circleY = Mathf.Sin(angle) * radius; float amplitude = 1; float frequency = scale; float noiseVal = 0; for (int i = 0; i < layers; i++) { noiseVal += (float)noise.Evaluate(circleX * frequency + offset.x, circleY * frequency + offset.x, px * frequency) * amplitude; amplitude *= persistence; frequency *= lacunarity; } noiseMap[x * res + y] = noiseVal; maxVal = Mathf.Max(maxVal, noiseVal); minVal = Mathf.Min(minVal, noiseVal); } } if (texture == null || texture.width != res) { texture = new Texture2D(res, res); } var cols = new Color[noiseMap.Length]; for (int i = 0; i < cols.Length; i++) { float v = Mathf.InverseLerp(minVal, maxVal, noiseMap[i]); cols[i] = new Color(v, v, v); } texture.SetPixels(cols); texture.Apply(); GetComponent <MeshRenderer> ().sharedMaterial.mainTexture = texture; }
public static float GetPixel(ChunkIndex chunkIndex, int x, int y, int z, NoiseSettings noiseSettings) { Vector3 chunkPos = chunkIndex.WorldPosition; float newX = x + noiseSettings.Seed + chunkPos.x; float newY = y + noiseSettings.Seed + chunkPos.y; float newZ = z + noiseSettings.Seed + chunkPos.z; float totalValue = 0f; for (int i = 0; i < noiseSettings.Octaves - 1; i++) { var frequency = Mathf.Pow(noiseSettings.Lacunarity, i); var amplitude = Mathf.Pow(noiseSettings.Roughness, i); totalValue += simplexNoise.Evaluate(i + newX * frequency / noiseSettings.Smoothness, i + newY * frequency / noiseSettings.Smoothness, i + newZ * frequency / noiseSettings.Smoothness) * amplitude; } var val = (((totalValue / 2.1f) + 1.2f) * noiseSettings.Amplitude) + noiseSettings.GroundHeight; return(val > 0 ? val : 1); }
public void GenPerlinNoise() { CreateTexture(ref perlinTexture); Color32[] colorArr = new Color32[pixelNum * pixelNum * pixelNum]; for (int channel = 0; channel < 4; channel++) { float amplitude = .5f; float nowFrequency = perlinFrequency * (2 << channel); for (int i = 0; i < octave; i++) { for (int z = 0; z < pixelNum; z++) { for (int y = 0; y < pixelNum; y++) { for (int x = 0; x < pixelNum; x++) { Color32 target = colorArr[z * pixelNum * pixelNum + y * pixelNum + x]; float perlinVal = amplitude * (float)(noise.Evaluate(x * nowFrequency, y * nowFrequency, z * nowFrequency) + 1) / 2; byte res = (byte)(perlinVal * 255); switch (channel) { case 0: target.r += res; break; case 1: target.g += res; break; case 2: target.b += res; break; case 3: target.a += res; break; } colorArr[z * pixelNum * pixelNum + y * pixelNum + x] = target; } } } if (i != octave - 1) { amplitude *= .5f; } nowFrequency *= 2; } } perlinTexture.SetPixels32(colorArr); perlinTexture.Apply(); }
public float Evaluate(Vector3 point) { float freq = settings.freq; float amplitude = settings.amplitude; float noiseValue = 0; for (int i = 0; i < settings.numLayers; i++) // add noise of increasing frequencies { float v = (noise.Evaluate((point + this.settings.noiseCenter) * freq) + 1) * 0.5f; noiseValue += v * -amplitude; freq *= settings.freqPower; amplitude *= settings.fallof; } return(noiseValue); }
public float[,] generateNoise(int chunkX, int chunkY) { // create an empty noise map with the mapDepth and mapWidth coordinates float[,] noiseMap = new float[width + 1, height + 1]; float maxHeight = float.MinValue; float minHeight = float.MaxValue; for (int x = 0; x < noiseMap.GetLength(0); x++) { for (int y = 0; y < noiseMap.GetLength(1); y++) { float frequency = 1; float amplitude = 1; float noiseHeight = 0; for (var i = 0; i < octaves; i++) { // calculate sample indices based on the coordinates and the scale float sampleX = (float)(x + (chunkX * height)) / scale * frequency; float sampleZ = (float)(y + (chunkY * height)) / scale * frequency; // generate noise value using PerlinNoise float noise = simplexNoise.Evaluate(sampleX, 0, sampleZ); noiseHeight += noise * amplitude; amplitude *= persistance; frequency *= lacunarity; } minHeight = noiseHeight < minHeight ? noiseHeight : minHeight; maxHeight = noiseHeight > maxHeight ? noiseHeight : maxHeight; noiseMap[x, y] = noiseHeight; } } for (var x = 0; x < noiseMap.GetLength(0); x++) { for (var y = 0; y < noiseMap.GetLength(1); y++) { noiseMap[(int)x, (int)y] = Mathf.InverseLerp(-2.5f, 2.5f, noiseMap[x, y]); } } return(noiseMap); }
public float Evaluate(Vector3 point) { float freq = settings.freq; float amplitude = settings.amplitude; float noiseValue = 0; for (int i = 0; i < settings.numLayers; i++) // add noise of increasing frequencies { float v = (noise.Evaluate((point + this.settings.noiseCenter) * freq) + 1) * 0.5f; noiseValue += v * amplitude; freq *= settings.freqPower; amplitude *= settings.fallof; } noiseValue = Mathf.Max(0, noiseValue - settings.minValue); // create a "floor" for the planet return(noiseValue); }
public float Evaluate(Vector3 _point) { float _noiseValue = 0f; float _frequency = settings.baseRoughness; float _amplitude = 1f; for (int i = 0; i < settings.numberOfLayers; i++) { float _v = noise.Evaluate(_point * _frequency + settings.center); _noiseValue += (_v + 1) * 0.5f * _amplitude; _frequency *= settings.roughness; _amplitude *= settings.persistence; } return((_noiseValue - settings.minValue) * settings.strength); }
public float Evaluate(Vector2Int position) { Vector3 point = new Vector3(position.x, position.y); float value = 0; float frequency = Settings.Frequency; float amplitude = 1; float maxAmplitude = 0; for (int i = 0; i < Settings.NumberOfLayers; i++) { value += (_noise.Evaluate(point * frequency) + 1) * .5f * amplitude; frequency /= (1 - Settings.Roughness); maxAmplitude += amplitude; amplitude *= Settings.Persistence; } if (Settings.NumberOfLayers > 0) { return(value / maxAmplitude); } return(0); }
public float Evaluate(Vector3 _point) { float _noiseValue = 0f; float _frequency = settings.baseRoughness; float _amplitude = 1f; float _weight = 1f; for (int i = 0; i < settings.numberOfLayers; i++) { float _v = 1 - Mathf.Abs(noise.Evaluate(_point * _frequency + settings.center)); _v *= _v; _v *= _weight; _weight = Mathf.Clamp01(_v * settings.weightMultiplier); _noiseValue += _v * _amplitude; _frequency *= settings.roughness; _amplitude *= settings.persistence; } return((_noiseValue - settings.minValue) * settings.strength); }
/// <summary> /// Create the values array /// </summary> /// <returns>Float values</returns> private float[,,] Gen(int size) { SimplexNoise sn = new SimplexNoise(seed); float[,,] values = new float[size + mv.sizeOffset(), size + mv.sizeOffset(), size + mv.sizeOffset()]; for (int x = 0; x < size + mv.sizeOffset(); x++) { for (int y = 0; y < size + mv.sizeOffset(); y++) { for (int z = 0; z < size + mv.sizeOffset(); z++) { float sampleX = (x + position.x * size) * noiseScale; float sampleY = (y + position.y * size) * noiseScale; float sampleZ = (z + position.z * size) * noiseScale; float noiseValue = ((float)sn.Evaluate(sampleX, sampleY, sampleZ, trength)) / 2 + 0.5f; values[x, y, z] = noiseValue; } } } return(values); }
private int GetHeight(int x, int z) { var noise = Math.Abs(SimplexNoise.Evaluate(x / 256f, z / 256f)); return(1 + (int)Math.Ceiling(noise * Height)); }
void Update4D() { SimplexNoise noise = new SimplexNoise(seed); var prng = new System.Random(seed); float[] noiseMap = new float[res * res]; float maxVal = float.MinValue; float minVal = float.MaxValue; var offsets = new Vector4[layers]; for (int i = 0; i < layers; i++) { offsets[i] = new Vector4((float)prng.NextDouble() * 2 - 1, (float)prng.NextDouble() * 2 - 1, (float)prng.NextDouble() * 2 - 1, (float)prng.NextDouble() * 2 - 1) * 1000; } // circumference should = 1 (length of cylinder) // 2*pi*r = 1 therefore r = 1/(2pi) float radius = 1 / (2 * Mathf.PI); for (int x = 0; x < res; x++) { float px = x / (res - 1f); float angleZW = px * Mathf.PI * 2; float circleZ = Mathf.Cos(angleZW) * radius + offset.x; float circleW = Mathf.Sin(angleZW) * radius + offset.y; for (int y = 0; y < res; y++) { float py = y / (res - 1f); float angle = py * Mathf.PI * 2; float circleX = Mathf.Cos(angle) * radius; float circleY = Mathf.Sin(angle) * radius; float amplitude = 1; float frequency = scale; float noiseVal = 0; for (int i = 0; i < layers; i++) { Vector4 p = new Vector4(circleX, circleY, circleZ, circleW) * frequency + offsets[i]; noiseVal += (float)noise.Evaluate(p.x, p.y, p.z, p.w) * amplitude; amplitude *= persistence; frequency *= lacunarity; } noiseMap[x * res + y] = noiseVal; maxVal = Mathf.Max(maxVal, noiseVal); minVal = Mathf.Min(minVal, noiseVal); } } if (texture == null || texture.width != res) { texture = new Texture2D(res, res); } texture.wrapMode = TextureWrapMode.Repeat; var cols = new Color[noiseMap.Length]; for (int i = 0; i < cols.Length; i++) { float v = Mathf.InverseLerp(minVal, maxVal, noiseMap[i]); cols[i] = new Color(v, v, v); } texture.SetPixels(cols); texture.Apply(); GetComponent <MeshRenderer> ().sharedMaterial.mainTexture = texture; }
public float GetValue(SimplexNoise n, float x, float y, float z) => (n.Evaluate(x * frequency, y * frequency, z * frequency) * amplitude * 2 - amplitude) * weight;
private float GetNoise(float x, float y, float z) { return((float)noise.Evaluate(x, y, z)); }
public static Node SimplexNoise() { var noise = new SimplexNoise(); return((float x, float y) => noise.Evaluate(x, y) / 2 + .5f); }