From 5f81b31fe4a4eac09d2f927035ce5fdfb78716f4 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 3 Jan 2022 13:55:43 -0800 Subject: [PATCH] More Instancers --- .../com/simibubi/create/AllTileEntities.java | 3 + .../components/actors/PIInstance.java | 78 +++++++++++++++++++ .../components/actors/PSIActorInstance.java | 30 +++++++ .../components/actors/PSIInstance.java | 48 ++++++++++++ .../PortableStorageInterfaceMovement.java | 19 ++++- .../PortableStorageInterfaceRenderer.java | 5 +- .../components/deployer/DeployerInstance.java | 40 ++++------ .../contraptions/fluids/PumpCogInstance.java | 59 +++++++++++++- .../contraptions/fluids/PumpRenderer.java | 2 + .../contraptions/fluids/PumpTileEntity.java | 2 +- 10 files changed, 251 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/actors/PIInstance.java create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIActorInstance.java create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIInstance.java diff --git a/src/main/java/com/simibubi/create/AllTileEntities.java b/src/main/java/com/simibubi/create/AllTileEntities.java index 6bdae2758..cfc775bb3 100644 --- a/src/main/java/com/simibubi/create/AllTileEntities.java +++ b/src/main/java/com/simibubi/create/AllTileEntities.java @@ -11,6 +11,7 @@ import com.simibubi.create.content.contraptions.components.actors.DrillRenderer; import com.simibubi.create.content.contraptions.components.actors.DrillTileEntity; import com.simibubi.create.content.contraptions.components.actors.HarvesterRenderer; import com.simibubi.create.content.contraptions.components.actors.HarvesterTileEntity; +import com.simibubi.create.content.contraptions.components.actors.PSIInstance; import com.simibubi.create.content.contraptions.components.actors.PortableFluidInterfaceTileEntity; import com.simibubi.create.content.contraptions.components.actors.PortableItemInterfaceTileEntity; import com.simibubi.create.content.contraptions.components.actors.PortableStorageInterfaceRenderer; @@ -484,12 +485,14 @@ public class AllTileEntities { public static final BlockEntityEntry PORTABLE_STORAGE_INTERFACE = Create.registrate() .tileEntity("portable_storage_interface", PortableItemInterfaceTileEntity::new) + .instance(() -> PSIInstance::new) .validBlocks(AllBlocks.PORTABLE_STORAGE_INTERFACE) .renderer(() -> PortableStorageInterfaceRenderer::new) .register(); public static final BlockEntityEntry PORTABLE_FLUID_INTERFACE = Create.registrate() .tileEntity("portable_fluid_interface", PortableFluidInterfaceTileEntity::new) + .instance(() -> PSIInstance::new) .validBlocks(AllBlocks.PORTABLE_FLUID_INTERFACE) .renderer(() -> PortableStorageInterfaceRenderer::new) .register(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PIInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PIInstance.java new file mode 100644 index 000000000..ec28142a6 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PIInstance.java @@ -0,0 +1,78 @@ +package com.simibubi.create.content.contraptions.components.actors; + +import com.jozufozu.flywheel.api.MaterialManager; +import com.jozufozu.flywheel.core.Materials; +import com.jozufozu.flywheel.core.materials.model.ModelData; +import com.simibubi.create.foundation.utility.AngleHelper; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.block.state.BlockState; + +public class PIInstance { + private final MaterialManager materialManager; + private final BlockState blockState; + private final BlockPos instancePos; + private final float angleX; + private final float angleY; + + private boolean lit; + ModelData middle; + ModelData top; + + public PIInstance(MaterialManager materialManager, BlockState blockState, BlockPos instancePos) { + this.materialManager = materialManager; + this.blockState = blockState; + this.instancePos = instancePos; + Direction facing = blockState.getValue(PortableStorageInterfaceBlock.FACING); + angleX = facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90; + angleY = AngleHelper.horizontalAngle(facing); + } + + public void init(boolean lit) { + this.lit = lit; + middle = materialManager.defaultSolid() + .material(Materials.TRANSFORMED) + .getModel(PortableStorageInterfaceRenderer.getMiddleForState(blockState, lit), blockState) + .createInstance(); + top = materialManager.defaultSolid() + .material(Materials.TRANSFORMED) + .getModel(PortableStorageInterfaceRenderer.getTopForState(blockState), blockState) + .createInstance(); + } + + public void beginFrame(float progress) { + middle.loadIdentity() + .translate(instancePos) + .centre() + .rotateY(angleY) + .rotateX(angleX) + .unCentre(); + + top.loadIdentity() + .translate(instancePos) + .centre() + .rotateY(angleY) + .rotateX(angleX) + .unCentre(); + + middle.translate(0, progress * 0.5f + 0.375f, 0); + top.translate(0, progress, 0); + + } + + public void tick(boolean lit) { + if (this.lit != lit) { + this.lit = lit; + materialManager.defaultSolid() + .material(Materials.TRANSFORMED) + .getModel(PortableStorageInterfaceRenderer.getMiddleForState(blockState, lit), blockState) + .stealInstance(middle); + } + } + + public void remove() { + middle.delete(); + top.delete(); + } +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIActorInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIActorInstance.java new file mode 100644 index 000000000..90d83d44e --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIActorInstance.java @@ -0,0 +1,30 @@ +package com.simibubi.create.content.contraptions.components.actors; + +import com.jozufozu.flywheel.api.MaterialManager; +import com.jozufozu.flywheel.core.virtual.VirtualRenderWorld; +import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; +import com.simibubi.create.foundation.utility.AnimationTickHolder; + +public class PSIActorInstance extends ActorInstance { + + private final PIInstance instance; + + public PSIActorInstance(MaterialManager materialManager, VirtualRenderWorld world, MovementContext context) { + super(materialManager, world, context); + + instance = new PIInstance(materialManager, context.state, context.localPos); + + instance.init(false); + instance.middle.setBlockLight(localBlockLight()); + instance.top.setBlockLight(localBlockLight()); + } + + @Override + public void beginFrame() { + PortableStorageInterfaceTileEntity psi = PortableStorageInterfaceRenderer.getTargetPSI(context); + instance.tick(psi != null && psi.isConnected()); + instance.beginFrame(psi == null ? 0f : psi.getExtensionDistance(AnimationTickHolder.getPartialTicks())); + } + +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIInstance.java new file mode 100644 index 000000000..5ddcac06e --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PSIInstance.java @@ -0,0 +1,48 @@ +package com.simibubi.create.content.contraptions.components.actors; + +import com.jozufozu.flywheel.api.MaterialManager; +import com.jozufozu.flywheel.api.instance.DynamicInstance; +import com.jozufozu.flywheel.api.instance.TickableInstance; +import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; +import com.simibubi.create.foundation.utility.AnimationTickHolder; + +public class PSIInstance extends BlockEntityInstance implements DynamicInstance, TickableInstance { + + private final PIInstance instance; + + public PSIInstance(MaterialManager materialManager, PortableStorageInterfaceTileEntity tile) { + super(materialManager, tile); + + instance = new PIInstance(materialManager, blockState, getInstancePosition()); + } + + @Override + public void init() { + instance.init(isLit()); + } + + @Override + public void tick() { + instance.tick(isLit()); + } + + @Override + public void beginFrame() { + instance.beginFrame(blockEntity.getExtensionDistance(AnimationTickHolder.getPartialTicks())); + } + + @Override + public void updateLight() { + relight(pos, instance.middle, instance.top); + } + + @Override + public void remove() { + instance.remove(); + } + + private boolean isLit() { + return blockEntity.isConnected(); + } + +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceMovement.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceMovement.java index dde358fd1..647f5bf37 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceMovement.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceMovement.java @@ -2,9 +2,14 @@ package com.simibubi.create.content.contraptions.components.actors; import java.util.Optional; +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.api.MaterialManager; +import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.core.virtual.VirtualRenderWorld; import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.foundation.utility.VecHelper; @@ -30,11 +35,23 @@ public class PortableStorageInterfaceMovement extends MovementBehaviour { .getNormal()).scale(1.85f); } + @Override + public boolean hasSpecialInstancedRendering() { + return true; + } + + @Nullable + @Override + public ActorInstance createInstance(MaterialManager materialManager, VirtualRenderWorld simulationWorld, MovementContext context) { + return new PSIActorInstance(materialManager, simulationWorld, context); + } + @Override @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffer) { - PortableStorageInterfaceRenderer.renderInContraption(context, renderWorld, matrices, buffer); + if (!Backend.isOn()) + PortableStorageInterfaceRenderer.renderInContraption(context, renderWorld, matrices, buffer); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceRenderer.java index 7c6439b7e..39ff32874 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/PortableStorageInterfaceRenderer.java @@ -86,17 +86,16 @@ public class PortableStorageInterfaceRenderer extends SafeTileEntityRenderer getModel() { BlockState referenceState = blockEntity.getBlockState(); Direction facing = referenceState.getValue(BlockStateProperties.FACING); return getRotatingMaterial().getModel(AllBlockPartials.MECHANICAL_PUMP_COG, referenceState, facing); } + + @Override + public void remove() { + super.remove(); + + for (ModelData arrow : arrows) { + arrow.delete(); + } + } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java index a8de02eb2..23e41b42a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpRenderer.java @@ -1,5 +1,6 @@ package com.simibubi.create.content.contraptions.fluids; +import com.jozufozu.flywheel.backend.Backend; import com.mojang.blaze3d.vertex.PoseStack; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.base.KineticTileEntity; @@ -26,6 +27,7 @@ public class PumpRenderer extends KineticTileEntityRenderer { protected void renderSafe(KineticTileEntity te, float partialTicks, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { super.renderSafe(te, partialTicks, ms, buffer, light, overlay); + if (Backend.canUseInstancing(te.getLevel())) return; if (!(te instanceof PumpTileEntity pump)) return; Vec3 rotationOffset = new Vec3(.5, 14 / 16f, .5); diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpTileEntity.java index 2bf7cbd9a..d984b5e0c 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/PumpTileEntity.java @@ -74,7 +74,7 @@ public class PumpTileEntity extends KineticTileEntity { if (!isVirtual()) return; } - + // if (pressureUpdate) // updatePressureChange();