From a74a2fb1d571da6a4a87578f354b18921b01d8d4 Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 12 Jun 2023 00:19:32 +0200 Subject: Add lines to world renderer --- .../moe/nea/firmament/features/world/FairySouls.kt | 7 ++- .../firmament/util/render/RenderInWorldContext.kt | 66 ++++++++++++++++++++-- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt b/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt index 9a04b37..678d626 100644 --- a/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt +++ b/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt @@ -21,15 +21,16 @@ package moe.nea.firmament.features.world import io.github.moulberry.repo.data.Coordinate import kotlinx.serialization.Serializable import kotlinx.serialization.serializer +import net.minecraft.util.math.Direction import moe.nea.firmament.events.ServerChatLineReceivedEvent import moe.nea.firmament.events.SkyblockServerUpdateEvent import moe.nea.firmament.events.WorldRenderLastEvent import moe.nea.firmament.features.FirmamentFeature +import moe.nea.firmament.gui.config.ManagedConfig import moe.nea.firmament.repo.RepoManager import moe.nea.firmament.util.MC import moe.nea.firmament.util.SBData import moe.nea.firmament.util.blockPos -import moe.nea.firmament.gui.config.ManagedConfig import moe.nea.firmament.util.data.ProfileSpecificDataHolder import moe.nea.firmament.util.render.RenderInWorldContext.Companion.renderInWorld import moe.nea.firmament.util.unformattedString @@ -132,6 +133,10 @@ object FairySouls : FirmamentFeature { currentMissingSouls.forEach { block(it.blockPos) } + color(1f, 0f, 1f, 1f) + currentLocationSouls.forEach { + wireframeCube(it.blockPos) + } } } } 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() } } -- cgit