Skip to content

Commit 0cffc94

Browse files
feat: Add DrawHelper#setupCameraTransformations
1 parent 0570ca6 commit 0cffc94

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

mod/src/main/kotlin/gg/skytils/skytilsmod/features/impl/dungeons/catlas/Catlas.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ object Catlas : EventSubscriber {
141141

142142
matrices.runWithGlobalState {
143143
matrices.push()
144-
DrawHelper.cameraOffset(matrices)
144+
DrawHelper.setupCameraTransformations(matrices)
145145
val linesBuffer = UBufferBuilder.create(UGraphics.DrawMode.LINES, UGraphics.CommonVertexFormats.POSITION_COLOR)
146146
val trianglesBuffer = UBufferBuilder.create(UGraphics.DrawMode.TRIANGLES, UGraphics.CommonVertexFormats.POSITION_COLOR)
147147
doors.forEach {

mod/src/main/kotlin/gg/skytils/skytilsmod/features/impl/handlers/Waypoints.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import gg.skytils.skytilsmod.core.PersistentSave
3434
import gg.skytils.skytilsmod.core.tickTimer
3535
import gg.skytils.skytilsmod.tweaker.DependencyLoader
3636
import gg.skytils.skytilsmod.utils.*
37+
import gg.skytils.skytilsmod.utils.rendering.DrawHelper
3738
import kotlinx.serialization.EncodeDefault
3839
import kotlinx.serialization.ExperimentalSerializationApi
3940
import kotlinx.serialization.Serializable
@@ -399,13 +400,15 @@ data class Waypoint @OptIn(ExperimentalSerializationApi::class) constructor(
399400
}
400401

401402
fun draw(partialTicks: Float, matrixStack: UMatrixStack) {
402-
val (viewerX, viewerY, viewerZ) = RenderUtil.getViewerPos(partialTicks)
403+
matrixStack.push()
404+
DrawHelper.setupCameraTransformations(matrixStack)
403405
RenderUtil.drawFilledBoundingBox(
404406
matrixStack,
405-
pos.toBoundingBox().expandBlock().offset(-viewerX, -viewerY, -viewerZ),
407+
pos.toBoundingBox().expandBlock(),
406408
color.withAlpha(color.alpha.coerceAtMost(128)),
407409
1f
408410
)
411+
matrixStack.pop()
409412
UGraphics.disableDepth()
410413
RenderUtil.renderWaypointText(
411414
name,

mod/src/main/kotlin/gg/skytils/skytilsmod/utils/RenderUtil.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ object RenderUtil {
9696
val buffer = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
9797
val matrices = UMatrixStack.Compat.get()
9898
matrices.push()
99-
DrawHelper.cameraOffset(matrices)
99+
DrawHelper.setupCameraTransformations(matrices)
100100
RenderSystem.lineWidth(width)
101101
DrawHelper.writeOutlineCube(buffer, matrices, aabb, color.multAlpha(1f))
102102
buffer.build()?.drawAndClose(if (throughWalls) SRenderPipelines.noDepthBoxPipeline else SRenderPipelines.boxPipeline)
@@ -140,7 +140,7 @@ object RenderUtil {
140140
alphaMultiplier: Float = 1f
141141
) {
142142
matrixStack.push()
143-
DrawHelper.cameraOffset(matrixStack)
143+
DrawHelper.setupCameraTransformations(matrixStack)
144144
RenderSystem.lineWidth(width.toFloat())
145145
val fixedColor = color.multAlpha(alphaMultiplier)
146146
val buffer = UBufferBuilder.create(UGraphics.DrawMode.LINE_STRIP, UGraphics.CommonVertexFormats.POSITION_COLOR)
@@ -159,7 +159,7 @@ object RenderUtil {
159159
alphaMultiplier: Float = 1f
160160
) {
161161
matrixStack.push()
162-
DrawHelper.cameraOffset(matrixStack)
162+
DrawHelper.setupCameraTransformations(matrixStack)
163163
RenderSystem.lineWidth(width.toFloat())
164164
val fixedColor = color.multAlpha(alphaMultiplier)
165165
val buffer = UBufferBuilder.create(UGraphics.DrawMode.LINE_STRIP, UGraphics.CommonVertexFormats.POSITION_COLOR)
@@ -226,7 +226,7 @@ object RenderUtil {
226226
//#endif
227227
// matrixStack.translate(0.0, -0.25, 0.0)
228228
//#if MC>=12000
229-
// matrixStack.multiply(mc.entityRenderDispatcher.rotation.invert())
229+
// matrixStack.multiply(mc.entityRenderDispatcher.rotation.invert(Quaternionf()))
230230
//#else
231231
//$$ matrixStack.rotate(-mc.entityRenderDispatcher.cameraPitch, 1.0f, 0.0f, 0.0f)
232232
//$$ matrixStack.rotate(mc.entityRenderDispatcher.cameraYaw, 0.0f, 1.0f, 0.0f)
@@ -250,7 +250,7 @@ object RenderUtil {
250250
shadow: Boolean = true, scale: Float = 1f, background: Boolean = true
251251
) {
252252
matrixStack.push()
253-
DrawHelper.cameraOffset(matrixStack)
253+
DrawHelper.setupCameraTransformations(matrixStack)
254254
DrawHelper.drawNametag(matrixStack, str, x, y, z, shadow, scale, background)
255255
matrixStack.pop()
256256
}

mod/src/main/kotlin/gg/skytils/skytilsmod/utils/rendering/DrawHelper.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import net.minecraft.util.Colors
3131
import net.minecraft.util.Identifier
3232
import net.minecraft.util.math.Box
3333
import net.minecraft.util.math.MathHelper
34+
import org.joml.Quaternionf
3435
import java.awt.Color
3536

3637
object DrawHelper {
@@ -44,6 +45,23 @@ object DrawHelper {
4445
matrices.translate(mc.gameRenderer.camera.pos.negate())
4546
}
4647

48+
/**
49+
* Applies the camera rotation to the given matrices.
50+
* This is useful for rendering things in world space, as it will negate the camera rotation.
51+
*/
52+
fun cameraRotation(matrices: UMatrixStack) {
53+
matrices.multiply(mc.gameRenderer.camera.rotation.conjugate(Quaternionf()))
54+
}
55+
56+
/**
57+
* Applies the camera offset and rotation to the given matrices.
58+
* This is useful for rendering things in world space, as it will negate the camera position and rotation.
59+
*/
60+
fun setupCameraTransformations(matrices: UMatrixStack) {
61+
cameraOffset(matrices)
62+
cameraRotation(matrices)
63+
}
64+
4765
/**
4866
* Writes a cube outline to the given buffer. Draw must still be called manually.
4967
* Buffer must be created with [gg.essential.universal.UGraphics.DrawMode.LINES]

0 commit comments

Comments
 (0)