Skip to content

Commit 3370bdc

Browse files
authored
fix: changed rendering order of dark and light energy overlays to fix visual glitch (#16)
* fix: changed rendering order of dark and light energy overlays to fix visual glitch * fix: resolves #15
1 parent 7a20e47 commit 3370bdc

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/main/java/dev/galacticraft/machinelib/client/api/screen/MachineScreen.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -754,10 +754,12 @@ protected final void renderBg(GuiGraphics graphics, float delta, int mouseX, int
754754
protected void drawCapacitor(GuiGraphics graphics, int mouseX, int mouseY) {
755755
long capacity = this.menu.energyStorage.getCapacity();
756756
if (capacity > 0 && this.capacitorHeight != 0) {
757-
graphics.blit(Constant.ScreenTexture.OVERLAY_BARS, this.leftPos + this.capacitorX, this.topPos + this.capacitorY, ENERGY_BACKGROUND_X, ENERGY_BACKGROUND_Y, OVERLAY_WIDTH, OVERLAY_HEIGHT, OVERLAY_TEX_WIDTH, OVERLAY_TEX_HEIGHT);
757+
int x = this.leftPos + this.capacitorX;
758+
int y = this.topPos + this.capacitorY;
758759
long amount = this.menu.energyStorage.getAmount();
759760
float scale = (float) ((double) amount / (double) capacity);
760-
graphics.blit(Constant.ScreenTexture.OVERLAY_BARS, this.leftPos + this.capacitorX, (int) Math.floor(this.topPos + this.capacitorY + this.capacitorHeight - (this.capacitorHeight * scale)), ENERGY_X, ENERGY_Y, OVERLAY_WIDTH, (int) Math.floor(OVERLAY_HEIGHT * scale), OVERLAY_TEX_WIDTH, OVERLAY_TEX_HEIGHT);
761+
graphics.blit(Constant.ScreenTexture.OVERLAY_BARS, x, y, ENERGY_X, ENERGY_Y, OVERLAY_WIDTH, OVERLAY_HEIGHT, OVERLAY_TEX_WIDTH, OVERLAY_TEX_HEIGHT);
762+
graphics.blit(Constant.ScreenTexture.OVERLAY_BARS, x, y, ENERGY_BACKGROUND_X, ENERGY_BACKGROUND_Y, OVERLAY_WIDTH, (int) (OVERLAY_HEIGHT * (1 - scale)), OVERLAY_TEX_WIDTH, OVERLAY_TEX_HEIGHT);
761763

762764
if (mouseIn(mouseX, mouseY, this.leftPos + this.capacitorX, this.topPos + this.capacitorY, 16, this.capacitorHeight)) {
763765
List<Component> lines = new ArrayList<>();

src/main/java/dev/galacticraft/machinelib/client/api/util/GraphicsUtil.java

+38-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@
2222

2323
package dev.galacticraft.machinelib.client.api.util;
2424

25+
import org.joml.Matrix4f;
26+
27+
import com.mojang.blaze3d.systems.RenderSystem;
28+
import com.mojang.blaze3d.vertex.BufferBuilder;
29+
import com.mojang.blaze3d.vertex.BufferUploader;
30+
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
31+
import com.mojang.blaze3d.vertex.PoseStack;
32+
import com.mojang.blaze3d.vertex.Tesselator;
33+
import com.mojang.blaze3d.vertex.VertexFormat;
34+
2535
import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering;
2636
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
2737
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
2838
import net.minecraft.client.gui.GuiGraphics;
39+
import net.minecraft.client.renderer.GameRenderer;
2940
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
41+
import net.minecraft.resources.ResourceLocation;
3042
import net.minecraft.util.FastColor;
3143
import net.minecraft.world.level.material.Fluids;
3244

@@ -49,17 +61,39 @@ public static void drawFluid(GuiGraphics graphics, int x, int y, int width, int
4961
assert sprite != null;
5062
}
5163

64+
int tileWidth = 16;
65+
int tileHeight = 16;
5266
int fluidHeight = (int) (((double) available / (double) capacity) * height);
5367
int startY = fillFromTop ? y : y + (height - fluidHeight);
68+
int depth = startY + fluidHeight;
69+
float u0 = sprite.getU0();
70+
float v0 = sprite.getV0();
5471

55-
for (int splitX = 0; splitX < width; splitX += Math.min(width, 16)) {
56-
int realWidth = Math.min(width - splitX, 16);
57-
for (int splitY = startY; splitY < startY + fluidHeight; splitY += realWidth) {
58-
graphics.blit(x + splitX, splitY, 0, realWidth, Math.min(realWidth, startY + fluidHeight - splitY), sprite, r, g, b, 1.0f);
72+
for (int splitX = 0; splitX < width; splitX += tileWidth) {
73+
int realWidth = Math.min(width - splitX, tileWidth);
74+
for (int splitY = startY; splitY < depth; splitY += tileHeight) {
75+
int realHeight = Math.min(depth - splitY, tileHeight);
76+
float u1 = sprite.getU((float) realWidth / (float) tileWidth);
77+
float v1 = sprite.getV((float) realHeight / (float) tileHeight);
78+
innerBlit(graphics.pose(), sprite.atlasLocation(), x + splitX, x + splitX + realWidth, splitY, splitY + realHeight, 0, u0, u1, v0, v1, r, g, b, 1.0f);
5979
}
6080
}
6181
}
6282

83+
private static void innerBlit(PoseStack poseStack, ResourceLocation resourceLocation, int x0, int x1, int y0, int y1, int z, float u0, float u1, float v0, float v1, float r, float g, float b, float a) {
84+
RenderSystem.setShaderTexture(0, resourceLocation);
85+
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
86+
RenderSystem.enableBlend();
87+
Matrix4f matrix = poseStack.last().pose();
88+
BufferBuilder bufferBuilder = Tesselator.getInstance().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
89+
bufferBuilder.addVertex(matrix, (float)x0, (float)y0, (float)z).setUv(u0, v0).setColor(r, g, b, a);
90+
bufferBuilder.addVertex(matrix, (float)x0, (float)y1, (float)z).setUv(u0, v1).setColor(r, g, b, a);
91+
bufferBuilder.addVertex(matrix, (float)x1, (float)y1, (float)z).setUv(u1, v1).setColor(r, g, b, a);
92+
bufferBuilder.addVertex(matrix, (float)x1, (float)y0, (float)z).setUv(u1, v0).setColor(r, g, b, a);
93+
BufferUploader.drawWithShader(bufferBuilder.buildOrThrow());
94+
RenderSystem.disableBlend();
95+
}
96+
6397
public static void highlightElement(GuiGraphics graphics, int left, int top, int x, int y, int width, int height, int color) {
6498
color |= (0xFF << 24);
6599
color ^= (0b1110000 << 24);

0 commit comments

Comments
 (0)