public ChunkRenderer(GraphicsDevice device, ContentManager content, Technique optimization) { Device = device; Content = content; Optimization = optimization; Chunks = new XChunk[SIZE * SIZE]; TextureAtlas = new Dictionary <string, Vector2>(); // TODO: AUTOMATED TEXTURE_RATIO_X = 63.85f / 256.0f; TEXTURE_RATIO_Y = 63.85f / 256.0f; TextureAtlas.Add("Top_Grass", new Vector2(1, 0)); TextureAtlas.Add("Side_Dirt", new Vector2(2, 0)); textureAtlas = Content.Load <Texture2D>("atlas"); internalEffect = new BasicEffect(Device) { Texture = textureAtlas, TextureEnabled = true }; }
public void expandWorld(Direction direction) { switch (direction) { case Direction.Forward: GlobalTranslation += new Vector3(0, 0, XChunk.Depth); Vector3 localVector = Vector3.Zero; for (int x = 0; x < SIZE; x++) { localVector.X = x * XChunk.Width; localVector.Z = (SIZE - 1) * XChunk.Depth; float[] heightMap = simplexNoise.GetNoiseMap2D((int)localVector.X + (int)GlobalTranslation.X, (int)localVector.Z + (int)GlobalTranslation.Z, XChunk.Width, XChunk.Depth); Chunks[x + 0 * SIZE].ReGenerate(localVector, GlobalTranslation); Chunks[x + 0 * SIZE].HeightMap = heightMap; XChunk currentChunk = Chunks[x + 0 * SIZE]; for (int z = 0; z < SIZE - 1; z++) { XChunk toChange = Chunks[x + (z + 1) * SIZE]; int oldId = toChange.ChunkId; toChange.ChunkId = currentChunk.ChunkId; currentChunk.ChunkId = oldId; toChange.LocalPosition -= new Vector3(0, 0, XChunk.Depth); Chunks[x + (z + 1) * SIZE] = currentChunk; Chunks[x + z * SIZE] = toChange; } } for (int i = 0; i < SIZE; i++) { Chunks[i + zAxis * SIZE].ReCreate = true; Chunks[i + (SIZE - 1) * SIZE].Generate(); } if (zAxis + 1 >= SIZE) { zAxis = 0; } zAxis++; XChunk.Flush(); break; } }
public void Start() { XChunk.Width = 16; XChunk.Depth = 16; XChunk.Height = 256; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { var ch = new XChunk(this, new Vector3(i * 16, 0, j * 16), new Vector3(0)); var hm = simplexNoise.GetNoiseMap2D(i * 16, j * 16, 16, 16); ch.HeightMap = hm; Chunks[i + j * SIZE] = ch; } } Task.Run(() => { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < Chunks.Length; i++) { Chunks[i].Generate(); } for (int i = 0; i < Chunks.Length; i++) { Chunks[i].CalculateOffset(); } object raw = File.ReadAllBytes(@"data\chunks\indices.byte").DeserializeToDynamicType(); int[] indices = (int[])raw; indexBuffer = new IndexBuffer(Device, IndexElementSize.ThirtyTwoBits, indices.Length, BufferUsage.WriteOnly); indexBuffer.SetData <int>(indices); indices = new int[0]; vertexBuffer = new VertexBuffer(Device, typeof(VertexPositionTexture), XChunk.TILE_SIZE * 10, BufferUsage.WriteOnly); buildBuffers(); sw.Stop(); Console.WriteLine("DONE IN " + sw.Elapsed.TotalSeconds + " SECONDS!"); IsReady = true; }); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } chunkRenderer.Update(gameTime); if (!chunkRenderer.IsReady) { return; } if (IsActive) { Camera.Update(); } if (Keyboard.GetState().IsKeyDown(Keys.W)) { Camera.Move(new Vector3(0, 0, -1)); } if (Keyboard.GetState().IsKeyDown(Keys.A)) { Camera.Move(new Vector3(-1, 0, 0)); } if (Keyboard.GetState().IsKeyDown(Keys.S)) { Camera.Move(new Vector3(0, 0, 1)); } if (Keyboard.GetState().IsKeyDown(Keys.D)) { Camera.Move(new Vector3(1, 0, 0)); } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { Camera.Move(new Vector3(0, 1, 0)); } if (Keyboard.GetState().IsKeyDown(Keys.LeftControl)) { Camera.Move(new Vector3(0, -1, 0)); } if (Keyboard.GetState().IsKeyDown(Keys.Up)) { Camera.Velocity *= 1.05f; } if (Keyboard.GetState().IsKeyDown(Keys.Down)) { Camera.Velocity /= 1.05f; } if (!pressed && Keyboard.GetState().IsKeyDown(Keys.Y)) { pressed = true; chunkRenderer.expandWorld(ChunkRenderer.Direction.Forward); } if (Keyboard.GetState().IsKeyUp(Keys.Y)) { pressed = false; } for (int i = 0; i < chunkRenderer.Chunks.Length; i++) { if (chunkRenderer.Chunks[i].ChunkBoundingBox.Contains(Camera.CameraPosition) == ContainmentType.Contains) { currentChunk = chunkRenderer.Chunks[i]; break; } } base.Update(gameTime); }