From 61b101f0c6c4ae203464d52d82af6ecc93be8f81 Mon Sep 17 00:00:00 2001 From: PepperBell <44146161+PepperCode1@users.noreply.github.com> Date: Sun, 16 May 2021 18:42:56 -0700 Subject: [PATCH] Refactor contraption matrices - Create ContraptionMatrices class for better management of matrices during the contraption rendering process - Fix diffuse lighting when rendering contraptions without Flywheel - Clean up TileEntityRenderHelper - Add disableDiffuseTransform to SuperByteBuffer and fix light calculation logic --- .../actors/DrillMovementBehaviour.java | 6 +- .../components/actors/DrillRenderer.java | 26 ++-- .../actors/HarvesterMovementBehaviour.java | 6 +- .../components/actors/HarvesterRenderer.java | 8 +- .../PortableStorageInterfaceMovement.java | 6 +- .../PortableStorageInterfaceRenderer.java | 44 +++--- .../actors/SawMovementBehaviour.java | 6 +- .../deployer/DeployerMovementBehaviour.java | 6 +- .../components/deployer/DeployerRenderer.java | 30 ++-- .../components/saw/SawRenderer.java | 36 ++--- .../AbstractContraptionEntity.java | 2 +- .../ContraptionEntityRenderer.java | 35 ++--- .../structureMovement/MovementBehaviour.java | 4 +- .../StabilizedBearingMovementBehaviour.java | 9 +- .../render/ContraptionMatrices.java | 65 +++++++++ .../render/ContraptionRenderDispatcher.java | 66 ++++----- .../render/RenderedContraption.java | 2 +- .../schematics/client/SchematicRenderer.java | 3 +- .../ponder/elements/WorldSectionElement.java | 2 +- .../foundation/render/SuperByteBuffer.java | 131 ++++++++++-------- .../render/TileEntityRenderHelper.java | 55 +++++--- 21 files changed, 314 insertions(+), 234 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionMatrices.java 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 e6bc4a8e4..34ab5045f 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 @@ -3,10 +3,10 @@ package com.simibubi.create.content.contraptions.components.actors; import javax.annotation.Nullable; import com.jozufozu.flywheel.backend.Backend; -import com.mojang.blaze3d.matrix.MatrixStack; 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.ContraptionKineticRenderer; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; @@ -36,9 +36,9 @@ public class DrillMovementBehaviour extends BlockBreakingMovementBehaviour { @Override @OnlyIn(value = Dist.CLIENT) public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { if (!Backend.canUseInstancing()) - DrillRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffer); + DrillRenderer.renderInContraption(context, renderWorld, matrices, buffer); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java index 127a29185..83b472863 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java @@ -5,6 +5,7 @@ import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.SuperByteBuffer; @@ -32,8 +33,7 @@ public class DrillRenderer extends KineticTileEntityRenderer { } public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal}; + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { BlockState state = context.state; SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.DRILL_HEAD, state); Direction facing = state.get(DrillBlock.FACING); @@ -44,18 +44,22 @@ public class DrillRenderer extends KineticTileEntityRenderer { float time = AnimationTickHolder.getRenderTime() / 20; float angle = (float) (((time * speed) % 360)); - for (MatrixStack m : matrixStacks) - MatrixStacker.of(m) - .centre() - .rotateY(AngleHelper.horizontalAngle(facing)) - .rotateX(AngleHelper.verticalAngle(facing)) - .rotateZ(angle) - .unCentre(); + MatrixStack m = matrices.contraptionStack; + m.push(); + MatrixStacker.of(m) + .centre() + .rotateY(AngleHelper.horizontalAngle(facing)) + .rotateX(AngleHelper.verticalAngle(facing)) + .rotateZ(angle) + .unCentre(); superBuffer - .light(msLocal.peek().getModel(), + .transform(matrices.contraptionStack) + .light(matrices.entityMatrix, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) - .renderInto(ms, buffer.getBuffer(RenderType.getSolid())); + .renderInto(matrices.entityStack, buffer.getBuffer(RenderType.getSolid())); + + m.pop(); } } 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 440ce6051..a0a993c24 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 @@ -7,11 +7,11 @@ import javax.annotation.Nullable; import org.apache.commons.lang3.mutable.MutableBoolean; import com.jozufozu.flywheel.backend.Backend; -import com.mojang.blaze3d.matrix.MatrixStack; 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.ContraptionKineticRenderer; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.foundation.utility.BlockHelper; import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; @@ -54,9 +54,9 @@ public class HarvesterMovementBehaviour extends MovementBehaviour { @Override public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) { + ContraptionMatrices matrices, IRenderTypeBuffer buffers) { if (!Backend.canUseInstancing()) - HarvesterRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffers); + HarvesterRenderer.renderInContraption(context, renderWorld, matrices, buffers); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java index 316c48364..3ab34b532 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java @@ -5,6 +5,7 @@ import static net.minecraft.state.properties.BlockStateProperties.HORIZONTAL_FAC import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.SuperByteBuffer; @@ -41,7 +42,7 @@ public class HarvesterRenderer extends SafeTileEntityRenderer sbb.light(msLocal.peek() - .getModel(), ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) - .renderInto(ms, vb), ms, msLocal); + render(blockState, progress, lit, sbb -> sbb.transform(matrices.contraptionStack) + .disableDiffuseTransform() + .light(matrices.entityMatrix, + ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) + .renderInto(matrices.entityStack, vb), matrices.contraptionStack); } private static void render(BlockState blockState, float progress, boolean lit, - Consumer drawCallback, MatrixStack... matrixStacks) { - for (MatrixStack ms : matrixStacks) - ms.push(); + Consumer drawCallback, MatrixStack ms) { + ms.push(); SuperByteBuffer middle = PartialBufferer.get(getMiddleForState(blockState, lit), blockState); SuperByteBuffer top = PartialBufferer.get(getTopForState(blockState), blockState); Direction facing = blockState.get(PortableStorageInterfaceBlock.FACING); - for (MatrixStack ms : matrixStacks) - MatrixStacker.of(ms) - .centre() - .rotateY(AngleHelper.horizontalAngle(facing)) - .rotateX(facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90) - .unCentre(); + MatrixStacker.of(ms) + .centre() + .rotateY(AngleHelper.horizontalAngle(facing)) + .rotateX(facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90) + .unCentre(); - for (MatrixStack ms : matrixStacks) { - ms.translate(0, progress / 2f, 0); - ms.push(); - ms.translate(0, 6 / 16f, 0); - } + ms.translate(0, progress / 2f, 0); + ms.push(); + ms.translate(0, 6 / 16f, 0); drawCallback.accept(middle); - for (MatrixStack ms : matrixStacks) { - ms.pop(); - ms.translate(0, progress / 2f, 0); - } + ms.pop(); + ms.translate(0, progress / 2f, 0); drawCallback.accept(top); - for (MatrixStack ms : matrixStacks) - ms.pop(); + ms.pop(); } protected static PortableStorageInterfaceTileEntity getTargetPSI(MovementContext context) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/SawMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/SawMovementBehaviour.java index 494b8c183..375445b5f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/SawMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/SawMovementBehaviour.java @@ -1,10 +1,10 @@ package com.simibubi.create.content.contraptions.components.actors; -import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.content.contraptions.components.saw.SawBlock; import com.simibubi.create.content.contraptions.components.saw.SawRenderer; import com.simibubi.create.content.contraptions.components.saw.SawTileEntity; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.foundation.utility.TreeCutter; import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; @@ -77,8 +77,8 @@ public class SawMovementBehaviour extends BlockBreakingMovementBehaviour { @Override @OnlyIn(value = Dist.CLIENT) public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - SawRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffer); + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { + SawRenderer.renderInContraption(context, renderWorld, matrices, buffer); } @Override 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 d8220145b..a8d81c209 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 @@ -8,7 +8,6 @@ import javax.annotation.Nullable; import org.apache.commons.lang3.tuple.Pair; import com.jozufozu.flywheel.backend.Backend; -import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllItems; import com.simibubi.create.AllTags.AllBlockTags; @@ -18,6 +17,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.ContraptionKineticRenderer; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.logistics.item.filter.FilterItem; import com.simibubi.create.content.schematics.ItemRequirement; import com.simibubi.create.content.schematics.SchematicWorld; @@ -256,9 +256,9 @@ public class DeployerMovementBehaviour extends MovementBehaviour { @Override public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) { + ContraptionMatrices matrices, IRenderTypeBuffer buffers) { if (!Backend.canUseInstancing()) - DeployerRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffers); + DeployerRenderer.renderInContraption(context, renderWorld, matrices, buffers); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerRenderer.java index 17fc3d8fa..ccac8329a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerRenderer.java @@ -12,6 +12,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.SuperByteBuffer; @@ -36,7 +37,6 @@ import net.minecraft.util.Direction; import net.minecraft.util.Direction.Axis; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.vector.Matrix4f; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3f; import net.minecraft.world.World; @@ -156,8 +156,7 @@ public class DeployerRenderer extends SafeTileEntityRenderer } public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal}; + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { IVertexBuilder builder = buffer.getBuffer(RenderType.getSolid()); BlockState blockState = context.state; BlockPos pos = BlockPos.ZERO; @@ -167,8 +166,6 @@ public class DeployerRenderer extends SafeTileEntityRenderer SuperByteBuffer pole = PartialBufferer.get(AllBlockPartials.DEPLOYER_POLE, blockState); SuperByteBuffer hand = PartialBufferer.get(handPose, blockState); - pole = transform(world, pole, blockState, pos, true); - hand = transform(world, hand, blockState, pos, false); double factor; if (context.contraption.stalled || context.position == null || context.data.contains("StationaryTimer")) { @@ -184,14 +181,21 @@ public class DeployerRenderer extends SafeTileEntityRenderer Vector3d offset = Vector3d.of(blockState.get(FACING) .getDirectionVec()).scale(factor); - Matrix4f lighting = msLocal.peek() - .getModel(); - for (MatrixStack m : matrixStacks) - m.translate(offset.x, offset.y, offset.z); - pole.light(lighting, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) - .renderInto(ms, builder); - hand.light(lighting, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) - .renderInto(ms, builder); + MatrixStack m = matrices.contraptionStack; + m.push(); + m.translate(offset.x, offset.y, offset.z); + + pole.transform(m); + hand.transform(m); + pole = transform(world, pole, blockState, pos, true); + hand = transform(world, hand, blockState, pos, false); + + pole.light(matrices.entityMatrix, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) + .renderInto(matrices.entityStack, builder); + hand.light(matrices.entityMatrix, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) + .renderInto(matrices.entityStack, builder); + + m.pop(); } static PartialModel getHandPose(DeployerTileEntity.Mode mode) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java index be13853e1..bba81950a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java @@ -10,6 +10,7 @@ import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.SuperByteBuffer; @@ -153,10 +154,8 @@ public class SawRenderer extends SafeTileEntityRenderer { } public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - MatrixStack[] matrixStacks = new MatrixStack[] { ms, msLocal }; + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { BlockState state = context.state; - SuperByteBuffer superBuffer; Direction facing = state.get(SawBlock.FACING); Vector3d facingVec = Vector3d.of(context.state.get(SawBlock.FACING) @@ -172,6 +171,7 @@ public class SawRenderer extends SafeTileEntityRenderer { boolean shouldAnimate = (context.contraption.stalled && horizontal) || (!context.contraption.stalled && !backwards && moving); + SuperByteBuffer superBuffer; if (SawBlock.isHorizontal(state)) { if (shouldAnimate) superBuffer = PartialBufferer.get(AllBlockPartials.SAW_BLADE_HORIZONTAL_ACTIVE, state); @@ -184,22 +184,26 @@ public class SawRenderer extends SafeTileEntityRenderer { superBuffer = PartialBufferer.get(AllBlockPartials.SAW_BLADE_VERTICAL_INACTIVE, state); } - for (MatrixStack m : matrixStacks) { + MatrixStack m = matrices.contraptionStack; + m.push(); + MatrixStacker.of(m) + .centre() + .rotateY(AngleHelper.horizontalAngle(facing)) + .rotateX(AngleHelper.verticalAngle(facing)); + if (!SawBlock.isHorizontal(state)) MatrixStacker.of(m) - .centre() - .rotateY(AngleHelper.horizontalAngle(facing)) - .rotateX(AngleHelper.verticalAngle(facing)); - if (!SawBlock.isHorizontal(state)) - MatrixStacker.of(m) - .rotateZ(state.get(SawBlock.AXIS_ALONG_FIRST_COORDINATE) ? 0 : 90); - MatrixStacker.of(m) - .unCentre(); - } + .rotateZ(state.get(SawBlock.AXIS_ALONG_FIRST_COORDINATE) ? 0 : 90); + MatrixStacker.of(m) + .unCentre(); superBuffer - .light(msLocal.peek().getModel(), - ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) - .renderInto(ms, buffer.getBuffer(RenderType.getCutoutMipped())); + .transform(m) + .disableDiffuseTransform() + .light(matrices.entityMatrix, + ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) + .renderInto(matrices.entityStack, buffer.getBuffer(RenderType.getCutoutMipped())); + + m.pop(); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java index 5c651529a..3f442ccfd 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java @@ -641,7 +641,7 @@ public abstract class AbstractContraptionEntity extends Entity implements IEntit } @OnlyIn(Dist.CLIENT) - public abstract void doLocalTransforms(float partialTicks, MatrixStack[] matrixStacks); + public abstract void doLocalTransforms(float partialTicks, MatrixStack[] matrixStack); public static class ContraptionRotationState { public static final ContraptionRotationState NONE = new ContraptionRotationState(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntityRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntityRenderer.java index 271319b91..8d8ed3b62 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/ContraptionEntityRenderer.java @@ -1,20 +1,19 @@ package com.simibubi.create.content.contraptions.components.structureMovement; import com.mojang.blaze3d.matrix.MatrixStack; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; -import com.simibubi.create.foundation.utility.AnimationTickHolder; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.culling.ClippingHelper; import net.minecraft.client.renderer.entity.EntityRenderer; import net.minecraft.client.renderer.entity.EntityRendererManager; import net.minecraft.util.ResourceLocation; -import net.minecraft.util.math.MathHelper; public class ContraptionEntityRenderer extends EntityRenderer { - public ContraptionEntityRenderer(EntityRendererManager p_i46179_1_) { - super(p_i46179_1_); + public ContraptionEntityRenderer(EntityRendererManager manager) { + super(manager); } @Override @@ -23,42 +22,26 @@ public class ContraptionEntityRenderer exte } @Override - public boolean shouldRender(C entity, ClippingHelper clippingHelper, double p_225626_3_, double p_225626_5_, - double p_225626_7_) { + public boolean shouldRender(C entity, ClippingHelper clippingHelper, double cameraX, double cameraY, + double cameraZ) { if (entity.getContraption() == null) return false; if (!entity.isAlive()) return false; - return super.shouldRender(entity, clippingHelper, p_225626_3_, p_225626_5_, p_225626_7_); + return super.shouldRender(entity, clippingHelper, cameraX, cameraY, cameraZ); } - + @Override public void render(C entity, float yaw, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffers, int overlay) { super.render(entity, yaw, partialTicks, ms, buffers, overlay); - // Keep a copy of the transforms in order to determine correct lighting - MatrixStack msLocal = translateTo(entity, AnimationTickHolder.getPartialTicks()); - MatrixStack[] matrixStacks = new MatrixStack[] { ms, msLocal }; - - ms.push(); - entity.doLocalTransforms(partialTicks, matrixStacks); + ContraptionMatrices matrices = new ContraptionMatrices(ms, entity); Contraption contraption = entity.getContraption(); if (contraption != null) { - ContraptionRenderDispatcher.render(entity, ms, buffers, msLocal, contraption); + ContraptionRenderDispatcher.render(entity, contraption, matrices, buffers); } - ms.pop(); - - } - - protected MatrixStack translateTo(AbstractContraptionEntity entity, float pt) { - MatrixStack matrixStack = new MatrixStack(); - double x = MathHelper.lerp(pt, entity.lastTickPosX, entity.getX()); - double y = MathHelper.lerp(pt, entity.lastTickPosY, entity.getY()); - double z = MathHelper.lerp(pt, entity.lastTickPosZ, entity.getZ()); - matrixStack.translate(x, y, z); - return matrixStack; } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java index a796eea93..5c33522eb 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java @@ -2,9 +2,9 @@ package com.simibubi.create.content.contraptions.components.structureMovement; import javax.annotation.Nullable; -import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionKineticRenderer; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import net.minecraft.client.renderer.IRenderTypeBuffer; @@ -62,7 +62,7 @@ public abstract class MovementBehaviour { @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {} + ContraptionMatrices matrices, IRenderTypeBuffer buffer) {} @OnlyIn(Dist.CLIENT) @Nullable 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 47a8ee094..8afaa8bcc 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 @@ -4,7 +4,6 @@ import javax.annotation.Nullable; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.core.PartialModel; -import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; @@ -13,6 +12,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.ContraptionKineticRenderer; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.SuperByteBuffer; @@ -33,7 +33,7 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour { @Override @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { if (Backend.canUseInstancing()) return; Direction facing = context.state.get(BlockStateProperties.FACING); @@ -53,13 +53,14 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour { orientation = rotation; + superBuffer.transform(matrices.contraptionStack); superBuffer.rotateCentered(orientation); // render superBuffer - .light(msLocal.peek().getModel(), + .light(matrices.entityMatrix, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)) - .renderInto(ms, buffer.getBuffer(RenderType.getSolid())); + .renderInto(matrices.entityStack, buffer.getBuffer(RenderType.getSolid())); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionMatrices.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionMatrices.java new file mode 100644 index 000000000..d6e82d17d --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionMatrices.java @@ -0,0 +1,65 @@ +package com.simibubi.create.content.contraptions.components.structureMovement.render; + +import com.mojang.blaze3d.matrix.MatrixStack; +import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; +import com.simibubi.create.foundation.utility.AnimationTickHolder; + +import net.minecraft.entity.Entity; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.vector.Matrix3f; +import net.minecraft.util.math.vector.Matrix4f; + +public class ContraptionMatrices { + public final MatrixStack entityStack; + public final MatrixStack contraptionStack; + public final Matrix4f entityMatrix; + + public ContraptionMatrices(MatrixStack entityStack, AbstractContraptionEntity entity) { + this.entityStack = entityStack; + this.contraptionStack = new MatrixStack(); + float partialTicks = AnimationTickHolder.getPartialTicks(); + entity.doLocalTransforms(partialTicks, new MatrixStack[] { this.contraptionStack }); + entityMatrix = translateTo(entity, partialTicks); + } + + public MatrixStack getFinalStack() { + MatrixStack finalStack = new MatrixStack(); + transform(finalStack, entityStack); + transform(finalStack, contraptionStack); + return finalStack; + } + + public Matrix4f getFinalModel() { + Matrix4f finalModel = entityStack.peek().getModel().copy(); + finalModel.multiply(contraptionStack.peek().getModel()); + return finalModel; + } + + public Matrix3f getFinalNormal() { + Matrix3f finalNormal = entityStack.peek().getNormal().copy(); + finalNormal.multiply(contraptionStack.peek().getNormal()); + return finalNormal; + } + + public Matrix4f getFinalLight() { + Matrix4f lightTransform = entityMatrix.copy(); + lightTransform.multiply(contraptionStack.peek().getModel()); + return lightTransform; + } + + public static Matrix4f translateTo(Entity entity, float partialTicks) { + double x = MathHelper.lerp(partialTicks, entity.lastTickPosX, entity.getX()); + double y = MathHelper.lerp(partialTicks, entity.lastTickPosY, entity.getY()); + double z = MathHelper.lerp(partialTicks, entity.lastTickPosZ, entity.getZ()); + return Matrix4f.translate((float) x, (float) y, (float) z); + } + + public static void transform(MatrixStack ms, MatrixStack transform) { + ms.peek().getModel() + .multiply(transform.peek() + .getModel()); + ms.peek().getNormal() + .multiply(transform.peek() + .getNormal()); + } +} 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 9c5d2665a..f098d58b8 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 @@ -122,20 +122,20 @@ public class ContraptionRenderDispatcher { glActiveTexture(GL_TEXTURE0); } - public static void render(AbstractContraptionEntity entity, MatrixStack ms, IRenderTypeBuffer buffers, - MatrixStack msLocal, Contraption contraption) { + public static void render(AbstractContraptionEntity entity, Contraption contraption, + ContraptionMatrices matrices, IRenderTypeBuffer buffers) { World world = entity.world; if (Backend.canUseVBOs() && Backend.isFlywheelWorld(world)) { RenderedContraption renderer = getRenderer(world, contraption); PlacementSimulationWorld renderWorld = renderer.renderWorld; - ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, ms, msLocal, buffers); + ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, matrices, buffers); } else { ContraptionWorldHolder holder = getWorldHolder(world, contraption); PlacementSimulationWorld renderWorld = holder.renderWorld; - ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, ms, msLocal, buffers); - ContraptionRenderDispatcher.renderStructure(world, renderWorld, contraption, ms, msLocal, buffers); + ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, matrices, buffers); + ContraptionRenderDispatcher.renderStructure(world, renderWorld, contraption, matrices, buffers); } } @@ -182,21 +182,21 @@ public class ContraptionRenderDispatcher { } public static void renderDynamic(World world, PlacementSimulationWorld renderWorld, Contraption c, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - renderTileEntities(world, renderWorld, c, ms, msLocal, buffer); + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { + renderTileEntities(world, renderWorld, c, matrices, buffer); if (buffer instanceof IRenderTypeBuffer.Impl) ((IRenderTypeBuffer.Impl) buffer).draw(); - renderActors(world, renderWorld, c, ms, msLocal, buffer); + renderActors(world, renderWorld, c, matrices, buffer); } public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, Contraption c, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - TileEntityRenderHelper.renderTileEntities(world, renderWorld, c.specialRenderedTileEntities, ms, msLocal, buffer); + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { + TileEntityRenderHelper.renderTileEntities(world, renderWorld, c.specialRenderedTileEntities, + matrices.getFinalStack(), matrices.getFinalLight(), buffer); } protected static void renderActors(World world, PlacementSimulationWorld renderWorld, Contraption c, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - MatrixStack[] matrixStacks = new MatrixStack[] {ms, msLocal}; + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { for (Pair actor : c.getActors()) { MovementContext context = actor.getRight(); if (context == null) @@ -204,23 +204,22 @@ public class ContraptionRenderDispatcher { if (context.world == null) context.world = world; Template.BlockInfo blockInfo = actor.getLeft(); - for (MatrixStack m : matrixStacks) { - m.push(); - MatrixStacker.of(m) - .translate(blockInfo.pos); - } + + MatrixStack m = matrices.contraptionStack; + m.push(); + MatrixStacker.of(m) + .translate(blockInfo.pos); MovementBehaviour movementBehaviour = AllMovementBehaviours.of(blockInfo.state); if (movementBehaviour != null) - movementBehaviour.renderInContraption(context, renderWorld, ms, msLocal, buffer); + movementBehaviour.renderInContraption(context, renderWorld, matrices, buffer); - for (MatrixStack m : matrixStacks) - m.pop(); + m.pop(); } } public static void renderStructure(World world, PlacementSimulationWorld renderWorld, Contraption c, - MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { + ContraptionMatrices matrices, IRenderTypeBuffer buffer) { SuperByteBufferCache bufferCache = CreateClient.bufferCache; List blockLayers = RenderType.getBlockLayers(); @@ -231,11 +230,11 @@ public class ContraptionRenderDispatcher { SuperByteBuffer contraptionBuffer = bufferCache.get(CONTRAPTION, key, () -> buildStructureBuffer(renderWorld, c, layer)); if (contraptionBuffer.isEmpty()) continue; - Matrix4f model = msLocal.peek() - .getModel(); - contraptionBuffer.light(model) + contraptionBuffer + .transform(matrices.contraptionStack) + .light(matrices.entityMatrix) .hybridLight() - .renderInto(ms, buffer.getBuffer(layer)); + .renderInto(matrices.entityStack, buffer.getBuffer(layer)); } } @@ -278,29 +277,18 @@ public class ContraptionRenderDispatcher { public static int getLight(World world, float lx, float ly, float lz) { BlockPos.Mutable pos = new BlockPos.Mutable(); - float sky = 0, block = 0; + float block = 0, sky = 0; float offset = 1 / 8f; for (float zOffset = offset; zOffset >= -offset; zOffset -= 2 * offset) for (float yOffset = offset; yOffset >= -offset; yOffset -= 2 * offset) for (float xOffset = offset; xOffset >= -offset; xOffset -= 2 * offset) { pos.setPos(lx + xOffset, ly + yOffset, lz + zOffset); - sky += world.getLightLevel(LightType.SKY, pos) / 8f; block += world.getLightLevel(LightType.BLOCK, pos) / 8f; + sky += world.getLightLevel(LightType.SKY, pos) / 8f; } - return ((int) sky) << 20 | ((int) block) << 4; - } - - public static int getContraptionLightAt(World world, PlacementSimulationWorld renderWorld, BlockPos pos, BlockPos lightPos) { - int worldLight = WorldRenderer.getLightmapCoordinates(world, lightPos); - - if (renderWorld != null) { - int renderWorldLight = WorldRenderer.getLightmapCoordinates(renderWorld, pos); - return SuperByteBuffer.maxLight(worldLight, renderWorldLight); - } - - return worldLight; + return LightTexture.pack((int) block, (int) sky); } public static int getContraptionWorldLight(MovementContext context, PlacementSimulationWorld renderWorld) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java index 11bf71ee2..b618896f3 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java @@ -72,7 +72,7 @@ public class RenderedContraption extends ContraptionWorldHolder { double z = MathHelper.lerp(pt, entity.lastTickPosZ, entity.getZ()) - camZ; stack.translate(x, y, z); - entity.doLocalTransforms(pt, new MatrixStack[]{ stack }); + entity.doLocalTransforms(pt, new MatrixStack[] { stack }); model = stack.peek().getModel(); diff --git a/src/main/java/com/simibubi/create/content/schematics/client/SchematicRenderer.java b/src/main/java/com/simibubi/create/content/schematics/client/SchematicRenderer.java index a585b429d..75f55c7ba 100644 --- a/src/main/java/com/simibubi/create/content/schematics/client/SchematicRenderer.java +++ b/src/main/java/com/simibubi/create/content/schematics/client/SchematicRenderer.java @@ -78,8 +78,7 @@ public class SchematicRenderer { SuperByteBuffer superByteBuffer = bufferCache.get(layer); superByteBuffer.renderInto(ms, buffer.getBuffer(layer)); } - TileEntityRenderHelper.renderTileEntities(schematic, schematic.getRenderedTileEntities(), ms, new MatrixStack(), - buffer); + TileEntityRenderHelper.renderTileEntities(schematic, schematic.getRenderedTileEntities(), ms, buffer); } protected void redraw(Minecraft minecraft) { diff --git a/src/main/java/com/simibubi/create/foundation/ponder/elements/WorldSectionElement.java b/src/main/java/com/simibubi/create/foundation/ponder/elements/WorldSectionElement.java index 40e7d49b0..8cac03a46 100644 --- a/src/main/java/com/simibubi/create/foundation/ponder/elements/WorldSectionElement.java +++ b/src/main/java/com/simibubi/create/foundation/ponder/elements/WorldSectionElement.java @@ -369,7 +369,7 @@ public class WorldSectionElement extends AnimatedSceneElement { private void renderTileEntities(PonderWorld world, MatrixStack ms, IRenderTypeBuffer buffer, float pt) { loadTEsIfMissing(world); - TileEntityRenderHelper.renderTileEntities(world, renderedTileEntities, ms, new MatrixStack(), buffer, pt); + TileEntityRenderHelper.renderTileEntities(world, renderedTileEntities, ms, buffer, pt); } private SuperByteBuffer buildStructureBuffer(PonderWorld world, RenderType layer) { diff --git a/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java b/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java index 1a6c81b39..e56ec5234 100644 --- a/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/render/SuperByteBuffer.java @@ -8,11 +8,12 @@ import com.mojang.blaze3d.vertex.IVertexBuilder; import com.simibubi.create.foundation.block.render.SpriteShiftEntry; import com.simibubi.create.foundation.utility.MatrixStacker; -import it.unimi.dsi.fastutil.longs.Long2DoubleMap; -import it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap; +import it.unimi.dsi.fastutil.longs.Long2IntMap; +import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.LightTexture; +import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; @@ -22,31 +23,27 @@ import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3f; import net.minecraft.util.math.vector.Vector4f; -import net.minecraft.world.LightType; import net.minecraft.world.World; import net.minecraftforge.client.model.pipeline.LightUtil; public class SuperByteBuffer extends TemplateBuffer { - public interface IVertexLighter { - public int getPackedLight(float x, float y, float z); - } - // Vertex Position private MatrixStack transforms; // Vertex Texture Coords private SpriteShiftFunc spriteShiftFunc; - // Vertex Lighting - private boolean shouldLight; - private boolean hybridLight; - private int packedLightCoords; - private Matrix4f lightTransform; - // Vertex Coloring private boolean shouldColor; private int r, g, b, a; + private boolean disableDiffuseTransform; + + // Vertex Lighting + private boolean useWorldLight; + private boolean hybridLight; + private int packedLightCoords; + private Matrix4f lightTransform; public SuperByteBuffer(BufferBuilder buf) { super(buf); @@ -63,8 +60,7 @@ public class SuperByteBuffer extends TemplateBuffer { return (v - sprite.getMinV()) / f * 16.0F; } - private static final Long2DoubleMap skyLightCache = new Long2DoubleOpenHashMap(); - private static final Long2DoubleMap blockLightCache = new Long2DoubleOpenHashMap(); + private static final Long2IntMap WORLD_LIGHT_CACHE = new Long2IntOpenHashMap(); Vector4f pos = new Vector4f(); Vector3f normal = new Vector3f(); Vector4f lightPos = new Vector4f(); @@ -78,7 +74,6 @@ public class SuperByteBuffer extends TemplateBuffer { Matrix3f normalMat = transforms.peek() .getNormal() .copy(); - // normalMat.multiply(transforms.peek().getNormal()); Matrix4f modelMat = input.peek() .getModel() @@ -88,11 +83,11 @@ public class SuperByteBuffer extends TemplateBuffer { .getModel(); modelMat.multiply(localTransforms); - if (shouldLight && lightTransform != null) { - skyLightCache.clear(); - blockLightCache.clear(); + if (useWorldLight) { + WORLD_LIGHT_CACHE.clear(); } + boolean hasDefaultLight = packedLightCoords != 0; float f = .5f; int vertexCount = vertexCount(buffer); for (int i = 0; i < vertexCount; i++) { @@ -127,7 +122,12 @@ public class SuperByteBuffer extends TemplateBuffer { int colorB = Math.min(255, (int) (((float) this.b) * instanceDiffuse)); builder.color(colorR, colorG, colorB, this.a); } else { - float diffuseMult = instanceDiffuse / staticDiffuse; + float diffuseMult; + if (disableDiffuseTransform) { + diffuseMult = 1.0f; + } else { + diffuseMult = instanceDiffuse / staticDiffuse; + } int colorR = Math.min(255, (int) (((float) Byte.toUnsignedInt(r)) * diffuseMult)); int colorG = Math.min(255, (int) (((float) Byte.toUnsignedInt(g)) * diffuseMult)); int colorB = Math.min(255, (int) (((float) Byte.toUnsignedInt(b)) * diffuseMult)); @@ -142,23 +142,29 @@ public class SuperByteBuffer extends TemplateBuffer { } else builder.texture(u, v); - if (shouldLight) { - int light = packedLightCoords; + int light; + if (useWorldLight) { + lightPos.set(((x - f) * 15 / 16f) + f, (y - f) * 15 / 16f + f, (z - f) * 15 / 16f + f, 1F); + lightPos.transform(localTransforms); if (lightTransform != null) { - lightPos.set(((x - f) * 15 / 16f) + f, (y - f) * 15 / 16f + f, (z - f) * 15 / 16f + f, 1F); - lightPos.transform(localTransforms); lightPos.transform(lightTransform); - - int worldLight = getLight(Minecraft.getInstance().world, lightPos); - light = maxLight(worldLight, light); } - if (hybridLight) - builder.light(maxLight(light, getLight(buffer, i))); - else - builder.light(light); - } else - builder.light(getLight(buffer, i)); + light = getLight(Minecraft.getInstance().world, lightPos); + if (hasDefaultLight) { + light = maxLight(light, packedLightCoords); + } + } else if (hasDefaultLight) { + light = packedLightCoords; + } else { + light = getLight(buffer, i); + } + + if (hybridLight) { + builder.light(maxLight(light, getLight(buffer, i))); + } else { + builder.light(light); + } builder.normal(nx, ny, nz) .endVertex(); @@ -170,15 +176,16 @@ public class SuperByteBuffer extends TemplateBuffer { public SuperByteBuffer reset() { transforms = new MatrixStack(); spriteShiftFunc = null; - shouldLight = false; - hybridLight = false; - packedLightCoords = 0; - lightTransform = null; shouldColor = false; r = 0; g = 0; b = 0; a = 0; + disableDiffuseTransform = false; + useWorldLight = false; + hybridLight = false; + packedLightCoords = 0; + lightTransform = null; return this; } @@ -234,6 +241,20 @@ public class SuperByteBuffer extends TemplateBuffer { .translate(-.5f, -.5f, -.5f); } + public SuperByteBuffer color(int color) { + shouldColor = true; + r = ((color >> 16) & 0xFF); + g = ((color >> 8) & 0xFF); + b = (color & 0xFF); + a = 255; + return this; + } + + public SuperByteBuffer disableDiffuseTransform() { + disableDiffuseTransform = true; + return this; + } + public SuperByteBuffer shiftUV(SpriteShiftEntry entry) { this.spriteShiftFunc = (builder, u, v) -> { float targetU = entry.getTarget() @@ -270,20 +291,24 @@ public class SuperByteBuffer extends TemplateBuffer { return this; } - public SuperByteBuffer light(int packedLightCoords) { - shouldLight = true; - this.packedLightCoords = packedLightCoords; + public SuperByteBuffer light() { + useWorldLight = true; return this; } public SuperByteBuffer light(Matrix4f lightTransform) { - shouldLight = true; + useWorldLight = true; this.lightTransform = lightTransform; return this; } + public SuperByteBuffer light(int packedLightCoords) { + this.packedLightCoords = packedLightCoords; + return this; + } + public SuperByteBuffer light(Matrix4f lightTransform, int packedLightCoords) { - shouldLight = true; + useWorldLight = true; this.lightTransform = lightTransform; this.packedLightCoords = packedLightCoords; return this; @@ -294,15 +319,6 @@ public class SuperByteBuffer extends TemplateBuffer { return this; } - public SuperByteBuffer color(int color) { - shouldColor = true; - r = ((color >> 16) & 0xFF); - g = ((color >> 8) & 0xFF); - b = (color & 0xFF); - a = 255; - return this; - } - public static int maxLight(int packedLight1, int packedLight2) { int blockLight1 = LightTexture.getBlockLightCoordinates(packedLight1); int skyLight1 = LightTexture.getSkyLightCoordinates(packedLight1); @@ -312,12 +328,8 @@ public class SuperByteBuffer extends TemplateBuffer { } private static int getLight(World world, Vector4f lightPos) { - BlockPos.Mutable pos = new BlockPos.Mutable(); - double sky = 0, block = 0; - pos.setPos(lightPos.getX() + 0, lightPos.getY() + 0, lightPos.getZ() + 0); - sky += skyLightCache.computeIfAbsent(pos.toLong(), $ -> world.getLightLevel(LightType.SKY, pos)); - block += blockLightCache.computeIfAbsent(pos.toLong(), $ -> world.getLightLevel(LightType.BLOCK, pos)); - return ((int) sky) << 20 | ((int) block) << 4; + BlockPos pos = new BlockPos(lightPos.getX(), lightPos.getY(), lightPos.getZ()); + return WORLD_LIGHT_CACHE.computeIfAbsent(pos.toLong(), $ -> WorldRenderer.getLightmapCoordinates(world, pos)); } public boolean isEmpty() { @@ -329,4 +341,9 @@ public class SuperByteBuffer extends TemplateBuffer { void shift(IVertexBuilder builder, float u, float v); } + @FunctionalInterface + public interface IVertexLighter { + public int getPackedLight(float x, float y, float z); + } + } diff --git a/src/main/java/com/simibubi/create/foundation/render/TileEntityRenderHelper.java b/src/main/java/com/simibubi/create/foundation/render/TileEntityRenderHelper.java index f54a5725f..f00dc81b1 100644 --- a/src/main/java/com/simibubi/create/foundation/render/TileEntityRenderHelper.java +++ b/src/main/java/com/simibubi/create/foundation/render/TileEntityRenderHelper.java @@ -2,15 +2,17 @@ package com.simibubi.create.foundation.render; import java.util.Iterator; +import javax.annotation.Nullable; + import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.Create; -import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import net.minecraft.client.renderer.IRenderTypeBuffer; +import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.tileentity.TileEntityRenderer; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; @@ -23,28 +25,26 @@ import net.minecraft.world.World; public class TileEntityRenderHelper { public static void renderTileEntities(World world, Iterable customRenderTEs, MatrixStack ms, - MatrixStack localTransform, IRenderTypeBuffer buffer) { - renderTileEntities(world, null, customRenderTEs, ms, localTransform, buffer); + IRenderTypeBuffer buffer) { + renderTileEntities(world, null, customRenderTEs, ms, null, buffer); } public static void renderTileEntities(World world, Iterable customRenderTEs, MatrixStack ms, - MatrixStack localTransform, IRenderTypeBuffer buffer, float pt) { - renderTileEntities(world, null, customRenderTEs, ms, localTransform, buffer, pt); + IRenderTypeBuffer buffer, float pt) { + renderTileEntities(world, null, customRenderTEs, ms, null, buffer, pt); } - public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, - Iterable customRenderTEs, MatrixStack ms, MatrixStack localTransform, IRenderTypeBuffer buffer) { - renderTileEntities(world, renderWorld, customRenderTEs, ms, localTransform, buffer, + public static void renderTileEntities(World world, @Nullable PlacementSimulationWorld renderWorld, + Iterable customRenderTEs, MatrixStack ms, @Nullable Matrix4f lightTransform, IRenderTypeBuffer buffer) { + renderTileEntities(world, renderWorld, customRenderTEs, ms, lightTransform, buffer, AnimationTickHolder.getPartialTicks()); } - public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, - Iterable customRenderTEs, MatrixStack ms, MatrixStack localTransform, IRenderTypeBuffer buffer, - float pt) { - Matrix4f matrix = localTransform.peek() - .getModel(); - - for (Iterator iterator = customRenderTEs.iterator(); iterator.hasNext();) { + public static void renderTileEntities(World world, @Nullable PlacementSimulationWorld renderWorld, + Iterable customRenderTEs, MatrixStack ms, @Nullable Matrix4f lightTransform, IRenderTypeBuffer buffer, + float pt) { + Iterator iterator = customRenderTEs.iterator(); + while (iterator.hasNext()) { TileEntity tileEntity = iterator.next(); // if (tileEntity instanceof IInstanceRendered) continue; // TODO: some things still need to render @@ -60,10 +60,15 @@ public class TileEntityRenderHelper { .translate(pos); try { - Vector4f vec = new Vector4f(pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, 1); - vec.transform(matrix); - BlockPos lightPos = new BlockPos(vec.getX(), vec.getY(), vec.getZ()); - int worldLight = ContraptionRenderDispatcher.getContraptionLightAt(world, renderWorld, pos, lightPos); + 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.getX(), lightVec.getY(), lightVec.getZ()); + } else { + lightPos = pos; + } + int worldLight = getCombinedLight(world, lightPos, renderWorld, pos); renderer.render(tileEntity, pt, ms, buffer, worldLight, OverlayTexture.DEFAULT_UV); } catch (Exception e) { @@ -82,4 +87,16 @@ public class TileEntityRenderHelper { } } + public static int getCombinedLight(World world, BlockPos worldPos, @Nullable PlacementSimulationWorld renderWorld, + BlockPos renderWorldPos) { + int worldLight = WorldRenderer.getLightmapCoordinates(world, worldPos); + + if (renderWorld != null) { + int renderWorldLight = WorldRenderer.getLightmapCoordinates(renderWorld, renderWorldPos); + return SuperByteBuffer.maxLight(worldLight, renderWorldLight); + } + + return worldLight; + } + }