From 0dcfc8e5f04010c129841ad73a891431557f2f22 Mon Sep 17 00:00:00 2001 From: Dragon2488 Date: Thu, 29 Sep 2016 07:06:37 +0700 Subject: [PATCH] Render Lags fix --- .../render/newblocks/BlockRenderer.java | 68 +++++++++++-------- .../common/render/newblocks/RenderUtil.java | 2 +- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/main/java/gregtech/common/render/newblocks/BlockRenderer.java b/src/main/java/gregtech/common/render/newblocks/BlockRenderer.java index 3bf08798..7efe3528 100644 --- a/src/main/java/gregtech/common/render/newblocks/BlockRenderer.java +++ b/src/main/java/gregtech/common/render/newblocks/BlockRenderer.java @@ -1,5 +1,6 @@ package gregtech.common.render.newblocks; +import com.google.common.collect.ImmutableList; import gregtech.api.enums.Textures; import gregtech.api.interfaces.ITexture; import gregtech.api.items.GT_Generic_Block; @@ -27,11 +28,18 @@ import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; @SideOnly(Side.CLIENT) public class BlockRenderer { + /** + * Quads cache for standard icon provider blocks + * used for block and itemblock rendering + */ + public static HashMap>> quadsCache = new HashMap<>(); + public static boolean shouldHook(IBlockState blockState) { if(blockState.getBlock() instanceof IBlockIconProvider) { return true; @@ -64,7 +72,7 @@ public class BlockRenderer { return new ItemblockIconProviderModel(stack, holder instanceof EntityPlayer ? (EntityPlayer) holder : null); } - private static class ItemblockIconProviderModel extends AbstractModel { + private static class ItemblockIconProviderModel extends AbstractIconProviderModel { private final ItemStack itemStack; private final EntityPlayer holder; @@ -77,42 +85,16 @@ public class BlockRenderer { } @Override - public List getQuads(@Nullable IBlockState state, @Nullable EnumFacing side, long rand) { - if(side != null) { - TextureAtlasSprite sideIcon = getSideSprite(side); - if (sideIcon != null) { - BakedQuad faceQuad = RenderUtil.renderSide(DefaultVertexFormats.BLOCK, sideIcon, side, -1, 0.0F, -1, false); - if (faceQuad != null) { - return Collections.singletonList(faceQuad); - } - } - } - return Collections.emptyList(); - } - - public TextureAtlasSprite getSideSprite(EnumFacing side) { + public TextureAtlasSprite getSideSprite(EnumFacing side, IBlockState state) { IBlockIconProvider provider = (IBlockIconProvider) block; return provider.getIcon(holder, itemStack, side); } } - private static class IconProviderModel extends AbstractModel { + private static class IconProviderModel extends AbstractIconProviderModel { @Override - public List getQuads(@Nullable IBlockState state, @Nullable EnumFacing side, long rand) { - if(side != null && state != null) { - TextureAtlasSprite sideIcon = getSideSprite(side, state); - if (sideIcon != null) { - BakedQuad faceQuad = RenderUtil.renderSide(DefaultVertexFormats.BLOCK, sideIcon, side, -1, 0.0F, -1, false); - if (faceQuad != null) { - return Collections.singletonList(faceQuad); - } - } - } - return Collections.emptyList(); - } - public TextureAtlasSprite getSideSprite(EnumFacing side, IBlockState blockState) { BlockPos pos = ((IExtendedBlockState) blockState).getValue(GT_Generic_Block.BLOCK_POS); IBlockIconProvider provider = (IBlockIconProvider) blockState.getBlock(); @@ -121,6 +103,34 @@ public class BlockRenderer { } + + private static abstract class AbstractIconProviderModel extends AbstractModel { + + @Override + public List getQuads(@Nullable IBlockState state, @Nullable EnumFacing side, long rand) { + if(side != null) { + TextureAtlasSprite sideIcon = getSideSprite(side, state); + if (sideIcon != null) { + HashMap> quads = quadsCache.get(sideIcon); + if(quads == null) { + quads = new HashMap<>(); + quadsCache.put(sideIcon, quads); + } + ImmutableList faceQuad = quads.get(side); + if(faceQuad == null) { + faceQuad = ImmutableList.of(RenderUtil.renderSide(DefaultVertexFormats.BLOCK, sideIcon, side, -1, 0.0F, -1, false)); + quads.put(side, faceQuad); + } + return faceQuad; + } + } + return Collections.emptyList(); + } + + public abstract TextureAtlasSprite getSideSprite(EnumFacing side, IBlockState blockState); + + } + private static class TextureProviderModel extends AbstractModel { @Override diff --git a/src/main/java/gregtech/common/render/newblocks/RenderUtil.java b/src/main/java/gregtech/common/render/newblocks/RenderUtil.java index 1ecda4d2..dfb353d5 100644 --- a/src/main/java/gregtech/common/render/newblocks/RenderUtil.java +++ b/src/main/java/gregtech/common/render/newblocks/RenderUtil.java @@ -58,7 +58,7 @@ public class RenderUtil { default: System.out.println("Can't render side " + side); - return null; + throw new IllegalArgumentException(); } }