public static void DiscardUnneededChunks() { /* Providing chunk data is expensive; there's no need to * do it if the specified chunk isn't actually going to be * rendered onto the screen. */ if (FilteredFeatures.IsEnabled(Feature.OptimizeChunkProviding)) { int discarded = 0; foreach (ProvideTask rt in m_Tasks.ToArray()) { if (!ChunkRenderer.HasNeeded(rt.Chunk)) { m_Skip.Add(rt); discarded++; } } if (discarded > 0) { FilteredConsole.WriteLine(FilterCategory.Optimization, "SKIPPED PROVIDING " + discarded + " UNNEEDED CHUNKS!"); discarded = 0; } } }
protected override void DrawTilesAbove(GameContext context) { // Draw the current rendering buffers. if (FilteredFeatures.IsEnabled(Feature.IsometricOcclusion)) { if (this.m_OccludingSpriteBatch.DepthTexture != RenderingBuffers.DepthBuffer && RenderingBuffers.DepthBuffer != null) { this.m_OccludingSpriteBatch.DepthTexture = RenderingBuffers.DepthBuffer; } if (RenderingBuffers.ScreenBuffer != null && FilteredFeatures.IsEnabled(Feature.RenderWorld)) { this.m_OccludingSpriteBatch.DrawOccluding(RenderingBuffers.ScreenBuffer, Vector2.Zero, Color.White); } this.m_OccludingSpriteBatch.End(); } else { if (FilteredFeatures.IsEnabled(Feature.RenderWorld)) { if (FilteredFeatures.IsEnabled(Feature.RenderingBuffers)) { context.SpriteBatch.Draw(RenderingBuffers.ScreenBuffer, Vector2.Zero, Color.White); } } //context.SpriteBatch.End(); } }
public Chunk Get(long x, long y, long z) { Chunk c = this.m_Octree.Find(x / 256, y / 256, z / 256); if (c != null && (c.X != x || c.Y != y || c.Z != z) && FilteredFeatures.IsEnabled(Feature.DebugOctreeValidation)) { throw new InvalidOperationException("Octree lookup result returned chunk with different position than originally stored!"); } return(c); }
public void Set(Chunk chunk) { this.m_Octree.Insert(chunk, chunk.X / 256, chunk.Y / 256, chunk.Z / 256); if (FilteredFeatures.IsEnabled(Feature.DebugOctreeValidation)) { if (this.m_Octree.Find(chunk.X / 256, chunk.Y / 256, chunk.Z / 256) != chunk) { throw new InvalidOperationException("Octree did not store data correctly for " + chunk.X / 256 + ", " + chunk.Y / 256 + ", " + chunk.Z / 256 + "."); } } }
public static void DiscardUnusedChunks() { /* If the chunk wasn't in the last used list, we no longer care * to render it to a texture, so discard from there. */ if (FilteredFeatures.IsEnabled(Feature.OptimizeChunkRendering)) { int discarded = 0; foreach (RenderTask rt in m_Tasks.ToArray()) { if (!m_NeededChunks.Contains(rt.Chunk)) { m_Tasks.Remove(rt); discarded++; } } if (discarded > 0) { FilteredConsole.WriteLine(FilterCategory.Optimization, "SKIPPED RENDERING " + discarded + " UNNEEDED CHUNKS!"); discarded = 0; } } /* We can't keep every chunk's texture loaded into memory or * else we quickly run out of graphics RAM to store everything. */ if (FilteredFeatures.IsEnabled(Feature.DiscardChunkTextures)) { int discarded = 0; while (m_LoadedChunks.Count > LastRenderedCountOnScreen + m_LastRenderedBuffer) { m_LoadedChunks[0].DiscardTexture(); m_LoadedChunks.RemoveAt(0); discarded++; } if (discarded > 0) { FilteredConsole.WriteLine(FilterCategory.Optimization, "DISCARDED " + discarded + " TEXTURES FROM MEMORY!"); } } }
protected override void PreBegin(GameContext context) { // Process a single texture block if the FPS is higher than 30. //if (context.GameTime.ElapsedGameTime.Milliseconds < 100) //{ ChunkProvider.ProcessSingle(); ChunkRenderer.ProcessSingle(context.GameTime, context); //} // Ensure we have an occluding sprite batch. if (FilteredFeatures.IsEnabled(Feature.IsometricOcclusion)) { if (this.m_OccludingSpriteBatch == null) { this.m_OccludingSpriteBatch = new OccludingSpriteBatch(context.Graphics.GraphicsDevice); } this.m_OccludingSpriteBatch.Begin(true); } else { context.Graphics.GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.Black, 1f, 0); } }
public override bool Update(GameContext context) { // TODO: Make use of this. //MouseState mouse = Mouse.GetState(); // Go back to title screen if needed. if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { (this.Game as RuntimeGame).SwitchWorld(new TitleWorld()); return(false); } // Update player and refocus screen. KeyboardState state = Keyboard.GetState(); GamePadState gpstate = GamePad.GetState(PlayerIndex.One); float mv = (float)Math.Sqrt(this.m_Player.MovementSpeed); if (state.IsKeyDown(Keys.W)) { this.m_Player.Y -= mv; this.m_Player.X -= mv; } if (state.IsKeyDown(Keys.S) || FilteredFeatures.IsEnabled(Feature.DebugMovement)) { this.m_Player.Y += mv; this.m_Player.X += mv; } if (state.IsKeyDown(Keys.A)) { this.m_Player.Y += mv; this.m_Player.X -= mv; } if (state.IsKeyDown(Keys.D)) { this.m_Player.Y -= mv; this.m_Player.X += mv; } if (state.IsKeyDown(Keys.I)) { this.m_Player.Z += 4f; } if (state.IsKeyDown(Keys.K)) { this.m_Player.Z -= 4f; } Vector2 v = new Vector2( gpstate.ThumbSticks.Left.X, -gpstate.ThumbSticks.Left.Y ); Matrix m = Matrix.CreateRotationZ(MathHelper.ToRadians(-45)); v = Vector2.Transform(v, m); this.m_Player.X += v.X * mv * (float)(Math.Sqrt(2) / 1.0); this.m_Player.Y += v.Y * mv * (float)(Math.Sqrt(2) / 1.0); //this.m_Player.Z = this.GetSurfaceZ(context, this.m_Player.X, this.m_Player.Y) * Scale.CUBE_Z; (context.WorldManager as IsometricWorldManager).Focus(this.m_Player.X, this.m_Player.Y, this.m_Player.Z); // Update autosave counter. this.m_AutoSave++; if (this.m_AutoSave >= AUTOSAVE_LIMIT) { this.m_AutoSave = 0; if (this.m_DiskLevel != null) { this.m_DiskLevel.Save(); } } return(true); // Update entities. }
private static void RenderTilesToTexture(RenderTask task, GameTime gt, GameContext context) { /* Our world is laid out in memory in terms of X / Y, but * we are rendering isometric, which means that the rendering * order for tiles must be like so: * * North * 1 3 5 9 13 19 25 * 2 6 10 14 20 26 32 * 4 8 15 21 27 33 37 * East 7 12 18 28 34 38 42 West * 11 17 24 31 39 43 45 * 16 23 30 36 41 46 48 * 22 29 35 40 44 47 49 * South * * We also need to account for situations where the user rotates * the isometric view. */ /* * North * 0 0.5 1 1.5 2 2.5 3 * -0.5 0 0.5 1 1.5 2 2.5 * -1 -0.5 0 0.5 1 1.5 2 * East -1.5 -1 -0.5 0 0.5 1 1.5 West * -2 -1.5 -1 -0.5 0 0.5 1 * -2.5 -2 -1.5 -1 -0.5 0 0.5 * -3 -2.5 -2 -1.5 -1 -0.5 0 * South * * v = (x - y) / 2.0 */ int DEBUG_ZOFFSET = 0;//TileIsometricifier.TILE_CUBE_HEIGHT * Settings.ChunkDepth - 200; if (m_CurrentRenderState == null) { RenderState rs = new RenderState(); rs.ZTop = Chunk.Depth; rs.ZBottom = 0; rs.ChunkTarget = RenderTargetFactory.Create( m_GraphicsDevice, TileIsometricifier.TILE_TOP_WIDTH * Chunk.Width, TileIsometricifier.TILE_TOP_HEIGHT * Chunk.Width + TileIsometricifier.TILE_CUBE_HEIGHT * Chunk.Depth + TileIsometricifier.CHUNK_HEIGHT_ALLOWANCE, true, SurfaceFormat.Bgra5551, DepthFormat.None); rs.ChunkDepthMap = RenderTargetFactory.Create( m_GraphicsDevice, TileIsometricifier.TILE_TOP_WIDTH * Chunk.Width, TileIsometricifier.TILE_TOP_HEIGHT * Chunk.Width + TileIsometricifier.TILE_CUBE_HEIGHT * Chunk.Depth + TileIsometricifier.CHUNK_HEIGHT_ALLOWANCE, true, SurfaceFormat.Bgra5551, DepthFormat.None); FilteredConsole.WriteLine(FilterCategory.GraphicsMemoryUsage, "Allocated textures for chunk " + task.Chunk.X + ", " + task.Chunk.Y + ", " + task.Chunk.Z + "."); m_GraphicsDevice.SetRenderTarget(rs.ChunkTarget); if (FilteredFeatures.IsEnabled(Feature.DebugChunkBackground)) { Color c = new Color( (float)m_DebugRandomizer.NextDouble(), (float)m_DebugRandomizer.NextDouble(), (float)m_DebugRandomizer.NextDouble() ); m_GraphicsDevice.Clear(ClearOptions.Target, c, 1.0f, 0); } else { m_GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1.0f, 0); } rs.ChunkDepthMapCleared = false; rs.SpriteBatch = new SpriteBatch(m_GraphicsDevice); rs.CurrentZ = rs.ZBottom; rs.RenderTask = task; rs.CellRenderOrder = GetCellRenderOrder(RenderToNE); rs.RenderMode = 0; m_CurrentRenderState = rs; } if (m_CurrentRenderState.RenderMode == 0 /* chunk texture */) { m_GraphicsDevice.SetRenderTarget(m_CurrentRenderState.ChunkTarget); m_CurrentRenderState.SpriteBatch.Begin(SpriteSortMode.Immediate, null); int count = 0; int zcount = 0; while (m_CurrentRenderState.CurrentZ < m_CurrentRenderState.ZTop && gt.ElapsedGameTime.TotalMilliseconds < Performance.RENDERING_MILLISECONDS) { int z = m_CurrentRenderState.CurrentZ; int rcx = TileIsometricifier.TILE_TOP_WIDTH * Chunk.Width / 2 - TileIsometricifier.TILE_TOP_WIDTH / 2; int rcy = TileIsometricifier.TILE_CUBE_HEIGHT * Chunk.Depth + TileIsometricifier.TILE_TOP_HEIGHT * Chunk.Width / 2 - DEBUG_ZOFFSET; int rw = TileIsometricifier.TILE_TOP_WIDTH; int rh = TileIsometricifier.TILE_TOP_HEIGHT / 2; for (int i = 0; i < m_CurrentRenderState.CellRenderOrder.Length; i++) { // Calculate the X / Y of the tile in the grid. int x = m_CurrentRenderState.CellRenderOrder[i] % RenderWidth; int y = m_CurrentRenderState.CellRenderOrder[i] / RenderWidth; // Calculate the render position on screen. int rx = rcx + (int)((x - y) / 2.0 * rw); int ry = rcy + (x + y) * rh - (rh / 2 * (RenderWidth + RenderHeight)) - (z * TileIsometricifier.TILE_CUBE_HEIGHT); Block b = task.Chunk.m_Blocks[x, y, z]; if (b == null) { if (FilteredFeatures.IsEnabled(Feature.DebugChunkTiles)) { m_CurrentRenderState.SpriteBatch.Draw( task.Textures["tiles.grass"], new Vector2(rx, ry), Color.White ); } continue; } Tile t = b.Tile; if (t.Image == null) { if (FilteredFeatures.IsEnabled(Feature.DebugChunkTiles)) { m_CurrentRenderState.SpriteBatch.Draw( task.Textures["tiles.dirt"], new Vector2(rx, ry), Color.White ); } continue; } Color col = new Color(1f, 1f, 1f, 1f).ToPremultiplied(); if (task.Chunk.X == 0 && task.Chunk.Y == 0 && x == 0 && y == 0) { col = new Color(1f, 0f, 0f, 1f).ToPremultiplied(); } if (FilteredFeatures.IsEnabled(Feature.DebugChunkTiles)) { m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image], new Vector2(rx, ry), col ); } if (FilteredFeatures.IsEnabled(Feature.RenderCellTops)) { m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image + ".isometric.top"], new Rectangle(rx, ry, TileIsometricifier.TILE_TOP_WIDTH, TileIsometricifier.TILE_TOP_HEIGHT), null, col, 0, new Vector2(0, 0), SpriteEffects.None, 0 // TODO: Use this to correct rendering artifacts. ); } if (FilteredFeatures.IsEnabled(Feature.RenderCellSides)) { m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image + ".isometric.sideL"], new Rectangle(rx, ry + TileIsometricifier.TILE_TOP_HEIGHT / 2, TileIsometricifier.TILE_SIDE_WIDTH, TileIsometricifier.TILE_SIDE_HEIGHT), null, col, 0, new Vector2(0, 0), SpriteEffects.None, 0 // TODO: Use this to correct rendering artifacts. ); m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image + ".isometric.sideR"], new Rectangle(rx + TileIsometricifier.TILE_TOP_WIDTH / 2, ry + TileIsometricifier.TILE_TOP_HEIGHT / 2, TileIsometricifier.TILE_SIDE_WIDTH, TileIsometricifier.TILE_SIDE_HEIGHT), null, col, 0, new Vector2(0, 0), SpriteEffects.None, 0 // TODO: Use this to correct rendering artifacts. ); } count++; } m_CurrentRenderState.CurrentZ++; zcount++; } FilteredConsole.WriteLine(FilterCategory.OptimizationTiming, "Rendered " + zcount + " levels, " + count + " cells to texture target in " + gt.ElapsedGameTime.Milliseconds + "ms."); m_CurrentRenderState.SpriteBatch.End(); m_GraphicsDevice.SetRenderTarget(null); } else if (m_CurrentRenderState.RenderMode == 1 /* depth map */ && FilteredFeatures.IsEnabled(Feature.IsometricOcclusion)) { m_GraphicsDevice.SetRenderTarget(m_CurrentRenderState.ChunkDepthMap); if (!m_CurrentRenderState.ChunkDepthMapCleared) { m_CurrentRenderState.SpriteBatch.Begin(SpriteSortMode.Immediate, null); m_GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1.0f, 0); m_CurrentRenderState.SpriteBatch.End(); m_CurrentRenderState.ChunkDepthMapCleared = true; } BlendState bs = new BlendState(); bs.AlphaBlendFunction = BlendFunction.Max; bs.AlphaSourceBlend = Blend.One; bs.AlphaDestinationBlend = Blend.One; bs.ColorBlendFunction = BlendFunction.Max; bs.ColorSourceBlend = Blend.One; bs.ColorDestinationBlend = Blend.One; m_CurrentRenderState.SpriteBatch.Begin(SpriteSortMode.Immediate, bs, null, null, null, context.Effects["IsometricDepthMap"]); context.Effects["IsometricDepthMap"].Parameters["RotationMode"].SetValue(RenderToNE); context.Effects["IsometricDepthMap"].CurrentTechnique.Passes[0].Apply(); int count = 0; int zcount = 0; while (m_CurrentRenderState.CurrentZ < m_CurrentRenderState.ZTop && gt.ElapsedGameTime.TotalMilliseconds < Performance.RENDERING_MILLISECONDS) { int z = m_CurrentRenderState.CurrentZ; int rcx = TileIsometricifier.TILE_TOP_WIDTH * Chunk.Width / 2 - TileIsometricifier.TILE_TOP_WIDTH / 2; int rcy = TileIsometricifier.TILE_CUBE_HEIGHT * Chunk.Depth + TileIsometricifier.TILE_TOP_HEIGHT * Chunk.Width / 2 - DEBUG_ZOFFSET; int rw = TileIsometricifier.TILE_TOP_WIDTH; int rh = TileIsometricifier.TILE_TOP_HEIGHT / 2; for (int i = 0; i < m_CurrentRenderState.CellRenderOrder.Length; i++) { // Calculate the X / Y of the tile in the grid. int x = m_CurrentRenderState.CellRenderOrder[i] % RenderWidth; int y = m_CurrentRenderState.CellRenderOrder[i] / RenderWidth; // Calculate the "depth" of the tile. int depth = x + y + z; // Calculate the render position on screen. int rx = rcx + (int)((x - y) / 2.0 * rw); int ry = rcy + (x + y) * rh - (rh / 2 * (RenderWidth + RenderHeight)) - (z * TileIsometricifier.TILE_CUBE_HEIGHT); Block b = task.Chunk.m_Blocks[x, y, z]; if (b == null) { continue; } Tile t = b.Tile; if (t.Image == null) { continue; } context.Effects["IsometricDepthMap"].Parameters["CellDepth"].SetValue((float)Math.Min(depth / 255f, 1.0f)); context.Effects["IsometricDepthMap"].CurrentTechnique.Passes[0].Apply(); m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image + ".isometric.top"], new Rectangle(rx, ry, TileIsometricifier.TILE_TOP_WIDTH, TileIsometricifier.TILE_TOP_HEIGHT), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0 ); m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image + ".isometric.sideL"], new Rectangle(rx, ry + 12, TileIsometricifier.TILE_SIDE_WIDTH, TileIsometricifier.TILE_SIDE_HEIGHT), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0 ); m_CurrentRenderState.SpriteBatch.Draw( task.Textures[t.Image + ".isometric.sideR"], new Rectangle(rx + 16, ry + 12, TileIsometricifier.TILE_SIDE_WIDTH, TileIsometricifier.TILE_SIDE_HEIGHT), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0 ); count++; } m_CurrentRenderState.CurrentZ++; zcount++; } FilteredConsole.WriteLine(FilterCategory.OptimizationTiming, "Rendered " + zcount + " levels, " + count + " cells to texture target in " + gt.ElapsedGameTime.TotalMilliseconds + "ms."); m_CurrentRenderState.SpriteBatch.End(); m_GraphicsDevice.SetRenderTarget(null); } if (m_CurrentRenderState.CurrentZ == m_CurrentRenderState.ZTop) { if (m_CurrentRenderState.RenderMode < 1) { m_CurrentRenderState.CurrentZ = m_CurrentRenderState.ZBottom; m_CurrentRenderState.RenderMode++; } else { m_CurrentRenderState.RenderTask.Result = m_CurrentRenderState.ChunkTarget; m_CurrentRenderState.RenderTask.DepthMap = m_CurrentRenderState.ChunkDepthMap; m_CurrentRenderState.RenderTask.HasResult = true; m_CurrentRenderState = null; } } }
protected override void DrawTilesBelow(GameContext context) { // Ensure we have a chunk manager to source chunks from. if (!(context.World is RPGWorld)) { return; } this.Octree = (context.World as RPGWorld).ChunkOctree; if (this.Octree == null) { return; } if (this.Chunk == null) { this.Chunk = this.Octree.Get(0, 0, 0); } // Determine our Z offset. int zoffset = -(Chunk.Depth - this.ZLevel) * TileIsometricifier.TILE_CUBE_HEIGHT; // Get rendering information. ChunkRenderer.ResetNeeded(); IEnumerable <RelativeRenderInformation> renders = this.GetRelativeRenderInformation(context, this.Chunk); ChunkRenderer.LastRenderedCountOnScreen = renders.Count(); // Render chunks. if (FilteredFeatures.IsEnabled(Feature.DepthBuffer)) { context.EndSpriteBatch(); context.Graphics.GraphicsDevice.SetRenderTarget(RenderingBuffers.ScreenBuffer); context.StartSpriteBatch(); } foreach (RelativeRenderInformation ri in renders) { if (ri.Target == this.Chunk) { this.m_ChunkCenterX = ri.X + TileIsometricifier.CHUNK_TOP_WIDTH / 2; this.m_ChunkCenterY = ri.Y; } Texture2D tex = ri.Target.Texture; ChunkRenderer.MarkNeeded(ri.Target); if (tex != null) { ChunkRenderer.MarkUsed(ri.Target); if (FilteredFeatures.IsEnabled(Feature.DepthBuffer)) { context.SpriteBatch.Draw(tex, new Vector2(ri.X, ri.Y + zoffset), Color.White); } else { if (FilteredFeatures.IsEnabled(Feature.RenderingBuffers)) { context.Graphics.GraphicsDevice.SetRenderTarget(RenderingBuffers.ScreenBuffer); } context.SpriteBatch.Draw(tex, new Vector2(ri.X, ri.Y + zoffset), Color.White); } FilteredConsole.WriteLine(FilterCategory.RenderingActive, "Rendering chunk at " + ri.X + ", " + ri.Y + "."); } else { FilteredConsole.WriteLine(FilterCategory.Rendering, "No texture yet for chunk to render at " + ri.X + ", " + ri.Y + "."); } } // Render depth maps. if (FilteredFeatures.IsEnabled(Feature.DepthBuffer)) { context.EndSpriteBatch(); context.Graphics.GraphicsDevice.SetRenderTarget(RenderingBuffers.DepthBuffer); context.StartSpriteBatch(); foreach (RelativeRenderInformation ri in renders) { Texture2D depth = ri.Target.DepthMap; if (depth != null) { ChunkRenderer.MarkUsed(ri.Target); if (FilteredFeatures.IsEnabled(Feature.DepthBuffer)) { context.SpriteBatch.Draw(depth, new Vector2(ri.X, ri.Y + zoffset), Color.White); } } } } // Finish drawing. context.EndSpriteBatch(); context.Graphics.GraphicsDevice.SetRenderTarget(null); context.StartSpriteBatch(); }
protected override void HandleRenderOfEntity(GameContext context, IEntity a) { if (!FilteredFeatures.IsEnabled(Feature.RenderEntities)) { return; } if (a is ChunkEntity) { // Ensure we have a chunk manager to source chunks from. if (!(context.World is RPGWorld)) { return; } this.Octree = (context.World as RPGWorld).ChunkOctree; if (this.Octree == null) { return; } if (this.Chunk == null) { this.Chunk = this.Octree.Get(0, 0, 0); } // Special handling for entities in the 3D world. ChunkEntity ce = a as ChunkEntity; Vector2 pos = this.TranslatePoint(ce.X, ce.Y, ce.Z); // Set depth information. if (RenderingBuffers.DepthBuffer != null && FilteredFeatures.IsEnabled(Feature.IsometricOcclusion)) { // Draw image with depth. float depth = (( ((int)((ce.X < 0) ? Chunk.Width : 0) + (ce.X / Scale.CUBE_X) % Chunk.Width) + ((int)((ce.Y < 0) ? Chunk.Height : 0) + (ce.Y / Scale.CUBE_Y) % Chunk.Height) + ((int)((ce.Z < 0) ? Chunk.Depth : 0) + ((ce.Z / Scale.CUBE_Z) - 1) % Chunk.Depth)) / 255f); this.m_OccludingSpriteBatch.DrawOccludable( context.Textures[ce.Image], new Rectangle((int)(pos.X - ce.ImageOffsetX), (int)(pos.Y - ce.ImageOffsetY), context.Textures[ce.Image].Width, context.Textures[ce.Image].Height), ce.Color.ToPremultiplied(), depth ); } else { // Draw image normally. context.SpriteBatch.Draw( context.Textures[ce.Image], new Rectangle((int)(pos.X - ce.ImageOffsetX), (int)(pos.Y - ce.ImageOffsetY), context.Textures[ce.Image].Width, context.Textures[ce.Image].Height), ce.Color.ToPremultiplied() ); } } else { // Render using the default settings. base.HandleRenderOfEntity(context, a); } }