void Awake() { TextureController.Init(); OptController.Init(); VoronoiNoise.Init(); tex = new TTexture(Vector3.zero, resolution, (int)size, type); }
private void GenerateVoronoiChebyshev() { for (int i = 0; i < vertices.Length; i++) { Vector3 v = vertices[i]; v.y = zOffset; voronoiBuffer[i] = VoronoiNoise.SampleChebyshev3D(v, frequency, octaves, lacunarity, persistence); } }
private void InitModules() { // init everything here so they don't need to be instantiated later VoronoiNoise.Init(); MaterialController.Init(); TextureController.Init(); OptController.Init(); Seeder.Init(islandDensity); method = Constants.NoiseFuncs [(int)noiseType]; }
/// <summary> /// Generate the Voronoi noise pattern map /// </summary> /// <returns></returns> public override HeightMap Generate() { var generator = new VoronoiNoise(Seed); var noise = generator.Generate(Dimensions.Width, Dimensions.Height, PointCount, Exponent); var map = new HeightMap(noise); map.Normalize(); return(map); }
void Start() { //Random.seed = Random.Range (0, 65000); m_surfaceSeed = Random.Range (0, 65000); //Make 2 perlin noise objects, one is used for the surface and the other for the caves m_surfacePerlin = new PerlinNoise(m_surfaceSeed); m_voronoi = new VoronoiNoise(m_surfaceSeed); GameObject.Find ("SaveTracker").GetComponent<SaveSceneComponent>().save.m_surfaceSeed = m_surfaceSeed; player = GameObject.FindWithTag ("Player").transform; // PerlinNoise m_cavePerlin = new PerlinNoise(m_caveSeed); //Set some varibles for the marching cubes plugin MarchingCubes.SetTarget(0.0f); MarchingCubes.SetWindingOrder(2, 1, 0); MarchingCubes.SetModeToCubes(); //create a array to hold the voxel chunks m_voxelChunk = new VoxelChunk[m_chunksX,m_chunksY,m_chunksZ]; //The offset is used to centre the terrain on the x and z axis. For the Y axis //you can have a certain amount of chunks above the y=0 and the rest will be below Vector3 offset = new Vector3(m_chunksX*m_voxelWidth*-0.5f, -(m_chunksY-m_chunksAbove0)*m_voxelHeight, m_chunksZ*m_voxelLength*-0.5f); for(int x = 0; x < m_chunksX; x++) { for(int y = 0; y < m_chunksY; y++) { for(int z = 0; z < m_chunksZ; z++) { //The position of the voxel chunk Vector3 pos = new Vector3(x*m_voxelWidth, y*m_voxelHeight, z*m_voxelLength); //Create the voxel object m_voxelChunk[x,y,z] = new VoxelChunk(pos+offset, m_voxelWidth, m_voxelHeight, m_voxelLength, m_surfaceLevel); //Create the voxel data m_voxelChunk[x,y,z].CreateVoxels(m_surfacePerlin, m_voronoi);//, m_cavePerlin); i++; //Smooth the voxels, is optional but I think it looks nicer // m_voxelChunk[x,y,z].SmoothVoxels(); //Create the normals. This will create smoothed normal. //This is optional and if not called the unsmoothed mesh normals will be used // m_voxelChunk[x,y,z].CalculateNormals(); //Creates the mesh form voxel data using the marching cubes plugin and creates the mesh collider m_voxelChunk[x,y,z].CreateMesh(m_material); } } } currentX = m_chunksX * m_voxelWidth; currentZ = m_chunksZ * m_voxelLength; }
//, PerlinNoise cavePerlin) public void CreateVoxels(PerlinNoise surfacePerlin, VoronoiNoise voronoiNoise) { //float startTime = Time.realtimeSinceStartup; //Creates the data the mesh is created form. Fills m_voxels with values between -1 and 1 where if(monoInstance == null) { //-1 is a soild voxel and 1 is a empty voxel. monoInstance = GameObject.Find ("CoRoutiner").GetComponent<MonoBehaviour>(); } monoInstance.StartCoroutine ("DoCoroutine", this.kriejt(surfacePerlin, voronoiNoise)); //Debug.Log("Create voxels time = " + (Time.realtimeSinceStartup-startTime).ToString() ); }
public static float[,] ValueNoiseMapGenerator(int width, int height, int seed, float noiseScale, int octaves, float persistance) { float[,] noiseMap = new float[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float valor = VoronoiNoise.FractalNoise2D(x, y, octaves, noiseScale, persistance, seed) * 2f; noiseMap[x, y] = valor; } } return(noiseMap); }
//, PerlinNoise cavePerlin) IEnumerator kriejt(PerlinNoise surfacePerlin, VoronoiNoise voronoiNoise) { int w = m_voxels.GetLength(0); int h = m_voxels.GetLength(1); int l = m_voxels.GetLength(2); for(int x = 0; x < w; x++) { for(int z = 0; z < l; z++) { //world pos is the voxels position plus the voxel chunks position float worldX = x+m_pos.x; float worldZ = z+m_pos.z; groundHt = SampleGround(worldX, worldZ, surfacePerlin, voronoiNoise); mountainHt = SampleMountains(worldX, worldZ, surfacePerlin); ht = mountainHt + groundHt; for(int y = 0; y < h; y++) { worldY = y+m_pos.y-m_surfaceLevel; //If we take the heigth value and add the world //the voxels will change from positiove to negative where the surface cuts through the voxel chunk m_voxels[x,y,z] = Mathf.Clamp(ht + worldY , -1.0f, 1.0f); //float caveHt = SampleCaves(worldX, worldY, worldZ, cavePerlin); //This fades the voxel value so the caves never appear more than 16 units from //the surface level. //float fade = 1.0f - Mathf.Clamp01(Mathf.Max(0.0f, worldY)/16.0f); //m_voxels[x,y,z] += caveHt * fade; // m_voxels[x,y,z] = Mathf.Clamp(m_voxels[x,y,z], -1.0f, 1.0f); } } } yield return new WaitForSeconds(0.001f); }
float SampleGround(float x, float z, PerlinNoise perlin, VoronoiNoise voronoi) { //This creates the noise used for the ground. //The last value (8.0f) is the amp that defines (roughly) the maximum //and minimum vaule the ground varies from the surface level //return perlin.FractalNoise2D(x, z, 12, 80.0f, 8.0f); test = voronoi.FractalNoise2D(x, z, 8, 32.0f, 8.0f) + perlin.FractalNoise2D(x, z, 12, 80.0f, 8.0f)/2; if(test > 1.85f) { return Mathf.Min (1.85f, test) + test/3f; } else { return test; } //return Mathf.Min (2.75f,test); }
IEnumerator lV(Vector3 pos) { MarchingCubes.SetTarget(0.0f); MarchingCubes.SetWindingOrder(2, 1, 0); MarchingCubes.SetModeToCubes(); m_surfacePerlin = new PerlinNoise(save.m_surfaceSeed); m_voronoi = new VoronoiNoise(save.m_surfaceSeed); m_voxelChunk = new VoxelChunk(pos, m_Width, m_Height, m_Length, surfaceLevel); m_voxelChunk.bDodane = true; m_voxelChunk.CreateVoxels(m_surfacePerlin, m_voronoi);//, m_cavePerlin); m_voxelChunk.CreateMesh (GameObject.Find ("TerrainGenerator").GetComponent<GenerateTerrain>().m_material); yield return new WaitForFixedUpdate(); }
public void GenerateMap() { System.Random random = new System.Random(); int RandomSeed = random.Next(0, 100000); System.Random local_seed = new System.Random(RandomSeed); int int_seed = local_seed.Next(-100000, 100000); float[,] NoiseMap; if (this.noiseSelector == NoiseSelector.Perlin) { NoiseMap = PerlinNoise.PerlinNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, this.noiseScale, octaves, persistance, lacunarity, offset); } else if (this.noiseSelector == NoiseSelector.Simplex) { NoiseMap = SimplexNoise.SimplexNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, octaves, offset); } else if (this.noiseSelector == NoiseSelector.Value) { NoiseMap = ValueNoise.ValueNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, this.noiseScale); } else if (this.noiseSelector == NoiseSelector.Voronoi) { NoiseMap = VoronoiNoise.ValueNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, this.noiseScale, octaves, persistance); } else if (this.noiseSelector == NoiseSelector.PerlinVoronoi) { float[,] perlinMap = PerlinNoise.PerlinNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, this.noiseScale, octaves, persistance, lacunarity, offset); float[,] voronoiMap = VoronoiNoise.ValueNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, this.noiseScale, octaves, persistance); NoiseMap = Convolution.ConvolutionMapGenerator(this.mapWidth, this.mapHeigth, perlinMap, voronoiMap); } else { //default: perlin noise NoiseMap = PerlinNoise.PerlinNoiseMapGenerator(this.mapWidth, this.mapHeigth, int_seed, this.noiseScale, octaves, persistance, lacunarity, offset); } Color[] HeighMap = new Color[mapWidth * mapHeigth]; for (int y = 0; y < mapHeigth; y++) { for (int x = 0; x < mapWidth; x++) { float currentHeigth = NoiseMap[x, y]; for (int i = 0; i < regions.Length; i++) { if (currentHeigth <= regions[i].heigth) { HeighMap[y * mapWidth + x] = regions[i].colour; break; } } } } MapDisplay display = FindObjectOfType <MapDisplay>(); if (drawMode == DrawMode.NoiseMap) { display.DrawTexture(TextureGenerator.TextureFromHeightMap(NoiseMap)); } else if (drawMode == DrawMode.HeighMap) { display.DrawTexture(TextureGenerator.TextureFromHeighMap(HeighMap, mapWidth, mapHeigth)); } }
public void CreateVoxels(PerlinNoise surfacePerlin, PerlinNoise cavePerlin) { //float startTime = Time.realtimeSinceStartup; //Creates the data the mesh is created form. Fills m_voxels with values between -1 and 1 where //-1 is a soild voxel and 1 is a empty voxel. int w = m_voxels.GetLength(0); int h = m_voxels.GetLength(1); int l = m_voxels.GetLength(2); for (int x = 0; x < w; x++) { for (int z = 0; z < l; z++) { //world pos is the voxels position plus the voxel chunks position float worldX = x + m_pos.x; float worldZ = z + m_pos.z; float ht = SampleGround(worldX, worldZ, surfacePerlin); // float localBiom = 0f; // localBiom += (float)z/h * ((float)m_neighbourBiomes[2] - (float)m_biome); // localBiom += (float)x/w * ((float)m_neighbourBiomes[1] - (float)m_biome); // localBiom += (1f-(float)z/h) * ((float)m_neighbourBiomes[0]- (float)m_biome); // localBiom += (1f-(float)x/w) * ((float)m_neighbourBiomes[3]- (float)m_biome); // float factor = Mathf.Clamp01((surfacePerlin.FractalNoise2D(x, z, 4, 400.0f, 1.0f) + 0.75f)/1.5f); // float it = ht + SampleIslands(worldX, worldZ, surfacePerlin); // ht = (ht+m_surfaceLevel+1f) * factor + it * (1f-factor); // float factor = Mathf.Clamp01((surfacePerlin.FractalNoise2D(x, z, 4, 400.0f, 1.5f) + 0.75f)/1.5f); // float it = ht + SampleSpikes(worldX, worldZ, surfacePerlin); // ht = it; //(ht-1f) * factor + it * (1f-factor); /* * float factor = Mathf.Clamp01((surfacePerlin.FractalNoise2D(x, z, 4, 400.0f, 1.0f) + 0.75f)/1.5f); * float st = SampleSpikes(worldX, worldZ, surfacePerlin); * ht = (ht+m_surfaceLevel-1f) * factor + (ht + st) * (1f-factor); * * factor = Mathf.Clamp01((surfacePerlin.FractalNoise2D(x, z, 4, 150.0f, 1.0f) + 0.75f)/1.5f); * float it = SampleIslands(worldX, worldZ, surfacePerlin); * ht = (ht+m_surfaceLevel+1f) * factor + (ht + it) * (1f-factor); * * ht -= m_surfaceLevel; * ht -= SampleMountains(worldX, worldZ, surfacePerlin); */ VoronoiNoise.SetDistanceToEuclidian(); VoronoiNoise.SetCombinationTo_D2_D0(); ht *= VoronoiNoise.FractalNoise2D(worldX, worldZ, 2, 500f, 2f, 6); float factor = Mathf.Clamp01((surfacePerlin.FractalNoise2D(worldX, worldZ, 4, 400.0f, 1.0f) + 0.9375f) / 1.875f); float st = SampleSpikes(worldX, worldZ, surfacePerlin); ht = (ht + m_surfaceLevel - 1f) * factor + (ht + st) * (1f - factor); factor = Mathf.Clamp01((surfacePerlin.FractalNoise2D(worldX, worldZ, 4, 150.0f, 1.0f) + 0.9375f) / 1.875f); float it = SampleIslands(worldX, worldZ, surfacePerlin); ht = (ht + m_surfaceLevel + 1f) * factor + (ht + it) * (1f - factor); ht -= m_surfaceLevel * 1.5f; // ht -= SampleMountains(worldX, worldZ, surfacePerlin); for (int y = 0; y < h; y++) { float worldY = y + m_pos.y - m_surfaceLevel; //If we take the heigth value and add the world //the voxels will change from positiove to negative where the surface cuts through the voxel chunk m_voxels[x, y, z] = Mathf.Clamp(ht + worldY, -1.0f, 1.0f); if (m_biome == 15) { float caveHt = SampleCaves(worldX, worldY, worldZ, cavePerlin); //This fades the voxel value so the caves never appear more than h units from //the surface level. float fade = 1.0f - Mathf.Clamp01(Mathf.Max(0.0f, worldY) / h); m_voxels[x, y, z] += caveHt * fade; m_voxels[x, y, z] = Mathf.Clamp(m_voxels[x, y, z], -1.0f, 1.0f); } } } } //Debug.Log("Create voxels time = " + (Time.realtimeSinceStartup-startTime).ToString() ); }
public static float gen(Vector2 p) { return(VoronoiNoise.noise(p) + 0.5f); }
IEnumerator test2() { PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); //List<ChunkData> chunks2 = new List<ChunkData>(); foreach(ChunkData chunk in chunks.ToArray ()) { for(int i = 0; i <= uncoverArea; i++) { //N if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x+(i*8)).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x+8*i+1, chunk.m_pos.y+1, chunk.m_pos.z+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //NW if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x+8*i).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z + 8*i).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x+8*i+1, chunk.m_pos.y+1, chunk.m_pos.z+8*i+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //NE if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x+8*i).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z - 8*i).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x+8*i+1, chunk.m_pos.y+1, chunk.m_pos.z-8*i+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //S if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x-8*i).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x-8*i+1, chunk.m_pos.y+1, chunk.m_pos.z+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //SW if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x-8*i).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z + 8*i).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x-8*i+1, chunk.m_pos.y+1, chunk.m_pos.z+8*i+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //SE if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x-8*i).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z - 8*i).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x-8*i+1, chunk.m_pos.y+1, chunk.m_pos.z-8*i+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //W if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z + 8*i).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x+1, chunk.m_pos.y+1, chunk.m_pos.z+8*i+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } //E if(!GameObject.Find ("Voxel Mesh " + (chunk.m_pos.x).ToString() + " " + (chunk.m_pos.y).ToString() + " " + (chunk.m_pos.z - 8*i).ToString())) { // PerlinNoise m_surfacePerlin = new PerlinNoise(terrain.m_surfaceSeed); // VoronoiNoise m_voronoi = new VoronoiNoise(terrain.m_surfaceSeed); Vector3 pos = new Vector3(chunk.m_pos.x+1, chunk.m_pos.y+1, chunk.m_pos.z-8*i+1); m_voxelChunktemp = new VoxelChunk(pos, terrain.m_voxelWidth, terrain.m_voxelHeight, terrain.m_voxelLength, terrain.m_surfaceLevel); m_voxelChunktemp.CreateVoxels (m_surfacePerlin, m_voronoi); m_voxelChunktemp.CreateMesh (terrain.m_material); // yield return new WaitForSeconds(.1f); //chunk.bOnce = true; } yield return new WaitForSeconds(0.001f); } chunk.bUsed = true; //chunks2.Add (chunk); chunks.Remove (chunk); yield return new WaitForSeconds(0.001f); } //chunks = chunks.Except (chunks2).ToList (); //chunks2.Clear (); //yield return new WaitForSeconds(.2f); }