Optimized floor rendering

This commit is contained in:
Anuken
2018-05-02 11:38:44 -04:00
parent 72fc673020
commit 3fcdfbd01a
12 changed files with 90 additions and 60 deletions

View File

@@ -4,7 +4,7 @@ import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.content.Items;
import io.anuke.mindustry.content.Liquids;
import io.anuke.mindustry.content.StatusEffects;
import io.anuke.mindustry.graphics.DrawLayer;
import io.anuke.mindustry.graphics.CacheLayer;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
@@ -28,7 +28,7 @@ public class Blocks {
space = new Floor("space") {{
variants = 0;
drawLayer = DrawLayer.space;
cacheLayer = CacheLayer.space;
solid = true;
}},
@@ -46,7 +46,7 @@ public class Blocks {
status = StatusEffects.wet;
statusIntensity = 1f;
drownTime = 140f;
drawLayer = DrawLayer.water;
cacheLayer = CacheLayer.water;
}},
water = new Floor("water") {{
@@ -57,7 +57,7 @@ public class Blocks {
statusIntensity = 0.9f;
liquidDrop = Liquids.water;
liquid = true;
drawLayer = DrawLayer.water;
cacheLayer = CacheLayer.water;
}},
lava = new Floor("lava") {{
@@ -69,7 +69,7 @@ public class Blocks {
variants = 0;
liquidDrop = Liquids.lava;
liquid = true;
drawLayer = DrawLayer.lava;
cacheLayer = CacheLayer.lava;
}},
oil = new Floor("oil") {{
@@ -80,7 +80,7 @@ public class Blocks {
variants = 0;
liquidDrop = Liquids.oil;
liquid = true;
drawLayer = DrawLayer.oil;
cacheLayer = CacheLayer.oil;
}},
stone = new Floor("stone") {{

View File

@@ -25,7 +25,6 @@ public class MapRenderer {
}
public void resize(int width, int height){
//TODO just freezes on tablets
if(chunks != null){
for(int x = 0; x < chunks.length; x ++){
for(int y = 0; y < chunks[0].length; y ++){

View File

@@ -107,7 +107,7 @@ public class BlockRenderer{
Graphics.end();
floorRenderer.beginDraw();
floorRenderer.drawLayer(DrawLayer.walls);
floorRenderer.drawLayer(CacheLayer.walls);
floorRenderer.endDraw();
Graphics.begin();

View File

@@ -8,7 +8,7 @@ import io.anuke.ucore.graphics.Shader;
import static io.anuke.mindustry.Vars.renderer;
public enum DrawLayer {
public enum CacheLayer {
water{
@Override
public void begin(){

View File

@@ -8,7 +8,7 @@ import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.IntArray;
import com.badlogic.gdx.utils.IntSet;
import com.badlogic.gdx.utils.IntSet.IntSetIterator;
import com.badlogic.gdx.utils.async.AsyncExecutor;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.game.EventType.WorldLoadEvent;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
@@ -27,10 +27,8 @@ import static io.anuke.mindustry.Vars.tilesize;
import static io.anuke.mindustry.Vars.world;
public class FloorRenderer {
private final static int vsize = 4;
private final static int chunksize = 32;
private final static int chunksize = 64;
private AsyncExecutor executor = new AsyncExecutor(8);
private Chunk[][] cache;
private CacheBatch cbatch;
private IntSet drawnLayerSet = new IntSet();
@@ -59,7 +57,7 @@ public class FloorRenderer {
}
}
int layers = DrawLayer.values().length;
int layers = CacheLayer.values().length;
drawnLayers.clear();
drawnLayerSet.clear();
@@ -95,7 +93,7 @@ public class FloorRenderer {
beginDraw();
for(int i = 0; i < drawnLayers.size; i ++) {
DrawLayer layer = DrawLayer.values()[drawnLayers.get(i)];
CacheLayer layer = CacheLayer.values()[drawnLayers.get(i)];
drawLayer(layer);
}
@@ -115,7 +113,7 @@ public class FloorRenderer {
cbatch.endDraw();
}
public void drawLayer(DrawLayer layer){
public void drawLayer(CacheLayer layer){
OrthographicCamera camera = Core.camera;
int crangex = (int)(camera.viewportWidth * camera.zoom / (chunksize * tilesize))+1;
@@ -133,6 +131,7 @@ public class FloorRenderer {
}
Chunk chunk = cache[worldx][worldy];
if(chunk.caches[layer.ordinal()] == -1) continue;
cbatch.drawCache(chunk.caches[layer.ordinal()]);
}
}
@@ -148,13 +147,28 @@ public class FloorRenderer {
private void cacheChunk(int cx, int cy){
Chunk chunk = cache[cx][cy];
//long time = TimeUtils.nanoTime();
for(DrawLayer layer : DrawLayer.values()){
ObjectSet<CacheLayer> used = new ObjectSet<>();
for(int tilex = cx * chunksize; tilex < (cx + 1) * chunksize; tilex++) {
for (int tiley = cy * chunksize; tiley < (cy + 1) * chunksize; tiley++) {
Tile tile = world.tile(tilex, tiley);
if (tile != null){
used.add(tile.block().cacheLayer == CacheLayer.walls ?
CacheLayer.walls : tile.floor().cacheLayer);
}
}
}
for(CacheLayer layer : used){
cacheChunkLayer(cx, cy, chunk, layer);
}
// Log.info("Time to cache a chunk: {0}", TimeUtils.timeSinceNanos(time) / 1000000f);
}
private void cacheChunkLayer(int cx, int cy, Chunk chunk, DrawLayer layer){
private void cacheChunkLayer(int cx, int cy, Chunk chunk, CacheLayer layer){
Graphics.useBatch(cbatch);
cbatch.begin();
@@ -164,13 +178,13 @@ public class FloorRenderer {
Tile tile = world.tile(tilex, tiley);
if(tile == null) continue;
if(tile.floor().drawLayer == layer && tile.block().drawLayer != DrawLayer.walls){
if(tile.floor().cacheLayer == layer && tile.block().cacheLayer != CacheLayer.walls){
tile.floor().draw(tile);
}else if(tile.floor().drawLayer.ordinal() < layer.ordinal() && tile.block().drawLayer != DrawLayer.walls && layer != DrawLayer.walls){
}else if(tile.floor().cacheLayer.ordinal() < layer.ordinal() && tile.block().cacheLayer != CacheLayer.walls && layer != CacheLayer.walls){
tile.floor().drawNonLayer(tile);
}
if(tile.block().drawLayer == layer && layer == DrawLayer.walls){
if(tile.block().cacheLayer == layer && layer == CacheLayer.walls){
Block block = tile.block();
block.draw(tile);
}
@@ -183,7 +197,7 @@ public class FloorRenderer {
}
private class Chunk{
int[] caches = new int[DrawLayer.values().length];
int[] caches = new int[CacheLayer.values().length];
}
public void clearTiles(){

View File

@@ -12,7 +12,7 @@ import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.entities.effect.DamageArea;
import io.anuke.mindustry.entities.effect.Puddle;
import io.anuke.mindustry.entities.effect.Rubble;
import io.anuke.mindustry.graphics.DrawLayer;
import io.anuke.mindustry.graphics.CacheLayer;
import io.anuke.mindustry.graphics.Layer;
import io.anuke.mindustry.graphics.Palette;
import io.anuke.mindustry.net.Net;
@@ -90,8 +90,8 @@ public class Block extends BaseBlock {
public boolean expanded = false;
/**Max of timers used.*/
public int timers = 0;
/**Draw layer. Only used for 'cached' rendering.*/
public DrawLayer drawLayer = DrawLayer.normal;
/**Cache layer. Only used for 'cached' rendering.*/
public CacheLayer cacheLayer = CacheLayer.normal;
/**Layer to draw extra stuff on.*/
public Layer layer = null;
/**Extra layer to draw extra extra stuff on.*/

View File

@@ -33,9 +33,6 @@ public class Tile implements Position{
/**Tile traversal cost*/
public float cost = 1f;
public TileEntity entity;
public float pathDistance = -1;
public float vecx, vecy;
public Tile(int x, int y){
this.x = (short)x;

View File

@@ -3,6 +3,7 @@ package io.anuke.mindustry.world.blocks.types;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.content.StatusEffects;
import io.anuke.mindustry.content.fx.BlockFx;
import io.anuke.mindustry.entities.StatusEffect;
@@ -11,13 +12,16 @@ import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Effects.Effect;
import io.anuke.ucore.function.Predicate;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.util.Geometry;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.world;
public class Floor extends Block{
protected TextureRegion tempRegion = new TextureRegion();
public Predicate<Block> blends = block -> block != this;
protected TextureRegion edgeRegion;
protected TextureRegion[] edgeRegions;
protected Vector2[] offsets;
protected Predicate<Block> blends = block -> block != this;
protected boolean blend = true;
public float speedMultiplier = 1f;
@@ -35,6 +39,36 @@ public class Floor extends Block{
variants = 3;
}
@Override
public void load() {
super.load();
if(blend) {
edgeRegion = Draw.hasRegion(name + "edge") ? Draw.region(name + "edge") : Draw.region(edge + "edge");
edgeRegions = new TextureRegion[8];
offsets = new Vector2[8];
for(int i = 0; i < 8; i ++){
int dx = Geometry.d8[i].x, dy = Geometry.d8[i].y;
TextureRegion result = new TextureRegion();
int sx = -dx*8+2, sy = -dy*8+2;
int x = Mathf.clamp(sx, 0, 12);
int y = Mathf.clamp(sy, 0, 12);
int w = Mathf.clamp(sx+8, 0, 12) - x, h = Mathf.clamp(sy+8, 0, 12) - y;
float rx = Mathf.clamp(dx*8, 0, 8-w);
float ry = Mathf.clamp(dy*8, 0, 8-h);
result.setTexture(edgeRegion.getTexture());
result.setRegion(edgeRegion.getRegionX()+x, edgeRegion.getRegionY()+y+h, w, -h);
edgeRegions[i] = result;
offsets[i] = new Vector2(-4 + rx, -4 + ry);
}
}
}
@Override
public void init(){
super.init();
@@ -63,36 +97,21 @@ public class Floor extends Block{
private void drawEdges(Tile tile, boolean sameLayer){
if(!blend) return;
for(int dx = -1; dx <= 1; dx ++){
for(int dy = -1; dy <= 1; dy ++){
for(int i = 0; i < 8; i ++){
int dx = Geometry.d8[i].x, dy = Geometry.d8[i].y;
if(dx == 0 && dy == 0) continue;
Tile other = world.tile(tile.x+dx, tile.y+dy);
Tile other = world.tile(tile.x+dx, tile.y+dy);
if(other == null) continue;
if(other == null) continue;
Floor floor = other.floor();
Block floor = other.floor();
if(floor.id <= this.id || !blends.test(floor) || (floor.cacheLayer.ordinal() > this.cacheLayer.ordinal() && !sameLayer) ||
(sameLayer && floor.cacheLayer == this.cacheLayer)) continue;
if(floor.id <= this.id || !blends.test(floor) || (floor.drawLayer.ordinal() > this.drawLayer.ordinal() && !sameLayer) ||
(sameLayer && floor.drawLayer == this.drawLayer)) continue;
TextureRegion region = floor.edgeRegions[i];
TextureRegion region = Draw.hasRegion(floor.name() + "edge") ? Draw.region(floor.name() + "edge") :
Draw.region(floor.edge + "edge");
int sx = -dx*8+2, sy = -dy*8+2;
int x = Mathf.clamp(sx, 0, 12);
int y = Mathf.clamp(sy, 0, 12);
int w = Mathf.clamp(sx+8, 0, 12) - x, h = Mathf.clamp(sy+8, 0, 12) - y;
float rx = Mathf.clamp(dx*8, 0, 8-w);
float ry = Mathf.clamp(dy*8, 0, 8-h);
tempRegion.setTexture(region.getTexture());
tempRegion.setRegion(region.getRegionX()+x, region.getRegionY()+y+h, w, -h);
Draw.crect(tempRegion, tile.worldx()-4 + rx, tile.worldy()-4 + ry, w, h);
}
Draw.crect(region, tile.worldx() + floor.offsets[i].x, tile.worldy() + floor.offsets[i].y, region.getRegionWidth(), region.getRegionHeight());
}
}

View File

@@ -1,13 +1,13 @@
package io.anuke.mindustry.world.blocks.types;
import io.anuke.mindustry.graphics.DrawLayer;
import io.anuke.mindustry.graphics.CacheLayer;
import io.anuke.mindustry.world.Block;
public class StaticBlock extends Block{
public StaticBlock(String name) {
super(name);
drawLayer = DrawLayer.walls;
cacheLayer = CacheLayer.walls;
}
}