From 6224a3e4446f7232e80baa1aecd2d7b8dfa3dc26 Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Wed, 10 Nov 2021 17:27:03 +0100 Subject: [PATCH] Time well spent - Fixed shaft models misaligning near large cogwheels --- .../com/simibubi/create/AllBlockPartials.java | 2 +- .../com/simibubi/create/AllTileEntities.java | 8 +- .../base/KineticTileEntityRenderer.java | 3 +- .../BracketedKineticTileInstance.java | 92 +++++++++++++++++++ .../BracketedKineticTileRenderer.java | 71 ++++++++++++++ .../create/models/block/cogwheel_shaft.json | 22 +++++ 6 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/relays/elementary/BracketedKineticTileInstance.java create mode 100644 src/main/java/com/simibubi/create/content/contraptions/relays/elementary/BracketedKineticTileRenderer.java create mode 100644 src/main/resources/assets/create/models/block/cogwheel_shaft.json diff --git a/src/main/java/com/simibubi/create/AllBlockPartials.java b/src/main/java/com/simibubi/create/AllBlockPartials.java index cccc00d34..f3557c64e 100644 --- a/src/main/java/com/simibubi/create/AllBlockPartials.java +++ b/src/main/java/com/simibubi/create/AllBlockPartials.java @@ -18,7 +18,7 @@ public class AllBlockPartials { SCHEMATICANNON_PIPE = get("schematicannon/pipe"), SHAFTLESS_COGWHEEL = get("cogwheel_shaftless"), SHAFTLESS_LARGE_COGWHEEL = get("large_cogwheel_shaftless"), - SHAFT_HALF = get("shaft_half"), + COGWHEEL_SHAFT = get("cogwheel_shaft"), SHAFT_HALF = get("shaft_half"), BELT_PULLEY = get("belt_pulley"), BELT_START = get("belt/start"), BELT_MIDDLE = get("belt/middle"), BELT_END = get("belt/end"), BELT_START_BOTTOM = get("belt/start_bottom"), diff --git a/src/main/java/com/simibubi/create/AllTileEntities.java b/src/main/java/com/simibubi/create/AllTileEntities.java index 9df1b57b9..d56938d0f 100644 --- a/src/main/java/com/simibubi/create/AllTileEntities.java +++ b/src/main/java/com/simibubi/create/AllTileEntities.java @@ -104,6 +104,8 @@ import com.simibubi.create.content.contraptions.relays.belt.BeltInstance; import com.simibubi.create.content.contraptions.relays.belt.BeltRenderer; import com.simibubi.create.content.contraptions.relays.belt.BeltTileEntity; import com.simibubi.create.content.contraptions.relays.elementary.BracketedKineticTileEntity; +import com.simibubi.create.content.contraptions.relays.elementary.BracketedKineticTileInstance; +import com.simibubi.create.content.contraptions.relays.elementary.BracketedKineticTileRenderer; import com.simibubi.create.content.contraptions.relays.elementary.SimpleKineticTileEntity; import com.simibubi.create.content.contraptions.relays.encased.AdjustablePulleyTileEntity; import com.simibubi.create.content.contraptions.relays.encased.ClutchTileEntity; @@ -188,11 +190,11 @@ public class AllTileEntities { // Kinetics public static final TileEntityEntry BRACKETED_KINETIC = Create.registrate() .tileEntity("simple_kinetic", BracketedKineticTileEntity::new) - .instance(() -> SingleRotatingInstance::new) + .instance(() -> BracketedKineticTileInstance::new) .validBlocks(AllBlocks.SHAFT, AllBlocks.COGWHEEL, AllBlocks.LARGE_COGWHEEL) - .renderer(() -> KineticTileEntityRenderer::new) + .renderer(() -> BracketedKineticTileRenderer::new) .register(); - + public static final TileEntityEntry MOTOR = Create.registrate() .tileEntity("motor", CreativeMotorTileEntity::new) .instance(() -> HalfShaftInstance::new) diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java index 44b0e3dad..0a7c53bbe 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java @@ -97,9 +97,8 @@ public class KineticTileEntityRenderer extends SafeTileEntityRenderer half = getRotatingMaterial().getModel(AllBlockPartials.COGWHEEL_SHAFT, blockState, + facing, () -> this.rotateToAxis(axis)); + + additionalShaft = setup(half.createInstance(), speed); + additionalShaft.setRotationOffset(offset); + } + + @Override + protected Instancer getModel() { + if (!ICogWheel.isLargeCog(tile.getBlockState())) + return super.getModel(); + + Axis axis = KineticTileEntityRenderer.getRotationAxisOf(tile); + Direction facing = Direction.fromAxisAndDirection(axis, AxisDirection.POSITIVE); + return getRotatingMaterial().getModel(AllBlockPartials.SHAFTLESS_LARGE_COGWHEEL, blockState, facing, + () -> this.rotateToAxis(axis)); + } + + private PoseStack rotateToAxis(Axis axis) { + Direction facing = Direction.fromAxisAndDirection(axis, AxisDirection.POSITIVE); + PoseStack poseStack = new PoseStack(); + new MatrixTransformStack(poseStack).centre() + .rotateToFace(facing) + .multiply(Vector3f.XN.rotationDegrees(-90)) + .unCentre(); + return poseStack; + } + + @Override + public void update() { + super.update(); + if (additionalShaft != null) { + updateRotation(additionalShaft); + additionalShaft.setRotationOffset(BracketedKineticTileRenderer.getShaftAngleOffset(axis, pos)); + } + } + + @Override + public void updateLight() { + super.updateLight(); + if (additionalShaft != null) + relight(pos, additionalShaft); + } + + @Override + public void remove() { + super.remove(); + if (additionalShaft != null) + additionalShaft.delete(); + } + +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/BracketedKineticTileRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/BracketedKineticTileRenderer.java new file mode 100644 index 000000000..675249471 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/BracketedKineticTileRenderer.java @@ -0,0 +1,71 @@ +package com.simibubi.create.content.contraptions.relays.elementary; + +import com.jozufozu.flywheel.backend.Backend; +import com.mojang.blaze3d.vertex.PoseStack; +import com.simibubi.create.AllBlockPartials; +import com.simibubi.create.AllBlocks; +import com.simibubi.create.content.contraptions.base.KineticTileEntity; +import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; +import com.simibubi.create.foundation.render.PartialBufferer; +import com.simibubi.create.foundation.render.SuperByteBuffer; +import com.simibubi.create.foundation.utility.AnimationTickHolder; + +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider.Context; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.core.Direction.Axis; +import net.minecraft.core.Direction.AxisDirection; + +public class BracketedKineticTileRenderer extends KineticTileEntityRenderer { + + public BracketedKineticTileRenderer(Context context) { + super(context); + } + + @Override + protected void renderSafe(KineticTileEntity te, float partialTicks, PoseStack ms, MultiBufferSource buffer, + int light, int overlay) { + + if (Backend.getInstance() + .canUseInstancing(te.getLevel())) + return; + + if (!AllBlocks.LARGE_COGWHEEL.has(te.getBlockState())) { + super.renderSafe(te, partialTicks, ms, buffer, light, overlay); + return; + } + + // Large cogs sometimes have to offset their teeth by 11.25 degrees in order to + // mesh properly + + Axis axis = getRotationAxisOf(te); + BlockPos pos = te.getBlockPos(); + + Direction facing = Direction.fromAxisAndDirection(axis, AxisDirection.POSITIVE); + renderRotatingBuffer(te, + PartialBufferer.getFacingVertical(AllBlockPartials.SHAFTLESS_LARGE_COGWHEEL, te.getBlockState(), facing), + ms, buffer.getBuffer(RenderType.solid()), light); + + float offset = getShaftAngleOffset(axis, pos); + float time = AnimationTickHolder.getRenderTime(te.getLevel()); + float angle = ((time * te.getSpeed() * 3f / 10 + offset) % 360) / 180 * (float) Math.PI; + + SuperByteBuffer shaft = + PartialBufferer.getFacingVertical(AllBlockPartials.COGWHEEL_SHAFT, te.getBlockState(), facing); + kineticRotationTransform(shaft, te, axis, angle, light); + shaft.renderInto(ms, buffer.getBuffer(RenderType.solid())); + + } + + public static float getShaftAngleOffset(Axis axis, BlockPos pos) { + float offset = 0; + double d = (((axis == Axis.X) ? 0 : pos.getX()) + ((axis == Axis.Y) ? 0 : pos.getY()) + + ((axis == Axis.Z) ? 0 : pos.getZ())) % 2; + if (d == 0) + offset = 22.5f; + return offset; + } + +} diff --git a/src/main/resources/assets/create/models/block/cogwheel_shaft.json b/src/main/resources/assets/create/models/block/cogwheel_shaft.json new file mode 100644 index 000000000..255b85db0 --- /dev/null +++ b/src/main/resources/assets/create/models/block/cogwheel_shaft.json @@ -0,0 +1,22 @@ +{ + "credit": "Made with Blockbench", + "textures": { + "0": "create:block/axis_top", + "1": "create:block/cogwheel_axis", + "particle": "create:block/axis_top" + }, + "elements": [ + { + "from": [6, 0, 6], + "to": [10, 16, 10], + "faces": { + "north": {"uv": [6, 0, 10, 16], "texture": "#1"}, + "east": {"uv": [6, 0, 10, 16], "texture": "#1"}, + "south": {"uv": [6, 0, 10, 16], "texture": "#1"}, + "west": {"uv": [6, 0, 10, 16], "texture": "#1"}, + "up": {"uv": [6, 6, 10, 10], "texture": "#0"}, + "down": {"uv": [6, 6, 10, 10], "texture": "#0"} + } + } + ] +} \ No newline at end of file