Change fluid display rendering so the texture doesn't get stretched

This commit is contained in:
Mrbysco 2024-03-10 16:15:54 +01:00
parent e4fdc6d7da
commit 50b5209e84

View file

@ -11,12 +11,20 @@
package de.ellpeck.actuallyadditions.mod.inventory.gui;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FastColor;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
@ -39,7 +47,7 @@ public class FluidDisplay {
private int y;
private boolean outline;
private ResourceLocation resLoc;
private TextureAtlasSprite sprite;
private boolean drawTextNextTo;
@ -96,21 +104,38 @@ public class FluidDisplay {
float green = (float)(FastColor.ARGB32.green(color) / 255.0);
float blue = (float)(FastColor.ARGB32.blue(color) / 255.0);
float alpha = (float)(FastColor.ARGB32.alpha(color) / 255.0);
ResourceLocation stillTexture = fluidTypeExtension.getStillTexture();
if (this.resLoc == null || this.oldFluid != stack.getFluid()) {
if (this.sprite == null || this.oldFluid != fluid) {
this.oldFluid = stack.getFluid();
if (fluidTypeExtension.getStillTexture() != null) {
this.resLoc = new ResourceLocation(fluidTypeExtension.getStillTexture().getNamespace(), "textures/" + IClientFluidTypeExtensions.of(fluid).getStillTexture().getPath() + ".png");
AbstractTexture texture = Minecraft.getInstance().getTextureManager().getTexture(InventoryMenu.BLOCK_ATLAS);
if (texture instanceof TextureAtlas) {
TextureAtlasSprite sprite = ((TextureAtlas) texture).getSprite(stillTexture);
if (sprite != null) {
this.sprite = sprite;
}
}
}
if (this.resLoc != null) {
if (this.sprite != null) {
float minU = sprite.getU0();
float maxU = sprite.getU1();
float minV = sprite.getV0();
float maxV = sprite.getV1();
float deltaV = maxV - minV;
double tankLevel = ((float) this.fluidReference.getFluidAmount() / (float) fluidReference.getCapacity()) * 83;
RenderSystem.setShaderTexture(0, InventoryMenu.BLOCK_ATLAS);
RenderSystem.setShaderColor(red, green, blue, alpha);
RenderSystem.enableBlend();
RenderSystem.blendFuncSeparate(770, 771, 1, 0);
int i = this.fluidReference.getFluid().getAmount() * 83 / this.fluidReference.getCapacity();
guiGraphics.blit(this.resLoc, barX + 1, barY + 84 - i, 0, 0, 16, i, 16, 512);
int count = 1 + ((int) Math.ceil(tankLevel)) / 16;
for (int i = 0; i < count; i++) {
double subHeight = Math.min(16.0, tankLevel - (16.0 * i));
double offsetY = 84 - 16.0 * i - subHeight;
drawQuad(barX + 1, barY + offsetY, 16, subHeight, minU, (float) (maxV - deltaV * (subHeight / 16.0)), maxU, maxV);
}
RenderSystem.disableBlend();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
}
@ -121,6 +146,17 @@ public class FluidDisplay {
}
}
private void drawQuad(double x, double y, double width, double height, float minU, float minV, float maxU, float maxV) {
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder buffer = tesselator.getBuilder();
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
buffer.vertex(x, y + height, 0).uv(minU, maxV).endVertex();
buffer.vertex(x + width, y + height, 0).uv(maxU, maxV).endVertex();
buffer.vertex(x + width, y, 0).uv(maxU, minV).endVertex();
buffer.vertex(x, y, 0).uv(minU, minV).endVertex();
tesselator.end();
}
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
if (mouseX >= this.x && mouseY >= this.y && mouseX < this.x + (this.outline
? 26