Skip to content

Commit

Permalink
Improve the Quantum Storage Renderer (GregTechCEu#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude authored Apr 4, 2024
1 parent a27b30b commit 06938f0
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 50 deletions.
29 changes: 23 additions & 6 deletions src/main/java/gregtech/api/gui/resources/TextTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,26 @@ public class TextTexture implements IGuiTexture {
public TextType type;
@SideOnly(Side.CLIENT)
private List<String> texts;
private final boolean isClient = FMLCommonHandler.instance().getSide().isClient();

public TextTexture(String text, int color) {
this.color = color;
this.type = TextType.NORMAL;
if (FMLCommonHandler.instance().getSide().isClient()) {
if (isClient) {
this.text = I18n.format(text);
texts = Collections.singletonList(this.text);
}
}

public TextTexture() {
this.color = 0xFFFFFF;
this.type = TextType.NORMAL;
this.text = "";

if (isClient)
this.texts = Collections.singletonList(this.text);
}

public TextTexture setColor(int color) {
this.color = color;
return this;
Expand All @@ -42,11 +52,18 @@ public TextTexture setDropShadow(boolean dropShadow) {

public TextTexture setWidth(int width) {
this.width = width;
if (FMLCommonHandler.instance().getSide().isClient()) {
if (this.width > 0) {
texts = Minecraft.getMinecraft().fontRenderer.listFormattedStringToWidth(text, width);
} else {
texts = Collections.singletonList(text);
return this;
}

public TextTexture setText(String text) {
if (!this.text.equals(text)) {
this.text = text;
if (isClient) {
if (this.width > 0) {
texts = Minecraft.getMinecraft().fontRenderer.listFormattedStringToWidth(text, width);
} else {
texts = Collections.singletonList(text);
}
}
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void render(@NotNull MetaTileEntityHolder te, double x, double y, double
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);

MetaTileEntity metaTileEntity = te.getMetaTileEntity();
if (metaTileEntity instanceof IFastRenderMetaTileEntity) {
if (metaTileEntity instanceof IFastRenderMetaTileEntity fastRender) {
CCRenderState renderState = CCRenderState.instance();
renderState.reset();
renderState.bind(buffer);
renderState.setBrightness(te.getWorld(), te.getPos());
((IFastRenderMetaTileEntity) metaTileEntity).renderMetaTileEntityFast(renderState,
fastRender.renderMetaTileEntityFast(renderState,
new Matrix4().translate(x, y, z), partialTicks);
}
if (metaTileEntity != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package gregtech.client.renderer.texture.custom;

import gregtech.api.gui.resources.TextTexture;
import gregtech.api.metatileentity.ITieredMetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.util.TextFormattingUtil;
import gregtech.client.renderer.texture.Textures;
import gregtech.client.renderer.texture.cube.SimpleSidedCubeRenderer.RenderSide;
import gregtech.client.utils.RenderUtil;
import gregtech.common.ConfigHolder;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumChest;

import net.minecraft.client.Minecraft;
Expand All @@ -19,6 +22,7 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -40,6 +44,8 @@ public class QuantumStorageRenderer implements TextureUtils.IIconRegister {

private static final EnumMap<EnumFacing, Cuboid6> boxFacingMap = new EnumMap<>(EnumFacing.class);

private static final TextTexture textRenderer = new TextTexture().setWidth(32);

@SideOnly(Side.CLIENT)
private TextureAtlasSprite glassTexture;

Expand All @@ -62,46 +68,68 @@ public void registerIcons(TextureMap textureMap) {
.registerSprite(new ResourceLocation("gregtech:blocks/overlay/machine/overlay_screen_glass"));
}

public void renderMachine(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline,
EnumFacing frontFacing, int tier) {
public <T extends MetaTileEntity & ITieredMetaTileEntity> void renderMachine(CCRenderState renderState,
Matrix4 translation,
IVertexOperation[] pipeline,
T mte) {
EnumFacing frontFacing = mte.getFrontFacing();
int tier = mte.getTier();
Textures.renderFace(renderState, translation, pipeline, frontFacing, glassBox, glassTexture,
BlockRenderLayer.CUTOUT_MIPPED);

TextureAtlasSprite hullTexture = Textures.VOLTAGE_CASINGS[tier]
.getSpriteOnSide(RenderSide.bySide(EnumFacing.NORTH));
boxFacingMap.keySet().forEach(facing -> {
for (EnumFacing box : EnumFacing.VALUES) {
if ((facing != frontFacing || box != frontFacing) &&
(facing != EnumFacing.DOWN || box.getAxis().isVertical())) { // Don't render the front facing
// box from the front, nor allow
// Z-fighting to occur on the
// bottom
Textures.renderFace(renderState, translation, pipeline, facing, boxFacingMap.get(box), hullTexture,
BlockRenderLayer.CUTOUT_MIPPED);
}
}
});

for (var facing : boxFacingMap.keySet()) {
// do not render the box at the front face when "facing" is "frontFacing"
if (facing == frontFacing) continue;

// render when the box face matches facing
Textures.renderFace(renderState, translation, pipeline, facing, boxFacingMap.get(facing),
hullTexture, BlockRenderLayer.CUTOUT_MIPPED);

// render when the box face is opposite of facing
Textures.renderFace(renderState, translation, pipeline, facing.getOpposite(), boxFacingMap.get(facing),
hullTexture, BlockRenderLayer.CUTOUT_MIPPED);
}

// render the sides of the box that face the front face
if (frontFacing.getAxis() == EnumFacing.Axis.Y) return;
Textures.renderFace(renderState, translation, pipeline, frontFacing, boxFacingMap.get(EnumFacing.DOWN),
hullTexture, BlockRenderLayer.CUTOUT_MIPPED);
Textures.renderFace(renderState, translation, pipeline, frontFacing, boxFacingMap.get(EnumFacing.UP),
hullTexture, BlockRenderLayer.CUTOUT_MIPPED);

EnumFacing facing = frontFacing.rotateYCCW();
Textures.renderFace(renderState, translation, pipeline, frontFacing, boxFacingMap.get(facing),
hullTexture, BlockRenderLayer.CUTOUT_MIPPED);
Textures.renderFace(renderState, translation, pipeline, frontFacing, boxFacingMap.get(facing.getOpposite()),
hullTexture, BlockRenderLayer.CUTOUT_MIPPED);
}

public static void renderChestStack(double x, double y, double z, MetaTileEntityQuantumChest machine,
ItemStack stack, long count, float partialTicks) {
if (stack.isEmpty() || count == 0)
if (!ConfigHolder.client.enableFancyChestRender || stack.isEmpty() || count == 0)
return;

float lastBrightnessX = OpenGlHelper.lastBrightnessX;
float lastBrightnessY = OpenGlHelper.lastBrightnessY;
World world = machine.getWorld();
setLightingCorrectly(world, machine.getPos());
EnumFacing frontFacing = machine.getFrontFacing();
RenderItem itemRenderer = Minecraft.getMinecraft().getRenderItem();
float tick = world.getWorldTime() + partialTicks;
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
GlStateManager.translate(0.5D, 0.5D, 0.5D);
GlStateManager.rotate(tick * (float) Math.PI * 2 / 40, 0, 1, 0);
GlStateManager.scale(0.6f, 0.6f, 0.6f);
itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED);
GlStateManager.popMatrix();

if (canRender(x, y, z, 8 *
MathHelper.clamp((double) Minecraft.getMinecraft().gameSettings.renderDistanceChunks / 8, 1.0, 2.5))) {
RenderItem itemRenderer = Minecraft.getMinecraft().getRenderItem();
float tick = world.getWorldTime() + partialTicks;
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
GlStateManager.translate(0.5D, 0.5D, 0.5D);
GlStateManager.rotate(tick * (float) Math.PI * 2 / 40, 0, 1, 0);
GlStateManager.scale(0.6f, 0.6f, 0.6f);
itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED);
GlStateManager.popMatrix();
}

OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
renderAmountText(x, y, z, count, frontFacing);
Expand All @@ -110,20 +138,20 @@ public static void renderChestStack(double x, double y, double z, MetaTileEntity

public static void renderTankFluid(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline,
FluidTank tank, IBlockAccess world, BlockPos pos, EnumFacing frontFacing) {
float lastBrightnessX = OpenGlHelper.lastBrightnessX;
float lastBrightnessY = OpenGlHelper.lastBrightnessY;
FluidStack stack = tank.getFluid();
if (stack == null || stack.amount == 0 || !ConfigHolder.client.enableFancyChestRender)
return;

if (world != null) {
renderState.setBrightness(world, pos);
}
FluidStack stack = tank.getFluid();
if (stack == null || stack.amount == 0)
return;

Cuboid6 partialFluidBox = new Cuboid6(1.0625 / 16.0, 2.0625 / 16.0, 1.0625 / 16.0, 14.9375 / 16.0,
14.9375 / 16.0, 14.9375 / 16.0);

double fillFraction = (double) stack.amount / tank.getCapacity();
if (tank.getFluid().getFluid().isGaseous()) {
boolean gas = stack.getFluid().isGaseous();
if (gas) {
partialFluidBox.min.y = Math.max(13.9375 - (11.875 * fillFraction), 2.0) / 16.0;
} else {
partialFluidBox.max.y = Math.min((11.875 * fillFraction) + 2.0625, 14.0) / 16.0;
Expand All @@ -133,15 +161,34 @@ public static void renderTankFluid(CCRenderState renderState, Matrix4 translatio
ResourceLocation fluidStill = stack.getFluid().getStill(stack);
TextureAtlasSprite fluidStillSprite = Minecraft.getMinecraft().getTextureMapBlocks()
.getAtlasSprite(fluidStill.toString());
for (EnumFacing facing : EnumFacing.VALUES) {
Textures.renderFace(renderState, translation, pipeline, facing, partialFluidBox, fluidStillSprite,
BlockRenderLayer.CUTOUT_MIPPED);
}

Textures.renderFace(renderState, translation, pipeline, frontFacing, partialFluidBox, fluidStillSprite,
BlockRenderLayer.CUTOUT_MIPPED);

Textures.renderFace(renderState, translation, pipeline, gas ? EnumFacing.DOWN : EnumFacing.UP, partialFluidBox,
fluidStillSprite,
BlockRenderLayer.CUTOUT_MIPPED);

GlStateManager.resetColor();

renderState.reset();
}

/**
* Takes in the difference in x, y, and z from the camera to the rendering TE and
* calculates the squared distance and checks if it's within the range squared
*
* @param x the difference in x from entity to this rendering TE
* @param y the difference in y from entity to this rendering TE
* @param z the difference in z from entity to this rendering TE
* @param range distance needed to be rendered
* @return true if the camera is within the given range, otherwise false
*/
public static boolean canRender(double x, double y, double z, double range) {
double distance = (x * x) + (y * y) + (z * z);
return distance < range * range;
}

public static void renderTankAmount(double x, double y, double z, EnumFacing frontFacing, long amount) {
float lastBrightnessX = OpenGlHelper.lastBrightnessX;
float lastBrightnessY = OpenGlHelper.lastBrightnessY;
Expand All @@ -153,6 +200,9 @@ public static void renderTankAmount(double x, double y, double z, EnumFacing fro
}

public static void renderAmountText(double x, double y, double z, long amount, EnumFacing frontFacing) {
if (!ConfigHolder.client.enableFancyChestRender || !canRender(x, y, z, 64))
return;

GlStateManager.pushMatrix();
GlStateManager.translate(x, y, z);
GlStateManager.translate(frontFacing.getXOffset() * -1 / 16f, frontFacing.getYOffset() * -1 / 16f,
Expand All @@ -167,7 +217,8 @@ public static void renderAmountText(double x, double y, double z, long amount, E
GlStateManager.scale(1f / 64, 1f / 64, 0);
GlStateManager.translate(-32, -32, 0);
GlStateManager.disableLighting();
new TextTexture(amountText, 0xFFFFFF).draw(0, 24, 64, 28);
textRenderer.setText(amountText);
textRenderer.draw(0, 24, 64, 28);
GlStateManager.enableLighting();
GlStateManager.popMatrix();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/gregtech/common/ConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public static class ClientOptions {

@Config.Comment({
"Whether or not to enable Emissive Textures for Electric Blast Furnace Coils when the multiblock is working.",
"Default: false" })
"Default: true" })
public boolean coilsActiveEmissiveTextures = true;

@Config.Comment({ "Whether or not sounds should be played when using tools outside of crafting.",
Expand Down Expand Up @@ -456,6 +456,10 @@ public static class ClientOptions {
@Config.Comment({ "Prevent optical and laser cables from animating when active.", "Default: false" })
public boolean preventAnimatedCables = false;

@Config.Comment({ "Enable the fancy rendering for Super/Quantum Chests/Tanks.",
"Default: true" })
public boolean enableFancyChestRender = true;

public static class GuiConfig {

@Config.Comment({ "The scrolling speed of widgets", "Default: 13" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,
Textures.QUANTUM_STORAGE_RENDERER.renderMachine(renderState, translation,
ArrayUtils.add(pipeline,
new ColourMultiplier(GTUtility.convertRGBtoOpaqueRGBA_CL(getPaintingColorForRendering()))),
this.getFrontFacing(), this.getTier());
this);
Textures.CREATIVE_CONTAINER_OVERLAY.renderSided(EnumFacing.UP, renderState, translation, pipeline);
Textures.PIPE_OUT_OVERLAY.renderSided(this.getOutputFacing(), renderState, translation, pipeline);
Textures.ITEM_OUTPUT_OVERLAY.renderSided(this.getOutputFacing(), renderState, translation, pipeline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,
Textures.QUANTUM_STORAGE_RENDERER.renderMachine(renderState, translation,
ArrayUtils.add(pipeline,
new ColourMultiplier(GTUtility.convertRGBtoOpaqueRGBA_CL(getPaintingColorForRendering()))),
this.getFrontFacing(), this.getTier());
this);
Textures.CREATIVE_CONTAINER_OVERLAY.renderSided(EnumFacing.UP, renderState, translation, pipeline);
if (this.getOutputFacing() != null) {
Textures.PIPE_OUT_OVERLAY.renderSided(this.getOutputFacing(), renderState, translation, pipeline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,
Textures.QUANTUM_STORAGE_RENDERER.renderMachine(renderState, translation,
ArrayUtils.add(pipeline,
new ColourMultiplier(GTUtility.convertRGBtoOpaqueRGBA_CL(getPaintingColorForRendering()))),
this.getFrontFacing(), this.tier);
this);
Textures.QUANTUM_CHEST_OVERLAY.renderSided(EnumFacing.UP, renderState, translation, pipeline);
if (outputFacing != null) {
Textures.PIPE_OUT_OVERLAY.renderSided(outputFacing, renderState, translation, pipeline);
Expand Down
Loading

0 comments on commit 06938f0

Please sign in to comment.