aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authoringlettronald <inglettronald@gmail.com>2023-07-03 04:36:10 -0500
committeringlettronald <inglettronald@gmail.com>2023-07-03 04:36:10 -0500
commitec276d4a14735d3c69f7bb62f3389705c8b07e15 (patch)
treecb5bb038a42970106d45a291d1660f6d910c4e73 /src/main/kotlin
parentb47a1f596e85358a834f0dc105010be79cd5b2c2 (diff)
downloadDulkirMod-Fabric-ec276d4a14735d3c69f7bb62f3389705c8b07e15.tar.gz
DulkirMod-Fabric-ec276d4a14735d3c69f7bb62f3389705c8b07e15.tar.bz2
DulkirMod-Fabric-ec276d4a14735d3c69f7bb62f3389705c8b07e15.zip
Basic Functionality for text in world
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt7
-rw-r--r--src/main/kotlin/com/dulkirfabric/util/WorldRenderUtils.kt83
2 files changed, 86 insertions, 4 deletions
diff --git a/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt b/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt
index ab52f81..ea05247 100644
--- a/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt
+++ b/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt
@@ -3,7 +3,9 @@ package com.dulkirfabric.features
import com.dulkirfabric.events.WorldRenderLastEvent
import com.dulkirfabric.util.WorldRenderUtils
import meteordevelopment.orbit.EventHandler
+import net.minecraft.text.Text
import net.minecraft.util.math.Box
+import net.minecraft.util.math.Vec3d
import java.awt.Color
object RenderBoxTest {
@@ -15,5 +17,10 @@ object RenderBoxTest {
event.context, Box(-183.0, 78.0, -465.0, -182.0, 79.0, -464.0),
Color(255, 100, 150, 255), 10f, false
)
+
+ WorldRenderUtils.renderTextWithDistance(Text.literal("Hello worldgpqy"), event.context, event.context.consumers()!!,
+ Vec3d(2.0, 0.0, 0.0), false,
+ depthTest = false
+ )
}
} \ No newline at end of file
diff --git a/src/main/kotlin/com/dulkirfabric/util/WorldRenderUtils.kt b/src/main/kotlin/com/dulkirfabric/util/WorldRenderUtils.kt
index d14c11b..89648c5 100644
--- a/src/main/kotlin/com/dulkirfabric/util/WorldRenderUtils.kt
+++ b/src/main/kotlin/com/dulkirfabric/util/WorldRenderUtils.kt
@@ -3,19 +3,22 @@ package com.dulkirfabric.util
import com.mojang.blaze3d.systems.RenderSystem
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext
import net.minecraft.client.MinecraftClient
-import net.minecraft.client.render.BufferBuilder
-import net.minecraft.client.render.GameRenderer
-import net.minecraft.client.render.VertexFormat
-import net.minecraft.client.render.VertexFormats
+import net.minecraft.client.font.TextRenderer
+import net.minecraft.client.render.*
import net.minecraft.client.util.math.MatrixStack
+import net.minecraft.text.Style
+import net.minecraft.text.Text
+import net.minecraft.util.Formatting
import net.minecraft.util.math.Box
import net.minecraft.util.math.Vec3d
import org.joml.Vector3f
import java.awt.Color
+import kotlin.math.max
import kotlin.math.pow
object WorldRenderUtils {
+
private fun line(
matrix: MatrixStack.Entry, buffer: BufferBuilder,
x1: Number, y1: Number, z1: Number,
@@ -198,4 +201,76 @@ object WorldRenderUtils {
RenderSystem.enableCull()
matrices.pop()
}
+
+ fun renderText(string: Text,
+ context: WorldRenderContext,
+ vertexConsumer: VertexConsumerProvider,
+ pos: Vec3d,
+ shadow: Boolean = true,
+ depthTest: Boolean = true,
+ )
+ {
+ val d: Double = pos.distanceTo(MinecraftClient.getInstance().player?.pos)
+ val matrices = context.matrixStack()
+ matrices.push()
+ matrices.translate(pos.x - context.camera().pos.x,
+ pos.y - context.camera().pos.y,
+ pos.z - context.camera().pos.z)
+ matrices.multiply(context.camera().rotation)
+ val scale = max(d.toFloat() / 7f, 1f)
+ matrices.scale(-.025f * scale, -.025f * scale, .025f * scale)
+ val matrix4f = matrices.peek().positionMatrix
+ val textRenderer = MinecraftClient.getInstance().textRenderer
+ val j: Int = (.25 * 255.0f).toInt() shl 24
+ textRenderer.draw(
+ string, -textRenderer.getWidth(string).toFloat() / 2f, 0f, 0xFFFFFF, shadow, matrix4f, vertexConsumer,
+ if (depthTest) TextRenderer.TextLayerType.NORMAL else TextRenderer.TextLayerType.SEE_THROUGH,
+ j, LightmapTextureManager.MAX_LIGHT_COORDINATE
+ )
+ matrices.pop()
+ }
+
+ fun renderTextWithDistance(string: Text,
+ context: WorldRenderContext,
+ vertexConsumer: VertexConsumerProvider,
+ pos: Vec3d,
+ shadow: Boolean = true,
+ depthTest: Boolean = true,
+ )
+ {
+ val d: Double = pos.distanceTo(MinecraftClient.getInstance().player?.pos)
+ val matrices = context.matrixStack()
+ matrices.push()
+ matrices.translate(pos.x - context.camera().pos.x,
+ pos.y - context.camera().pos.y,
+ pos.z - context.camera().pos.z)
+ matrices.multiply(context.camera().rotation)
+ val scale = max(d.toFloat() / 7f, 1f)
+ matrices.scale(-.025f * scale, -.025f * scale, 1.0f)
+ val matrix4f = matrices.peek().positionMatrix
+ val textRenderer = MinecraftClient.getInstance().textRenderer
+ val j: Int = (.25 * 255.0f).toInt() shl 24
+ if (!depthTest) {
+ RenderSystem.disableDepthTest()
+ RenderSystem.depthMask(false)
+ } else {
+ RenderSystem.enableDepthTest()
+ }
+ textRenderer.draw(
+ string, -textRenderer.getWidth(string).toFloat() / 2, 0f, 0xFFFFFF, shadow, matrix4f, vertexConsumer,
+ if (depthTest) TextRenderer.TextLayerType.NORMAL else TextRenderer.TextLayerType.SEE_THROUGH,
+ j, LightmapTextureManager.MAX_LIGHT_COORDINATE
+ )
+ val distStr = d.toInt().toString()
+ val distText = Text.literal(distStr + "m").setStyle(Style.EMPTY.withColor(Formatting.YELLOW))
+ textRenderer.draw(
+ distText, -textRenderer.getWidth(distText).toFloat() / 2, 10f, 0xFFFFFF, shadow, matrix4f, vertexConsumer,
+ if (depthTest) TextRenderer.TextLayerType.NORMAL else TextRenderer.TextLayerType.SEE_THROUGH,
+ j, LightmapTextureManager.MAX_LIGHT_COORDINATE
+ )
+ RenderSystem.depthMask(true)
+ RenderSystem.enableDepthTest()
+ matrices.pop()
+ }
+
} \ No newline at end of file