From dbb2371801f72238888ddc1130c0f08fdc1ce337 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 20 Jul 2018 18:41:08 -0400 Subject: [PATCH] Re-added block shadows / Block optimization --- build.gradle | 2 +- .../src/io/anuke/mindustry/core/Renderer.java | 4 ++ .../mindustry/graphics/BlockRenderer.java | 62 ++++++++++++++----- .../anuke/mindustry/world/blocks/Floor.java | 2 +- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 370bf9c320..7b0c2961a0 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ allprojects { gdxVersion = '1.9.8' roboVMVersion = '2.3.0' aiVersion = '1.8.1' - uCoreVersion = '9807f5009e3e58045effb565d9106559413380ff' + uCoreVersion = 'b93d02efe3fdbe1a10b33d59597da8693d4f7de3' getVersionString = { String buildVersion = getBuildVersion() diff --git a/core/src/io/anuke/mindustry/core/Renderer.java b/core/src/io/anuke/mindustry/core/Renderer.java index 1ec331a955..18278c0dc7 100644 --- a/core/src/io/anuke/mindustry/core/Renderer.java +++ b/core/src/io/anuke/mindustry/core/Renderer.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture.TextureWrap; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.ObjectIntMap; @@ -63,6 +64,8 @@ public class Renderer extends RendererModule{ private FogRenderer fog = new FogRenderer(); public Renderer(){ + Core.batch = new SpriteBatch(4096); + pixelate = true; Lines.setCircleVertices(14); @@ -211,6 +214,7 @@ public class Renderer extends RendererModule{ drawAndInterpolate(groundEffectGroup, e -> !(e instanceof BelowLiquidTrait)); blocks.processBlocks(); + blocks.drawShadows(); blocks.drawBlocks(Layer.block); Graphics.shader(Shaders.blockbuild, false); diff --git a/core/src/io/anuke/mindustry/graphics/BlockRenderer.java b/core/src/io/anuke/mindustry/graphics/BlockRenderer.java index ded9838951..0d491b41e3 100644 --- a/core/src/io/anuke/mindustry/graphics/BlockRenderer.java +++ b/core/src/io/anuke/mindustry/graphics/BlockRenderer.java @@ -8,7 +8,11 @@ import io.anuke.mindustry.game.EventType.WorldLoadGraphicsEvent; import io.anuke.mindustry.game.Team; import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Tile; +import io.anuke.ucore.core.Core; import io.anuke.ucore.core.Events; +import io.anuke.ucore.core.Graphics; +import io.anuke.ucore.graphics.Draw; +import io.anuke.ucore.graphics.Surface; import io.anuke.ucore.util.Mathf; import static io.anuke.mindustry.Vars.*; @@ -16,14 +20,16 @@ import static io.anuke.ucore.core.Core.camera; public class BlockRenderer{ private final static int initialRequests = 32 * 32; + private final static int expandr = 4; private FloorRenderer floorRenderer; private Array requests = new Array<>(initialRequests); - private int lastCamX, lastCamY; + private int lastCamX, lastCamY, lastRangeX, lastRangeY; private Layer lastLayer; private int requestidx = 0; private int iterateidx = 0; + private Surface shadows = Graphics.createSurface().setSize(2, 2); public BlockRenderer(){ floorRenderer = new FloorRenderer(); @@ -38,11 +44,27 @@ public class BlockRenderer{ Events.on(TileChangeEvent.class, tile -> { threads.runGraphics(() -> { - //TODO invalidate camera position if it's in the view range + int avgx = Mathf.scl(camera.position.x, tilesize); + int avgy = Mathf.scl(camera.position.y, tilesize); + int rangex = (int) (camera.viewportWidth * camera.zoom / tilesize / 2) + 2; + int rangey = (int) (camera.viewportHeight * camera.zoom / tilesize / 2) + 2; + + if(Math.abs(avgx - tile.x) <= rangex && Math.abs(avgy - tile.y) <= rangey){ + lastCamY = lastCamX = -99; //invalidate camera position so blocks get updated + } }); }); } + public void drawShadows(){ + Draw.color(0, 0, 0, 0.15f); + Draw.rect(shadows.texture(), + Core.camera.position.x - Core.camera.position.x % tilesize, + Core.camera.position.y - Core.camera.position.y % tilesize, + shadows.width(), -shadows.height()); + Draw.color(); + } + /**Process all blocks to draw, simultaneously drawing block shadows.*/ public void processBlocks(){ iterateidx = 0; @@ -51,18 +73,24 @@ public class BlockRenderer{ int avgx = Mathf.scl(camera.position.x, tilesize); int avgy = Mathf.scl(camera.position.y, tilesize); - if(avgx == lastCamX && avgy == lastCamY){ + int rangex = (int) (camera.viewportWidth * camera.zoom / tilesize / 2) + 2; + int rangey = (int) (camera.viewportHeight * camera.zoom / tilesize / 2) + 2; + + if(avgx == lastCamX && avgy == lastCamY && lastRangeX == rangex && lastRangeY == rangey){ return; } + int shadowW = rangex * tilesize*2, shadowH = rangey * tilesize*2; + + if(shadows.width() != shadowW || shadows.height() != shadowH){ + shadows.setSize(shadowW, shadowH); + } + requestidx = 0; - int rangex = (int) (camera.viewportWidth * camera.zoom / tilesize / 2) + 2; - int rangey = (int) (camera.viewportHeight * camera.zoom / tilesize / 2) + 2; - - int expandr = 4; - - //Graphics.surface(renderer.effectSurface, true, false); + Graphics.end(); + Core.batch.getProjectionMatrix().setToOrtho2D(Mathf.round(Core.camera.position.x, tilesize)-shadowW/2f, Mathf.round(Core.camera.position.y, tilesize)-shadowH/2f, shadowW, shadowH); + Graphics.surface(shadows); int minx = Math.max(avgx - rangex - expandr, 0); int miny = Math.max(avgy - rangey - expandr, 0); @@ -79,9 +107,9 @@ public class BlockRenderer{ if(tile != null){ Block block = tile.block(); - //if(!expanded && block != Blocks.air && world.isAccessible(x, y)){ - // tile.block().drawShadow(tile); - //} + if(!expanded && block != Blocks.air && world.isAccessible(x, y)){ + tile.block().drawShadow(tile); + } if(block != Blocks.air){ if(!expanded){ @@ -103,15 +131,17 @@ public class BlockRenderer{ } } - //TODO this actually isn't necessary - //Draw.color(0, 0, 0, 0.15f); - //Graphics.flushSurface(); - //Draw.color(); + Graphics.surface(); + Graphics.end(); + Core.batch.setProjectionMatrix(camera.combined); + Graphics.begin(); Sort.instance().sort(requests.items, 0, requestidx); lastCamX = avgx; lastCamY = avgy; + lastRangeX = rangex; + lastRangeY = rangey; } public int getRequests(){ diff --git a/core/src/io/anuke/mindustry/world/blocks/Floor.java b/core/src/io/anuke/mindustry/world/blocks/Floor.java index 1717cf5de9..72b8fbdec1 100644 --- a/core/src/io/anuke/mindustry/world/blocks/Floor.java +++ b/core/src/io/anuke/mindustry/world/blocks/Floor.java @@ -178,7 +178,7 @@ public class Floor extends Block{ Floor floor = other.floor(); - if((floor.id <= this.id && !(other.getElevation() > tile.getElevation())) || !blends.test(floor) || (floor.cacheLayer.ordinal() > this.cacheLayer.ordinal() && !sameLayer) || + if((floor.id <= this.id && !(tile.getElevation() != -1 && other.getElevation() > tile.getElevation())) || !blends.test(floor) || (floor.cacheLayer.ordinal() > this.cacheLayer.ordinal() && !sameLayer) || (sameLayer && floor.cacheLayer == this.cacheLayer)) continue; TextureRegion region = floor.edgeRegions[i];