Skip to content

Commit

Permalink
🚧 WorldToScreen-method for future additions.
Browse files Browse the repository at this point in the history
🚚 Moved scissor utils to RenderUtil.
  • Loading branch information
RXJpaw committed Dec 9, 2023
1 parent bdc54aa commit 60b88af
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
44 changes: 44 additions & 0 deletions src/main/java/pw/rxj/iron_quarry/renderer/RenderUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 39 additions & 0 deletions src/main/java/pw/rxj/iron_quarry/renderer/ScreenPos.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/pw/rxj/iron_quarry/util/ZUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 60b88af

Please sign in to comment.