-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 WorldToScreen-method for future additions.
🚚 Moved scissor utils to RenderUtil.
- Loading branch information
Showing
4 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters