public Chunk GenerateVoxels3D(Int2 chunkIndex) { var maxHeight = 128; var chunk = new Chunk(chunkIndex, new Voxel[Chunk.SizeXy * Chunk.SizeXy * maxHeight]); for (var i = 0; i < Chunk.SizeXy; i++) { for (var k = 0; k < Chunk.SizeXy; k++) { for (var j = 0; j <= maxHeight - 1; j++) { var pixel = Noise.CalcPixel3D(i + (chunk.ChunkIndex.X * Chunk.SizeXy), j, k + chunk.ChunkIndex.Y * Chunk.SizeXy, .015f); if (pixel < 128)// && pixel > 16) { chunk.SetVoxel(i, j, k, VoxelDefinition.Sand.Create()); } } chunk.SetVoxel(i, 1, k, VoxelDefinition.Sand.Create()); } } return(chunk); }
private float SampleSimplex(float x, float y, float z) { float[] frequencies = { 0.02f }; float[] amplitudes = { 1, 0.5f, 0.25f }; float point = 0; for (int i = 0; i < frequencies.Length && i < amplitudes.Length; i++) { point += Noise.CalcPixel3D(x * frequencies[i], y * frequencies[i] * terrainMaster.horizontalness, z * frequencies[i]) * amplitudes[i]; } return(point); }
private static void SetBlocks3DNoise(Position globalChunkPos, Chunk chunk) { for (int x = 0; x < CHUNK_SIZE; x++) { for (int y = 0; y < CHUNK_SIZE; y++) { for (int z = 0; z < CHUNK_SIZE; z++) { Position blockPos = globalChunkPos.Add(x, y, z); float noiseValue = Noise.CalcPixel3D(blockPos.X, blockPos.Y, blockPos.Z, 0.015f); chunk.Blocks[x, y, z] = new Block(BlockDefManager.GetBlockDef(noiseValue > 80 ? 2 : 0), blockPos, chunk); } } } }
private float SamplePerlin(float x, float y, float z) { //float fMountain = 0.0008f; //float mountainValue = (Noise.CalcPixel3D(x * fMountain, 0, z * fMountain) + 1) / 2f; // Put in range [0, 1] //mountainValue = mountainValue * mountainValue * mountainValue * mountainValue; ////float fPlains = 0.01f; ////float plainValue = Noise.CalcPixel3D(x * fPlains, 137, z * fPlains); ////float value = (mountainValue * 9 + plainValue) / 10; //float value = mountainValue; ////float heightTreshold = terrainMaster.chunkSize.y / 2 * value; //// Make sure heightTreshold is always at least a "tick" below the last computed point ////heightTreshold = Mathf.Min(heightTreshold, terrainMaster.chunkSize.y / 2 - terrainMaster.resolution.y - 1); //float heightTreshold = value * terrainMaster.maxMountainHeight; //float fMountain = 0.01f; //float mountainValue = (Noise.CalcPixel3D(x * fMountain, 0, z * fMountain) + 1) / 2f; // Put in range [0, 1] //mountainValue = mountainValue * mountainValue * mountainValue * mountainValue; float fMountain2 = 0.008f; float mountainValue2 = Noise.CalcPixel3D(x * fMountain2, 512, z * fMountain2); // Keep in range [-1, 1] mountainValue2 = mountainValue2 * mountainValue2 * mountainValue2; //float heightTreshold = mountainValue * terrainMaster.maxMountainHeight + mountainValue2 * terrainMaster.chunkSize.y; float heightTreshold = mountainValue2 * terrainMaster.chunkSize.y; if (y > heightTreshold) { float t = 1 - Mathf.InverseLerp(heightTreshold, terrainMaster.chunkSize.y, y); return(-(1 - t * t * t * t * t * t * t * t)); } else { float t = Mathf.InverseLerp(0, heightTreshold, y); return(1 - t * t * t * t * t * t * t * t); } }
private float[,,] GetNoise() { float[,,] noise = new float[Settings.ChunkCubicSize.x, Settings.ChunkCubicSize.y, Settings.ChunkCubicSize.z]; Vector3Int offset = Settings.ChunkCubicSize * ChunkCoordinate; for (int i = 0; i < Settings.ChunkCubicSize.x; i++) { for (int j = 0; j < Settings.ChunkCubicSize.y; j++) { for (int k = 0; k < Settings.ChunkCubicSize.z; k++) { Vector3Int noisePixelPosition = new Vector3Int(i + offset.x, j + offset.y, k + offset.z); noise[i, j, k] = _noiseGenerator.CalcPixel3D(noisePixelPosition.x, noisePixelPosition.y, noisePixelPosition.z, Settings.NoiseScale); } } } return(noise); }
private void AddNoiseEffect(Chunk chunk, int x, int z) { int size = Chunk.chunkSize; int stoneHeight = Mathf.FloorToInt(stoneBaseHeight); stoneHeight += Mathf.FloorToInt(Noise.CalcPixel3D(x, 0, z, stoneMountainFrequency, Mathf.FloorToInt(stoneMountainHeight))); if (stoneHeight < stoneMinHeight) { stoneHeight = Mathf.FloorToInt(stoneMinHeight); } stoneHeight += Mathf.FloorToInt(Noise.CalcPixel3D(x, 0, z, stoneBaseNoise, Mathf.FloorToInt(stoneBaseNoiseHeight))); int dirtHeight = stoneHeight + Mathf.FloorToInt(dirtBaseHeight); dirtHeight += Mathf.FloorToInt(Noise.CalcPixel3D(x, 100, z, dirtNoise, Mathf.FloorToInt(dirtNoiseHeight))); for (int y = chunk.ChunkPos.y - 8; y < size + chunk.ChunkPos.y + size; y++) { int caveChance = Mathf.FloorToInt(Noise.CalcPixel3D(x, y, z, caveFrequency, 100)); if (y <= stoneHeight && caveSize < caveChance) { SetBlock(x, y, z, new Block(), chunk); } else if (y <= dirtHeight && caveSize < caveChance) { SetBlock(x, y, z, new Dirt(), chunk); //if (y == dirtHeight && Noise.CalcPixel3D(x, 0, z, treeFrequency, 100) < treeDensity) CreateTree(x, y + 1, z, chunk); } else { SetBlock(x, y, z, new Air(), chunk); } } }
public static float[,,] GetNoise(int seed, int minX, int minY, int minZ, int maxX, int maxY, int maxZ, float scale) { int width = maxX - minX; int height = maxY - minY; int depth = maxZ - minZ; Noise.Seed = seed; float[,,] data = new float[width, height, depth]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { for (int z = 0; z < depth; z++) { data[x, y, z] = Noise.CalcPixel3D(minX + x, minY + y, minZ + z, scale); } } } return(data); }
public void Update(uint x, uint y) { position = new Vector2i(Convert.ToInt32(x), Convert.ToInt32(y)); Random rnd = new Random(); Pixel[] neighbors = new Pixel[] { Up(), UpRight(), UpLeft(), Left(), Right(), DownLeft(), DownRight(), Down() }; if (Material == Sand) { byte max = 0; moisture = 0; foreach (Pixel neighbor in neighbors) { if (neighbor.Material == Water) { moisture = max = 10; } if (neighbor.Material == Sand) { if (neighbor.moisture - 1 > max) { moisture = Convert.ToByte(neighbor.moisture - 1); } } } color.A = Convert.ToByte(255 - moisture * 5); if (y + 1 < Program._Height) { if ((Down(Air, x, y) | (Down(Water, x, y))) & !UpdatedThisFrame) { MoveDown(x, y); if (rnd.Next(0, 2) == 0) { if (x != 0) { Program.field[x, y].MoveLeft(x, y); if (Program.field[x, y].Material == Water) { Program.field[x, y].color = new Color(255, 255, 255); } } } else { if (x + 1 < Program._Width) { Program.field[x, y].MoveRight(x, y); if (Program.field[x, y].Material == Water) { Program.field[x, y].color = new Color(255, 255, 255); } } } } else if ((DownLeft(Air, x, y) | (DownLeft(Water, x, y))) & (DownRight(Air, x, y) | (DownRight(Water, x, y))) & !UpdatedThisFrame) { if (rnd.Next(0, 2) == 0) { MoveDownLeft(x, y); if (Program.field[x, y].Material == Water) { Program.field[x, y].color = new Color(255, 255, 255); } } else { MoveDownRight(x, y); if (Program.field[x, y].Material == Water) { Program.field[x, y].color = new Color(255, 255, 255); } } } else if ((DownLeft(Air, x, y) | (DownLeft(Water, x, y))) & !UpdatedThisFrame) { MoveDownLeft(x, y); if (Program.field[x, y].Material == Water) { Program.field[x, y].color = new Color(255, 255, 255); } } else if ((DownRight(Air, x, y) | (DownRight(Water, x, y))) & !UpdatedThisFrame) { MoveDownRight(x, y); if (Program.field[x, y].Material == Water) { Program.field[x, y].color = new Color(255, 255, 255); } } } } else if (Material == Water) { foreach (Pixel neighbor in neighbors) { if (neighbor.Material == Water) { byte _B = Convert.ToByte(((255 - Pressure * 50) + neighbor.color.B) / 2); byte _RG = Convert.ToByte((neighbor.color.R + color.R) / 2); color = new Color(_RG, _RG, _B); _RG = Convert.ToByte((255 - Math.Abs(Noise.CalcPixel3D(Convert.ToInt32(x), Convert.ToInt32(y), Convert.ToInt32(Program.CurrentTick), 0.05f) - 128) - Pressure * 50 + _RG) / 10); if (color.B != OriginColor.B) { _B = Convert.ToByte(color.B - ((color.B - OriginColor.B) / (Math.Abs(color.B - OriginColor.B)))); } color = new Color(_RG, _RG, _B); //OriginColor = new Color(Convert.ToByte(rnd.Next(200, 255)), Convert.ToByte(rnd.Next(200, 255)), Convert.ToByte(rnd.Next(115, 255))); //if (Up(Air, x, y)) //{ // this.color = Color.White; //} } } UpdatedThisFrame = false; if (y + 1 < Program._Height) { if (Down(Air, x, y) & !UpdatedThisFrame) { MoveDown(x, y); } else if (DownLeft(Air, x, y) & DownRight(Air, x, y) & !UpdatedThisFrame) { int rndVal = rnd.Next(0, 2); //Console.WriteLine(rndVal); if (rndVal == 0) { MoveDownLeft(x, y); direction = dLeft; } else { MoveDownRight(x, y); direction = dRight; } } else if (Right(Air, x, y) & !UpdatedThisFrame & direction == dRight) { MoveRight(x, y); } else if (Left(Air, x, y) & !UpdatedThisFrame & direction == dLeft) { MoveLeft(x, y); } else if (!Right(Air, x, y)) { direction = !dRight; } else if (!Left(Air, x, y)) { direction = !dLeft; } } FluidPhysics(neighbors); //this.color.B = Convert.ToByte(255 - NewPressure); } else if (Material == Dirt) { } if (Material == Generator) { if (y + 1 < Program._Height & Program.Placing) { Program.field[x, y + 1] = new Pixel(Program.mP); } if (y - 1 > 0 & Program.voiding) { Program.field[x, y - 1] = new Pixel(Air); if (x != 0) { Program.field[x - 1, y - 1] = new Pixel(Air); } if (x + 1 < Program._Width) { Program.field[x + 1, y - 1] = new Pixel(Air); } } } UpdatedThisFrame = true; }