From fa51b68709131ccf533b86c28e540ee77b5fc350 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 13 Jun 2022 12:31:41 -0700 Subject: [PATCH] Unbotch the batch - Contraptions now render properly with batching - Belts can be placed sideways again --- .../actors/DrillMovementBehaviour.java | 3 +- .../actors/HarvesterMovementBehaviour.java | 3 +- .../PortableStorageInterfaceMovement.java | 9 ++-- .../deployer/DeployerMovementBehaviour.java | 3 +- .../structureMovement/Contraption.java | 46 +++++++++---------- .../StabilizedBearingMovementBehaviour.java | 3 +- .../render/ContraptionRenderDispatcher.java | 10 +++- .../render/FlwContraption.java | 26 ++++++----- .../render/FlwContraptionManager.java | 5 +- .../relays/belt/item/BeltConnectorItem.java | 15 +++--- .../CarriageContraptionEntityRenderer.java | 2 +- .../render/TileEntityRenderHelper.java | 29 ++++++++---- 12 files changed, 91 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java index 3772c57ef..7becc131c 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java @@ -9,6 +9,7 @@ import com.simibubi.create.AllBlocks; 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.content.contraptions.components.structureMovement.render.FlwContraption; import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.client.renderer.MultiBufferSource; @@ -38,7 +39,7 @@ public class DrillMovementBehaviour extends BlockBreakingMovementBehaviour { @OnlyIn(value = Dist.CLIENT) public void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffer) { - if (!Backend.isOn()) + if (!FlwContraption.canInstance()) DrillRenderer.renderInContraption(context, renderWorld, matrices, buffer); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java index 9d37b474d..702c804c0 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java @@ -11,6 +11,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov 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.content.contraptions.components.structureMovement.render.FlwContraption; import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.utility.BlockHelper; import com.simibubi.create.foundation.utility.VecHelper; @@ -58,7 +59,7 @@ public class HarvesterMovementBehaviour implements MovementBehaviour { @Override public void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffers) { - if (!Backend.isOn()) + if (!FlwContraption.canInstance()) HarvesterRenderer.renderInContraption(context, renderWorld, matrices, buffers); } 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 bb1ac6c10..a6f155b2a 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 @@ -11,6 +11,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov 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.content.contraptions.components.structureMovement.render.FlwContraption; import com.simibubi.create.content.logistics.trains.entity.CarriageContraption; import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.animation.LerpedFloat; @@ -55,7 +56,7 @@ public class PortableStorageInterfaceMovement implements MovementBehaviour { @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffer) { - if (!Backend.isOn()) + if (!FlwContraption.canInstance()) PortableStorageInterfaceRenderer.renderInContraption(context, renderWorld, matrices, buffer); } @@ -70,9 +71,9 @@ public class PortableStorageInterfaceMovement implements MovementBehaviour { @Override public void tick(MovementContext context) { - if (context.world.isClientSide) + if (context.world.isClientSide) getAnimation(context).tickChaser(); - + boolean onCarriage = context.contraption instanceof CarriageContraption; if (onCarriage && context.motion.length() > 1 / 4f) return; @@ -152,7 +153,7 @@ public class PortableStorageInterfaceMovement implements MovementBehaviour { public void stopMoving(MovementContext context) { // reset(context); } - + @Override public void cancelStall(MovementContext context) { reset(context); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovementBehaviour.java index f492814b3..7e8685195 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovementBehaviour.java @@ -20,6 +20,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov import com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; +import com.simibubi.create.content.contraptions.components.structureMovement.render.FlwContraption; import com.simibubi.create.content.logistics.item.filter.FilterItem; import com.simibubi.create.content.logistics.trains.entity.CarriageContraptionEntity; import com.simibubi.create.content.schematics.ItemRequirement; @@ -290,7 +291,7 @@ public class DeployerMovementBehaviour implements MovementBehaviour { @Override public void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffers) { - if (!Backend.isOn()) + if (!FlwContraption.canInstance()) DeployerRenderer.renderInContraption(context, renderWorld, matrices, buffers); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java index 1d4c16b01..ca5b45a2c 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java @@ -849,35 +849,35 @@ public abstract class Contraption { this.blocks.put(info.pos, info); - if (world.isClientSide) { - Block block = info.state.getBlock(); - CompoundTag tag = info.nbt; - MovementBehaviour movementBehaviour = AllMovementBehaviours.of(block); - if (tag == null) - return; + if (!world.isClientSide) + return; - tag.putInt("x", info.pos.getX()); - tag.putInt("y", info.pos.getY()); - tag.putInt("z", info.pos.getZ()); + Block block = info.state.getBlock(); + CompoundTag tag = info.nbt; + MovementBehaviour movementBehaviour = AllMovementBehaviours.of(block); + if (tag == null) + return; - BlockEntity te = BlockEntity.loadStatic(info.pos, info.state, tag); - if (te == null) - return; - te.setLevel(world); - if (te instanceof KineticTileEntity) - ((KineticTileEntity) te).setSpeed(0); - te.getBlockState(); + tag.putInt("x", info.pos.getX()); + tag.putInt("y", info.pos.getY()); + tag.putInt("z", info.pos.getZ()); - if (movementBehaviour == null || !movementBehaviour.hasSpecialInstancedRendering()) - maybeInstancedTileEntities.add(te); + BlockEntity te = BlockEntity.loadStatic(info.pos, info.state, tag); + if (te == null) + return; + te.setLevel(world); + if (te instanceof KineticTileEntity) + ((KineticTileEntity) te).setSpeed(0); + te.getBlockState(); - if (movementBehaviour != null && !movementBehaviour.renderAsNormalTileEntity()) - return; + if (movementBehaviour == null || !movementBehaviour.hasSpecialInstancedRendering()) + maybeInstancedTileEntities.add(te); - presentTileEntities.put(info.pos, te); - specialRenderedTileEntities.add(te); - } + if (movementBehaviour != null && !movementBehaviour.renderAsNormalTileEntity()) + return; + presentTileEntities.put(info.pos, te); + specialRenderedTileEntities.add(te); }); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java index 47aed8c84..dd8792dd3 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java @@ -16,6 +16,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Ori import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; +import com.simibubi.create.content.contraptions.components.structureMovement.render.FlwContraption; import com.simibubi.create.foundation.render.CachedBufferer; import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.utility.AnimationTickHolder; @@ -34,7 +35,7 @@ public class StabilizedBearingMovementBehaviour implements MovementBehaviour { @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffer) { - if (Backend.isOn()) + if (FlwContraption.canInstance()) return; Direction facing = context.state.getValue(BlockStateProperties.FACING); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java index 9b7944a71..5fb4abd97 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java @@ -6,6 +6,7 @@ import org.apache.commons.lang3.tuple.Pair; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.gl.error.GlError; +import com.jozufozu.flywheel.config.BackendType; import com.jozufozu.flywheel.core.model.ModelUtil; import com.jozufozu.flywheel.core.virtual.VirtualRenderWorld; import com.jozufozu.flywheel.event.BeginFrameEvent; @@ -48,7 +49,7 @@ public class ContraptionRenderDispatcher { /** * Reset a contraption's renderer. - * + * * @param contraption The contraption to invalidate. * @return true if there was a renderer associated with the given contraption. */ @@ -119,7 +120,12 @@ public class ContraptionRenderDispatcher { BlockPos origin = c.anchor; int height = contraptionWorld.getHeight(); int minBuildHeight = contraptionWorld.getMinBuildHeight(); - VirtualRenderWorld renderWorld = new VirtualRenderWorld(world, origin, height, minBuildHeight); + VirtualRenderWorld renderWorld = new VirtualRenderWorld(world, origin, height, minBuildHeight) { + @Override + public boolean supportsFlywheel() { + return Backend.getBackendType() == BackendType.INSTANCING; + } + }; renderWorld.setBlockEntities(c.presentTileEntities.values()); for (StructureTemplate.StructureBlockInfo info : c.getBlocks() diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraption.java index f641522e9..310d7a229 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraption.java @@ -13,6 +13,7 @@ import com.jozufozu.flywheel.backend.instancing.SerialTaskEngine; import com.jozufozu.flywheel.backend.instancing.batching.BatchingEngine; import com.jozufozu.flywheel.backend.instancing.instancing.InstancingEngine; import com.jozufozu.flywheel.backend.model.ArrayModelRenderer; +import com.jozufozu.flywheel.config.BackendType; import com.jozufozu.flywheel.core.model.Model; import com.jozufozu.flywheel.core.model.WorldModel; import com.jozufozu.flywheel.core.virtual.VirtualRenderWorld; @@ -53,13 +54,17 @@ public class FlwContraption extends ContraptionRenderInfo { var restoreState = GlStateTracker.getRestoreState(); buildLayers(); - if (Backend.isOn()) { + if (canInstance()) { buildInstancedTiles(); buildActors(); } restoreState.restore(); } + public static boolean canInstance() { + return Backend.getBackendType() == BackendType.INSTANCING; + } + public ContraptionLighter getLighter() { return lighter; } @@ -139,7 +144,7 @@ public class FlwContraption extends ContraptionRenderInfo { List blockLayers = RenderType.chunkBufferLayers(); Collection renderedBlocks = contraption.getRenderedBlocks(); - + for (RenderType layer : blockLayers) { Model layerModel = new WorldModel(renderWorld, layer, renderedBlocks, layer + "_" + contraption.entity.getId()); renderLayers.put(layer, new ArrayModelRenderer(layerModel)); @@ -147,16 +152,15 @@ public class FlwContraption extends ContraptionRenderInfo { } private void buildInstancedTiles() { - Collection tileEntities = contraption.maybeInstancedTileEntities; - if (!tileEntities.isEmpty()) { - for (BlockEntity te : tileEntities) { - if (InstancedRenderRegistry.canInstance(te.getType())) { - Level world = te.getLevel(); - te.setLevel(renderWorld); - instanceWorld.tileInstanceManager.add(te); - te.setLevel(world); - } + for (BlockEntity te : contraption.maybeInstancedTileEntities) { + if (!InstancedRenderRegistry.canInstance(te.getType())) { + continue; } + + Level world = te.getLevel(); + te.setLevel(renderWorld); + instanceWorld.tileInstanceManager.add(te); + te.setLevel(world); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraptionManager.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraptionManager.java index 4a50d28bb..0c64664c4 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraptionManager.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/FlwContraptionManager.java @@ -43,11 +43,12 @@ public class FlwContraptionManager extends ContraptionRenderingWorld iterator = customRenderTEs.iterator(); while (iterator.hasNext()) { BlockEntity tileEntity = iterator.next(); - if (Backend.canUseInstancing(renderWorld) && InstancedRenderRegistry.shouldSkipRender(tileEntity)) + if (Backend.getBackendType() == BackendType.INSTANCING && Backend.isFlywheelWorld(renderWorld) && InstancedRenderRegistry.shouldSkipRender(tileEntity)) continue; BlockEntityRenderer renderer = Minecraft.getInstance().getBlockEntityRenderDispatcher().getRenderer(tileEntity); @@ -63,16 +64,16 @@ public class TileEntityRenderHelper { .translate(pos); try { - BlockPos lightPos; - if (lightTransform != null) { - Vector4f lightVec = new Vector4f(pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, 1); - lightVec.transform(lightTransform); - lightPos = new BlockPos(lightVec.x(), lightVec.y(), lightVec.z()); + int worldLight = getCombinedLight(world, getLightPos(lightTransform, pos), renderWorld, pos); + + if (renderWorld != null) { + // Swap the real world for the render world so that the renderer gets contraption-local information + tileEntity.setLevel(renderWorld); + renderer.render(tileEntity, pt, ms, buffer, worldLight, OverlayTexture.NO_OVERLAY); + tileEntity.setLevel(world); } else { - lightPos = pos; + renderer.render(tileEntity, pt, ms, buffer, worldLight, OverlayTexture.NO_OVERLAY); } - int worldLight = getCombinedLight(world, lightPos, renderWorld, pos); - renderer.render(tileEntity, pt, ms, buffer, worldLight, OverlayTexture.NO_OVERLAY); } catch (Exception e) { iterator.remove(); @@ -90,6 +91,16 @@ public class TileEntityRenderHelper { } } + private static BlockPos getLightPos(@Nullable Matrix4f lightTransform, BlockPos contraptionPos) { + if (lightTransform != null) { + Vector4f lightVec = new Vector4f(contraptionPos.getX() + .5f, contraptionPos.getY() + .5f, contraptionPos.getZ() + .5f, 1); + lightVec.transform(lightTransform); + return new BlockPos(lightVec.x(), lightVec.y(), lightVec.z()); + } else { + return contraptionPos; + } + } + public static int getCombinedLight(Level world, BlockPos worldPos, @Nullable VirtualRenderWorld renderWorld, BlockPos renderWorldPos) { int worldLight = LevelRenderer.getLightColor(world, worldPos);