From 60b88af34960fc76c8326638469986d17946e354 Mon Sep 17 00:00:00 2001 From: RXJpaw Date: Sat, 9 Dec 2023 02:05:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WorldToScreen-method=20for=20fut?= =?UTF-8?q?ure=20additions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🚚 Moved scissor utils to RenderUtil. --- .../rxj/iron_quarry/renderer/RenderUtil.java | 44 +++++++++++++++++++ .../rxj/iron_quarry/renderer/ScreenPos.java | 39 ++++++++++++++++ .../iron_quarry/screen/QuarryBlockScreen.java | 7 +-- .../java/pw/rxj/iron_quarry/util/ZUtil.java | 15 ------- 4 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 src/main/java/pw/rxj/iron_quarry/renderer/RenderUtil.java create mode 100644 src/main/java/pw/rxj/iron_quarry/renderer/ScreenPos.java diff --git a/src/main/java/pw/rxj/iron_quarry/renderer/RenderUtil.java b/src/main/java/pw/rxj/iron_quarry/renderer/RenderUtil.java new file mode 100644 index 0000000..0e0f905 --- /dev/null +++ b/src/main/java/pw/rxj/iron_quarry/renderer/RenderUtil.java @@ -0,0 +1,44 @@ +package pw.rxj.iron_quarry.renderer; + +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.Camera; +import net.minecraft.client.util.Window; +import net.minecraft.util.math.Matrix4f; +import net.minecraft.util.math.Vec3d; +import net.minecraft.util.math.Vec3f; +import net.minecraft.util.math.Vector4f; + +public class RenderUtil { + public static void enableScaledScissor(int x, int y, int width, int height) { + int scaleFactor = (int) MinecraftClient.getInstance().getWindow().getScaleFactor(); + + RenderSystem.enableScissor(x * scaleFactor, MinecraftClient.getInstance().getWindow().getFramebufferHeight() - (y + height) * scaleFactor, width * scaleFactor, height * scaleFactor); + } + public static void disableScaledScissor() { + RenderSystem.disableScissor(); + } + public static void runScissored(int x, int y, int width, int height, Runnable runnable) { + enableScaledScissor(x, y, width, height); + runnable.run(); + disableScaledScissor(); + } + + public static ScreenPos worldToScreen(Vec3d worldPos, Matrix4f positionMatrix, Matrix4f projectionMatrix) { + MinecraftClient minecraftClient = MinecraftClient.getInstance(); + Camera camera = minecraftClient.gameRenderer.getCamera(); + Window window = minecraftClient.getWindow(); + + Vector4f relativeWorldPos = new Vector4f(new Vec3f(camera.getPos().negate().add(worldPos))); + relativeWorldPos.transform(positionMatrix); + relativeWorldPos.transform(projectionMatrix); + + var depth = relativeWorldPos.getW(); + if (depth != 0) relativeWorldPos.normalizeProjectiveCoordinates(); + + float screenX = window.getScaledWidth() * (0.5F + relativeWorldPos.getX() * 0.5F); + float screenY = window.getScaledHeight() * (0.5F - relativeWorldPos.getY() * 0.5F); + + return ScreenPos.from(screenX, screenY, depth); + } +} diff --git a/src/main/java/pw/rxj/iron_quarry/renderer/ScreenPos.java b/src/main/java/pw/rxj/iron_quarry/renderer/ScreenPos.java new file mode 100644 index 0000000..d9b1d19 --- /dev/null +++ b/src/main/java/pw/rxj/iron_quarry/renderer/ScreenPos.java @@ -0,0 +1,39 @@ +package pw.rxj.iron_quarry.renderer; + +import net.minecraft.util.math.Vector4f; + +public class ScreenPos { + private final float x; + private final float y; + private final float depth; + + private ScreenPos(float x, float y, float depth) { + this.x = x; + this.y = y; + this.depth = depth; + } + + public static ScreenPos from(float x, float y, float depth) { + return new ScreenPos(x, y, depth); + } + public static ScreenPos of(Vector4f transformed) { + return from(transformed.getX(), transformed.getX(), transformed.getW()); + } + + public float getX() { + return this.x; + } + public float getY() { + return this.y; + } + public float getDepth() { + return this.depth; + } + + public boolean isAhead() { + return this.getDepth() >= 0; + } + public boolean isBehind() { + return this.getDepth() < 0; + } +} diff --git a/src/main/java/pw/rxj/iron_quarry/screen/QuarryBlockScreen.java b/src/main/java/pw/rxj/iron_quarry/screen/QuarryBlockScreen.java index aa73c97..c06ae45 100644 --- a/src/main/java/pw/rxj/iron_quarry/screen/QuarryBlockScreen.java +++ b/src/main/java/pw/rxj/iron_quarry/screen/QuarryBlockScreen.java @@ -17,6 +17,7 @@ import pw.rxj.iron_quarry.compat.Compat; import pw.rxj.iron_quarry.records.IoOption; import pw.rxj.iron_quarry.records.TexturePosition; +import pw.rxj.iron_quarry.renderer.RenderUtil; import pw.rxj.iron_quarry.screenhandler.QuarryBlockScreenHandler; import pw.rxj.iron_quarry.types.Face; import pw.rxj.iron_quarry.types.IoState; @@ -155,7 +156,7 @@ protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int RenderSystem.setShaderTexture(0, AUGMENTATION_CONFIGURATION_TEXTURE); this.drawTexture(matrices, augmentsMenuX, augmentsMenuY, 0, 0, augmentsMenuWidth, augmentsMenuHeight); - ZUtil.runScissored(augmentsMenuX, augmentsMenuY, augmentsMenuWidth, augmentsMenuHeight, () -> { + RenderUtil.runScissored(augmentsMenuX, augmentsMenuY, augmentsMenuWidth, augmentsMenuHeight, () -> { this.textRenderer.drawWithShadow(matrices, ReadableString.translatable("screen.iron_quarry.quarry_block.title.augmentation"), augmentsMenuX + 20, augmentsMenuY + 7, 0xFFFFFF); }); @@ -194,7 +195,7 @@ protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int RenderSystem.setShaderTexture(0, OPTIONS_CONFIGURATION_TEXTURE); drawTexture(matrices, ioConfigX, ioConfigY, 0, 0, ioConfigWidth, ioConfigHeight); - ZUtil.runScissored(ioConfigX, ioConfigY, ioConfigWidth, ioConfigHeight, () -> { + RenderUtil.runScissored(ioConfigX, ioConfigY, ioConfigWidth, ioConfigHeight, () -> { textRenderer.drawWithShadow(matrices, ReadableString.translatable("screen.iron_quarry.quarry_block.title.configuration"), ioConfigX + 20, ioConfigY + 7, 0xFFFFFF); }); @@ -238,7 +239,7 @@ protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int protected void drawSlot(MatrixStack matrices, Slot slot) { if(AUGMENT_SLOTS.contains((ManagedSlot) slot)) { TrackableZone.Zone zone = AugmentsConfig.zone; - ZUtil.runScissored(zone.x, zone.y, zone.width, zone.height, () -> { + RenderUtil.runScissored(zone.x, zone.y, zone.width, zone.height, () -> { super.drawSlot(matrices, slot); }); } else { diff --git a/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java b/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java index a3b4ac7..3855333 100644 --- a/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java +++ b/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java @@ -1,8 +1,6 @@ package pw.rxj.iron_quarry.util; -import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.block.BlockState; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; @@ -107,17 +105,4 @@ public static String toString(OrderedText orderedText) { return RegistryKey.of(RegistryKey.ofRegistry(registry), value); } - public static void enableScaledScissor(int x, int y, int width, int height) { - int scaleFactor = (int) MinecraftClient.getInstance().getWindow().getScaleFactor(); - - RenderSystem.enableScissor(x * scaleFactor, MinecraftClient.getInstance().getWindow().getFramebufferHeight() - (y + height) * scaleFactor, width * scaleFactor, height * scaleFactor); - } - public static void disableScaledScissor() { - RenderSystem.disableScissor(); - } - public static void runScissored(int x, int y, int width, int height, Runnable runnable) { - ZUtil.enableScaledScissor(x, y, width, height); - runnable.run(); - ZUtil.disableScaledScissor(); - } }