public void DrawMesh(LibNoise.Unity.Noise2D noiseMap, Texture2D texture, float hMultiplier, AnimationCurve curve, int lod) { ChunkManager chunkMang = FindObjectOfType <ChunkManager>(); chunkMang.SplitWorldMap(texture, noiseMap, chunkRows, chunkCols); chunkMang.GenerateAllChunks(hMultiplier, curve, lod); }
public void GenerateHeightMap() { var perlin = new LibNoise.Unity.Generator.Perlin(Frequency, Lacunarity, Persistence, Octaves, Seed, LibNoise.Unity.QualityMode.High); //var ridged = new LibNoise.Unity.Generator.RidgedMultifractal(Frequency, Lacunarity, Octaves, Seed, LibNoise.Unity.QualityMode.Medium); //var turb = new LibNoise.Unity.Operator.Turbulence(0.25, perlin); //var layered = new LibNoise.Unity.Operator.Multiply(perlin, ridged); //var var scaled = new LibNoise.Unity.Operator.ScaleBias(Scaling, ScaleBias, perlin); var final = new LibNoise.Unity.Operator.Terrace(false, scaled); //for (float i = 0; i < 50; i += 10) final.Add(0f); final.Add(10f); final.Add(100f); var noise = new LibNoise.Unity.Noise2D(TotalSize, final); noise.GeneratePlanar(ClipX, ClipX + (ClipSize * ChunksPerSide), ClipY, ClipY + (ClipSize * ChunksPerSide)); GameObject.Destroy(HeightMap); HeightMap = noise.GetTexture(LibNoise.Unity.Gradient.Grayscale); HeightMap.Apply(); GameObject.Destroy(NormalMap); NormalMap = noise.GetNormalMap(1f); NormalMap.Apply(); floatMap = new float[TotalSize * TotalSize]; for (int y = 0; y < TotalSize; y++) for (int x = 0; x < TotalSize; x++) floatMap[y * TotalSize + x] = noise[x, y]; }
public void Generate() { // Create the module network ModuleBase moduleBase; switch(noise) { case NoiseType.Billow: moduleBase = new Billow(); break; case NoiseType.RiggedMultifractal: moduleBase = new RiggedMultifractal(); break; case NoiseType.Voronoi: moduleBase = new Voronoi(); break; case NoiseType.Mix: Perlin perlin = new Perlin(); RiggedMultifractal rigged = new RiggedMultifractal(); moduleBase = new Add(perlin, rigged); break; default: moduleBase = new Perlin(); break; } // Initialize the noise map this.m_noiseMap = new Noise2D(resolution, resolution, moduleBase); this.m_noiseMap.GeneratePlanar( offset + -1 * 1/zoom, offset + offset + 1 * 1/zoom, offset + -1 * 1/zoom, offset + 1 * 1/zoom); Debug.Log (moduleBase.GetValue (0, 0, UnityEngine.Random.value)); // Generate the textures this.m_textures[0] = this.m_noiseMap.GetTexture(LibNoise.Unity.Gradient.Grayscale); this.m_textures[0].Apply(); this.m_textures[1] = this.m_noiseMap.GetTexture(LibNoise.Unity.Gradient.Terrain); this.m_textures[1].Apply(); this.m_textures[2] = this.m_noiseMap.GetNormalMap(3.0f); this.m_textures[2].Apply(); //display on plane GetComponent<Renderer>().material.mainTexture = m_textures[0]; //write images to disk File.WriteAllBytes(Application.dataPath + "/../Gray.png", m_textures[0].EncodeToPNG() ); File.WriteAllBytes(Application.dataPath + "/../Terrain.png", m_textures[1].EncodeToPNG() ); File.WriteAllBytes(Application.dataPath + "/../Normal.png", m_textures[2].EncodeToPNG() ); Debug.Log("Wrote Textures out to "+Application.dataPath + "/../"); }
public void SplitWorldMap(Texture2D wMap, LibNoise.Unity.Noise2D nMap, int rows, int cols) { chunkCols = cols; chunkRows = rows; worldMap = wMap; noiseMap = nMap; SplitTexture(); }
public MakePerlin(float seed) { perlin = new Texture2D (256, 256); ModuleBase moduleBase; moduleBase = new Perlin(m_frequency, m_lacunarity, m_persistence, m_octaveCount, (int)seed, m_quality); noiseMap = new Noise2D(perlin.width, perlin.height, moduleBase); float zoom = 1f; float offset = 0f; noiseMap.GeneratePlanar( offset + -1 * 1/zoom, offset + offset + 1 * 1/zoom, offset + -1 * 1/zoom, offset + 1 * 1/zoom); perlin = noiseMap.GetTexture (); }
// Generate noise map //mapSizeSqrt, mapSizeSqrt, seed, side, offset, octaves, persistence, lacunarity, noiseSource public static float[,] GenerateNoiseMap(int seed, Vector3 offset, GPUNoiseGenerator.NoiseData noise, float scale, NoiseSource source, int?meshGridSize = null, float zoom = 1, int performanceTestLoops = 1, float radius = 100) { int mapSizeSqrt = (int)noise.resolution; float[,] noiseMap = new float[mapSizeSqrt, mapSizeSqrt]; System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch(); st.Start(); for (int performanceLoopIndex = 0; performanceLoopIndex < performanceTestLoops; ++performanceLoopIndex) { switch (source) { case NoiseSource.Unity: #region [ - Unity Noise - ] Vector2 chunkOffset = offset; //GenerateFalloffMap(ref falloffMap, mapChunkSize); /* Generate Height Map * (mapSide tarkottaa yhen spherical cuben reunan HeightMapin kokoa) * [5] * [4] * [1][2][3] * [0] */ // Laskee sphericalCuben valitun lähtöpisteen for (int i = 0, len = int.Parse(((int)noise.side).ToString()); i <= len; ++i) { switch (i) { case 0: chunkOffset.x += mapSizeSqrt; break; case 1: chunkOffset.x -= mapSizeSqrt; chunkOffset.y += mapSizeSqrt; break; case 2: chunkOffset.x += mapSizeSqrt; break; case 3: chunkOffset.x += mapSizeSqrt; break; case 4: chunkOffset.x -= mapSizeSqrt; chunkOffset.y += mapSizeSqrt; break; case 5: chunkOffset.y += mapSizeSqrt; break; } } // Laskee quartereitten (..quartereitten (..quartereitten (..jne))) lähtöpisteen int quarterSize = mapSizeSqrt; for (int i = 0, len = noise.quarter.Length; i < len; ++i) { //quarterSize /= 2; // quarterin leveys\korkeus scale *= 2; switch (noise.quarter[i]) { case '0': // vasenala // -> piste ei liiku minnekkään Debug.Log("scale: " + scale + ", quarter size: " + quarterSize + ", side:" + noise.quarter[i] + ", offset: " + chunkOffset.x + " | " + chunkOffset.y); break; case '1': // oikeeala chunkOffset.x += quarterSize / scale; Debug.Log("scale: " + scale + ", quarter size: " + quarterSize + ", side:" + noise.quarter[i] + ", offset: " + chunkOffset.x + " | " + chunkOffset.y); break; case '2': // vasenylä chunkOffset.y += quarterSize / scale; Debug.Log("scale: " + scale + ", quarter size: " + quarterSize + ", side:" + noise.quarter[i] + ", offset: " + chunkOffset.x + " | " + chunkOffset.y); break; case '3': // oikeeylä chunkOffset.x += quarterSize / scale; chunkOffset.y += quarterSize / scale; Debug.Log("scale: " + scale + ", quarter size: " + quarterSize + ", side:" + noise.quarter[i] + ", offset: " + chunkOffset.x + " | " + chunkOffset.y); break; } } Vector2[] octaveOffset = new Vector2[noise.octaves]; maxPossibleHeight = 0; noise.amplitude = 1; noise.frequency = 1; NormalizeMode normalizeMode = NormalizeMode.Global; System.Random prng = new System.Random(seed); Vector2 randomOffsetFromSeed = new Vector2(prng.Next(-100000, 100000) - chunkOffset.x, prng.Next(-100000, 100000) - chunkOffset.y); for (i = 0; i < noise.octaves; ++i) { octaveOffset[i] = randomOffsetFromSeed; maxPossibleHeight += noise.amplitude; noise.amplitude *= noise.persistence; } if (scale <= 0) { scale = 0.0001f; } maxLocalNoiseHeight = float.MinValue; minLocalNoiseHeight = float.MaxValue; halfWidth = mapSizeSqrt / 2f; halfHeight = mapSizeSqrt / 2f; for (y = 0; y < mapSizeSqrt; ++y) { for (x = 0; x < mapSizeSqrt; ++x) { noise.amplitude = 1; noise.frequency = 1; noiseHeight = 0; for (i = 0; i < noise.octaves; ++i) // Oktaavien teko kuluttaa sikana eli tarkkana { sampleX = (x - mapSizeSqrt) / scale * noise.frequency + octaveOffset[i].x * noise.frequency; sampleY = (y - mapSizeSqrt) / scale * noise.frequency + octaveOffset[i].y * noise.frequency; perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1; // tää on raskas, * 2 - 1 tekee sen että se menee [-1, 1]) noiseHeight += perlinValue * noise.amplitude; //Debug.Log("X: " + x + ", Y: " + y + " | " + perlinValue + " | " + amplitude); noise.amplitude *= noise.persistence; noise.frequency *= noise.lacunarity; } if (noiseHeight > maxLocalNoiseHeight) { maxLocalNoiseHeight = noiseHeight; } else if (noiseHeight < minLocalNoiseHeight) { minLocalNoiseHeight = noiseHeight; } noiseMap[x, y] = noiseHeight; //Debug.Log("X: " + x + ", Y: " + y + " | " + noiseMap[x, y]); //noiseMap[x, y] = Mathf.InverseLerp(minLocalNoiseHeight, maxLocalNoiseHeight, noiseMap[x, y]); } } for (int y = 0; y < mapSizeSqrt; ++y) { for (int x = 0; x < mapSizeSqrt; ++x) { if (normalizeMode == NormalizeMode.Local) { noiseMap[x, y] = Mathf.InverseLerp(minLocalNoiseHeight, maxLocalNoiseHeight, noiseMap[x, y]); } else { float normalizedHeight = (noiseMap[x, y] + 1) / (maxPossibleHeight / 0.9f); noiseMap[x, y] = Mathf.Clamp(normalizedHeight, 0, int.MaxValue); } } } #endregion break; case NoiseSource.LibNoise: #region [ - LibNoise - ] Perlin perlinNoise = new Perlin { Quality = QualityMode.Medium, OctaveCount = noise.octaves, Lacunarity = noise.lacunarity, Seed = seed, Persistence = noise.persistence, Frequency = noise.frequency }; LibNoise.Unity.ModuleBase moduleBase; moduleBase = perlinNoise; Noise2D noise2D = new LibNoise.Unity.Noise2D((int)noise.resolution, (int)noise.resolution, moduleBase); //noise.GeneratePlanar((double)offset.x, (double)(offset.x + mapWidth), (double)(offset.y + mapHeight), (double)offset.y); noise2D.GeneratePlanar((int)noise.resolution, 0, (int)noise.resolution, 0); noiseMap = noise2D.m_data; #endregion break; case NoiseSource.GPU_2DFloatArray: #region [ - GPU 2D Float Array - ] noiseMap = GPUNoiseGenerator.GenerateNoiseArray(seed, noise, radius, meshGridSize, offset, zoom); #endregion break; case NoiseSource.GPU_RenderTextureTo2DFloatArray: #region [ - GPU Render Texture to 2D Float Array - ] noiseMap = GPUNoiseGenerator.GenerateNoiseArrayFromRenderTexture(seed, noise); #endregion break; default: return(null); } } st.Stop(); if (performanceTestLoops > 1) { Debug.Log(string.Format("Generated noise with {0} {1} times and it took {2} ms to complete.", source.ToString(), performanceTestLoops, st.ElapsedMilliseconds)); } return(noiseMap); }