void GenerateTerrain() { int halfHeight = mapSize.y / 2; for (int x = 0; x < mapSize.x; x++) { for (int y = 0; y < mapSize.y; y++) { float density = halfHeight - y; density += Noise.CalcPixel1DFractal(x, 0.03f, 3) * 30.0f; float noise = Noise.CalcPixel2DFractal(x, y, 0.01f, 3); if (density > 0) { if (noise <= 0.3) { continue; } float randomLight = Noise.CalcPixel2D(x, y, 0.03f); if (randomLight >= 0.91f) { float randomType = Noise.CalcPixel2D(x, y, 0.01f); SetTile(new int2(x, y), tileInformations[(int)TileUtil.MinMaxNormalization(randomType, 0, 1, 3, 6)].id); } else { SetTile(new int2(x, y), tileInformations[1].id); } } } } }
/// <summary> /// Creates a noise map of layered simplex noise /// </summary> /// <param name="width">The width of the map.</param> /// <param name="height">The height of the map.</param> /// <param name="scale">How big the noise is gonna be scaled.</param> /// <param name="octaves">The amount of noise layers.</param> /// <param name="persistance">The change of impact/ amplitude each noise layer has (higher value = more impact).</param> /// <param name="lacunarity">The change of scale ich noise layer has (higher value = bigger scale).</param> /// <param name="offsetX">The x-offset of the map.</param> /// <param name="offsetY">The y-offset of the map.</param> /// <returns></returns> public static float[,] GenerateMap( int width, int height, int offsetX = 0, int offsetY = 0, int seed = 0, float scale = 1, int octaves = 4, float persistance = 0.5f, float lacunarity = 2f) { var map = new float[width, height]; Noise.Seed = seed; for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { var value = 0F; var frequency = 1F; var amplitude = 1F; var posX = x + offsetX; var posY = y + offsetY; for (var i = 0; i < octaves; ++i) { var noise = Noise.CalcPixel2D(posX, posY, scale * frequency) * amplitude; value += noise; amplitude *= persistance; frequency *= lacunarity; } map[x, y] = value; } } //normalize if (NormalizeValues) { for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { var value = _Normalize(map[x, y], 0, 417) * MaxValue; map[x, y] = value; } } } return(map); }
private static void SetBlocks2DNoise(Position globalChunkPos, Chunk chunk, int baseGlobalPosition) { int raise = globalChunkPos.Y - baseGlobalPosition + 90; float heightSmoother = 0.5f; for (int x = 0; x < CHUNK_SIZE; x++) { for (int z = 0; z < CHUNK_SIZE; z++) { Position blockPos = globalChunkPos.Add(x, 0, z); float noiseValue = Noise.CalcPixel2D(Mathf.Abs(blockPos.X), Mathf.Abs(blockPos.Z), 0.002f) * heightSmoother; for (int y = 0; y < CHUNK_SIZE; y++) { chunk.Blocks[x, y, z] = new Block(BlockDefManager.GetBlockDef(noiseValue > (raise + y) ? 1 : 0), blockPos.Add(0, y, 0), chunk); } } } }
public Map GenerateMap() { Config.PhysicalEnvironment.VoxelTypes.Add(Config.GroundVoxelType); Chunks = new Chunk[Config.MapWidth, Config.MapHeight]; for (uint chunkI = 0; chunkI < Config.MapWidth; chunkI++) { for (uint chunkJ = 0; chunkJ < Config.MapHeight; chunkJ++) { Chunk chunk = Chunks[chunkI, chunkJ] = new Chunk(this, new Vector2f(chunkI * Config.ChunkSize, chunkJ * Config.ChunkSize)); for (uint voxelI = 0; voxelI < Config.ChunkSize; voxelI++) { float height = Noise.CalcPixel1D( (int)((chunkI * Config.ChunkSize) + voxelI), Config.HorizontalNoiseScale) * (Config.VerticalNoiseScale / 128f); for (uint voxelJ = 0; voxelJ < Config.ChunkSize; voxelJ++) { bool isGround = (chunkJ * Config.ChunkSize) + voxelJ > height + Config.GroundLevel; bool isFloating = Noise.CalcPixel2D( (int)((chunkI * Config.ChunkSize) + voxelI), (int)((chunkJ * Config.ChunkSize) + voxelJ), Config.FloatingNoiseScale) / 128f < Config.FloatingNoiseThreshold; chunk[voxelI, voxelJ] = isGround || isFloating ? new Voxel(this, Config.GroundVoxelType) : new Voxel(this); } } } } return(this); }
public void Run() { Random r = new Random(); Noise.Seed = r.Next(0, 255); float gridCount = 256 + 128; float gridWidth = this.width / 2f / gridCount; float gridHeight = this.height / gridCount; uint lineCount = 128 + 64; uint lerpSteps = (uint)gridCount; double viewDistance = 256 + 128; double FOV = viewDistance * 0.5625; double FOVDecrease = FOV / lineCount; double viewDistanceDecrease = viewDistance / lineCount; Vertex[] textureMap = new Vertex[(uint)(gridCount * gridCount * 4)]; VertexBuffer tm = new VertexBuffer((uint)(gridCount * gridCount * 4), PrimitiveType.Quads, VertexBuffer.UsageSpecifier.Stream); double[] heightMap = new double[(int)(gridCount * gridCount)]; Vertex[] quadVertices = new Vertex[(int)(lineCount * lerpSteps * 4f)]; VertexBuffer quadBuffer = new VertexBuffer((uint)(lineCount * lerpSteps * 4f), PrimitiveType.Quads, VertexBuffer.UsageSpecifier.Stream); VertexArray skyBox = new VertexArray(PrimitiveType.Quads, 4); Vertex v = new Vertex(new Vector2f(0, 0), new Color(Color.Black)); v.Color = new Color(70, 161, 236, 255); v.Position = new Vector2f(0f, 0f); skyBox[0] = v; v.Position = new Vector2f(this.width / 2f, 0f); skyBox[1] = v; v.Color = new Color(239, 195, 128, 255); v.Position = new Vector2f(this.width / 2f, this.height); skyBox[2] = v; v.Position = new Vector2f(0f, this.height); skyBox[3] = v; VertexArray lineVertices = new VertexArray(PrimitiveType.Lines, lineCount * 2); // 2D HEIGHT AND COLOR MAP uint terrainIndex = 0; uint heightIndex = 0; for (float i = 0; i < gridCount; i += 1) { for (float j = 0; j < gridCount; j += 1) { v.Position.X = j * gridWidth; v.Position.Y = i * gridHeight; double heightMapH = Noise.CalcPixel2D((int)v.Position.X, (int)v.Position.Y, 0.007f); if (heightMapH < 64) { if (heightMapH < 24) { v.Color = this.HSLToRGB(215.0, this.Lerp(heightMapH, 0f, 24, 0.55, 1), this.Lerp(heightMapH, 0f, 24, 0.55, 0.45), 255); } else { v.Color = this.HSLToRGB(215.0, 1.0, this.Lerp(heightMapH, 0.0, 64.0, 0.55, 0.7), 255); } heightMapH = 64; } else if (heightMapH < 96) { v.Color = this.HSLToRGB(50.0, 1.0, this.Lerp(heightMapH, 64.0, 86.0, 0.64, 0.80), 255); } else { v.Color = this.HSLToRGB(this.Lerp(heightMapH, 96f, 255f, 115f, 130), this.Lerp(heightMapH, 86.0, 220.0, 1.0, 0.40), this.Lerp(heightMapH, 86.0, 220.0, 0.6, 0.25), 255); heightMapH += 20 * r.NextDouble(); if (r.NextDouble() < 0.1) { v.Color = this.HSLToRGB(this.Lerp(heightMapH, 96f, 255f, 0f, 355f), 0.7, 0.57, 255); heightMapH += 20 * r.NextDouble(); } } heightMap[heightIndex++] = heightMapH; textureMap[terrainIndex++] = v; v.Position.X += gridWidth; textureMap[terrainIndex++] = v; v.Position.Y += gridHeight; textureMap[terrainIndex++] = v; v.Position.X -= gridWidth; textureMap[terrainIndex++] = v; } } Shader shader = new Shader(null, null, "C:\\Users\\Alfred\\RiderProjects\\VoxelSpace\\VoxelSpace\\fra.frag"); Shader shader2 = new Shader("C:\\Users\\Alfred\\RiderProjects\\VoxelSpace\\VoxelSpace\\ver.vert", null, "C:\\Users\\Alfred\\RiderProjects\\VoxelSpace\\VoxelSpace\\fra2.frag"); RenderStates renderState = new RenderStates(shader); renderState.BlendMode = BlendMode.Alpha; RenderStates renderState2 = new RenderStates(shader2); renderState2.BlendMode = BlendMode.Alpha; shader.SetUniform("u_resolution", new Vector2f(this.width, this.height)); shader2.SetUniform("u_resolution", new Vector2f(this.width, this.height)); float angle = 0.0f; float counter = 0f; float targetHeight = 0f; float playerHeight = 0f; short[] ringBuffer = new short[(int)(0.3f * this.music.SampleRate) * this.music.ChannelCount]; int ringBufferIndex = 0; //this.song.Play(); this.ambience.Play(); while (this.window.IsOpen) { this.deltaTime = this.clock.Restart().AsSeconds(); this.deltaTimeCounter += this.deltaTime; shader.SetUniform("u_time", this.deltaTimeCounter / 4f); shader2.SetUniform("u_time", counter / 32); counter++; float abc = this.music.SampleRate * this.deltaTime * this.music.ChannelCount; float modu2 = this.song.PlayingOffset.AsSeconds() * this.music.SampleRate * this.music.ChannelCount; for (int i = 0; i < abc; i++) { int modu = this.Mod(ringBufferIndex, ringBuffer.Length - 1); short value = this.copyBuffer[this.Mod((int)(modu2 + i), this.copyBuffer.Length - 1)]; ringBuffer[modu] = value; ringBufferIndex++; } float rms = this.AudioRMS(ringBuffer) * 2; //Console.WriteLine(1f / this.deltaTime); this.window.DispatchEvents(); this.window.Clear(Color.Black); this.window.SetView(this.view2D); tm.Update(textureMap); this.window.Draw(tm); // 2D SHIP AND FOV LINES this.shipPos += this.shipVec * (float)this.shipVel; this.shipShape.Position = this.shipPos; this.window.Draw(this.shipShape); double f = FOV; double vd = viewDistance; v.Color = Color.White; for (uint i = 0; i < lineCount * 2; i += 2) { v.Position = this.shipPos + this.shipVec * (float)vd + this.NormalDirection(this.shipVec) * (float)-f; lineVertices[i] = v; v.Position = this.shipPos + this.shipVec * (float)vd + this.NormalDirection(this.shipVec) * (float)f; lineVertices[i + 1] = v; vd -= viewDistanceDecrease; f -= FOVDecrease; } this.window.Draw(lineVertices); // 3D SKY BOX this.window.SetView(this.view3D); this.window.Draw(skyBox, renderState); // 3D VOXEL SPACE double sx = Math.Floor(this.shipPos.X / gridWidth - 1); double sy = Math.Floor(this.shipPos.Y / gridHeight - 1); if (sx >= 0 && sx < gridCount && sy >= 0 && sy < gridCount) { targetHeight = (float)heightMap[(int)(sy * gridCount + sx)]; } else { targetHeight = 64; } playerHeight += (targetHeight - playerHeight) / 10; shader2.SetUniform("player_height", playerHeight); uint horizonHeight = (uint)(this.height / 3f); float alphaFadeIn = 255f / lineCount * 4; uint lineIndex = 0; uint voxelIndex = 0; for (int i = 0; i < lineCount; i++) { Vector2f lineStart = lineVertices[lineIndex++].Position; Vector2f lineEnd = lineVertices[lineIndex++].Position; for (int j = 0; j < lerpSteps; j++) { int xCoord = (int)(Math.Floor(this.Lerp(j, 0, lerpSteps, lineStart.X, lineEnd.X) / gridWidth) - 1); int yCoord = (int)(Math.Floor(this.Lerp(j, 0, lerpSteps, lineStart.Y, lineEnd.Y) / gridHeight) - 1); if (xCoord >= 0 && xCoord < gridCount && yCoord >= 0 && yCoord < gridCount) { double heightM = heightMap[(int)(yCoord * gridCount + xCoord)]; Color color = textureMap[(uint)(yCoord * gridCount + xCoord) * 4].Color; color.A = (byte)Math.Clamp(i * alphaFadeIn, 0, 255); v.Position.X = j * (this.width / 2 / lerpSteps); v.Position.Y = (float)this.Lerp(heightM, 0, 255, this.height, horizonHeight) + playerHeight; v.Color = color; quadVertices[voxelIndex++] = v; v.Position.X += this.width / 2f / lerpSteps; quadVertices[voxelIndex++] = v; v.Position.Y = this.height; quadVertices[voxelIndex++] = v; v.Position.X -= this.width / 2f / lerpSteps; quadVertices[voxelIndex++] = v; } else { v.Color = Color.Transparent; quadVertices[voxelIndex++] = v; v.Position.X += this.width / 2f / lerpSteps; quadVertices[voxelIndex++] = v; v.Position.Y = this.height; quadVertices[voxelIndex++] = v; v.Position.X -= this.width / 2f / lerpSteps; quadVertices[voxelIndex++] = v; } } } shader2.SetUniform("u_audio_average", rms); quadBuffer.Update(quadVertices); this.window.Draw(quadBuffer, renderState2); this.window.Display(); if (this.turnLeft) { this.shipVec = this.RotatePoint(this.shipVec, 0.05f); this.NormalizeVector(ref this.shipVec); angle -= 0.01f; shader.SetUniform("player_angle", angle); } else if (this.turnRight) { this.shipVec = this.RotatePoint(this.shipVec, -0.05f); this.NormalizeVector(ref this.shipVec); angle += 0.01f; shader.SetUniform("player_angle", angle); } } }
public Pixel(byte material) { Material = material; if (material == Generator) { color = Color.Magenta; } if (material == Air) { color = Color.Transparent; //color = new Color(Convert.ToByte(Math.Min(Math.Clamp(Program._uy, 0, Program._Height), 255)), Convert.ToByte(Math.Min(Math.Clamp(Program._uy, Program._Height / 2, Program._Height), 255)), 255); } if (material == Sand) { //this.color = Color.Yellow; Random rnd = new Random(); byte _RG = Convert.ToByte(rnd.Next(200, 255)); color = new Color(_RG, _RG, Convert.ToByte(rnd.Next(100, _RG))); OriginColor = color; } if (Material == Water) { Random rnd = new Random(); byte _RG = Convert.ToByte(rnd.Next(0, 100)); color = new Color(_RG, _RG, Convert.ToByte(rnd.Next(115, 255))); Pressure = 1; OriginColor = color; } if (Material == Dirt) { uint x = Program._ux; uint y = Program._uy; int xInt = Convert.ToInt32(x + Program.xOffset); int yInt = Convert.ToInt32(y - Program.yOffset); byte _RGB = Convert.ToByte(Math.Abs(128 - Noise.CalcPixel2D(xInt, yInt, 0.2f))); if (Convert.ToByte(Math.Abs(128 - Noise.CalcPixel2D(xInt, yInt, 0.1f))) > 100) { _RGB += Convert.ToByte(Math.Abs(128 - Noise.CalcPixel2D(xInt, yInt, 0.1f))); } if (Convert.ToByte(Math.Abs(128 - Noise.CalcPixel2D(xInt, yInt, 0.01f))) > 100) { _RGB += Convert.ToByte(Math.Abs(128 - Noise.CalcPixel2D(xInt, yInt, 0.01f))); } byte _R = Convert.ToByte((_RGB + 60) / 2); byte _G = Convert.ToByte((_RGB + 35) / 2); byte _B = Convert.ToByte((_RGB + 3) / 2); color = new Color(_R, _G, _B); if (Up(Air, x, y)) { _RGB = Convert.ToByte(Math.Abs(128 - Noise.CalcPixel2D(xInt, yInt, 0.2f))); color = new Color(0, _RGB, 0); } if (Up(Water, x, y)) { Program.field[x, y] = new Pixel(Sand); } if (Up().Up(Water, x, y)) { Program.field[x, y] = new Pixel(Sand); } } }
public World CreateWorld() { m_world = new World(settingsScriptableObject); m_layout = m_world.layout; m_width = m_world.size.x; m_height = m_world.size.y; m_seed = 123; SimplexNoise.Noise.Seed = 209323094; m_rand = new Unity.Mathematics.Random(m_seed); m_state = GenState.PRE_GEN; StartCoroutine(GenerateRoutine()); //if (File.Exists("test.txt")) //File.Delete("test.txt"); //StreamWriter writer = File.AppendText("test.txt"); for (int r = 0; r <= m_height; r++) // height { //writer.WriteLine(); int r_offset = Mathf.FloorToInt(r / 2f); for (int q = -r_offset; q <= m_width - r_offset; q++) // width with offset { Hex hex = new Hex(q, r, -q - r); var mapKey = hex.GetKey(); //writer.Write(mapKey.ToString() + "|"); Point pixel = m_layout.HexToPixel(hex); if (!m_world.TileData.ContainsKey(mapKey)) { GameObject gameobject = Instantiate(prefab, new Vector3((float)pixel.x, (float)pixel.y, 0), Quaternion.identity); TileObject tObj = gameobject.GetComponent <TileObject>(); m_world.TileData.Add(mapKey, tObj); HexData hData = tObj.hexData; float d = Noise.CalcPixel2D(q, r, .1f); if (d < 55) { hData.isOcean = true; } tObj.hex = hex; hData.age = Random.Range(0, 50); hData.height = d; float distFromEquator = (float)m_world.Equator - Mathf.Abs((float)m_world.Equator - (float)r); float tempFromDist = m_world.settings.poleTemp + (m_world.settings.equatorTemp - m_world.settings.poleTemp) * (distFromEquator / m_world.Equator); hData.temp = tempFromDist + Random.Range(0, m_world.size.y * .25f + 1); hData.wetness = Random.Range(0f, 255f); Tile tile = tObj.FindCorrectTile(); tObj.SetTile(tile); } } } m_world.windManager.Init(); //writer.Close(); m_state = GenState.GENERATE; Generate(); return(m_world); }