| 1 | /*
|
| 2 | * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
|
| 3 | *
|
| 4 | * Copyright (c) 2015 - 2026 CCBlueX
|
| 5 | *
|
| 6 | * LiquidBounce is free software: you can redistribute it and/or modify
|
| 7 | * it under the terms of the GNU General Public License as published by
|
| 8 | * the Free Software Foundation, either version 3 of the License, or
|
| 9 | * (at your option) any later version.
|
| 10 | *
|
| 11 | * LiquidBounce is distributed in the hope that it will be useful,
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | * GNU General Public License for more details.
|
| 15 | *
|
| 16 | * You should have received a copy of the GNU General Public License
|
| 17 | * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
|
| 18 | */
|
| 19 |
|
| 20 | @file:Suppress("detekt:TooManyFunctions")
|
| 21 |
|
| 22 | package net.ccbluex.liquidbounce.render
|
| 23 |
|
| 24 | import com.mojang.blaze3d.buffers.GpuBufferSlice
|
| 25 | import com.mojang.blaze3d.pipeline.RenderPipeline
|
| 26 | import com.mojang.blaze3d.pipeline.RenderTarget
|
| 27 | import com.mojang.blaze3d.vertex.PoseStack
|
| 28 | import com.mojang.blaze3d.vertex.VertexConsumer
|
| 29 | import net.ccbluex.fastutil.objectObjectMapOf
|
| 30 | import net.ccbluex.liquidbounce.event.events.WorldRenderEvent
|
| 31 | import net.ccbluex.liquidbounce.features.addon.AddonApi
|
| 32 | import net.ccbluex.liquidbounce.render.engine.type.Color4b
|
| 33 | import net.ccbluex.liquidbounce.render.engine.type.Vec3f
|
| 34 | import net.ccbluex.liquidbounce.render.utils.DistanceFadeUniformValueGroup
|
| 35 | import net.ccbluex.liquidbounce.render.utils.VertexList
|
| 36 | import net.ccbluex.liquidbounce.render.utils.forEachVertex
|
| 37 | import net.ccbluex.liquidbounce.render.utils.UnitCircle
|
| 38 | import net.ccbluex.liquidbounce.utils.client.gpuDevice
|
| 39 | import net.ccbluex.liquidbounce.utils.render.writeStd140
|
| 40 | import net.minecraft.client.renderer.texture.AbstractTexture
|
| 41 | import net.minecraft.core.Direction
|
| 42 | import net.minecraft.core.Vec3i
|
| 43 | import net.minecraft.util.Mth
|
| 44 | import net.minecraft.world.phys.AABB
|
| 45 | import net.minecraft.world.phys.Vec3
|
| 46 | import net.minecraft.world.phys.shapes.VoxelShape
|
| 47 | import org.joml.Vector3f
|
| 48 | import org.joml.Vector3fc
|
| 49 | import java.util.function.Consumer
|
| 50 |
|
| 51 | /**
|
| 52 | * This variable should be used when rendering long lines, meaning longer than ~2 in 3d.
|
| 53 | * [WorldRenderEnvironment.longLines] is available for this.
|
| 54 | *
|
| 55 | * Context:
|
| 56 | * For some reason, newer drivers for AMD Vega iGPUs (about end 2023 until now) fail to correctly smooth lines.
|
| 57 | *
|
| 58 | * This has to be removed or limited to old driver versions when AMD actually fixes the bug in their drivers.
|
| 59 | * But as of now, 01.02.2025, they haven't.
|
| 60 | */
|
| 61 | @JvmField
|
| 62 | val HAS_AMD_VEGA_APU = gpuDevice.deviceInfo.name.startsWith("AMD Radeon(TM) RX Vega") &&
|
| 63 | gpuDevice.deviceInfo.vendorName == "ATI Technologies Inc."
|
| 64 |
|
| 65 | @AddonApi
|
| 66 | @JvmField
|
| 67 | val FULL_BOX = AABB(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
|
| 68 |
|
| 69 | @JvmField
|
| 70 | val EMPTY_BOX = AABB(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
|
| 71 |
|
| 72 | private val ROUNDED_RECT_AS_OUTLINE_CIRCLE_UBO by lazy(LazyThreadSafetyMode.NONE) {
|
| 73 | val slice = ClientUniformDefine.ROUNDED_RECT.createSingleBuffer()
|
| 74 | slice.writeStd140 {
|
| 75 | putVec2(1f, 1f)
|
| 76 | putFloat(2f)
|
| 77 | }
|
| 78 | slice
|
| 79 | }
|
| 80 |
|
| 81 | @AddonApi
|
| 82 | inline fun WorldRenderEvent.renderEnvironment(draw: WorldRenderEnvironment.() -> Unit) {
|
| 83 | environment.draw()
|
| 84 | }
|
| 85 |
|
| 86 | @AddonApi
|
| 87 | @JvmSynthetic
|
| 88 | inline fun WorldRenderEnvironment.withPositionRelativeToCamera(draw: WorldRenderEnvironment.() -> Unit) {
|
| 89 | poseStack.withPush {
|
| 90 | translate(camera.position().reverse())
|
| 91 | draw()
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | @AddonApi
|
| 96 | @JvmSynthetic
|
| 97 | inline fun WorldRenderEnvironment.withPositionRelativeToCamera(
|
| 98 | x: Double, y: Double, z: Double, draw: WorldRenderEnvironment.() -> Unit
|
| 99 | ) {
|
| 100 | poseStack.withPush {
|
| 101 | val cameraPos = camera.position()
|
| 102 | translate(x - cameraPos.x, y - cameraPos.y, z - cameraPos.z)
|
| 103 | draw()
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | /**
|
| 108 | * Positions the render origin at the camera-relative coordinates of [pos] before drawing.
|
| 109 | */
|
| 110 | @AddonApi
|
| 111 | @JvmSynthetic
|
| 112 | inline fun WorldRenderEnvironment.withPositionRelativeToCamera(pos: Vec3, draw: WorldRenderEnvironment.() -> Unit) =
|
| 113 | withPositionRelativeToCamera(pos.x, pos.y, pos.z, draw)
|
| 114 |
|
| 115 | /**
|
| 116 | * Shortcut of `withPositionRelativeToCamera(Vec3.atLowerCornerOf(pos))`
|
| 117 | */
|
| 118 | @AddonApi
|
| 119 | @JvmSynthetic
|
| 120 | inline fun WorldRenderEnvironment.withPositionRelativeToCamera(pos: Vec3i, draw: WorldRenderEnvironment.() -> Unit) =
|
| 121 | withPositionRelativeToCamera(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble(), draw)
|
| 122 |
|
| 123 | // The same for Java, which cannot pass the receiver lambdas above.
|
| 124 |
|
| 125 | @AddonApi
|
| 126 | fun WorldRenderEnvironment.withPositionRelativeToCamera(draw: Consumer<WorldRenderEnvironment>) =
|
| 127 | withPositionRelativeToCamera { draw.accept(this) }
|
| 128 |
|
| 129 | @AddonApi
|
| 130 | fun WorldRenderEnvironment.withPositionRelativeToCamera(
|
| 131 | x: Double, y: Double, z: Double, draw: Consumer<WorldRenderEnvironment>
|
| 132 | ) =
|
| 133 | withPositionRelativeToCamera(x, y, z) { draw.accept(this) }
|
| 134 |
|
| 135 | @AddonApi
|
| 136 | fun WorldRenderEnvironment.withPositionRelativeToCamera(pos: Vec3, draw: Consumer<WorldRenderEnvironment>) =
|
| 137 | withPositionRelativeToCamera(pos) { draw.accept(this) }
|
| 138 |
|
| 139 | @AddonApi
|
| 140 | fun WorldRenderEnvironment.withPositionRelativeToCamera(pos: Vec3i, draw: Consumer<WorldRenderEnvironment>) =
|
| 141 | withPositionRelativeToCamera(pos) { draw.accept(this) }
|
| 142 |
|
| 143 | internal inline fun RenderTarget.drawGenericBlockESP(
|
| 144 | renderState: CachedMeshStorage,
|
| 145 | pipeline: RenderPipeline,
|
| 146 | distanceFade: DistanceFadeUniformValueGroup,
|
| 147 | dynamicTransforms: () -> GpuBufferSlice = ::getDynamicTransformsUniform,
|
| 148 | ): Boolean {
|
| 149 | if (!renderState.isReady) return false
|
| 150 |
|
| 151 | distanceFade.updateIfDirty()
|
| 152 | val dynamicTransforms = dynamicTransforms()
|
| 153 | this.createRenderPass({ renderState.label + " Pass" }).use { pass ->
|
| 154 | pass.setPipeline(pipeline)
|
| 155 |
|
| 156 | pass.bindProjectionUniform()
|
| 157 | pass.bindGlobalsUniform()
|
| 158 | pass.bindDynamicTransformsUniform(dynamicTransforms)
|
| 159 | renderState.bindUniform(pass)
|
| 160 | distanceFade.bindUniform(pass)
|
| 161 | renderState.bindAndDraw(pass)
|
| 162 | }
|
| 163 | return true
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * Variant of [drawCustomMesh] that binds [sampler0] as `Sampler0`.
|
| 168 | */
|
| 169 | inline fun WorldRenderEnvironment.drawCustomMeshTextured(
|
| 170 | sampler0: AbstractTexture,
|
| 171 | pipeline: RenderPipeline = ClientRenderPipelines.texQuads(noDepthTest = true),
|
| 172 | uniforms: Map<String, GpuBufferSlice> = emptyMap(),
|
| 173 | drawer: VertexConsumer.(PoseStack.Pose) -> Unit,
|
| 174 | ) = drawCustomMesh(
|
| 175 | pipeline = pipeline,
|
| 176 | textures = objectObjectMapOf("Sampler0", sampler0),
|
| 177 | uniforms = uniforms,
|
| 178 | drawer = drawer,
|
| 179 | )
|
| 180 |
|
| 181 | /**
|
| 182 | * Preferred mesh draw helper for world rendering code.
|
| 183 | */
|
| 184 | inline fun WorldRenderEnvironment.drawCustomMesh(
|
| 185 | pipeline: RenderPipeline,
|
| 186 | textures: Map<String, AbstractTexture> = emptyMap(),
|
| 187 | uniforms: Map<String, GpuBufferSlice> = emptyMap(),
|
| 188 | drawer: VertexConsumer.(PoseStack.Pose) -> Unit,
|
| 189 | ) {
|
| 190 | start(
|
| 191 | pipeline = pipeline,
|
| 192 | textures = textures,
|
| 193 | uniforms = uniforms,
|
| 194 | ).use { scope ->
|
| 195 | drawer(scope.consumer, poseStack.last())
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | /**
|
| 200 | * Draws a line with endpoint [p1] and [p2] and color [argb].
|
| 201 | */
|
| 202 | fun WorldRenderEnvironment.drawLine(p1: Vec3f, p2: Vec3f, argb: Int) =
|
| 203 | drawCustomMesh(ClientRenderPipelines.lines(noDepthTest = true)) { pose ->
|
| 204 | addVertex(pose, p1).setColor(argb)
|
| 205 | addVertex(pose, p2).setColor(argb)
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * Draws a line with endpoint [p1] and [p2] and color [argb].
|
| 210 | */
|
| 211 | @AddonApi
|
| 212 | fun WorldRenderEnvironment.drawLine(p1: Vec3, p2: Vec3, argb: Int) =
|
| 213 | drawCustomMesh(ClientRenderPipelines.lines(noDepthTest = true)) { pose ->
|
| 214 | addVertex(pose, p1).setColor(argb)
|
| 215 | addVertex(pose, p2).setColor(argb)
|
| 216 | }
|
| 217 |
|
| 218 | /**
|
| 219 | * Draws lines with [width].
|
| 220 | * Modern GL doesn't support `glLineWidth` well, so draw with shader simulation.
|
| 221 | */
|
| 222 | fun WorldRenderEnvironment.drawLinesWithWidth(argb: Int, width: Float, vararg positions: Vec3f) {
|
| 223 | if (positions.isEmpty()) return
|
| 224 | require(positions.size and 1 == 0)
|
| 225 |
|
| 226 | drawCustomMesh(pipeline = ClientRenderPipelines.LinesWithWidth) { pose ->
|
| 227 | for (i in 0 until positions.size step 2) {
|
| 228 | val p1 = positions[i]
|
| 229 | val p2 = positions[i + 1]
|
| 230 | val norm1 = (p1 - p2).normalized()
|
| 231 | addVertex(pose, p1)
|
| 232 | .setColor(argb)
|
| 233 | .setNormal(pose, norm1)
|
| 234 | .setLineWidth(width)
|
| 235 | addVertex(pose, p2)
|
| 236 | .setColor(argb)
|
| 237 | .setNormal(pose, -norm1)
|
| 238 | .setLineWidth(width)
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | fun WorldRenderEnvironment.drawLinesWithWidth(argb: Int, width: Float, positions: VertexList) {
|
| 244 | if (positions.size == 0) return
|
| 245 | require(positions.size and 1 == 0)
|
| 246 |
|
| 247 | val p1 = Vector3f()
|
| 248 | val p2 = Vector3f()
|
| 249 | val norm1 = Vector3f()
|
| 250 | drawCustomMesh(pipeline = ClientRenderPipelines.LinesWithWidth) { pose ->
|
| 251 | for (i in 0 until positions.size step 2) {
|
| 252 | positions.vec(i, p1)
|
| 253 | positions.vec(i + 1, p2)
|
| 254 | val norm1 = p1.sub(p2, norm1).normalize()
|
| 255 |
|
| 256 | addVertex(pose, p1)
|
| 257 | .setColor(argb)
|
| 258 | .setNormal(pose, norm1)
|
| 259 | .setLineWidth(width)
|
| 260 | addVertex(pose, p2)
|
| 261 | .setColor(argb)
|
| 262 | .setNormal(pose, norm1.negate())
|
| 263 | .setLineWidth(width)
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | /**
|
| 269 | * Function to draw lines using the specified [positions] vectors.
|
| 270 | *
|
| 271 | * @param positions The vectors representing the lines.
|
| 272 | */
|
| 273 | fun WorldRenderEnvironment.drawLines(argb: Int, vararg positions: Vec3f) {
|
| 274 | if (positions.isEmpty()) return
|
| 275 | require(positions.size and 1 == 0)
|
| 276 |
|
| 277 | drawCustomMesh(pipeline = ClientRenderPipelines.lines(noDepthTest = true)) { pose ->
|
| 278 | for (pos in positions) {
|
| 279 | addVertex(pose, pos).setColor(argb)
|
| 280 | }
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | fun WorldRenderEnvironment.drawLines(argb: Int, positions: VertexList) {
|
| 285 | if (positions.size == 0) return
|
| 286 | require(positions.size and 1 == 0)
|
| 287 |
|
| 288 | drawCustomMesh(pipeline = ClientRenderPipelines.lines(noDepthTest = true)) { pose ->
|
| 289 | positions.forEachVertex { x, y, z ->
|
| 290 | addVertex(pose, x, y, z).setColor(argb)
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | /**
|
| 296 | * Function to draw a line strip using the specified [positions] vectors.
|
| 297 | *
|
| 298 | * @param positions The vectors representing the line strip.
|
| 299 | */
|
| 300 | fun WorldRenderEnvironment.drawLineStrip(argb: Int, vararg positions: Vec3f) {
|
| 301 | if (positions.isEmpty()) return
|
| 302 |
|
| 303 | drawCustomMesh(pipeline = ClientRenderPipelines.LineStrip) { pose ->
|
| 304 | for (pos in positions) {
|
| 305 | addVertex(pose, pos).setColor(argb)
|
| 306 | }
|
| 307 | }
|
| 308 | }
|
| 309 |
|
| 310 | fun WorldRenderEnvironment.drawLineStrip(argb: Int, positions: VertexList) {
|
| 311 | if (positions.size == 0) return
|
| 312 |
|
| 313 | drawCustomMesh(pipeline = ClientRenderPipelines.LineStrip) { pose ->
|
| 314 | positions.forEachVertex { x, y, z ->
|
| 315 | addVertex(pose, x, y, z).setColor(argb)
|
| 316 | }
|
| 317 | }
|
| 318 | }
|
| 319 |
|
| 320 | fun WorldRenderEnvironment.drawTexQuad(
|
| 321 | sampler0: AbstractTexture,
|
| 322 | argb: Int,
|
| 323 | ) {
|
| 324 | drawCustomMeshTextured(sampler0) { pose ->
|
| 325 | addVertex(pose, -0.5f, -0.5f, 0f).setUv(0f, 0f).setColor(argb)
|
| 326 | addVertex(pose, -0.5f, 0.5f, 0f).setUv(0f, 1f).setColor(argb)
|
| 327 | addVertex(pose, 0.5f, 0.5f, 0f).setUv(1f, 1f).setColor(argb)
|
| 328 | addVertex(pose, 0.5f, -0.5f, 0f).setUv(1f, 0f).setColor(argb)
|
| 329 | }
|
| 330 | }
|
| 331 |
|
| 332 |
|
| 333 | fun WorldRenderEnvironment.drawSquareTexture(
|
| 334 | sampler0: AbstractTexture,
|
| 335 | size: Float,
|
| 336 | argb: Int,
|
| 337 | anchor: AnchorPoint = AnchorPoint.TOP_LEFT,
|
| 338 | noDepthTest: Boolean = false,
|
| 339 | ) = drawCustomMeshTextured(sampler0, pipeline = ClientRenderPipelines.texQuads(noDepthTest) ) { matrix ->
|
| 340 | val minX = size * anchor.xFactor
|
| 341 | val maxX = minX + size
|
| 342 | val minY = size * anchor.yFactor
|
| 343 | val maxY = minY + size
|
| 344 |
|
| 345 | addVertex(matrix, minX, maxY, 0.0f)
|
| 346 | .setUv(0.0f, 0.0f)
|
| 347 | .setColor(argb)
|
| 348 |
|
| 349 | addVertex(matrix, minX, minY, 0.0f)
|
| 350 | .setUv(0.0f, 1.0f)
|
| 351 | .setColor(argb)
|
| 352 |
|
| 353 | addVertex(matrix, maxX, minY, 0.0f)
|
| 354 | .setUv(1.0f, 1.0f)
|
| 355 | .setColor(argb)
|
| 356 |
|
| 357 | addVertex(matrix, maxX, maxY, 0.0f)
|
| 358 | .setUv(1.0f, 0.0f)
|
| 359 | .setColor(argb)
|
| 360 |
|
| 361 | }
|
| 362 |
|
| 363 | fun WorldRenderEnvironment.drawSquareTextureGradient(
|
| 364 | sampler0: AbstractTexture,
|
| 365 | outerRadius: Float,
|
| 366 | innerRadius: Float,
|
| 367 | outerColor: Color4b,
|
| 368 | innerColor: Color4b,
|
| 369 | anchor: AnchorPoint = AnchorPoint.TOP_LEFT,
|
| 370 | subdivisions: Int = 16,
|
| 371 | startOffset: Float = 0.5f,
|
| 372 | noDepthTest: Boolean = true,
|
| 373 | ) {
|
| 374 | if (outerRadius <= 0f || (outerColor.isTransparent && innerColor.isTransparent)) {
|
| 375 | return
|
| 376 | }
|
| 377 |
|
| 378 | val size = outerRadius * 2f
|
| 379 | val minX = size * anchor.xFactor
|
| 380 | val minY = size * anchor.yFactor
|
| 381 |
|
| 382 | val step = size / subdivisions
|
| 383 | val centerX = minX + outerRadius
|
| 384 | val centerY = minY + outerRadius
|
| 385 |
|
| 386 | val baseRatio = (innerRadius / outerRadius).coerceIn(0f, 1f)
|
| 387 | val effectiveRatio = maxOf(baseRatio, startOffset.coerceIn(0f, 0.99f))
|
| 388 |
|
| 389 | fun getColorForPos(x: Float, y: Float): Int {
|
| 390 | val dx = x - centerX
|
| 391 | val dy = y - centerY
|
| 392 | val distRatio = (kotlin.math.sqrt(dx * dx + dy * dy) / outerRadius).coerceIn(0f, 1f)
|
| 393 |
|
| 394 | val t = when (distRatio <= effectiveRatio) {
|
| 395 | true -> 0.0
|
| 396 | else -> ((distRatio - effectiveRatio) / (1.0 - effectiveRatio)).coerceIn(0.0, 1.0)
|
| 397 | }
|
| 398 |
|
| 399 | return innerColor.interpolateTo(outerColor, t).argb
|
| 400 | }
|
| 401 |
|
| 402 | drawCustomMeshTextured(sampler0, ClientRenderPipelines.texQuads(noDepthTest)) { matrix ->
|
| 403 | for (row in 0 until subdivisions) {
|
| 404 | for (col in 0 until subdivisions) {
|
| 405 | val x1 = minX + col * step
|
| 406 | val x2 = x1 + step
|
| 407 | val y1 = minY + row * step
|
| 408 | val y2 = y1 + step
|
| 409 |
|
| 410 | val u1 = col.toFloat() / subdivisions
|
| 411 | val u2 = (col + 1).toFloat() / subdivisions
|
| 412 | val v1 = row.toFloat() / subdivisions
|
| 413 | val v2 = (row + 1).toFloat() / subdivisions
|
| 414 |
|
| 415 | val c11 = getColorForPos(x1, y1)
|
| 416 | val c12 = getColorForPos(x1, y2)
|
| 417 | val c22 = getColorForPos(x2, y2)
|
| 418 | val c21 = getColorForPos(x2, y1)
|
| 419 |
|
| 420 | addVertex(matrix, x1, y2, 0.0f).setUv(u1, v2).setColor(c12)
|
| 421 | addVertex(matrix, x1, y1, 0.0f).setUv(u1, v1).setColor(c11)
|
| 422 | addVertex(matrix, x2, y1, 0.0f).setUv(u2, v1).setColor(c21)
|
| 423 | addVertex(matrix, x2, y2, 0.0f).setUv(u2, v2).setColor(c22)
|
| 424 | }
|
| 425 | }
|
| 426 | }
|
| 427 | }
|
| 428 |
|
| 429 | @JvmOverloads
|
| 430 | fun WorldRenderEnvironment.drawTriangle(p1: Vec3f, p2: Vec3f, p3: Vec3f, argb: Int, noDepthTest: Boolean = true) {
|
| 431 | drawCustomMesh(ClientRenderPipelines.triangles(noDepthTest)) { matrix ->
|
| 432 | addVertex(matrix, p1).setColor(argb)
|
| 433 | addVertex(matrix, p2).setColor(argb)
|
| 434 | addVertex(matrix, p3).setColor(argb)
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | /**
|
| 439 | * Function to draw a colored [box].
|
| 440 | */
|
| 441 | @AddonApi
|
| 442 | @JvmOverloads
|
| 443 | fun WorldRenderEnvironment.drawBox(
|
| 444 | box: AABB,
|
| 445 | faceColor: Color4b? = Color4b.TRANSPARENT,
|
| 446 | outlineColor: Color4b? = Color4b.TRANSPARENT,
|
| 447 | faceVertices: Int = -1,
|
| 448 | outlineVertices: Int = -1,
|
| 449 | noDepthTest: Boolean = true,
|
| 450 | ) {
|
| 451 | if (faceColor != null && !faceColor.isTransparent) {
|
| 452 | drawCustomMesh(ClientRenderPipelines.quads(noDepthTest)) { pose ->
|
| 453 | addBoxFaces(pose.pose(), box, color = faceColor, verticesToUse = faceVertices)
|
| 454 | }
|
| 455 | }
|
| 456 |
|
| 457 | if (outlineColor != null && !outlineColor.isTransparent) {
|
| 458 | drawCustomMesh(ClientRenderPipelines.lines(noDepthTest)) { pose ->
|
| 459 | addBoxOutlines(pose.pose(), box, outlineColor, outlineVertices)
|
| 460 | }
|
| 461 | }
|
| 462 | }
|
| 463 |
|
| 464 | fun WorldRenderEnvironment.drawShape(
|
| 465 | shape: VoxelShape,
|
| 466 | faceColor: Color4b? = Color4b.TRANSPARENT,
|
| 467 | outlineColor: Color4b? = Color4b.TRANSPARENT,
|
| 468 | ) {
|
| 469 | if (faceColor != null && !faceColor.isTransparent) {
|
| 470 | drawCustomMesh(ClientRenderPipelines.quads(noDepthTest = true)) { pose ->
|
| 471 | addShapeFaces(pose.pose(), shape, color = faceColor)
|
| 472 | }
|
| 473 | }
|
| 474 |
|
| 475 | if (outlineColor != null && !outlineColor.isTransparent) {
|
| 476 | drawCustomMesh(ClientRenderPipelines.lines(noDepthTest = true)) { pose ->
|
| 477 | addShapeOutlines(pose.pose(), shape, outlineColor)
|
| 478 | }
|
| 479 | }
|
| 480 | }
|
| 481 |
|
| 482 | fun WorldRenderEnvironment.drawShapeSide(
|
| 483 | shape: VoxelShape,
|
| 484 | side: Direction,
|
| 485 | hitPos: Vec3,
|
| 486 | faceColor: Color4b? = Color4b.TRANSPARENT,
|
| 487 | outlineColor: Color4b? = Color4b.TRANSPARENT,
|
| 488 | ) {
|
| 489 | if (faceColor != null && !faceColor.isTransparent) {
|
| 490 | drawCustomMesh(ClientRenderPipelines.quads(noDepthTest = true)) { pose ->
|
| 491 | addShapeSideFaces(pose.pose(), shape, side, hitPos, color = faceColor)
|
| 492 | }
|
| 493 | }
|
| 494 |
|
| 495 | if (outlineColor != null && !outlineColor.isTransparent) {
|
| 496 | drawCustomMesh(ClientRenderPipelines.lines(noDepthTest = true)) { pose ->
|
| 497 | addShapeSideOutlines(pose.pose(), shape, side, hitPos, outlineColor)
|
| 498 | }
|
| 499 | }
|
| 500 | }
|
| 501 |
|
| 502 | /**
|
| 503 | * Function to draw a colored [box] with specified [side].
|
| 504 | */
|
| 505 | fun WorldRenderEnvironment.drawBoxSide(
|
| 506 | box: AABB,
|
| 507 | side: Direction,
|
| 508 | faceColor: Color4b? = Color4b.TRANSPARENT,
|
| 509 | outlineColor: Color4b? = Color4b.TRANSPARENT,
|
| 510 | ) = drawBox(
|
| 511 | box,
|
| 512 | faceColor,
|
| 513 | outlineColor,
|
| 514 | faceVertices = BoxVertexIterator.FACE.sideMask(side),
|
| 515 | outlineVertices = BoxVertexIterator.OUTLINE.sideMask(side),
|
| 516 | )
|
| 517 |
|
| 518 | /**
|
| 519 | * Function to draw a colored [box] with specified [sides].
|
| 520 | */
|
| 521 | fun WorldRenderEnvironment.drawBoxSides(
|
| 522 | box: AABB,
|
| 523 | sides: Iterable<Direction>,
|
| 524 | faceColor: Color4b? = Color4b.TRANSPARENT,
|
| 525 | outlineColor: Color4b? = Color4b.TRANSPARENT,
|
| 526 | ) = drawBox(
|
| 527 | box,
|
| 528 | faceColor,
|
| 529 | outlineColor,
|
| 530 | faceVertices = BoxVertexIterator.FACE.sideMask(sides),
|
| 531 | outlineVertices = BoxVertexIterator.OUTLINE.sideMask(sides),
|
| 532 | )
|
| 533 |
|
| 534 | /**
|
| 535 | * Function to draw a flat plane on the XZ axis with an optional outline.
|
| 536 | */
|
| 537 | fun WorldRenderEnvironment.drawPlane(
|
| 538 | sizeX: Float,
|
| 539 | sizeZ: Float,
|
| 540 | fillColor: Color4b? = Color4b.TRANSPARENT,
|
| 541 | outlineColor: Color4b? = Color4b.TRANSPARENT,
|
| 542 | noDepthTest: Boolean = true
|
| 543 | ) {
|
| 544 | if (fillColor != null && !fillColor.isTransparent) {
|
| 545 | val argb = fillColor.argb
|
| 546 | drawCustomMesh(ClientRenderPipelines.quads(noDepthTest = noDepthTest)) { matrix ->
|
| 547 | addVertex(matrix, 0f, 0f, 0f).setColor(argb)
|
| 548 | addVertex(matrix, 0f, 0f, sizeZ).setColor(argb)
|
| 549 | addVertex(matrix, sizeX, 0f, sizeZ).setColor(argb)
|
| 550 | addVertex(matrix, sizeX, 0f, 0f).setColor(argb)
|
| 551 | }
|
| 552 | }
|
| 553 |
|
| 554 | if (outlineColor != null && !outlineColor.isTransparent) {
|
| 555 | val argb = outlineColor.argb
|
| 556 | drawCustomMesh(ClientRenderPipelines.lines(noDepthTest = noDepthTest)) { matrix ->
|
| 557 | addVertex(matrix, 0f, 0f, 0f).setColor(argb)
|
| 558 | addVertex(matrix, 0f, 0f, sizeZ).setColor(argb)
|
| 559 |
|
| 560 | addVertex(matrix, 0f, 0f, sizeZ).setColor(argb)
|
| 561 | addVertex(matrix, sizeX, 0f, sizeZ).setColor(argb)
|
| 562 |
|
| 563 | addVertex(matrix, sizeX, 0f, sizeZ).setColor(argb)
|
| 564 | addVertex(matrix, sizeX, 0f, 0f).setColor(argb)
|
| 565 |
|
| 566 | addVertex(matrix, sizeX, 0f, 0f).setColor(argb)
|
| 567 | addVertex(matrix, 0f, 0f, 0f).setColor(argb)
|
| 568 | }
|
| 569 | }
|
| 570 | }
|
| 571 |
|
| 572 | /**
|
| 573 | * Function to draw a circle of the size [outerRadius] with a cutout of size [innerRadius]
|
| 574 | *
|
| 575 | * @param outerRadius The radius of the circle
|
| 576 | * @param innerRadius The radius inside the circle (the cutout)
|
| 577 | * @param outerColor The color of the outer edges
|
| 578 | * @param innerColor The color of the inner edges
|
| 579 | */
|
| 580 | fun WorldRenderEnvironment.drawGradientCircle(
|
| 581 | outerRadius: Float,
|
| 582 | innerRadius: Float,
|
| 583 | outerColor: Color4b,
|
| 584 | innerColor: Color4b,
|
| 585 | innerOffset: Vector3fc = Vector3f(),
|
| 586 | noDepthTest: Boolean = true,
|
| 587 | ) {
|
| 588 | if (outerRadius <= 0f || outerColor.isTransparent && innerColor.isTransparent) {
|
| 589 | return
|
| 590 | }
|
| 591 |
|
| 592 | if (Mth.equal(innerOffset.lengthSquared(), 0f)) {
|
| 593 | val innerRatio = (innerRadius / outerRadius).coerceIn(0f, 1f)
|
| 594 |
|
| 595 | drawGradientCircleQuad(outerRadius, outerColor, innerColor, innerRatio, noDepthTest)
|
| 596 | return
|
| 597 | }
|
| 598 |
|
| 599 | drawCustomMesh(ClientRenderPipelines.triangleStrip(noDepthTest)) { matrix ->
|
| 600 | val innerP = Vector3f()
|
| 601 | val outerP = Vector3f()
|
| 602 | UnitCircle.forEach { cosine, sine ->
|
| 603 | outerP.set(cosine * outerRadius, 0f, sine * outerRadius)
|
| 604 | innerP.set(cosine * innerRadius, 0f, sine * innerRadius).add(innerOffset)
|
| 605 |
|
| 606 | addVertex(matrix, outerP).setColor(outerColor.argb)
|
| 607 | addVertex(matrix, innerP).setColor(innerColor.argb)
|
| 608 | }
|
| 609 | }
|
| 610 | }
|
| 611 |
|
| 612 | private fun WorldRenderEnvironment.drawGradientCircleQuad(
|
| 613 | radius: Float,
|
| 614 | outerColor: Color4b,
|
| 615 | innerColor: Color4b,
|
| 616 | innerRatio: Float,
|
| 617 | noDepthTest: Boolean,
|
| 618 | ) {
|
| 619 | fun packColorRG(color: Color4b): Int =
|
| 620 | ((color.r and 0xFF) shl 8) or (color.g and 0xFF)
|
| 621 |
|
| 622 | fun packColorBA(color: Color4b): Int =
|
| 623 | ((color.b and 0xFF) shl 8) or (color.a and 0xFF)
|
| 624 |
|
| 625 | val outerRg = packColorRG(outerColor)
|
| 626 | val outerBa = packColorBA(outerColor)
|
| 627 | val innerRg = packColorRG(innerColor)
|
| 628 | val innerBa = packColorBA(innerColor)
|
| 629 |
|
| 630 | drawCustomMesh(ClientRenderPipelines.gradientCircle(noDepthTest)) { matrix ->
|
| 631 | addVertex(matrix, -radius, 0f, -radius)
|
| 632 | .setUv(0f, 0f)
|
| 633 | .setUv1(outerRg, outerBa)
|
| 634 | .setUv2(innerRg, innerBa)
|
| 635 | .setLineWidth(innerRatio)
|
| 636 | addVertex(matrix, -radius, 0f, radius)
|
| 637 | .setUv(0f, 1f)
|
| 638 | .setUv1(outerRg, outerBa)
|
| 639 | .setUv2(innerRg, innerBa)
|
| 640 | .setLineWidth(innerRatio)
|
| 641 | addVertex(matrix, radius, 0f, radius)
|
| 642 | .setUv(1f, 1f)
|
| 643 | .setUv1(outerRg, outerBa)
|
| 644 | .setUv2(innerRg, innerBa)
|
| 645 | .setLineWidth(innerRatio)
|
| 646 | addVertex(matrix, radius, 0f, -radius)
|
| 647 | .setUv(1f, 0f)
|
| 648 | .setUv1(outerRg, outerBa)
|
| 649 | .setUv2(innerRg, innerBa)
|
| 650 | .setLineWidth(innerRatio)
|
| 651 | }
|
| 652 | }
|
| 653 |
|
| 654 | private fun WorldRenderEnvironment.drawRoundedRectQuad(
|
| 655 | radius: Float,
|
| 656 | argb: Int,
|
| 657 | noDepthTest: Boolean,
|
| 658 | uniform: GpuBufferSlice,
|
| 659 | ) {
|
| 660 | drawCustomMesh(
|
| 661 | pipeline = ClientRenderPipelines.roundedRect(noDepthTest),
|
| 662 | uniforms = objectObjectMapOf(ClientUniformDefine.ROUNDED_RECT.uboName, uniform),
|
| 663 | ) { pose ->
|
| 664 | addVertex(pose, -radius, 0f, -radius).setUv(0f, 0f).setColor(argb)
|
| 665 | addVertex(pose, -radius, 0f, radius).setUv(0f, 1f).setColor(argb)
|
| 666 | addVertex(pose, radius, 0f, radius).setUv(1f, 1f).setColor(argb)
|
| 667 | addVertex(pose, radius, 0f, -radius).setUv(1f, 0f).setColor(argb)
|
| 668 | }
|
| 669 | }
|
| 670 |
|
| 671 | fun WorldRenderEnvironment.drawCircle(
|
| 672 | radius: Float,
|
| 673 | color: Color4b,
|
| 674 | ) {
|
| 675 | if (radius <= 0f || color.isTransparent) {
|
| 676 | return
|
| 677 | }
|
| 678 |
|
| 679 | drawGradientCircleQuad(
|
| 680 | radius = radius,
|
| 681 | outerColor = color,
|
| 682 | innerColor = color,
|
| 683 | innerRatio = 0f,
|
| 684 | noDepthTest = true,
|
| 685 | )
|
| 686 | }
|
| 687 |
|
| 688 | /**
|
| 689 | * Function to draw the outline of a circle of the size [radius]
|
| 690 | *
|
| 691 | * @param radius The radius
|
| 692 | * @param color The color
|
| 693 | */
|
| 694 | @JvmOverloads
|
| 695 | fun WorldRenderEnvironment.drawCircleOutline(radius: Float, color: Color4b, noDepthTest: Boolean = true) {
|
| 696 | if (radius <= 0f || color.isTransparent) {
|
| 697 | return
|
| 698 | }
|
| 699 |
|
| 700 | drawRoundedRectQuad(
|
| 701 | radius = radius,
|
| 702 | argb = color.argb,
|
| 703 | noDepthTest = noDepthTest,
|
| 704 | uniform = ROUNDED_RECT_AS_OUTLINE_CIRCLE_UBO,
|
| 705 | )
|
| 706 | }
|
| 707 |
|
| 708 | fun WorldRenderEnvironment.drawGradientSides(
|
| 709 | height: Double,
|
| 710 | baseColor: Color4b,
|
| 711 | topColor: Color4b,
|
| 712 | box: AABB
|
| 713 | ) {
|
| 714 | if (height == 0.0) {
|
| 715 | return
|
| 716 | }
|
| 717 |
|
| 718 | drawCustomMesh(ClientRenderPipelines.quads(noDepthTest = true)) { pose ->
|
| 719 | addVertex(pose, box.minX, 0.0, box.minZ).setColor(baseColor)
|
| 720 | addVertex(pose, box.minX, height, box.minZ).setColor(topColor)
|
| 721 | addVertex(pose, box.maxX, height, box.minZ).setColor(topColor)
|
| 722 | addVertex(pose, box.maxX, 0.0, box.minZ).setColor(baseColor)
|
| 723 |
|
| 724 | addVertex(pose, box.maxX, 0.0, box.minZ).setColor(baseColor)
|
| 725 | addVertex(pose, box.maxX, height, box.minZ).setColor(topColor)
|
| 726 | addVertex(pose, box.maxX, height, box.maxZ).setColor(topColor)
|
| 727 | addVertex(pose, box.maxX, 0.0, box.maxZ).setColor(baseColor)
|
| 728 |
|
| 729 | addVertex(pose, box.maxX, 0.0, box.maxZ).setColor(baseColor)
|
| 730 | addVertex(pose, box.maxX, height, box.maxZ).setColor(topColor)
|
| 731 | addVertex(pose, box.minX, height, box.maxZ).setColor(topColor)
|
| 732 | addVertex(pose, box.minX, 0.0, box.maxZ).setColor(baseColor)
|
| 733 |
|
| 734 | addVertex(pose, box.minX, 0.0, box.maxZ).setColor(baseColor)
|
| 735 | addVertex(pose, box.minX, height, box.maxZ).setColor(topColor)
|
| 736 | addVertex(pose, box.minX, height, box.minZ).setColor(topColor)
|
| 737 | addVertex(pose, box.minX, 0.0, box.minZ).setColor(baseColor)
|
| 738 | }
|
| 739 | }
|
| 740 |
|
| 741 | @Suppress("unused")
|
| 742 | enum class AnchorPoint(val xFactor: Float, val yFactor: Float) {
|
| 743 | TOP_LEFT(-1.0f, 0.0f), TOP_CENTER(-0.5f, 0.0f), TOP_RIGHT(0.0f, 0.0f),
|
| 744 | CENTER_LEFT(-1.0f, -0.5f), CENTER(-0.5f, -0.5f), CENTER_RIGHT(0.0f, -0.5f),
|
| 745 | BOTTOM_LEFT(-1.0f, -1.0f), BOTTOM_CENTER(-0.5f, -1.0f), BOTTOM_RIGHT(0.0f, -1.0f)
|
| 746 | }
|
| 747 |
|