diff options
Diffstat (limited to 'src/main/kotlin/util')
| -rw-r--r-- | src/main/kotlin/util/FragmentGuiScreen.kt | 10 | ||||
| -rw-r--r-- | src/main/kotlin/util/MC.kt | 5 | ||||
| -rw-r--r-- | src/main/kotlin/util/MoulConfigFragment.kt | 6 | ||||
| -rw-r--r-- | src/main/kotlin/util/MoulConfigUtils.kt | 6 | ||||
| -rw-r--r-- | src/main/kotlin/util/mc/ItemUtil.kt | 30 | ||||
| -rw-r--r-- | src/main/kotlin/util/render/CustomRenderLayers.kt | 10 | ||||
| -rw-r--r-- | src/main/kotlin/util/render/DrawContextExt.kt | 85 | ||||
| -rw-r--r-- | src/main/kotlin/util/render/FacingThePlayerContext.kt | 5 | ||||
| -rw-r--r-- | src/main/kotlin/util/render/RenderCircleProgress.kt | 14 | ||||
| -rw-r--r-- | src/main/kotlin/util/render/RenderInWorldContext.kt | 25 | ||||
| -rw-r--r-- | src/main/kotlin/util/render/TranslatedScissors.kt | 28 |
11 files changed, 129 insertions, 95 deletions
diff --git a/src/main/kotlin/util/FragmentGuiScreen.kt b/src/main/kotlin/util/FragmentGuiScreen.kt index 5e13d51..c4d9ac0 100644 --- a/src/main/kotlin/util/FragmentGuiScreen.kt +++ b/src/main/kotlin/util/FragmentGuiScreen.kt @@ -19,13 +19,9 @@ abstract class FragmentGuiScreen( popup = MoulConfigFragment(context, position) { popup = null } } - override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) { - super.render(context, mouseX, mouseY, delta) - context.matrices.push() - context.matrices.translate(0f, 0f, 1000f) - popup?.render(context, mouseX, mouseY, delta) - context.matrices.pop() - } + fun renderPopup(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) { + popup?.render(context, mouseX, mouseY, delta) + } private inline fun ifPopup(ifYes: (MoulConfigFragment) -> Unit): Boolean { val p = popup ?: return false diff --git a/src/main/kotlin/util/MC.kt b/src/main/kotlin/util/MC.kt index e85b119..d597beb 100644 --- a/src/main/kotlin/util/MC.kt +++ b/src/main/kotlin/util/MC.kt @@ -16,11 +16,13 @@ import net.minecraft.client.world.ClientWorld import net.minecraft.entity.Entity import net.minecraft.item.Item import net.minecraft.item.ItemStack +import net.minecraft.nbt.NbtOps import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket import net.minecraft.registry.BuiltinRegistries import net.minecraft.registry.Registry import net.minecraft.registry.RegistryKey import net.minecraft.registry.RegistryKeys +import net.minecraft.registry.RegistryOps import net.minecraft.registry.RegistryWrapper import net.minecraft.resource.ReloadableResourceManagerImpl import net.minecraft.text.Text @@ -74,7 +76,7 @@ object MC { fun sendCommand(command: String) { // TODO: add a queue to this and sendServerChat ErrorUtil.softCheck("Server commands have an implied /", !command.startsWith("/")) - player?.networkHandler?.sendCommand(command) + player?.networkHandler?.sendChatCommand(command) } fun onMainThread(block: () -> Unit) { @@ -117,6 +119,7 @@ object MC { inline val window get() = instance.window inline val currentRegistries: RegistryWrapper.WrapperLookup? get() = world?.registryManager val defaultRegistries: RegistryWrapper.WrapperLookup by lazy { BuiltinRegistries.createWrapperLookup() } + val defaultRegistryNbtOps by lazy { RegistryOps.of(NbtOps.INSTANCE, defaultRegistries) } inline val currentOrDefaultRegistries get() = currentRegistries ?: defaultRegistries val defaultItems: RegistryWrapper.Impl<Item> by lazy { defaultRegistries.getOrThrow(RegistryKeys.ITEM) } var currentTick = 0 diff --git a/src/main/kotlin/util/MoulConfigFragment.kt b/src/main/kotlin/util/MoulConfigFragment.kt index 28ccfd0..1aaea0a 100644 --- a/src/main/kotlin/util/MoulConfigFragment.kt +++ b/src/main/kotlin/util/MoulConfigFragment.kt @@ -31,10 +31,10 @@ class MoulConfigFragment( override fun render(drawContext: DrawContext?, i: Int, j: Int, f: Float) { val ctx = createContext(drawContext) val m = drawContext!!.matrices - m.push() - m.translate(position.x.toFloat(), position.y.toFloat(), 0F) + m.pushMatrix() + m.translate(position.x.toFloat(), position.y.toFloat()) context.root.render(ctx) - m.pop() + m.popMatrix() ctx.renderContext.renderExtraLayers() } diff --git a/src/main/kotlin/util/MoulConfigUtils.kt b/src/main/kotlin/util/MoulConfigUtils.kt index 51ff340..048ec0a 100644 --- a/src/main/kotlin/util/MoulConfigUtils.kt +++ b/src/main/kotlin/util/MoulConfigUtils.kt @@ -316,10 +316,10 @@ object MoulConfigUtils { mouseY: Int ) { val immContext = createInPlaceFullContext(this, mouseX, mouseY) - matrices.push() - matrices.translate(x.toFloat(), y.toFloat(), 0F) + matrices.pushMatrix() + matrices.translate(x.toFloat(), y.toFloat()) component.render(immContext.translated(x, y, w, h)) - matrices.pop() + matrices.popMatrix() } diff --git a/src/main/kotlin/util/mc/ItemUtil.kt b/src/main/kotlin/util/mc/ItemUtil.kt index 13519cf..3cabb8e 100644 --- a/src/main/kotlin/util/mc/ItemUtil.kt +++ b/src/main/kotlin/util/mc/ItemUtil.kt @@ -1,20 +1,30 @@ package moe.nea.firmament.util.mc +import kotlin.jvm.optionals.getOrNull import net.minecraft.item.ItemStack +import net.minecraft.nbt.NbtCompound +import net.minecraft.nbt.NbtOps +import net.minecraft.registry.RegistryOps +import net.minecraft.registry.RegistryWrapper import net.minecraft.text.Text +import moe.nea.firmament.util.MC fun ItemStack.appendLore(args: List<Text>) { - if (args.isEmpty()) return - modifyLore { - val loreList = loreAccordingToNbt.toMutableList() - for (arg in args) { - loreList.add(arg) - } - loreList - } + if (args.isEmpty()) return + modifyLore { + val loreList = loreAccordingToNbt.toMutableList() + for (arg in args) { + loreList.add(arg) + } + loreList + } } fun ItemStack.modifyLore(update: (List<Text>) -> List<Text>) { - val loreList = loreAccordingToNbt - loreAccordingToNbt = update(loreList) + val loreList = loreAccordingToNbt + loreAccordingToNbt = update(loreList) +} + +fun loadItemFromNbt(nbt: NbtCompound, registries: RegistryWrapper.WrapperLookup = MC.defaultRegistries): ItemStack? { + return ItemStack.CODEC.decode(RegistryOps.of(NbtOps.INSTANCE, registries), nbt).result().getOrNull()?.first } diff --git a/src/main/kotlin/util/render/CustomRenderLayers.kt b/src/main/kotlin/util/render/CustomRenderLayers.kt index f713a81..6c14bf6 100644 --- a/src/main/kotlin/util/render/CustomRenderLayers.kt +++ b/src/main/kotlin/util/render/CustomRenderLayers.kt @@ -31,7 +31,7 @@ object CustomRenderPipelines { .withDepthTestFunction(DepthTestFunction.NO_DEPTH_TEST) .build() val COLORED_OMNIPRESENT_QUADS = - RenderPipeline.builder(RenderPipelines.MATRICES_COLOR_SNIPPET)// TODO: split this up to support better transparent ordering. + RenderPipeline.builder(RenderPipelines.TRANSFORMS_AND_PROJECTION_SNIPPET)// TODO: split this up to support better transparent ordering. .withLocation(Firmament.identifier("colored_omnipresent_quads")) .withVertexShader("core/position_color") .withFragmentShader("core/position_color") @@ -46,7 +46,7 @@ object CustomRenderPipelines { RenderPipeline.builder(RenderPipelines.POSITION_TEX_COLOR_SNIPPET) .withVertexFormat(VertexFormats.POSITION_TEXTURE_COLOR, DrawMode.TRIANGLES) .withLocation(Firmament.identifier("gui_textured_overlay_tris_circle")) - .withUniform("InnerCutoutRadius", UniformType.FLOAT) + .withUniform("InnerCutoutRadius", UniformType.UNIFORM_BUFFER) .withFragmentShader(Firmament.identifier("circle_discard_color")) .withBlend(BlendFunction.TRANSLUCENT) .build() @@ -57,12 +57,12 @@ object CustomRenderPipelines { .withSampler("Sampler0") .withSampler("Sampler1") .withSampler("Sampler3") - .withUniform("Animation", UniformType.FLOAT) + .withUniform("Animation", UniformType.UNIFORM_BUFFER) .build() } object CustomRenderLayers { - inline fun memoizeTextured(crossinline func: (Identifier) -> RenderLayer) = memoize(func) + inline fun memoizeTextured(crossinline func: (Identifier) -> RenderLayer.MultiPhase) = memoize(func) inline fun <T, R> memoize(crossinline func: (T) -> R): Function<T, R> { return Util.memoize { it: T -> func(it) } } @@ -73,7 +73,7 @@ object CustomRenderLayers { RenderLayer.DEFAULT_BUFFER_SIZE, CustomRenderPipelines.GUI_TEXTURED_NO_DEPTH_TRIS, RenderLayer.MultiPhaseParameters.builder().texture( - RenderPhase.Texture(texture, TriState.DEFAULT, false) + RenderPhase.Texture(texture, false) ) .build(false) ) diff --git a/src/main/kotlin/util/render/DrawContextExt.kt b/src/main/kotlin/util/render/DrawContextExt.kt index a833c86..d11ba6a 100644 --- a/src/main/kotlin/util/render/DrawContextExt.kt +++ b/src/main/kotlin/util/render/DrawContextExt.kt @@ -2,26 +2,27 @@ package moe.nea.firmament.util.render import com.mojang.blaze3d.systems.RenderSystem import me.shedaniel.math.Color -import org.joml.Matrix4f import util.render.CustomRenderLayers +import net.minecraft.client.gl.RenderPipelines import net.minecraft.client.gui.DrawContext -import net.minecraft.client.render.RenderLayer +import net.minecraft.client.gui.ScreenRect +import net.minecraft.client.gui.render.state.special.SpecialGuiElementRenderState import net.minecraft.util.Identifier import moe.nea.firmament.util.MC fun DrawContext.isUntranslatedGuiDrawContext(): Boolean { - return (matrices.peek().positionMatrix.properties() and Matrix4f.PROPERTY_IDENTITY.toInt()) != 0 + return matrices.m00 == 1F && matrices.m11 == 1f && matrices.m01 == 0F && matrices.m10 == 0F && matrices.m20 == 1f && matrices.m21 == 1F } @Deprecated("Use the other drawGuiTexture") fun DrawContext.drawGuiTexture( x: Int, y: Int, z: Int, width: Int, height: Int, sprite: Identifier -) = this.drawGuiTexture(RenderLayer::getGuiTextured, sprite, x, y, width, height) +) = this.drawGuiTexture(RenderPipelines.GUI_TEXTURED, sprite, x, y, width, height) fun DrawContext.drawGuiTexture( sprite: Identifier, x: Int, y: Int, width: Int, height: Int -) = this.drawGuiTexture(RenderLayer::getGuiTextured, sprite, x, y, width, height) +) = this.drawGuiTexture(RenderPipelines.GUI_TEXTURED, sprite, x, y, width, height) fun DrawContext.drawTexture( sprite: Identifier, @@ -34,18 +35,20 @@ fun DrawContext.drawTexture( textureWidth: Int, textureHeight: Int ) { - this.drawTexture(RenderLayer::getGuiTextured, - sprite, - x, - y, - u, - v, - width, - height, - width, - height, - textureWidth, - textureHeight) + this.drawTexture( + RenderPipelines.GUI_TEXTURED, + sprite, + x, + y, + u, + v, + width, + height, + width, + height, + textureWidth, + textureHeight + ) } fun DrawContext.drawLine(fromX: Int, fromY: Int, toX: Int, toY: Int, color: Color) { @@ -55,14 +58,46 @@ fun DrawContext.drawLine(fromX: Int, fromY: Int, toX: Int, toY: Int, color: Colo drawLine(toX, toY, fromX, fromY, color) return } + val rect = ScreenRect(fromX, fromY, toX - fromX, toY - fromY).transform(matrices) RenderSystem.lineWidth(MC.window.scaleFactor.toFloat()) - draw { vertexConsumers -> - val buf = vertexConsumers.getBuffer(CustomRenderLayers.LINES) - val matrix = this.matrices.peek() - buf.vertex(matrix, fromX.toFloat(), fromY.toFloat(), 0F).color(color.color) - .normal(toX - fromX.toFloat(), toY - fromY.toFloat(), 0F) - buf.vertex(matrix, toX.toFloat(), toY.toFloat(), 0F).color(color.color) - .normal(toX - fromX.toFloat(), toY - fromY.toFloat(), 0F) - } + // TODO: this also requires adding a List<SpecialGuiElementRenderer<?>> entry in guirenderer + // TODO: state.addSpecialElement(object : SpecialGuiElementRenderState { + // override fun x1(): Int { + // return fromX + // } + // + // override fun x2(): Int { + // return toY + // } + // + // override fun y1(): Int { + // return fromY + // } + // + // override fun y2(): Int { + // return toY + // } + // + // override fun scale(): Float { + // return 1f + // } + // + // override fun scissorArea(): ScreenRect? { + // return rect + // } + // + // override fun bounds(): ScreenRect? { + // return rect + // } + // + // }) +// draw { vertexConsumers -> +// val buf = vertexConsumers.getBuffer(CustomRenderLayers.LINES) +// val matrix = this.matrices.peek() +// buf.vertex(matrix, fromX.toFloat(), fromY.toFloat(), 0F).color(color.color) +// .normal(toX - fromX.toFloat(), toY - fromY.toFloat(), 0F) +// buf.vertex(matrix, toX.toFloat(), toY.toFloat(), 0F).color(color.color) +// .normal(toX - fromX.toFloat(), toY - fromY.toFloat(), 0F) +// } } diff --git a/src/main/kotlin/util/render/FacingThePlayerContext.kt b/src/main/kotlin/util/render/FacingThePlayerContext.kt index 670beb6..0e5788a 100644 --- a/src/main/kotlin/util/render/FacingThePlayerContext.kt +++ b/src/main/kotlin/util/render/FacingThePlayerContext.kt @@ -3,9 +3,12 @@ package moe.nea.firmament.util.render import io.github.notenoughupdates.moulconfig.platform.next import org.joml.Matrix4f +import util.render.CustomRenderLayers import net.minecraft.client.font.TextRenderer import net.minecraft.client.render.LightmapTextureManager import net.minecraft.client.render.RenderLayer +import net.minecraft.client.render.RenderLayers +import net.minecraft.client.render.TexturedRenderLayers import net.minecraft.client.render.VertexConsumer import net.minecraft.text.Text import net.minecraft.util.Identifier @@ -70,7 +73,7 @@ class FacingThePlayerContext(val worldContext: RenderInWorldContext) { u1: Float, v1: Float, u2: Float, v2: Float, ) { - val buf = worldContext.vertexConsumers.getBuffer(RenderLayer.getGuiTexturedOverlay(texture)) + val buf = worldContext.vertexConsumers.getBuffer(CustomRenderLayers.GUI_TEXTURED_NO_DEPTH_TRIS.apply(texture)) // TODO: this is strictly an incorrect render layer val hw = width / 2F val hh = height / 2F val matrix4f: Matrix4f = worldContext.matrixStack.peek().positionMatrix diff --git a/src/main/kotlin/util/render/RenderCircleProgress.kt b/src/main/kotlin/util/render/RenderCircleProgress.kt index 81dde6f..2c875f8 100644 --- a/src/main/kotlin/util/render/RenderCircleProgress.kt +++ b/src/main/kotlin/util/render/RenderCircleProgress.kt @@ -4,7 +4,7 @@ import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.vertex.VertexFormat import io.github.notenoughupdates.moulconfig.platform.next import java.util.OptionalInt -import org.joml.Matrix4f +import org.joml.Matrix3x2f import util.render.CustomRenderLayers import net.minecraft.client.gui.DrawContext import net.minecraft.client.render.BufferBuilder @@ -19,7 +19,7 @@ object RenderCircleProgress { fun renderCircularSlice( drawContext: DrawContext, - layer: RenderLayer, + layer: RenderLayer.MultiPhase, u1: Float, u2: Float, v1: Float, @@ -28,13 +28,12 @@ object RenderCircleProgress { color: Int = -1, innerCutoutRadius: Float = 0F ) { - drawContext.draw() val sections = angleRadians.nonNegligibleSubSectionsAlignedWith((τ / 8f).toFloat()) .zipWithNext().toList() BufferAllocator(layer.vertexFormat.vertexSize * sections.size * 3).use { allocator -> val bufferBuilder = BufferBuilder(allocator, VertexFormat.DrawMode.TRIANGLES, layer.vertexFormat) - val matrix: Matrix4f = drawContext.matrices.peek().positionMatrix + val matrix: Matrix3x2f = drawContext.matrices for ((sectionStart, sectionEnd) in sections) { val firstPoint = Projections.Two.projectAngleOntoUnitBox(sectionStart.toDouble()) @@ -69,14 +68,15 @@ object RenderCircleProgress { val indexBufferConstructor = RenderSystem.getSequentialBuffer(VertexFormat.DrawMode.TRIANGLES) val indexBuffer = indexBufferConstructor.getIndexBuffer(buffer.drawParameters.indexCount) RenderSystem.getDevice().createCommandEncoder().createRenderPass( - MC.instance.framebuffer.colorAttachment, + { "Firmament Circle Renderer" }, + MC.instance.framebuffer.colorAttachmentView, OptionalInt.empty(), ).use { renderPass -> renderPass.setPipeline(layer.pipeline) - renderPass.setUniform("InnerCutoutRadius", innerCutoutRadius) +// renderPass.setUniform("InnerCutoutRadius", innerCutoutRadius) renderPass.setIndexBuffer(indexBuffer, indexBufferConstructor.indexType) renderPass.setVertexBuffer(0, vertexBuffer) - renderPass.drawIndexed(0, buffer.drawParameters.indexCount) + renderPass.drawIndexed(0, 0, buffer.drawParameters.indexCount, 1) } } } diff --git a/src/main/kotlin/util/render/RenderInWorldContext.kt b/src/main/kotlin/util/render/RenderInWorldContext.kt index 4963920..2f665f2 100644 --- a/src/main/kotlin/util/render/RenderInWorldContext.kt +++ b/src/main/kotlin/util/render/RenderInWorldContext.kt @@ -11,7 +11,6 @@ import net.minecraft.client.render.RenderLayer import net.minecraft.client.render.RenderTickCounter import net.minecraft.client.render.VertexConsumer import net.minecraft.client.render.VertexConsumerProvider -import net.minecraft.client.render.VertexFormats import net.minecraft.client.texture.Sprite import net.minecraft.client.util.math.MatrixStack import net.minecraft.text.Text @@ -30,18 +29,6 @@ class RenderInWorldContext private constructor( private val tickCounter: RenderTickCounter, val vertexConsumers: VertexConsumerProvider.Immediate, ) { - - - @Deprecated("stateful color management is no longer a thing") - fun color(color: me.shedaniel.math.Color) { - color(color.red / 255F, color.green / 255f, color.blue / 255f, color.alpha / 255f) - } - - @Deprecated("stateful color management is no longer a thing") - fun color(red: Float, green: Float, blue: Float, alpha: Float) { - RenderSystem.setShaderColor(red, green, blue, alpha) - } - fun block(blockPos: BlockPos, color: Int) { matrixStack.push() matrixStack.translate(blockPos.x.toFloat(), blockPos.y.toFloat(), blockPos.z.toFloat()) @@ -126,6 +113,7 @@ class RenderInWorldContext private constructor( fun wireframeCube(blockPos: BlockPos, lineWidth: Float = 10F) { val buf = vertexConsumers.getBuffer(RenderLayer.LINES) matrixStack.push() + // TODO: add color arg to this // TODO: this does not render through blocks (or water layers) anymore RenderSystem.lineWidth(lineWidth / pow(camera.pos.squaredDistanceTo(blockPos.toCenterPos()), 0.25).toFloat()) matrixStack.translate(blockPos.x.toFloat(), blockPos.y.toFloat(), blockPos.z.toFloat()) @@ -134,16 +122,16 @@ class RenderInWorldContext private constructor( vertexConsumers.draw() } - fun line(vararg points: Vec3d, lineWidth: Float = 10F) { - line(points.toList(), lineWidth) + fun line(vararg points: Vec3d, color: Int, lineWidth: Float = 10F) { + line(points.toList(), color, lineWidth) } - fun tracer(toWhere: Vec3d, lineWidth: Float = 3f) { + fun tracer(toWhere: Vec3d, color: Int, lineWidth: Float = 3f) { val cameraForward = Vector3f(0f, 0f, -1f).rotate(camera.rotation) - line(camera.pos.add(Vec3d(cameraForward)), toWhere, lineWidth = lineWidth) + line(camera.pos.add(Vec3d(cameraForward)), toWhere, color = color, lineWidth = lineWidth) } - fun line(points: List<Vec3d>, lineWidth: Float = 10F) { + fun line(points: List<Vec3d>, color: Int, lineWidth: Float = 10F) { RenderSystem.lineWidth(lineWidth) val buffer = vertexConsumers.getBuffer(CustomRenderLayers.LINES) @@ -263,7 +251,6 @@ class RenderInWorldContext private constructor( event.matrices.pop() event.vertexConsumers.draw() - RenderSystem.setShaderColor(1F, 1F, 1F, 1F) } } } diff --git a/src/main/kotlin/util/render/TranslatedScissors.kt b/src/main/kotlin/util/render/TranslatedScissors.kt index 8f8bdcf..70565ed 100644 --- a/src/main/kotlin/util/render/TranslatedScissors.kt +++ b/src/main/kotlin/util/render/TranslatedScissors.kt @@ -1,26 +1,26 @@ - package moe.nea.firmament.util.render -import org.joml.Matrix4f +import org.joml.Vector3f import org.joml.Vector4f import net.minecraft.client.gui.DrawContext fun DrawContext.enableScissorWithTranslation(x1: Float, y1: Float, x2: Float, y2: Float) { - enableScissor(x1.toInt(), y1.toInt(), x2.toInt(), y2.toInt()) + enableScissor(x1.toInt(), y1.toInt(), x2.toInt(), y2.toInt()) } + fun DrawContext.enableScissorWithoutTranslation(x1: Float, y1: Float, x2: Float, y2: Float) { - val pMat = matrices.peek().positionMatrix.invert(Matrix4f()) - val target = Vector4f() + val pMat = matrices.invert() + var target = Vector3f() - target.set(x1, y1, 0f, 1f) - target.mul(pMat) - val scissorX1 = target.x - val scissorY1 = target.y + target.set(x1, y1, 0f) + target.mul(pMat) + val scissorX1 = target.x + val scissorY1 = target.y - target.set(x2, y2, 0f, 1f) - target.mul(pMat) - val scissorX2 = target.x - val scissorY2 = target.y + target.set(x2, y2, 0f) + target.mul(pMat) + val scissorX2 = target.x + val scissorY2 = target.y - enableScissor(scissorX1.toInt(), scissorY1.toInt(), scissorX2.toInt(), scissorY2.toInt()) + enableScissor(scissorX1.toInt(), scissorY1.toInt(), scissorX2.toInt(), scissorY2.toInt()) } |
