Skip to content

Commit 6994dd0

Browse files
committed
refine waypoint rendering
1 parent fed8dd1 commit 6994dd0

File tree

6 files changed

+206
-90
lines changed

6 files changed

+206
-90
lines changed

1.20/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("UnstableApiUsage")
2+
13
plugins {
24
id("fabric-loom")
35
id("io.github.p03w.machete")

1.20/src/main/java/io/github/axolotlclient/waypoints/waypoints/WaypointRenderer.java

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package io.github.axolotlclient.waypoints.waypoints;
2424

25+
import java.lang.Math;
2526
import java.util.concurrent.atomic.AtomicReference;
2627

2728
import com.mojang.blaze3d.systems.RenderSystem;
@@ -37,10 +38,7 @@
3738
import net.minecraft.client.renderer.RenderType;
3839
import net.minecraft.world.phys.Vec3;
3940
import org.jetbrains.annotations.Nullable;
40-
import org.joml.Matrix4f;
41-
import org.joml.Quaternionf;
42-
import org.joml.Vector3f;
43-
import org.joml.Vector4f;
41+
import org.joml.*;
4442

4543
public class WaypointRenderer {
4644

@@ -78,15 +76,16 @@ private void renderWaypoint(Waypoint waypoint, PoseStack stack, Vec3 camPos, Cam
7876
int width = textWidth + Waypoint.displayXOffset() * 2;
7977
int textHeight = minecraft.font.lineHeight;
8078
int height = textHeight + Waypoint.displayYOffset() * 2;
81-
var displayStart = projectToScreen(cam, fov, width, height, waypoint.x() - (width / 2f * 0.04), waypoint.y() - (height / 2f * 0.04), waypoint.z());
79+
var displayStart = projectToScreen(cam, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f(-(width / 2f * 0.04f), (height / 2f * 0.04f)));
8280
if (displayStart == null) return;
83-
var displayEnd = projectToScreen(cam, fov, width, height, waypoint.x() + (width / 2f * 0.04), waypoint.y() + (height / 2f * 0.04), waypoint.z());
81+
var displayEnd = projectToScreen(cam, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f(width / 2f * 0.04f, -(height / 2f * 0.04f)));
8482
if (displayEnd == null) return;
8583
float projWidth = Math.abs(displayEnd.x() - displayStart.x());
8684
float projHeight = Math.abs(displayEnd.y() - displayStart.y());
87-
if (projWidth / width <= 1 && projHeight / height <= 1) {
85+
if (projWidth < width && projHeight < height) {
8886
return;
8987
}
88+
9089
stack.pushPose();
9190
stack.translate(waypoint.x() - camPos.x(), waypoint.y() - camPos.y(), waypoint.z() - camPos.z());
9291
stack.mulPose(cam.rotation());
@@ -133,7 +132,7 @@ public void renderWaypoints(GuiGraphics graphics, float delta) {
133132
}
134133

135134
private void renderWaypoint(Waypoint waypoint, GuiGraphics graphics, float tick, Camera camera, AtomicReference<Runnable> positionDrawn) {
136-
var fov = ((GameRendererAccessor) minecraft.gameRenderer).invokeGetFov(camera, tick, true);
135+
var fov = ((GameRendererAccessor) minecraft.gameRenderer).invokeGetFov(camera, tick, false);
137136
var pose = graphics.pose();
138137

139138
var textWidth = minecraft.font.width(waypoint.display());
@@ -142,27 +141,44 @@ private void renderWaypoint(Waypoint waypoint, GuiGraphics graphics, float tick,
142141
int height = textHeight + Waypoint.displayYOffset() * 2;
143142
var camPos = camera.getPosition();
144143

145-
var displayStart = projectToScreen(camera, fov, width, height, waypoint.x() - (width / 2f * 0.04), waypoint.y() - (height / 2f * 0.04), waypoint.z());
146-
if (displayStart == null) return;
147-
var displayEnd = projectToScreen(camera, fov, width, height, waypoint.x() + (width / 2f * 0.04), waypoint.y() + (height / 2f * 0.04), waypoint.z());
148-
if (displayEnd == null) return;
149-
float projWidth = Math.abs(displayEnd.x() - displayStart.x());
150-
float projHeight = Math.abs(displayEnd.y() - displayStart.y());
151-
Result result = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z());
144+
var displayStart = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f(-(width / 2f * 0.04f), (height / 2f * 0.04f)));
145+
var displayEnd = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f((width / 2f * 0.04f), -(height / 2f * 0.04f)));
146+
Result result = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), null);
152147
if (result == null) return;
148+
float projWidth;
149+
float projHeight;
150+
if (displayStart != null && displayEnd != null) {
151+
projWidth = Math.abs(displayEnd.x() - displayStart.x());
152+
projHeight = Math.abs(displayEnd.y() - displayStart.y());
153+
} else {
154+
projWidth = 0;
155+
projHeight = 0;
156+
}
153157

154158
pose.translate(result.x(), result.y(), 0);
155-
156-
if (!AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get() && (result.x() < -width / 2f || result.x() > graphics.guiWidth() + width / 2f || result.y() < -height / 2f || result.y() > graphics.guiHeight() + height / 2f)) {
157-
return;
158-
}
159-
if (projWidth / width > 1 || projHeight / height > 1) {
159+
boolean outOfView = result.x() < -width / 2f || result.x() > graphics.guiWidth() + width / 2f || result.y() < -height / 2f || result.y() > graphics.guiHeight() + height / 2f;
160+
if (!AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get() && outOfView) {
160161
return;
161162
}
162163

163-
if (positionDrawn.get() == null && Math.abs(result.x() - graphics.guiWidth() / 2f) < width / 2f && Math.abs(result.y() - graphics.guiHeight() / 2f) < height / 2f) {
164+
boolean _3dOnScreen;
165+
if (displayEnd != null && displayStart != null) {
166+
float minX = displayStart.x();
167+
float minY = displayStart.y();
168+
float maxX = displayEnd.x();
169+
float maxY = displayEnd.y();
170+
int guiWidth = graphics.guiWidth();
171+
int guiHeight = graphics.guiHeight();
172+
_3dOnScreen = minX > 0 && minY > 0 && minX < guiWidth && minY < guiHeight ||
173+
minX > 0 && maxY > 0 && minX < guiWidth && maxY < guiHeight ||
174+
maxX > 0 && maxY > 0 && maxX < guiWidth && maxY < guiHeight ||
175+
maxX > 0 && minY > 0 && maxX < guiWidth && minY < guiHeight;
176+
} else {
177+
_3dOnScreen = false;
178+
}
179+
if (positionDrawn.get() == null && Math.abs(result.x() - graphics.guiWidth() / 2f) < (_3dOnScreen ? Math.max(projWidth, width) : width) / 2f && Math.abs(result.y() - graphics.guiHeight() / 2f) < (_3dOnScreen ? Math.max(height, projHeight) : height) / 2f) {
164180
pose.pushPose();
165-
pose.translate(0, height / 2f + 2, 0);
181+
pose.translate(0, Math.max(height, projHeight + 4) / 2f + 4, 0);
166182
var pos = pose.last().pose().transformPosition(new Vector3f());
167183
positionDrawn.set(() -> {
168184
var line1 = waypoint.name();
@@ -182,20 +198,32 @@ private void renderWaypoint(Waypoint waypoint, GuiGraphics graphics, float tick,
182198
pose.popPose();
183199
}
184200

201+
if ((projWidth >= width || projHeight >= height) && _3dOnScreen) {
202+
return;
203+
}
204+
185205
graphics.fill(-width / 2, -height / 2, width / 2, height / 2, waypoint.color().toInt());
186206
graphics.drawString(minecraft.font, waypoint.display(), -textWidth / 2, -textHeight / 2, -1, false);
187207
}
188208

189-
private @Nullable Result projectToScreen(Camera camera, double fov, int width, int height, double x, double y, double z) {
209+
private @Nullable Result projectToScreen(Camera camera, double fov, int width, int height, double x, double y, double z, Vector2f orthoOffset) {
190210
viewProj.set(x, y, z, 1);
211+
if (orthoOffset != null) {
212+
var vec = new Matrix4f();
213+
var camRot = camera.rotation().rotateY((float) -(Math.PI), new Quaternionf()).invert();
214+
vec.rotate(camRot.invert(new Quaternionf()));
215+
vec.translate(orthoOffset.x(), orthoOffset.y(), 0);
216+
vec.rotate(camRot);
217+
vec.transform(viewProj);
218+
}
191219
view.rotation(camera.rotation().rotateY((float) -(Math.PI), new Quaternionf()).invert()).translate(camera.getPosition().toVector3f().negate());
192220

193221
Matrix4f projection = minecraft.gameRenderer.getProjectionMatrix(fov);
194222
projection.mul(view);
195223
viewProj.mul(projection);
196224

197225

198-
if (AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get()) {
226+
if (orthoOffset == null && AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get()) {
199227
viewProj.w = Math.max(Math.abs(viewProj.x()), Math.max(Math.abs(viewProj.y()), viewProj.w()));
200228
}
201229

1.21.8/src/main/java/io/github/axolotlclient/waypoints/waypoints/WaypointRenderer.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,16 @@ private void renderWaypoint(Waypoint waypoint, PoseStack stack, Vec3 camPos, Cam
101101
int width = textWidth + Waypoint.displayXOffset() * 2;
102102
int textHeight = minecraft.font.lineHeight;
103103
int height = textHeight + Waypoint.displayYOffset() * 2;
104-
var displayStart = projectToScreen(cam, fov, width, height, waypoint.x() - (width / 2f * 0.04), waypoint.y() - (height / 2f * 0.04), waypoint.z());
104+
var displayStart = projectToScreen(cam, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f(-(width / 2f * 0.04f), (height / 2f * 0.04f)));
105105
if (displayStart == null) return;
106-
var displayEnd = projectToScreen(cam, fov, width, height, waypoint.x() + (width / 2f * 0.04), waypoint.y() + (height / 2f * 0.04), waypoint.z());
106+
var displayEnd = projectToScreen(cam, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f(width / 2f * 0.04f, -(height / 2f * 0.04f)));
107107
if (displayEnd == null) return;
108108
float projWidth = Math.abs(displayEnd.x() - displayStart.x());
109109
float projHeight = Math.abs(displayEnd.y() - displayStart.y());
110-
if (projWidth / width <= 1 && projHeight / height <= 1) {
110+
if (projWidth < width && projHeight < height) {
111111
return;
112112
}
113+
113114
stack.pushPose();
114115
stack.translate(waypoint.x() - camPos.x(), waypoint.y() - camPos.y(), waypoint.z() - camPos.z());
115116
stack.mulPose(cam.rotation().invert(new Quaternionf()));
@@ -165,36 +166,52 @@ private void renderWaypoint(Waypoint waypoint, GuiGraphics graphics, DeltaTracke
165166
int height = textHeight + Waypoint.displayYOffset() * 2;
166167
var camPos = camera.position();
167168

168-
var displayStart = projectToScreen(camera, fov, width, height, waypoint.x() - (width / 2f * 0.04), waypoint.y() - (height / 2f * 0.04), waypoint.z());
169-
if (displayStart == null) return;
170-
var displayEnd = projectToScreen(camera, fov, width, height, waypoint.x() + (width / 2f * 0.04), waypoint.y() + (height / 2f * 0.04), waypoint.z());
171-
if (displayEnd == null) return;
172-
float projWidth = Math.abs(displayEnd.x() - displayStart.x());
173-
float projHeight = Math.abs(displayEnd.y() - displayStart.y());
174-
Result result = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z());
169+
var displayStart = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f(-(width / 2f * 0.04f), (height / 2f * 0.04f)));
170+
var displayEnd = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), new Vector2f((width / 2f * 0.04f), -(height / 2f * 0.04f)));
171+
Result result = projectToScreen(camera, fov, width, height, waypoint.x(), waypoint.y(), waypoint.z(), null);
175172
if (result == null) return;
173+
float projWidth;
174+
float projHeight;
175+
if (displayStart != null && displayEnd != null) {
176+
projWidth = Math.abs(displayEnd.x() - displayStart.x());
177+
projHeight = Math.abs(displayEnd.y() - displayStart.y());
178+
} else {
179+
projWidth = 0;
180+
projHeight = 0;
181+
}
176182

177183
pose.translate(result.x(), result.y());
178-
179-
if (!AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get() && (result.x() < -width / 2f || result.x() > graphics.guiWidth() + width / 2f || result.y() < -height / 2f || result.y() > graphics.guiHeight() + height / 2f)) {
184+
boolean outOfView = result.x() < -width / 2f || result.x() > graphics.guiWidth() + width / 2f || result.y() < -height / 2f || result.y() > graphics.guiHeight() + height / 2f;
185+
if (!AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get() && outOfView) {
180186
return;
181187
}
182188

183-
if (projWidth / width > 1 || projHeight / height > 1) {
184-
return;
189+
boolean _3dOnScreen;
190+
if (displayEnd != null && displayStart != null) {
191+
float minX = displayStart.x();
192+
float minY = displayStart.y();
193+
float maxX = displayEnd.x();
194+
float maxY = displayEnd.y();
195+
int guiWidth = graphics.guiWidth();
196+
int guiHeight = graphics.guiHeight();
197+
_3dOnScreen = minX > 0 && minY > 0 && minX < guiWidth && minY < guiHeight ||
198+
minX > 0 && maxY > 0 && minX < guiWidth && maxY < guiHeight ||
199+
maxX > 0 && maxY > 0 && maxX < guiWidth && maxY < guiHeight ||
200+
maxX > 0 && minY > 0 && maxX < guiWidth && minY < guiHeight;
201+
} else {
202+
_3dOnScreen = false;
185203
}
186-
187-
if (positionDrawn.get() == null && Math.abs(result.x() - graphics.guiWidth() / 2f) < width / 2f && Math.abs(result.y() - graphics.guiHeight() / 2f) < height / 2f) {
204+
if (positionDrawn.get() == null && Math.abs(result.x() - graphics.guiWidth() / 2f) < (_3dOnScreen ? Math.max(projWidth, width) : width) / 2f && Math.abs(result.y() - graphics.guiHeight() / 2f) < (_3dOnScreen ? Math.max(height, projHeight) : height) / 2f) {
188205
pose.pushMatrix();
189-
pose.translate(0, height / 2f + 2);
206+
pose.translate(0, Math.max(height, projHeight + 4) / 2f + 4);
190207
var pos = pose.transformPosition(new Vector2f());
191208
positionDrawn.set(() -> {
192209
var line1 = waypoint.name();
193210
pose.pushMatrix();
194211
pose.translate(pos);
195212
int line1W = minecraft.font.width(line1);
196-
graphics.fill(-line1W/2 -2, -2, line1W/2+2, minecraft.font.lineHeight+2, Colors.GRAY.withAlpha(100).toInt());
197-
graphics.renderOutline(-line1W/2 -2, -2, line1W+4, minecraft.font.lineHeight+4, Colors.GRAY.toInt());
213+
graphics.fill(-line1W / 2 - 2, -2, line1W / 2 + 2, minecraft.font.lineHeight + 2, Colors.GRAY.withAlpha(100).toInt());
214+
graphics.renderOutline(-line1W / 2 - 2, -2, line1W + 4, minecraft.font.lineHeight + 4, Colors.GRAY.toInt());
198215
graphics.drawString(minecraft.font, line1, -line1W / 2, 0, -1, true);
199216
if (!waypoint.closerToThan(camPos.x(), camPos.y(), camPos.z(), CUTOFF_DIST)) {
200217
pose.translate(0, minecraft.font.lineHeight + 4);
@@ -206,20 +223,31 @@ private void renderWaypoint(Waypoint waypoint, GuiGraphics graphics, DeltaTracke
206223
pose.popMatrix();
207224
}
208225

226+
if ((projWidth >= width || projHeight >= height) && _3dOnScreen) {
227+
return;
228+
}
209229

210230
graphics.fill(-width / 2, -height / 2, width / 2, height / 2, waypoint.color().toInt());
211231
graphics.drawString(minecraft.font, waypoint.display(), -textWidth / 2, -textHeight / 2, -1, false);
212232
}
213233

214-
private @Nullable Result projectToScreen(Camera camera, float fov, float xScreenMargin, float yScreenMargin, double x, double y, double z) {
234+
private @Nullable Result projectToScreen(Camera camera, float fov, float xScreenMargin, float yScreenMargin, double x, double y, double z, Vector2f orthoOffset) {
215235
viewProj.set(x, y, z, 1);
236+
if (orthoOffset != null) {
237+
var vec = new Matrix4f();
238+
vec.rotate(camera.rotation().invert(new Quaternionf()));
239+
vec.translate(orthoOffset.x(), orthoOffset.y(), 0);
240+
vec.rotate(camera.rotation());
241+
vec.transform(viewProj);
242+
}
243+
216244
view.rotation(camera.rotation()).translate(camera.position().toVector3f().negate());
217245

218246
Matrix4f projection = minecraft.gameRenderer.getProjectionMatrix(fov);
219247
projection.mul(view);
220248
viewProj.mul(projection);
221249

222-
if (AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get()) {
250+
if (orthoOffset == null && AxolotlClientWaypoints.renderOutOfViewWaypointsOnScreenEdge.get()) {
223251
viewProj.w = Math.max(Math.abs(viewProj.x()), Math.max(Math.abs(viewProj.y()), viewProj.w()));
224252
}
225253

1.21/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("UnstableApiUsage")
2+
13
plugins {
24
id("fabric-loom")
35
id("io.github.p03w.machete")

0 commit comments

Comments
 (0)