Skip to content

Commit

Permalink
fix: don't include Renderer as it requires other dependencies to be i…
Browse files Browse the repository at this point in the history
…nstalled manually
  • Loading branch information
Octol1ttle committed Aug 20, 2024
1 parent 8e6dea8 commit a58fd2d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ dependencies {

// Embedded: Alternate HudRendererCallback to fix HUD not rendering behind chat
include(modImplementation "com.github.shadowhunt22:alternate-hudrenderercallback:${project.ahrc_version}")
// Embedded: Renderer for worldspace->screenspace conversions
include(modImplementation "com.github.0x3C50:Renderer:${project.renderer_version}")
// Required: Fabric API for callbacks
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fapi_version}+${project.minecraft_version}"
// Required: YACL for config storage and screen
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ archives_base_name=flightassistant

# Dependencies
ahrc_version=1.20.x-0.1.3
renderer_version=aa67e1c979
fapi_version=0.96.4
yacl_version=3.3.1
modmenu_version=9.0.0
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/ru/octol1ttle/flightassistant/DrawHelper.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ru.octol1ttle.flightassistant;

import java.awt.Color;
import me.x150.renderer.util.RendererUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec3d;
import ru.octol1ttle.flightassistant.compatibility.immediatelyfast.HUDBatching;
import ru.octol1ttle.flightassistant.util.ScreenSpaceRendering;

public abstract class DrawHelper {
private static final int SINGLE_LINE_DRAWN = 1;
Expand All @@ -18,26 +18,26 @@ public static MutableText asText(String key, Object... args) {
}

public static Vec3d getScreenSpace(Vec3d delta) {
Vec3d vec = RendererUtils.worldSpaceToScreenSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(delta));
if (!RendererUtils.screenSpaceCoordinateIsVisible(vec)) {
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(delta));
if (!ScreenSpaceRendering.isVisible(vec)) {
return null;
}

return vec;
}

public static Integer getScreenSpaceX(float pitch, float yaw) {
Vec3d vec = RendererUtils.worldSpaceToScreenSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(Vec3d.fromPolar(-pitch, yaw)));
if (!RendererUtils.screenSpaceCoordinateIsVisible(vec)) {
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(Vec3d.fromPolar(-pitch, yaw)));
if (!ScreenSpaceRendering.isVisible(vec)) {
return null;
}

return FAMathHelper.round(vec.x);
}

public static Integer getScreenSpaceY(float pitch, float yaw) {
Vec3d vec = RendererUtils.worldSpaceToScreenSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(Vec3d.fromPolar(-pitch, yaw)));
if (!RendererUtils.screenSpaceCoordinateIsVisible(vec)) {
Vec3d vec = ScreenSpaceRendering.fromWorldSpace(MinecraftClient.getInstance().getEntityRenderDispatcher().camera.getPos().add(Vec3d.fromPolar(-pitch, yaw)));
if (!ScreenSpaceRendering.isVisible(vec)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ru.octol1ttle.flightassistant;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.shadowhunter22.api.client.renderer.v1.AlternateHudRendererCallback;
import java.util.Optional;
import me.x150.renderer.event.RenderEvents;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
Expand Down Expand Up @@ -40,6 +40,7 @@
import ru.octol1ttle.flightassistant.config.ComputerConfig;
import ru.octol1ttle.flightassistant.config.FAConfig;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;
import ru.octol1ttle.flightassistant.util.ScreenSpaceRendering;
import ru.octol1ttle.flightassistant.util.events.ChangeLookDirectionEvents;
import ru.octol1ttle.flightassistant.util.events.FireworkBoostCallback;

Expand Down Expand Up @@ -90,6 +91,9 @@ public void onEndTick(MinecraftClient client) {
@Override
public void onStart(WorldRenderContext context) {
ComputerHost.instance().tick();
ScreenSpaceRendering.lastProjMat.set(RenderSystem.getProjectionMatrix());
ScreenSpaceRendering.lastModMat.set(RenderSystem.getModelViewMatrix());
ScreenSpaceRendering.lastWorldSpaceMatrix.set(context.matrixStack().peek().getPositionMatrix());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package ru.octol1ttle.flightassistant.util;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.opengl.GL11;

/**
* @author 0x150
* @see <a href="https://github.com/0x3C50/Renderer">Original source code</a>
*/
public class ScreenSpaceRendering {
private static final MinecraftClient client = MinecraftClient.getInstance();

@ApiStatus.Internal
public static final Matrix4f lastProjMat = new Matrix4f();
@ApiStatus.Internal
public static final Matrix4f lastModMat = new Matrix4f();
@ApiStatus.Internal
public static final Matrix4f lastWorldSpaceMatrix = new Matrix4f();

/**
*
* Transforms an input position into a (x, y, d) coordinate, transformed to screen space. d specifies the far plane of the position, and can be used to check if the position is on screen. Use {@link #isVisible(Vec3d)}.
* Example:
* <pre>
* {@code
* // Hud render event
* Vec3d targetPos = new Vec3d(100, 64, 100); // world space
* Vec3d screenSpace = ScreenSpaceRendering.fromWorldSpace(targetPos);
* if (ScreenSpaceRendering.isVisible(screenSpace)) {
* // do something with screenSpace.x and .y
* }
* }
* </pre>
*
* @param pos The world space coordinates to translate
* @return The (x, y, d) coordinates
* @throws NullPointerException If {@code pos} is null
*/
@Contract(value = "_ -> new", pure = true)
public static Vec3d fromWorldSpace(@NotNull Vec3d pos) {
Camera camera = client.getEntityRenderDispatcher().camera;
int displayHeight = client.getWindow().getHeight();
int[] viewport = new int[4];
GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
Vector3f target = new Vector3f();

double deltaX = pos.x - camera.getPos().x;
double deltaY = pos.y - camera.getPos().y;
double deltaZ = pos.z - camera.getPos().z;

Vector4f transformedCoordinates = new Vector4f((float) deltaX, (float) deltaY, (float) deltaZ, 1.f).mul(
lastWorldSpaceMatrix);

Matrix4f matrixProj = new Matrix4f(lastProjMat);
Matrix4f matrixModel = new Matrix4f(lastModMat);

matrixProj.mul(matrixModel)
.project(transformedCoordinates.x(), transformedCoordinates.y(), transformedCoordinates.z(), viewport,
target);

return new Vec3d(target.x / client.getWindow().getScaleFactor(),
(displayHeight - target.y) / client.getWindow().getScaleFactor(), target.z);
}

/**
* Checks if a screen space coordinate (x, y, d) is on screen
*
* @param pos The (x, y, d) coordinates to check
* @return True if the coordinates are visible
*/
public static boolean isVisible(Vec3d pos) {
return pos != null && pos.z > -1 && pos.z < 1;
}
}

0 comments on commit a58fd2d

Please sign in to comment.