From 9ded16fbabb38e3415cc7b5452cbaea1bef98f61 Mon Sep 17 00:00:00 2001 From: Rabbitminers Date: Thu, 23 Mar 2023 17:41:15 +0000 Subject: [PATCH] Integrated BogeyRenderer To BogeyInstance (Also Corrected Rendering In Contraption) --- .../logistics/trains/BogeyRenderer.java | 23 +- .../trains/entity/BogeyInstance.java | 215 ++---------------- 2 files changed, 41 insertions(+), 197 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java b/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java index 447982088..f5e0d9e16 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/BogeyRenderer.java @@ -41,12 +41,20 @@ import java.util.Set; * overriden methods for each size */ +// Seperate From BogeyInstance So It Can Be Used Inworld + public abstract class BogeyRenderer { Map renderers = new EnumMap<>(BogeySize.class); Map contraptionModelData = new HashMap<>(); - public Transform[] getTransformsFromPartial(PartialModel model, boolean inContraption, int size) { - return inContraption ? contraptionModelData.get(model) : createModelData(model, size); + public Transform[] getTransformsFromPartial(PartialModel model, PoseStack ms, boolean inContraption, int size) { + return (inContraption) ? transformContraptionModelData(model, ms) : createModelData(model, size); + } + + private Transform[] transformContraptionModelData(PartialModel model, PoseStack ms) { + ModelData[] modelData = contraptionModelData.get(model); + Arrays.stream(modelData).forEach(modelDataElement -> modelDataElement.setTransform(ms)); + return modelData; } private Transform[] createModelData(PartialModel model, int size) { @@ -57,9 +65,10 @@ public abstract class BogeyRenderer { .toArray(SuperByteBuffer[]::new); } - public Transform getTransformFromPartial(PartialModel model, boolean inContraption) { + public Transform getTransformFromPartial(PartialModel model, PoseStack ms, boolean inContraption) { BlockState air = Blocks.AIR.defaultBlockState(); - return inContraption ? contraptionModelData.get(model)[0] + System.out.println(CachedBufferer.partial(model, air)); + return inContraption ? contraptionModelData.get(model)[0].setTransform(ms) : CachedBufferer.partial(model, air); } @@ -103,6 +112,12 @@ public abstract class BogeyRenderer { return renderers.keySet(); } + public void emptyTransforms() { + for (ModelData[] data : contraptionModelData.values()) + for (ModelData model : data) + model.setEmptyTransform(); + } + public void updateLight(int blockLight, int skyLight) { for (ModelData[] data : contraptionModelData.values()) for (ModelData model : data) diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java index 9a8a6c9c1..eacc51ada 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java @@ -6,230 +6,59 @@ import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.materials.model.ModelData; import com.jozufozu.flywheel.util.AnimationTickHolder; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.AllBlocks; import com.simibubi.create.content.contraptions.relays.elementary.ShaftBlock; +import com.simibubi.create.content.logistics.trains.BogeyRenderer; import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.Iterate; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.BlockAndTintGetter; import net.minecraft.world.level.LightLayer; import net.minecraft.world.phys.Vec3; -public sealed class BogeyInstance { +import java.util.EnumMap; +import java.util.Map; + +public abstract class BogeyInstance { public final CarriageBogey bogey; - private final ModelData[] shafts; + public final BogeyRenderer renderer; + private final BogeyRenderer.BogeySize size; - protected BogeyInstance(CarriageBogey bogey, MaterialManager materialManager) { + public BogeyInstance(CarriageBogey bogey, BogeyRenderer renderer, BogeyRenderer.BogeySize size, MaterialManager materialManager) { this.bogey = bogey; + this.renderer = renderer; + this.size = size; - shafts = new ModelData[2]; - - materialManager.defaultSolid() - .material(Materials.TRANSFORMED) - .getModel(AllBlocks.SHAFT.getDefaultState() - .setValue(ShaftBlock.AXIS, Direction.Axis.Z)) - .createInstances(shafts); - + renderer.initialiseContraptionModelData(materialManager, size); } - public void remove() { - for (ModelData shaft : shafts) - shaft.delete(); - } - - public void hiddenFrame() { + protected void hiddenFrame() { beginFrame(0, null); } - + public void beginFrame(float wheelAngle, PoseStack ms) { if (ms == null) { - for (int i : Iterate.zeroAndOne) - shafts[i].setEmptyTransform(); + renderer.emptyTransforms(); return; } - for (int i : Iterate.zeroAndOne) - shafts[i].setTransform(ms) - .translate(-.5f, .25f, i * -1) - .centre() - .rotateZ(wheelAngle) - .unCentre(); - } + renderer.render(new CompoundTag(), wheelAngle, ms, this.size); + }; public void updateLight(BlockAndTintGetter world, CarriageContraptionEntity entity) { var lightPos = new BlockPos(getLightPos(entity)); - - updateLight(world.getBrightness(LightLayer.BLOCK, lightPos), world.getBrightness(LightLayer.SKY, lightPos)); + renderer.updateLight(world.getBrightness(LightLayer.BLOCK, lightPos), + world.getBrightness(LightLayer.SKY, lightPos)); } private Vec3 getLightPos(CarriageContraptionEntity entity) { - if (bogey.getAnchorPosition() != null) { - return bogey.getAnchorPosition(); - } else { - return entity.getLightProbePosition(AnimationTickHolder.getPartialTicks()); - } - } - - public void updateLight(int blockLight, int skyLight) { - for (ModelData shaft : shafts) { - shaft.setBlockLight(blockLight) - .setSkyLight(skyLight); - } - } - - public static final class Frame extends BogeyInstance { - - private final ModelData frame; - private final ModelData[] wheels; - - public Frame(CarriageBogey bogey, MaterialManager materialManager) { - super(bogey, materialManager); - - frame = materialManager.defaultSolid() - .material(Materials.TRANSFORMED) - .getModel(AllBlockPartials.BOGEY_FRAME) - .createInstance(); - - wheels = new ModelData[2]; - - materialManager.defaultSolid() - .material(Materials.TRANSFORMED) - .getModel(AllBlockPartials.SMALL_BOGEY_WHEELS) - .createInstances(wheels); - } - - @Override - public void beginFrame(float wheelAngle, PoseStack ms) { - super.beginFrame(wheelAngle, ms); - - if (ms == null) { - frame.setEmptyTransform(); - for (int side : Iterate.positiveAndNegative) - wheels[(side + 1) / 2].setEmptyTransform(); - return; - } - - frame.setTransform(ms); - - for (int side : Iterate.positiveAndNegative) { - wheels[(side + 1) / 2].setTransform(ms) - .translate(0, 12 / 16f, side) - .rotateX(wheelAngle); - } - } - - @Override - public void updateLight(int blockLight, int skyLight) { - super.updateLight(blockLight, skyLight); - frame.setBlockLight(blockLight) - .setSkyLight(skyLight); - for (ModelData wheel : wheels) - wheel.setBlockLight(blockLight) - .setSkyLight(skyLight); - } - - @Override - public void remove() { - super.remove(); - frame.delete(); - for (ModelData wheel : wheels) - wheel.delete(); - } - } - - public static final class Drive extends BogeyInstance { - - private final ModelData[] secondShaft; - private final ModelData drive; - private final ModelData piston; - private final ModelData wheels; - private final ModelData pin; - - public Drive(CarriageBogey bogey, MaterialManager materialManager) { - super(bogey, materialManager); - Material mat = materialManager.defaultSolid() - .material(Materials.TRANSFORMED); - - secondShaft = new ModelData[2]; - - mat.getModel(AllBlocks.SHAFT.getDefaultState() - .setValue(ShaftBlock.AXIS, Direction.Axis.X)) - .createInstances(secondShaft); - - drive = mat.getModel(AllBlockPartials.BOGEY_DRIVE) - .createInstance(); - piston = mat.getModel(AllBlockPartials.BOGEY_PISTON) - .createInstance(); - wheels = mat.getModel(AllBlockPartials.LARGE_BOGEY_WHEELS) - .createInstance(); - pin = mat.getModel(AllBlockPartials.BOGEY_PIN) - .createInstance(); - } - - @Override - public void beginFrame(float wheelAngle, PoseStack ms) { - super.beginFrame(wheelAngle, ms); - - if (ms == null) { - for (int i : Iterate.zeroAndOne) - secondShaft[i].setEmptyTransform(); - drive.setEmptyTransform(); - piston.setEmptyTransform(); - wheels.setEmptyTransform(); - pin.setEmptyTransform(); - return; - } - - for (int i : Iterate.zeroAndOne) - secondShaft[i].setTransform(ms) - .translate(-.5f, .25f, .5f + i * -2) - .centre() - .rotateX(wheelAngle) - .unCentre(); - - drive.setTransform(ms); - piston.setTransform(ms) - .translate(0, 0, 1 / 4f * Math.sin(AngleHelper.rad(wheelAngle))); - - wheels.setTransform(ms) - .translate(0, 1, 0) - .rotateX(wheelAngle); - pin.setTransform(ms) - .translate(0, 1, 0) - .rotateX(wheelAngle) - .translate(0, 1 / 4f, 0) - .rotateX(-wheelAngle); - } - - @Override - public void updateLight(int blockLight, int skyLight) { - super.updateLight(blockLight, skyLight); - for (ModelData shaft : secondShaft) - shaft.setBlockLight(blockLight) - .setSkyLight(skyLight); - drive.setBlockLight(blockLight) - .setSkyLight(skyLight); - piston.setBlockLight(blockLight) - .setSkyLight(skyLight); - wheels.setBlockLight(blockLight) - .setSkyLight(skyLight); - pin.setBlockLight(blockLight) - .setSkyLight(skyLight); - } - - @Override - public void remove() { - super.remove(); - for (ModelData shaft : secondShaft) - shaft.delete(); - drive.delete(); - piston.delete(); - wheels.delete(); - pin.delete(); - } + return bogey.getAnchorPosition() != null ? bogey.getAnchorPosition() + : entity.getLightProbePosition(AnimationTickHolder.getPartialTicks()); } }