Re-added block shadows / Block optimization
This commit is contained in:
@@ -27,7 +27,7 @@ allprojects {
|
|||||||
gdxVersion = '1.9.8'
|
gdxVersion = '1.9.8'
|
||||||
roboVMVersion = '2.3.0'
|
roboVMVersion = '2.3.0'
|
||||||
aiVersion = '1.8.1'
|
aiVersion = '1.8.1'
|
||||||
uCoreVersion = '9807f5009e3e58045effb565d9106559413380ff'
|
uCoreVersion = 'b93d02efe3fdbe1a10b33d59597da8693d4f7de3'
|
||||||
|
|
||||||
getVersionString = {
|
getVersionString = {
|
||||||
String buildVersion = getBuildVersion()
|
String buildVersion = getBuildVersion()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
|
|||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.Texture.TextureWrap;
|
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.Rectangle;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.badlogic.gdx.utils.ObjectIntMap;
|
import com.badlogic.gdx.utils.ObjectIntMap;
|
||||||
@@ -63,6 +64,8 @@ public class Renderer extends RendererModule{
|
|||||||
private FogRenderer fog = new FogRenderer();
|
private FogRenderer fog = new FogRenderer();
|
||||||
|
|
||||||
public Renderer(){
|
public Renderer(){
|
||||||
|
Core.batch = new SpriteBatch(4096);
|
||||||
|
|
||||||
pixelate = true;
|
pixelate = true;
|
||||||
Lines.setCircleVertices(14);
|
Lines.setCircleVertices(14);
|
||||||
|
|
||||||
@@ -211,6 +214,7 @@ public class Renderer extends RendererModule{
|
|||||||
drawAndInterpolate(groundEffectGroup, e -> !(e instanceof BelowLiquidTrait));
|
drawAndInterpolate(groundEffectGroup, e -> !(e instanceof BelowLiquidTrait));
|
||||||
|
|
||||||
blocks.processBlocks();
|
blocks.processBlocks();
|
||||||
|
blocks.drawShadows();
|
||||||
blocks.drawBlocks(Layer.block);
|
blocks.drawBlocks(Layer.block);
|
||||||
|
|
||||||
Graphics.shader(Shaders.blockbuild, false);
|
Graphics.shader(Shaders.blockbuild, false);
|
||||||
|
|||||||
@@ -8,7 +8,11 @@ import io.anuke.mindustry.game.EventType.WorldLoadGraphicsEvent;
|
|||||||
import io.anuke.mindustry.game.Team;
|
import io.anuke.mindustry.game.Team;
|
||||||
import io.anuke.mindustry.world.Block;
|
import io.anuke.mindustry.world.Block;
|
||||||
import io.anuke.mindustry.world.Tile;
|
import io.anuke.mindustry.world.Tile;
|
||||||
|
import io.anuke.ucore.core.Core;
|
||||||
import io.anuke.ucore.core.Events;
|
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 io.anuke.ucore.util.Mathf;
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.*;
|
import static io.anuke.mindustry.Vars.*;
|
||||||
@@ -16,14 +20,16 @@ import static io.anuke.ucore.core.Core.camera;
|
|||||||
|
|
||||||
public class BlockRenderer{
|
public class BlockRenderer{
|
||||||
private final static int initialRequests = 32 * 32;
|
private final static int initialRequests = 32 * 32;
|
||||||
|
private final static int expandr = 4;
|
||||||
|
|
||||||
private FloorRenderer floorRenderer;
|
private FloorRenderer floorRenderer;
|
||||||
|
|
||||||
private Array<BlockRequest> requests = new Array<>(initialRequests);
|
private Array<BlockRequest> requests = new Array<>(initialRequests);
|
||||||
private int lastCamX, lastCamY;
|
private int lastCamX, lastCamY, lastRangeX, lastRangeY;
|
||||||
private Layer lastLayer;
|
private Layer lastLayer;
|
||||||
private int requestidx = 0;
|
private int requestidx = 0;
|
||||||
private int iterateidx = 0;
|
private int iterateidx = 0;
|
||||||
|
private Surface shadows = Graphics.createSurface().setSize(2, 2);
|
||||||
|
|
||||||
public BlockRenderer(){
|
public BlockRenderer(){
|
||||||
floorRenderer = new FloorRenderer();
|
floorRenderer = new FloorRenderer();
|
||||||
@@ -38,11 +44,27 @@ public class BlockRenderer{
|
|||||||
|
|
||||||
Events.on(TileChangeEvent.class, tile -> {
|
Events.on(TileChangeEvent.class, tile -> {
|
||||||
threads.runGraphics(() -> {
|
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.*/
|
/**Process all blocks to draw, simultaneously drawing block shadows.*/
|
||||||
public void processBlocks(){
|
public void processBlocks(){
|
||||||
iterateidx = 0;
|
iterateidx = 0;
|
||||||
@@ -51,18 +73,24 @@ public class BlockRenderer{
|
|||||||
int avgx = Mathf.scl(camera.position.x, tilesize);
|
int avgx = Mathf.scl(camera.position.x, tilesize);
|
||||||
int avgy = Mathf.scl(camera.position.y, 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int shadowW = rangex * tilesize*2, shadowH = rangey * tilesize*2;
|
||||||
|
|
||||||
|
if(shadows.width() != shadowW || shadows.height() != shadowH){
|
||||||
|
shadows.setSize(shadowW, shadowH);
|
||||||
|
}
|
||||||
|
|
||||||
requestidx = 0;
|
requestidx = 0;
|
||||||
|
|
||||||
int rangex = (int) (camera.viewportWidth * camera.zoom / tilesize / 2) + 2;
|
Graphics.end();
|
||||||
int rangey = (int) (camera.viewportHeight * camera.zoom / tilesize / 2) + 2;
|
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 expandr = 4;
|
|
||||||
|
|
||||||
//Graphics.surface(renderer.effectSurface, true, false);
|
|
||||||
|
|
||||||
int minx = Math.max(avgx - rangex - expandr, 0);
|
int minx = Math.max(avgx - rangex - expandr, 0);
|
||||||
int miny = Math.max(avgy - rangey - expandr, 0);
|
int miny = Math.max(avgy - rangey - expandr, 0);
|
||||||
@@ -79,9 +107,9 @@ public class BlockRenderer{
|
|||||||
if(tile != null){
|
if(tile != null){
|
||||||
Block block = tile.block();
|
Block block = tile.block();
|
||||||
|
|
||||||
//if(!expanded && block != Blocks.air && world.isAccessible(x, y)){
|
if(!expanded && block != Blocks.air && world.isAccessible(x, y)){
|
||||||
// tile.block().drawShadow(tile);
|
tile.block().drawShadow(tile);
|
||||||
//}
|
}
|
||||||
|
|
||||||
if(block != Blocks.air){
|
if(block != Blocks.air){
|
||||||
if(!expanded){
|
if(!expanded){
|
||||||
@@ -103,15 +131,17 @@ public class BlockRenderer{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO this actually isn't necessary
|
Graphics.surface();
|
||||||
//Draw.color(0, 0, 0, 0.15f);
|
Graphics.end();
|
||||||
//Graphics.flushSurface();
|
Core.batch.setProjectionMatrix(camera.combined);
|
||||||
//Draw.color();
|
Graphics.begin();
|
||||||
|
|
||||||
Sort.instance().sort(requests.items, 0, requestidx);
|
Sort.instance().sort(requests.items, 0, requestidx);
|
||||||
|
|
||||||
lastCamX = avgx;
|
lastCamX = avgx;
|
||||||
lastCamY = avgy;
|
lastCamY = avgy;
|
||||||
|
lastRangeX = rangex;
|
||||||
|
lastRangeY = rangey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRequests(){
|
public int getRequests(){
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ public class Floor extends Block{
|
|||||||
|
|
||||||
Floor floor = other.floor();
|
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;
|
(sameLayer && floor.cacheLayer == this.cacheLayer)) continue;
|
||||||
|
|
||||||
TextureRegion region = floor.edgeRegions[i];
|
TextureRegion region = floor.edgeRegions[i];
|
||||||
|
|||||||
Reference in New Issue
Block a user