aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-06-12 00:19:32 +0200
committernea <nea@nea.moe>2023-06-12 00:19:32 +0200
commita74a2fb1d571da6a4a87578f354b18921b01d8d4 (patch)
tree042dfc0104669983864ff5a1c1a59e32bfdbeee4 /src/main/kotlin/moe/nea/firmament/util
parent74e79759bf8888ea08a6d244a50e531aaae20f27 (diff)
downloadfirmament-a74a2fb1d571da6a4a87578f354b18921b01d8d4.tar.gz
firmament-a74a2fb1d571da6a4a87578f354b18921b01d8d4.tar.bz2
firmament-a74a2fb1d571da6a4a87578f354b18921b01d8d4.zip
Add lines to world renderer
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt66
1 files changed, 61 insertions, 5 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt b/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt
index 59a949a..9c38145 100644
--- a/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt
+++ b/src/main/kotlin/moe/nea/firmament/util/render/RenderInWorldContext.kt
@@ -19,7 +19,9 @@
package moe.nea.firmament.util.render
import com.mojang.blaze3d.systems.RenderSystem
+import java.lang.Math.pow
import org.joml.Matrix4f
+import org.joml.Vector3f
import net.minecraft.client.gl.VertexBuffer
import net.minecraft.client.render.BufferBuilder
import net.minecraft.client.render.Camera
@@ -28,10 +30,15 @@ import net.minecraft.client.render.Tessellator
import net.minecraft.client.render.VertexFormat
import net.minecraft.client.render.VertexFormats
import net.minecraft.client.util.math.MatrixStack
+import net.minecraft.client.util.math.MatrixStack.Entry
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
-class RenderInWorldContext private constructor(private val tesselator: Tessellator, private val matrixStack: MatrixStack) {
+class RenderInWorldContext private constructor(
+ private val tesselator: Tessellator,
+ private val matrixStack: MatrixStack,
+ private val camera: Camera
+) {
private val buffer = tesselator.buffer
fun color(red: Float, green: Float, blue: Float, alpha: Float) {
@@ -39,6 +46,7 @@ class RenderInWorldContext private constructor(private val tesselator: Tessellat
}
fun block(blockPos: BlockPos) {
+ RenderSystem.setShader(GameRenderer::getPositionColorProgram)
matrixStack.push()
matrixStack.translate(blockPos.x.toFloat(), blockPos.y.toFloat(), blockPos.z.toFloat())
buildCube(matrixStack.peek().positionMatrix, buffer)
@@ -47,6 +55,7 @@ class RenderInWorldContext private constructor(private val tesselator: Tessellat
}
fun tinyBlock(vec3d: Vec3d, size: Float) {
+ RenderSystem.setShader(GameRenderer::getPositionColorProgram)
matrixStack.push()
matrixStack.translate(vec3d.x, vec3d.y, vec3d.z)
matrixStack.scale(size, size, size)
@@ -56,11 +65,56 @@ class RenderInWorldContext private constructor(private val tesselator: Tessellat
matrixStack.pop()
}
- fun line(vararg points: Vec3d, size: Double = 2.0) {
+ fun wireframeCube(blockPos: BlockPos, lineWidth: Float = 10F) {
+ RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram)
+ matrixStack.push()
+ RenderSystem.lineWidth(lineWidth / pow(camera.pos.squaredDistanceTo(blockPos.toCenterPos()), 0.25).toFloat())
+ matrixStack.translate(blockPos.x.toFloat(), blockPos.y.toFloat(), blockPos.z.toFloat())
+ buildWireFrameCube(matrixStack.peek(), buffer)
+ tesselator.draw()
+ matrixStack.pop()
+ }
+ fun line(vararg points: Vec3d, lineWidth: Float = 10F) {
+ RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram)
+ RenderSystem.lineWidth(lineWidth / pow(camera.pos.squaredDistanceTo(points.first()), 0.25).toFloat())
+ buffer.begin(VertexFormat.DrawMode.LINES, VertexFormats.LINES)
+ buffer.fixedColor(255, 255, 255, 255)
+
+ points.toList().zipWithNext().forEach { (a, b) ->
+ doLine(matrixStack.peek(), buffer, a.x, a.y, a.z, b.x, b.y, b.z)
+ }
+ buffer.unfixColor()
+
+ tesselator.draw()
}
companion object {
+ private fun doLine(matrix: Entry, buf: BufferBuilder, i: Number, j: Number, k: Number, x: Number, y: Number, z: Number) {
+ val normal = Vector3f(x.toFloat(), y.toFloat(), z.toFloat())
+ .sub(i.toFloat(), j.toFloat(), k.toFloat())
+ .mul(-1F)
+ buf.vertex(matrix.positionMatrix, i.toFloat(), j.toFloat(), k.toFloat())
+ .normal(matrix.normalMatrix, normal.x, normal.y, normal.z).next()
+ buf.vertex(matrix.positionMatrix, x.toFloat(), y.toFloat(), z.toFloat())
+ .normal(matrix.normalMatrix, normal.x, normal.y, normal.z).next()
+ }
+
+
+ private fun buildWireFrameCube(matrix: MatrixStack.Entry, buf: BufferBuilder) {
+ buf.begin(VertexFormat.DrawMode.LINES, VertexFormats.LINES)
+ buf.fixedColor(255, 255, 255, 255)
+
+ for (i in 0..1) {
+ for (j in 0..1) {
+ doLine(matrix, buf, 0, i, j, 1, i, j)
+ doLine(matrix, buf, i, 0, j, i, 1, j)
+ doLine(matrix, buf, i, j, 0, i, j, 1)
+ }
+ }
+ buf.unfixColor()
+ }
+
private fun buildCube(matrix: Matrix4f, buf: BufferBuilder) {
buf.begin(VertexFormat.DrawMode.TRIANGLES, VertexFormats.POSITION_COLOR)
buf.fixedColor(255, 255, 255, 255)
@@ -107,19 +161,21 @@ class RenderInWorldContext private constructor(private val tesselator: Tessellat
RenderSystem.disableDepthTest()
RenderSystem.enableBlend()
RenderSystem.defaultBlendFunc()
- RenderSystem.setShader(GameRenderer::getPositionColorProgram)
+ RenderSystem.disableCull()
matrices.push()
matrices.translate(-camera.pos.x, -camera.pos.y, -camera.pos.z)
- val ctx = RenderInWorldContext(Tessellator.getInstance(), matrices)
+ val ctx = RenderInWorldContext(RenderSystem.renderThreadTesselator(), matrices, camera)
+
block(ctx)
matrices.pop()
- RenderSystem.setShaderColor(1F,1F,1F,1F)
+ RenderSystem.setShaderColor(1F, 1F, 1F, 1F)
VertexBuffer.unbind()
RenderSystem.enableDepthTest()
+ RenderSystem.enableCull()
RenderSystem.disableBlend()
}
}