From 8d938660e7a8e504c731ab29c70945c5220db34b Mon Sep 17 00:00:00 2001 From: Ben Staddon Date: Tue, 31 Oct 2017 17:09:39 +0000 Subject: [PATCH] Add caching of data for flat lighting (#4358) --- .../client/model/pipeline/BlockInfo.java | 30 ++++++++++++++++++- .../model/pipeline/VertexLighterFlat.java | 24 ++++++++------- .../model/pipeline/VertexLighterSmoothAo.java | 9 ++---- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java b/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java index 1c713956d..1ed42f50c 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/BlockInfo.java @@ -28,6 +28,8 @@ import net.minecraft.world.IBlockAccess; public class BlockInfo { + private static final EnumFacing[] SIDES = EnumFacing.values(); + private final BlockColors colors; private IBlockAccess world; private IBlockState state; @@ -40,6 +42,10 @@ public class BlockInfo private final float[][][][] blockLight = new float[3][2][2][2]; private final float[][][] ao = new float[3][3][3]; + private final int[] packed = new int[7]; + + private boolean full; + private float shx = 0, shy = 0, shz = 0; private int cachedTint = -1; @@ -123,7 +129,7 @@ public class BlockInfo } if(!full) { - for(EnumFacing side : EnumFacing.values()) + for(EnumFacing side : SIDES) { int x = side.getFrontOffsetX() + 1; int y = side.getFrontOffsetY() + 1; @@ -158,6 +164,18 @@ public class BlockInfo } } + public void updateFlatLighting() + { + full = state.isFullCube(); + packed[0] = state.getPackedLightmapCoords(world, blockPos); + + for (EnumFacing side : SIDES) + { + int i = side.ordinal() + 1; + packed[i] = state.getPackedLightmapCoords(world, blockPos.offset(side)); + } + } + public IBlockAccess getWorld() { return world; @@ -193,6 +211,16 @@ public class BlockInfo return ao; } + public int[] getPackedLight() + { + return packed; + } + + public boolean isFullCube() + { + return full; + } + public float getShx() { return shx; diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java index 8fd4c1446..a30166609 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterFlat.java @@ -231,20 +231,21 @@ public class VertexLighterFlat extends QuadGatheringTransformer protected void updateLightmap(float[] normal, float[] lightmap, float x, float y, float z) { - float e1 = 1 - 1e-2f; - float e2 = 0.95f; - BlockPos pos = blockInfo.getBlockPos(); + final float e1 = 1f - 1e-2f; + final float e2 = 0.95f; - boolean full = blockInfo.getState().isFullCube(); + boolean full = blockInfo.isFullCube(); + EnumFacing side = null; - if((full || y < -e1) && normal[1] < -e2) pos = pos.down(); - if((full || y > e1) && normal[1] > e2) pos = pos.up(); - if((full || z < -e1) && normal[2] < -e2) pos = pos.north(); - if((full || z > e1) && normal[2] > e2) pos = pos.south(); - if((full || x < -e1) && normal[0] < -e2) pos = pos.west(); - if((full || x > e1) && normal[0] > e2) pos = pos.east(); + if((full || y < -e1) && normal[1] < -e2) side = EnumFacing.DOWN; + else if((full || y > e1) && normal[1] > e2) side = EnumFacing.UP; + else if((full || z < -e1) && normal[2] < -e2) side = EnumFacing.NORTH; + else if((full || z > e1) && normal[2] > e2) side = EnumFacing.SOUTH; + else if((full || x < -e1) && normal[0] < -e2) side = EnumFacing.WEST; + else if((full || x > e1) && normal[0] > e2) side = EnumFacing.EAST; - int brightness = blockInfo.getState().getPackedLightmapCoords(blockInfo.getWorld(), pos); + int i = side == null ? 0 : side.ordinal() + 1; + int brightness = blockInfo.getPackedLight()[i]; lightmap[0] = ((float)((brightness >> 0x04) & 0xF) * 0x20) / 0xFFFF; lightmap[1] = ((float)((brightness >> 0x14) & 0xF) * 0x20) / 0xFFFF; @@ -294,5 +295,6 @@ public class VertexLighterFlat extends QuadGatheringTransformer public void updateBlockInfo() { blockInfo.updateShift(); + blockInfo.updateFlatLighting(); } } diff --git a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java index ebb894fb8..1e281b5f2 100644 --- a/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java +++ b/src/main/java/net/minecraftforge/client/model/pipeline/VertexLighterSmoothAo.java @@ -40,12 +40,7 @@ public class VertexLighterSmoothAo extends VertexLighterFlat @Override protected void updateColor(float[] normal, float[] color, float x, float y, float z, float tint, int multiplier) { - if(tint != -1) - { - color[0] *= (float)(multiplier >> 0x10 & 0xFF) / 0xFF; - color[1] *= (float)(multiplier >> 0x8 & 0xFF) / 0xFF; - color[2] *= (float)(multiplier & 0xFF) / 0xFF; - } + super.updateColor(normal, color, x, y, z, tint, multiplier); float a = getAo(x, y, z); color[0] *= a; color[1] *= a; @@ -179,7 +174,7 @@ public class VertexLighterSmoothAo extends VertexLighterFlat @Override public void updateBlockInfo() { - super.updateBlockInfo(); + blockInfo.updateShift(); blockInfo.updateLightMatrix(); } }