Skip to content

Commit

Permalink
Renderer: Remove need to call rotateToCamera and translateToPlayer
Browse files Browse the repository at this point in the history
Also adds lineWidth function
  • Loading branch information
camnwalter committed Dec 2, 2023
1 parent e865018 commit 7fb124c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
9 changes: 6 additions & 3 deletions api/ctjs.api
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,7 @@ public final class com/chattriggers/ctjs/api/render/Renderer {
public static final fun getRenderPos (FFF)Lcom/chattriggers/ctjs/api/vec/Vec3f;
public static final fun getStringWidth (Ljava/lang/String;)I
public static final fun light (II)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun lineWidth (F)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun multiply (Lorg/joml/Quaternionf;)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun normal (FFF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun overlay (II)Lcom/chattriggers/ctjs/api/render/Renderer;
Expand All @@ -1762,7 +1763,10 @@ public final class com/chattriggers/ctjs/api/render/Renderer {
public static final fun pushMatrix (Lgg/essential/universal/UMatrixStack;)Lcom/chattriggers/ctjs/api/render/Renderer;
public static synthetic fun pushMatrix$default (Lgg/essential/universal/UMatrixStack;ILjava/lang/Object;)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun rotate (F)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun rotateToCamera ()Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun rotate (FF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun rotate (FFF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun rotate (FFFF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static synthetic fun rotate$default (FFFFILjava/lang/Object;)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun scale (F)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun scale (FF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun scale (FFF)Lcom/chattriggers/ctjs/api/render/Renderer;
Expand All @@ -1771,7 +1775,6 @@ public final class com/chattriggers/ctjs/api/render/Renderer {
public static final fun translate (FF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun translate (FFF)Lcom/chattriggers/ctjs/api/render/Renderer;
public static synthetic fun translate$default (FFFILjava/lang/Object;)Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun translateToPlayer ()Lcom/chattriggers/ctjs/api/render/Renderer;
public static final fun tryBlendFuncSeparate (IIII)Lcom/chattriggers/ctjs/api/render/Renderer;
}

Expand Down Expand Up @@ -1845,8 +1848,8 @@ public final class com/chattriggers/ctjs/api/render/Renderer3d {
public static final fun drawString (Ljava/lang/String;FFFJZFZZZ)V
public static final fun drawString (Lorg/mozilla/javascript/NativeObject;)V
public static synthetic fun drawString$default (Ljava/lang/String;FFFJZFZZZILjava/lang/Object;)V
public static final fun endVertex ()Lcom/chattriggers/ctjs/api/render/Renderer3d;
public static final fun light (II)Lcom/chattriggers/ctjs/api/render/Renderer3d;
public static final fun lineWidth (F)Lcom/chattriggers/ctjs/api/render/Renderer3d;
public static final fun normal (FFF)Lcom/chattriggers/ctjs/api/render/Renderer3d;
public static final fun overlay (II)Lcom/chattriggers/ctjs/api/render/Renderer3d;
public static final fun pos (FFF)Lcom/chattriggers/ctjs/api/render/Renderer3d;
Expand Down
35 changes: 16 additions & 19 deletions src/main/kotlin/com/chattriggers/ctjs/api/render/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,6 @@ object Renderer {
matrixStack.pop()
}

@JvmStatic
fun rotateToCamera() = apply {
val camera = Client.getMinecraft().gameRenderer.camera
multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.pitch))
multiply(RotationAxis.POSITIVE_Y.rotationDegrees(camera.yaw + 180f))
}

@JvmStatic
fun translateToPlayer() = apply {
translate(
-Client.camera.getX().toFloat(),
-Client.camera.getY().toFloat(),
-Client.camera.getZ().toFloat()
)
}

@JvmStatic
@JvmOverloads
fun translate(x: Float, y: Float, z: Float = 0.0F) = apply {
Expand All @@ -273,8 +257,9 @@ object Renderer {
}

@JvmStatic
fun rotate(angle: Float) = apply {
matrixStack.rotate(angle, 0f, 0f, 1f)
@JvmOverloads
fun rotate(angle: Float, x: Float = 0f, y: Float = 0f, z: Float = 1f) = apply {
matrixStack.rotate(angle, x, y, z)
}

@JvmStatic
Expand Down Expand Up @@ -342,7 +327,8 @@ object Renderer {
@JvmStatic
@JvmOverloads
fun pos(x: Float, y: Float, z: Float = 0f) = apply {
Renderer3d.pos(x, y, z)
val camera = Client.getMinecraft().gameRenderer.camera.pos
Renderer3d.pos(x + camera.x.toFloat(), y + camera.y.toFloat(), z + camera.z.toFloat())
}

/**
Expand Down Expand Up @@ -436,6 +422,17 @@ object Renderer {
Renderer3d.light(u, v)
}

/**
* Sets the line width when rendering [DrawMode.LINES]
*
* @param width the width of the line
* @return [Renderer] to allow for method chaining
*/
@JvmStatic
fun lineWidth(width: Float) = apply {
Renderer3d.lineWidth(width)
}

/**
* Finalizes vertices and draws the world renderer.
*/
Expand Down
23 changes: 12 additions & 11 deletions src/main/kotlin/com/chattriggers/ctjs/api/render/Renderer3d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import gg.essential.elementa.dsl.component2
import gg.essential.elementa.dsl.component3
import gg.essential.elementa.dsl.component4
import gg.essential.universal.UGraphics
import gg.essential.universal.UMatrixStack
import net.minecraft.client.MinecraftClient
import net.minecraft.client.font.TextRenderer
import net.minecraft.client.render.Tessellator
Expand Down Expand Up @@ -43,7 +42,8 @@ object Renderer3d {
vertexFormat: Renderer.VertexFormat = Renderer.VertexFormat.POSITION,
) = apply {
Renderer.pushMatrix()
Renderer.enableBlend()
.enableBlend()
.disableCull()
Renderer.tryBlendFuncSeparate(770, 771, 1, 0)

worldRenderer.beginWithDefaultShader(drawMode.toUC(), vertexFormat.toMC())
Expand All @@ -66,7 +66,8 @@ object Renderer3d {
begin()
if (!firstVertex)
worldRenderer.endVertex()
worldRenderer.pos(Renderer.matrixStack, x.toDouble(), y.toDouble(), z.toDouble())
val camera = Client.getMinecraft().gameRenderer.camera.pos
worldRenderer.pos(Renderer.matrixStack, x.toDouble() - camera.x, y.toDouble() - camera.y, z.toDouble() - camera.z)
firstVertex = false
}

Expand Down Expand Up @@ -162,13 +163,14 @@ object Renderer3d {
}

/**
* End the current vertex
* Sets the line width when rendering [Renderer.DrawMode.LINES]
*
* @param width the width of the line
* @return [Renderer3d] to allow for method chaining
*/
@JvmStatic
fun endVertex() = apply {
worldRenderer.endVertex()
fun lineWidth(width: Float) = apply {
RenderSystem.lineWidth(width)
}

/**
Expand All @@ -185,6 +187,7 @@ object Renderer3d {
worldRenderer.drawDirect()
Renderer.colorize(1f, 1f, 1f, 1f)
.disableBlend()
.enableCull()
.popMatrix()
}

Expand Down Expand Up @@ -345,10 +348,8 @@ object Renderer3d {
z2: Float,
thickness: Float,
) {
Renderer.pushMatrix(UMatrixStack())
Renderer.pushMatrix()
.disableDepth()
.rotateToCamera()
.translateToPlayer()
.disableCull()
RenderSystem.lineWidth(thickness)

Expand All @@ -357,8 +358,8 @@ object Renderer3d {
val normalVec = Vector3f(x2 - x1, y2 - y1, z2 - z1).normalize()

begin(Renderer.DrawMode.LINES, Renderer.VertexFormat.LINES)
pos(x1, y1, z1).color(r, g, b, a).normal(normalVec.x, normalVec.y, normalVec.z).endVertex()
pos(x2, y2, z2).color(r, g, b, a).normal(normalVec.x, normalVec.y, normalVec.z).endVertex()
pos(x1, y1, z1).color(r, g, b, a).normal(normalVec.x, normalVec.y, normalVec.z)
pos(x2, y2, z2).color(r, g, b, a).normal(normalVec.x, normalVec.y, normalVec.z)
draw()

RenderSystem.lineWidth(1f)
Expand Down

0 comments on commit 7fb124c

Please sign in to comment.