From 7dffa61bae19b8b5edc7c1758b9bd2b35cff5f63 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 24 Jan 2023 12:09:37 +0700 Subject: [PATCH] rotateWithBlockFacing --- .../render/blockentity/HoloSignRenderer.kt | 11 +-- .../mc/otm/core/math/EuclidMath.kt | 84 +++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt index a06a15d92..8af4ac339 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/blockentity/HoloSignRenderer.kt @@ -4,14 +4,16 @@ import com.mojang.blaze3d.vertex.PoseStack import net.minecraft.client.renderer.MultiBufferSource import net.minecraft.client.renderer.blockentity.BlockEntityRenderer import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider +import net.minecraft.core.Direction +import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock import ru.dbotthepony.mc.otm.block.entity.decorative.HoloSignBlockEntity import ru.dbotthepony.mc.otm.client.font import ru.dbotthepony.mc.otm.client.render.DynamicBufferSource import ru.dbotthepony.mc.otm.client.render.TextAlign import ru.dbotthepony.mc.otm.client.render.drawAligned +import ru.dbotthepony.mc.otm.core.get import ru.dbotthepony.mc.otm.core.math.RGBAColor -import ru.dbotthepony.mc.otm.core.math.rotateX -import ru.dbotthepony.mc.otm.core.math.rotateY +import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer { override fun render( @@ -23,9 +25,8 @@ class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) p_112312_: Int ) { poseStack.pushPose() - poseStack.translate(0.5f, 0.5f, -0.1f) - poseStack.rotateX(Math.PI.toFloat()) - poseStack.rotateY(Math.PI.toFloat()) + poseStack.rotateWithBlockFacing(tile.blockState[RotatableMatteryBlock.FACING_FULL], clarifyingAxis = Direction.SOUTH) + poseStack.translate(0.0f, 0.0f, -0.1f) poseStack.scale(0.01f, 0.01f, 0.01f) val sorse = DynamicBufferSource.WORLD diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt index ce084ea57..2d9c2d4e2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/EuclidMath.kt @@ -561,6 +561,90 @@ fun PoseStack.rotateZ(rotation: Float): PoseStack { return this } +const val PIf = 3.1415927f + +/** + * aligns 0,0 with top left point of block corner when looking directly at it, + * and swaps Y to +Y when going down and -Y when going up + */ +fun PoseStack.rotateWithBlockFacing(rotation: Direction, clarifyingAxis: Direction? = null): PoseStack { + when (rotation) { + Direction.NORTH -> { + rotateX(PIf) + rotateY(PIf) + translate(-1f, -1f, 0f) + } + + Direction.EAST -> { + rotateX(PIf) + rotateY(PIf / -2f) + translate(-1f, -1f, -1f) + } + + Direction.WEST -> { + rotateX(PIf) + rotateY(PIf / 2f) + translate(0f, -1f, 0f) + } + + Direction.UP -> { + rotateX(PIf / 2f) + translate(0f, 0f, -1f) + + if (clarifyingAxis != null) { + when (clarifyingAxis) { + Direction.NORTH -> {} + Direction.SOUTH -> { + rotateZ(PIf) + translate(-1f, -1f, 0f) + } + Direction.WEST -> { + rotateZ(PIf / -2f) + translate(-1f, 0f, 0f) + } + Direction.EAST -> { + rotateZ(PIf / 2f) + translate(0f, -1f, 0f) + } + else -> {} + } + } + } + + Direction.DOWN -> { + rotateY(PIf) + rotateX(PIf / -2f) + translate(-1f, 0f, 0f) + + if (clarifyingAxis != null) { + when (clarifyingAxis) { + Direction.NORTH -> {} + Direction.SOUTH -> { + rotateZ(PIf) + translate(-1f, -1f, 0f) + } + Direction.WEST -> { + rotateZ(PIf / 2f) + translate(0f, -1f, 0f) + } + Direction.EAST -> { + rotateZ(PIf / -2f) + translate(-1f, 0f, 0f) + } + else -> {} + } + } + } + + Direction.SOUTH -> { + rotateX(PIf) + translate(0f, -1f, -1f) + } + } + + return this +} + fun PoseStack.rotateYDegrees(rotation: Float): PoseStack { mulPose(Quaternionf(AxisAngle4f(toRadians(rotation), 0f, 1f, 0f))) return this