Merge pull request #1599 from PepperCode1/contraption-lighting

Add Lighting to Non-Flywheel Contraptions
This commit is contained in:
Jozufozu 2021-05-18 13:58:56 -07:00 committed by GitHub
commit f6c27c395b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 578 additions and 488 deletions

View File

@ -3,11 +3,12 @@ package com.simibubi.create.content.contraptions.components.actors;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.jozufozu.flywheel.backend.Backend; 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.MovementContext;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; 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.ContraptionKineticRenderer;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -34,10 +35,10 @@ public class DrillMovementBehaviour extends BlockBreakingMovementBehaviour {
@Override @Override
@OnlyIn(value = Dist.CLIENT) @OnlyIn(value = Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
if (!Backend.canUseInstancing()) if (!Backend.canUseInstancing())
DrillRenderer.renderInContraption(context, ms, msLocal, buffer); DrillRenderer.renderInContraption(context, renderWorld, matrices, buffer);
} }
@Override @Override

View File

@ -5,12 +5,15 @@ import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; 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.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.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -29,9 +32,8 @@ public class DrillRenderer extends KineticTileEntityRenderer {
return PartialBufferer.getFacing(AllBlockPartials.DRILL_HEAD, te.getBlockState()); return PartialBufferer.getFacing(AllBlockPartials.DRILL_HEAD, te.getBlockState());
} }
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal};
BlockState state = context.state; BlockState state = context.state;
SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.DRILL_HEAD, state); SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.DRILL_HEAD, state);
Direction facing = state.get(DrillBlock.FACING); Direction facing = state.get(DrillBlock.FACING);
@ -42,18 +44,22 @@ public class DrillRenderer extends KineticTileEntityRenderer {
float time = AnimationTickHolder.getRenderTime() / 20; float time = AnimationTickHolder.getRenderTime() / 20;
float angle = (float) (((time * speed) % 360)); float angle = (float) (((time * speed) % 360));
for (MatrixStack m : matrixStacks) MatrixStack m = matrices.contraptionStack;
MatrixStacker.of(m) m.push();
.centre() MatrixStacker.of(m)
.rotateY(AngleHelper.horizontalAngle(facing)) .centre()
.rotateX(AngleHelper.verticalAngle(facing)) .rotateY(AngleHelper.horizontalAngle(facing))
.rotateZ(angle) .rotateX(AngleHelper.verticalAngle(facing))
.unCentre(); .rotateZ(angle)
.unCentre();
superBuffer superBuffer
.light(msLocal.peek() .transform(m)
.getModel()) .light(matrices.entityMatrix,
.renderInto(ms, buffer.getBuffer(RenderType.getSolid())); ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(matrices.entityStack, buffer.getBuffer(RenderType.getSolid()));
m.pop();
} }
} }

View File

@ -7,13 +7,14 @@ import javax.annotation.Nullable;
import org.apache.commons.lang3.mutable.MutableBoolean; import org.apache.commons.lang3.mutable.MutableBoolean;
import com.jozufozu.flywheel.backend.Backend; 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.MovementBehaviour;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; 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.ActorInstance;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionKineticRenderer; 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.BlockHelper;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
@ -52,10 +53,10 @@ public class HarvesterMovementBehaviour extends MovementBehaviour {
} }
@Override @Override
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffers) { ContraptionMatrices matrices, IRenderTypeBuffer buffers) {
if (!Backend.canUseInstancing()) if (!Backend.canUseInstancing())
HarvesterRenderer.renderInContraption(context, ms, msLocal, buffers); HarvesterRenderer.renderInContraption(context, renderWorld, matrices, buffers);
} }
@Override @Override

View File

@ -5,6 +5,7 @@ import static net.minecraft.state.properties.BlockStateProperties.HORIZONTAL_FAC
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.AllBlockPartials; import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; 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.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
@ -12,6 +13,7 @@ import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer
import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -39,8 +41,8 @@ public class HarvesterRenderer extends SafeTileEntityRenderer<HarvesterTileEntit
.renderInto(ms, buffer.getBuffer(RenderType.getCutoutMipped())); .renderInto(ms, buffer.getBuffer(RenderType.getCutoutMipped()));
} }
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffers) { ContraptionMatrices matrices, IRenderTypeBuffer buffers) {
BlockState blockState = context.state; BlockState blockState = context.state;
Direction facing = blockState.get(HORIZONTAL_FACING); Direction facing = blockState.get(HORIZONTAL_FACING);
SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.HARVESTER_BLADE, blockState); SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.HARVESTER_BLADE, blockState);
@ -50,11 +52,13 @@ public class HarvesterRenderer extends SafeTileEntityRenderer<HarvesterTileEntit
if (context.contraption.stalled) if (context.contraption.stalled)
speed = 0; speed = 0;
superBuffer.transform(matrices.contraptionStack);
transform(context.world, facing, superBuffer, speed); transform(context.world, facing, superBuffer, speed);
superBuffer.light(msLocal.peek() superBuffer
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context)) .light(matrices.entityMatrix,
.renderInto(ms, buffers.getBuffer(RenderType.getCutoutMipped())); ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(matrices.entityStack, buffers.getBuffer(RenderType.getCutoutMipped()));
} }
public static void transform(World world, Direction facing, SuperByteBuffer superBuffer, float speed) { public static void transform(World world, Direction facing, SuperByteBuffer superBuffer, float speed) {

View File

@ -2,10 +2,11 @@ package com.simibubi.create.content.contraptions.components.actors;
import java.util.Optional; import java.util.Optional;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour; 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.MovementContext;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionMatrices;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -31,9 +32,9 @@ public class PortableStorageInterfaceMovement extends MovementBehaviour {
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
PortableStorageInterfaceRenderer.renderInContraption(context, ms, msLocal, buffer); PortableStorageInterfaceRenderer.renderInContraption(context, renderWorld, matrices, buffer);
} }
@Override @Override

View File

@ -8,6 +8,7 @@ import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.simibubi.create.AllBlockPartials; import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; 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.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
@ -15,6 +16,7 @@ import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer
import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -41,8 +43,8 @@ public class PortableStorageInterfaceRenderer extends SafeTileEntityRenderer<Por
.renderInto(ms, vb), ms); .renderInto(ms, vb), ms);
} }
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
BlockState blockState = context.state; BlockState blockState = context.state;
PortableStorageInterfaceTileEntity te = getTargetPSI(context); PortableStorageInterfaceTileEntity te = getTargetPSI(context);
IVertexBuilder vb = buffer.getBuffer(RenderType.getSolid()); IVertexBuilder vb = buffer.getBuffer(RenderType.getSolid());
@ -55,44 +57,39 @@ public class PortableStorageInterfaceRenderer extends SafeTileEntityRenderer<Por
lit = te.isConnected(); lit = te.isConnected();
} }
render(blockState, progress, lit, sbb -> sbb.light(msLocal.peek() render(blockState, progress, lit, sbb -> sbb.transform(matrices.contraptionStack)
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context)) .disableDiffuseTransform()
.renderInto(ms, vb), ms, msLocal); .light(matrices.entityMatrix,
ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(matrices.entityStack, vb), matrices.contraptionStack);
} }
private static void render(BlockState blockState, float progress, boolean lit, private static void render(BlockState blockState, float progress, boolean lit,
Consumer<SuperByteBuffer> drawCallback, MatrixStack... matrixStacks) { Consumer<SuperByteBuffer> drawCallback, MatrixStack ms) {
for (MatrixStack ms : matrixStacks) ms.push();
ms.push();
SuperByteBuffer middle = PartialBufferer.get(getMiddleForState(blockState, lit), blockState); SuperByteBuffer middle = PartialBufferer.get(getMiddleForState(blockState, lit), blockState);
SuperByteBuffer top = PartialBufferer.get(getTopForState(blockState), blockState); SuperByteBuffer top = PartialBufferer.get(getTopForState(blockState), blockState);
Direction facing = blockState.get(PortableStorageInterfaceBlock.FACING); Direction facing = blockState.get(PortableStorageInterfaceBlock.FACING);
for (MatrixStack ms : matrixStacks) MatrixStacker.of(ms)
MatrixStacker.of(ms) .centre()
.centre() .rotateY(AngleHelper.horizontalAngle(facing))
.rotateY(AngleHelper.horizontalAngle(facing)) .rotateX(facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90)
.rotateX(facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90) .unCentre();
.unCentre();
for (MatrixStack ms : matrixStacks) { ms.translate(0, progress / 2f, 0);
ms.translate(0, progress / 2f, 0); ms.push();
ms.push(); ms.translate(0, 6 / 16f, 0);
ms.translate(0, 6 / 16f, 0);
}
drawCallback.accept(middle); drawCallback.accept(middle);
for (MatrixStack ms : matrixStacks) { ms.pop();
ms.pop(); ms.translate(0, progress / 2f, 0);
ms.translate(0, progress / 2f, 0);
}
drawCallback.accept(top); drawCallback.accept(top);
for (MatrixStack ms : matrixStacks) ms.pop();
ms.pop();
} }
protected static PortableStorageInterfaceTileEntity getTargetPSI(MovementContext context) { protected static PortableStorageInterfaceTileEntity getTargetPSI(MovementContext context) {

View File

@ -1,12 +1,13 @@
package com.simibubi.create.content.contraptions.components.actors; 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.SawBlock;
import com.simibubi.create.content.contraptions.components.saw.SawRenderer; 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.saw.SawTileEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; 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.TreeCutter;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -75,9 +76,9 @@ public class SawMovementBehaviour extends BlockBreakingMovementBehaviour {
@Override @Override
@OnlyIn(value = Dist.CLIENT) @OnlyIn(value = Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
SawRenderer.renderInContraption(context, ms, msLocal, buffer); SawRenderer.renderInContraption(context, renderWorld, matrices, buffer);
} }
@Override @Override

View File

@ -8,7 +8,6 @@ import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.AllTags.AllBlockTags; 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.MovementContext;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; 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.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.logistics.item.filter.FilterItem;
import com.simibubi.create.content.schematics.ItemRequirement; import com.simibubi.create.content.schematics.ItemRequirement;
import com.simibubi.create.content.schematics.SchematicWorld; import com.simibubi.create.content.schematics.SchematicWorld;
@ -27,6 +27,7 @@ import com.simibubi.create.foundation.item.ItemHelper.ExtractionCountMode;
import com.simibubi.create.foundation.utility.BlockHelper; import com.simibubi.create.foundation.utility.BlockHelper;
import com.simibubi.create.foundation.utility.NBTHelper; import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.NBTProcessors; import com.simibubi.create.foundation.utility.NBTProcessors;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -254,10 +255,10 @@ public class DeployerMovementBehaviour extends MovementBehaviour {
} }
@Override @Override
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffers) { ContraptionMatrices matrices, IRenderTypeBuffer buffers) {
if (!Backend.canUseInstancing()) if (!Backend.canUseInstancing())
DeployerRenderer.renderInContraption(context, ms, msLocal, buffers); DeployerRenderer.renderInContraption(context, renderWorld, matrices, buffers);
} }
@Override @Override

View File

@ -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.base.KineticTileEntityRenderer;
import com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode; 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.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.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
@ -21,6 +22,7 @@ import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.NBTHelper; import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -35,7 +37,6 @@ import net.minecraft.util.Direction;
import net.minecraft.util.Direction.Axis; import net.minecraft.util.Direction.Axis;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper; 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.Vector3d;
import net.minecraft.util.math.vector.Vector3f; import net.minecraft.util.math.vector.Vector3f;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -154,9 +155,8 @@ public class DeployerRenderer extends SafeTileEntityRenderer<DeployerTileEntity>
return buffer; return buffer;
} }
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal};
IVertexBuilder builder = buffer.getBuffer(RenderType.getSolid()); IVertexBuilder builder = buffer.getBuffer(RenderType.getSolid());
BlockState blockState = context.state; BlockState blockState = context.state;
BlockPos pos = BlockPos.ZERO; BlockPos pos = BlockPos.ZERO;
@ -166,8 +166,6 @@ public class DeployerRenderer extends SafeTileEntityRenderer<DeployerTileEntity>
SuperByteBuffer pole = PartialBufferer.get(AllBlockPartials.DEPLOYER_POLE, blockState); SuperByteBuffer pole = PartialBufferer.get(AllBlockPartials.DEPLOYER_POLE, blockState);
SuperByteBuffer hand = PartialBufferer.get(handPose, blockState); SuperByteBuffer hand = PartialBufferer.get(handPose, blockState);
pole = transform(world, pole, blockState, pos, true);
hand = transform(world, hand, blockState, pos, false);
double factor; double factor;
if (context.contraption.stalled || context.position == null || context.data.contains("StationaryTimer")) { if (context.contraption.stalled || context.position == null || context.data.contains("StationaryTimer")) {
@ -183,14 +181,21 @@ public class DeployerRenderer extends SafeTileEntityRenderer<DeployerTileEntity>
Vector3d offset = Vector3d.of(blockState.get(FACING) Vector3d offset = Vector3d.of(blockState.get(FACING)
.getDirectionVec()).scale(factor); .getDirectionVec()).scale(factor);
Matrix4f lighting = msLocal.peek() MatrixStack m = matrices.contraptionStack;
.getModel(); m.push();
for (MatrixStack m : matrixStacks) m.translate(offset.x, offset.y, offset.z);
m.translate(offset.x, offset.y, offset.z);
pole.light(lighting, ContraptionRenderDispatcher.getLightOnContraption(context)) pole.transform(m);
.renderInto(ms, builder); hand.transform(m);
hand.light(lighting, ContraptionRenderDispatcher.getLightOnContraption(context)) pole = transform(world, pole, blockState, pos, true);
.renderInto(ms, builder); 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) { static PartialModel getHandPose(DeployerTileEntity.Mode mode) {

View File

@ -10,6 +10,7 @@ import com.simibubi.create.CreateClient;
import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; 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.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.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
@ -18,6 +19,7 @@ import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer
import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -151,11 +153,9 @@ public class SawRenderer extends SafeTileEntityRenderer<SawTileEntity> {
return KineticTileEntityRenderer.shaft(KineticTileEntityRenderer.getRotationAxisOf(te)); return KineticTileEntityRenderer.shaft(KineticTileEntityRenderer.getRotationAxisOf(te));
} }
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[] { ms, msLocal };
BlockState state = context.state; BlockState state = context.state;
SuperByteBuffer superBuffer;
Direction facing = state.get(SawBlock.FACING); Direction facing = state.get(SawBlock.FACING);
Vector3d facingVec = Vector3d.of(context.state.get(SawBlock.FACING) Vector3d facingVec = Vector3d.of(context.state.get(SawBlock.FACING)
@ -171,6 +171,7 @@ public class SawRenderer extends SafeTileEntityRenderer<SawTileEntity> {
boolean shouldAnimate = boolean shouldAnimate =
(context.contraption.stalled && horizontal) || (!context.contraption.stalled && !backwards && moving); (context.contraption.stalled && horizontal) || (!context.contraption.stalled && !backwards && moving);
SuperByteBuffer superBuffer;
if (SawBlock.isHorizontal(state)) { if (SawBlock.isHorizontal(state)) {
if (shouldAnimate) if (shouldAnimate)
superBuffer = PartialBufferer.get(AllBlockPartials.SAW_BLADE_HORIZONTAL_ACTIVE, state); superBuffer = PartialBufferer.get(AllBlockPartials.SAW_BLADE_HORIZONTAL_ACTIVE, state);
@ -183,22 +184,26 @@ public class SawRenderer extends SafeTileEntityRenderer<SawTileEntity> {
superBuffer = PartialBufferer.get(AllBlockPartials.SAW_BLADE_VERTICAL_INACTIVE, state); 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) MatrixStacker.of(m)
.centre() .rotateZ(state.get(SawBlock.AXIS_ALONG_FIRST_COORDINATE) ? 0 : 90);
.rotateY(AngleHelper.horizontalAngle(facing)) MatrixStacker.of(m)
.rotateX(AngleHelper.verticalAngle(facing)); .unCentre();
if (!SawBlock.isHorizontal(state))
MatrixStacker.of(m)
.rotateZ(state.get(SawBlock.AXIS_ALONG_FIRST_COORDINATE) ? 0 : 90);
MatrixStacker.of(m)
.unCentre();
}
superBuffer superBuffer
.light(msLocal.peek() .transform(m)
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context)) .disableDiffuseTransform()
.renderInto(ms, buffer.getBuffer(RenderType.getCutoutMipped())); .light(matrices.entityMatrix,
ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(matrices.entityStack, buffer.getBuffer(RenderType.getCutoutMipped()));
m.pop();
} }
} }

View File

@ -1,20 +1,19 @@
package com.simibubi.create.content.contraptions.components.structureMovement; package com.simibubi.create.content.contraptions.components.structureMovement;
import com.mojang.blaze3d.matrix.MatrixStack; 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.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.culling.ClippingHelper; import net.minecraft.client.renderer.culling.ClippingHelper;
import net.minecraft.client.renderer.entity.EntityRenderer; import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager; import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
public class ContraptionEntityRenderer<C extends AbstractContraptionEntity> extends EntityRenderer<C> { public class ContraptionEntityRenderer<C extends AbstractContraptionEntity> extends EntityRenderer<C> {
public ContraptionEntityRenderer(EntityRendererManager p_i46179_1_) { public ContraptionEntityRenderer(EntityRendererManager manager) {
super(p_i46179_1_); super(manager);
} }
@Override @Override
@ -23,42 +22,26 @@ public class ContraptionEntityRenderer<C extends AbstractContraptionEntity> exte
} }
@Override @Override
public boolean shouldRender(C entity, ClippingHelper clippingHelper, double p_225626_3_, double p_225626_5_, public boolean shouldRender(C entity, ClippingHelper clippingHelper, double cameraX, double cameraY,
double p_225626_7_) { double cameraZ) {
if (entity.getContraption() == null) if (entity.getContraption() == null)
return false; return false;
if (!entity.isAlive()) if (!entity.isAlive())
return false; 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 @Override
public void render(C entity, float yaw, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffers, public void render(C entity, float yaw, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffers,
int overlay) { int overlay) {
super.render(entity, yaw, partialTicks, ms, buffers, overlay); super.render(entity, yaw, partialTicks, ms, buffers, overlay);
// Keep a copy of the transforms in order to determine correct lighting ContraptionMatrices matrices = new ContraptionMatrices(ms, entity);
MatrixStack msLocal = translateTo(entity, AnimationTickHolder.getPartialTicks());
MatrixStack[] matrixStacks = new MatrixStack[] { ms, msLocal };
ms.push();
entity.doLocalTransforms(partialTicks, matrixStacks);
Contraption contraption = entity.getContraption(); Contraption contraption = entity.getContraption();
if (contraption != null) { 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;
} }
} }

View File

@ -2,9 +2,10 @@ package com.simibubi.create.content.contraptions.components.structureMovement;
import javax.annotation.Nullable; 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.ActorInstance;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionKineticRenderer; 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; import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.item.ItemEntity;
@ -60,8 +61,8 @@ public abstract class MovementBehaviour {
} }
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) {} ContraptionMatrices matrices, IRenderTypeBuffer buffer) {}
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
@Nullable @Nullable

View File

@ -4,7 +4,6 @@ import javax.annotation.Nullable;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.core.PartialModel; import com.jozufozu.flywheel.backend.core.PartialModel;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.AllBlockPartials; import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity;
@ -13,10 +12,12 @@ 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.OrientedContraptionEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance; 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.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.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.render.PartialBufferer; import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
@ -31,8 +32,8 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour {
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
if (Backend.canUseInstancing()) return; if (Backend.canUseInstancing()) return;
Direction facing = context.state.get(BlockStateProperties.FACING); Direction facing = context.state.get(BlockStateProperties.FACING);
@ -52,12 +53,14 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour {
orientation = rotation; orientation = rotation;
superBuffer.transform(matrices.contraptionStack);
superBuffer.rotateCentered(orientation); superBuffer.rotateCentered(orientation);
// render // render
superBuffer.light(msLocal.peek() superBuffer
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context)); .light(matrices.entityMatrix,
superBuffer.renderInto(ms, buffer.getBuffer(RenderType.getSolid())); ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(matrices.entityStack, buffer.getBuffer(RenderType.getSolid()));
} }
@Override @Override

View File

@ -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());
}
}

View File

@ -1,12 +1,12 @@
package com.simibubi.create.content.contraptions.components.structureMovement.render; package com.simibubi.create.content.contraptions.components.structureMovement.render;
import static org.lwjgl.opengl.GL13.GL_QUADS; import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL12.GL_TEXTURE_3D;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0; import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.GL_TEXTURE4; import static org.lwjgl.opengl.GL13.GL_TEXTURE4;
import static org.lwjgl.opengl.GL13.GL_TEXTURE_3D;
import static org.lwjgl.opengl.GL13.glActiveTexture; import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL13.glDisable;
import static org.lwjgl.opengl.GL13.glEnable;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -36,7 +36,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo; import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.BlockModelRenderer; import net.minecraft.client.renderer.BlockModelRenderer;
import net.minecraft.client.renderer.BlockRendererDispatcher; import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GameRenderer; import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -44,7 +44,6 @@ import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup; import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.world.ClientWorld; import net.minecraft.client.world.ClientWorld;
@ -57,25 +56,35 @@ import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.data.EmptyModelData; import net.minecraftforge.client.model.data.EmptyModelData;
public class ContraptionRenderDispatcher { public class ContraptionRenderDispatcher {
public static final Int2ObjectMap<RenderedContraption> renderers = new Int2ObjectOpenHashMap<>(); private static final BlockModelRenderer MODEL_RENDERER = new BlockModelRenderer(Minecraft.getInstance().getBlockColors());
private static final BlockModelShapes BLOCK_MODELS = Minecraft.getInstance().getModelManager().getBlockModelShapes();
private static int worldHolderRefreshCounter;
public static final Int2ObjectMap<RenderedContraption> RENDERERS = new Int2ObjectOpenHashMap<>();
public static final Int2ObjectMap<ContraptionWorldHolder> WORLD_HOLDERS = new Int2ObjectOpenHashMap<>();
public static final Compartment<Pair<Contraption, Integer>> CONTRAPTION = new Compartment<>(); public static final Compartment<Pair<Contraption, Integer>> CONTRAPTION = new Compartment<>();
protected static PlacementSimulationWorld renderWorld;
public static void tick() { public static void tick() {
if (Minecraft.getInstance().isGamePaused()) return; if (Minecraft.getInstance().isGamePaused()) return;
for (RenderedContraption contraption : renderers.values()) { for (RenderedContraption contraption : RENDERERS.values()) {
contraption.getLighter().tick(contraption); contraption.getLighter().tick(contraption);
contraption.kinetics.tick(); contraption.kinetics.tick();
} }
worldHolderRefreshCounter++;
if (worldHolderRefreshCounter >= 20) {
removeDeadHolders();
worldHolderRefreshCounter = 0;
}
} }
public static void beginFrame(ClientWorld world, MatrixStack stack, ActiveRenderInfo info, GameRenderer gameRenderer, LightTexture lightTexture) { public static void beginFrame(ClientWorld world, MatrixStack stack, ActiveRenderInfo info, GameRenderer gameRenderer, LightTexture lightTexture) {
double camX = info.getProjectedView().x; double camX = info.getProjectedView().x;
double camY = info.getProjectedView().y; double camY = info.getProjectedView().y;
double camZ = info.getProjectedView().z; double camZ = info.getProjectedView().z;
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : RENDERERS.values()) {
renderer.beginFrame(info, camX, camY, camZ); renderer.beginFrame(info, camX, camY, camZ);
} }
} }
@ -83,7 +92,7 @@ public class ContraptionRenderDispatcher {
public static void renderLayer(ClientWorld world, RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ) { public static void renderLayer(ClientWorld world, RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ) {
removeDeadContraptions(); removeDeadContraptions();
if (renderers.isEmpty()) return; if (RENDERERS.isEmpty()) return;
layer.startDrawing(); layer.startDrawing();
glEnable(GL_TEXTURE_3D); glEnable(GL_TEXTURE_3D);
@ -96,13 +105,13 @@ public class ContraptionRenderDispatcher {
structureShader.uploadViewProjection(viewProjection); structureShader.uploadViewProjection(viewProjection);
structureShader.uploadCameraPos(camX, camY, camZ); structureShader.uploadCameraPos(camX, camY, camZ);
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : RENDERERS.values()) {
renderer.doRenderLayer(layer, structureShader); renderer.doRenderLayer(layer, structureShader);
} }
} }
if (Backend.canUseInstancing()) { if (Backend.canUseInstancing()) {
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : RENDERERS.values()) {
renderer.kinetics.render(layer, viewProjection, camX, camY, camZ, renderer::setup); renderer.kinetics.render(layer, viewProjection, camX, camY, camZ, renderer::setup);
renderer.teardown(); renderer.teardown();
} }
@ -113,70 +122,81 @@ public class ContraptionRenderDispatcher {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
} }
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, matrices, buffers);
} else {
ContraptionWorldHolder holder = getWorldHolder(world, contraption);
PlacementSimulationWorld renderWorld = holder.renderWorld;
ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, matrices, buffers);
ContraptionRenderDispatcher.renderStructure(world, renderWorld, contraption, matrices, buffers);
}
}
private static RenderedContraption getRenderer(World world, Contraption c) { private static RenderedContraption getRenderer(World world, Contraption c) {
int entityId = c.entity.getEntityId(); int entityId = c.entity.getEntityId();
RenderedContraption contraption = renderers.get(entityId); RenderedContraption contraption = RENDERERS.get(entityId);
if (contraption == null) { if (contraption == null) {
contraption = new RenderedContraption(world, c); PlacementSimulationWorld renderWorld = setupRenderWorld(world, c);
renderers.put(entityId, contraption); contraption = new RenderedContraption(world, renderWorld, c);
RENDERERS.put(entityId, contraption);
} }
return contraption; return contraption;
} }
public static void render(AbstractContraptionEntity entity, MatrixStack ms, IRenderTypeBuffer buffers, private static ContraptionWorldHolder getWorldHolder(World world, Contraption c) {
MatrixStack msLocal, Contraption contraption) { int entityId = c.entity.getEntityId();
if (Backend.canUseVBOs() && Backend.isFlywheelWorld(entity.world)) { ContraptionWorldHolder holder = WORLD_HOLDERS.get(entityId);
ContraptionRenderDispatcher.renderDynamic(entity.world, contraption, ms, msLocal, buffers);
} else { if (holder == null) {
ContraptionRenderDispatcher.renderDynamic(entity.world, contraption, ms, msLocal, buffers); PlacementSimulationWorld renderWorld = setupRenderWorld(world, c);
ContraptionRenderDispatcher.renderStructure(entity.world, contraption, ms, msLocal, buffers); holder = new ContraptionWorldHolder(c, renderWorld);
WORLD_HOLDERS.put(entityId, holder);
} }
return holder;
} }
public static void renderStructure(World world, Contraption c, MatrixStack ms, MatrixStack msLocal, public static PlacementSimulationWorld setupRenderWorld(World world, Contraption c) {
IRenderTypeBuffer buffer) { PlacementSimulationWorld renderWorld = new PlacementSimulationWorld(world);
SuperByteBufferCache bufferCache = CreateClient.bufferCache;
List<RenderType> blockLayers = RenderType.getBlockLayers();
buffer.getBuffer(RenderType.getSolid()); renderWorld.setTileEntities(c.presentTileEntities.values());
for (int i = 0; i < blockLayers.size(); i++) {
RenderType layer = blockLayers.get(i); for (Template.BlockInfo info : c.getBlocks()
Pair<Contraption, Integer> key = Pair.of(c, i); .values())
SuperByteBuffer contraptionBuffer = bufferCache.get(CONTRAPTION, key, () -> buildStructureBuffer(c, layer)); // Skip individual lighting updates to prevent lag with large contraptions
if (contraptionBuffer.isEmpty()) renderWorld.setBlockState(info.pos, info.state, 128);
continue;
Matrix4f model = msLocal.peek() renderWorld.updateLightSources();
.getModel(); renderWorld.lighter.tick(Integer.MAX_VALUE, false, false);
contraptionBuffer.light(model)
.renderInto(ms, buffer.getBuffer(layer)); return renderWorld;
}
} }
public static void renderDynamic(World world, Contraption c, MatrixStack ms, MatrixStack msLocal, public static void renderDynamic(World world, PlacementSimulationWorld renderWorld, Contraption c,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
renderTileEntities(world, c, ms, msLocal, buffer); renderTileEntities(world, renderWorld, c, matrices, buffer);
if (buffer instanceof IRenderTypeBuffer.Impl) if (buffer instanceof IRenderTypeBuffer.Impl)
((IRenderTypeBuffer.Impl) buffer).draw(); ((IRenderTypeBuffer.Impl) buffer).draw();
renderActors(world, c, ms, msLocal, buffer); renderActors(world, renderWorld, c, matrices, buffer);
} }
public static void renderTileEntities(World world, Contraption c, MatrixStack ms, MatrixStack msLocal, public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, Contraption c,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
PlacementSimulationWorld renderWorld = null; TileEntityRenderHelper.renderTileEntities(world, renderWorld, c.specialRenderedTileEntities,
if (Backend.canUseVBOs() && Backend.isFlywheelWorld(world)) { matrices.getFinalStack(), matrices.getFinalLight(), buffer);
RenderedContraption renderer = getRenderer(world, c);
renderWorld = renderer.renderWorld;
}
TileEntityRenderHelper.renderTileEntities(world, renderWorld, c.specialRenderedTileEntities, ms, msLocal, buffer);
} }
protected static void renderActors(World world, Contraption c, MatrixStack ms, MatrixStack msLocal, protected static void renderActors(World world, PlacementSimulationWorld renderWorld, Contraption c,
IRenderTypeBuffer buffer) { ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal};
for (Pair<Template.BlockInfo, MovementContext> actor : c.getActors()) { for (Pair<Template.BlockInfo, MovementContext> actor : c.getActors()) {
MovementContext context = actor.getRight(); MovementContext context = actor.getRight();
if (context == null) if (context == null)
@ -184,123 +204,108 @@ public class ContraptionRenderDispatcher {
if (context.world == null) if (context.world == null)
context.world = world; context.world = world;
Template.BlockInfo blockInfo = actor.getLeft(); Template.BlockInfo blockInfo = actor.getLeft();
for (MatrixStack m : matrixStacks) {
m.push(); MatrixStack m = matrices.contraptionStack;
MatrixStacker.of(m) m.push();
.translate(blockInfo.pos); MatrixStacker.of(m)
} .translate(blockInfo.pos);
MovementBehaviour movementBehaviour = AllMovementBehaviours.of(blockInfo.state); MovementBehaviour movementBehaviour = AllMovementBehaviours.of(blockInfo.state);
if (movementBehaviour != null) if (movementBehaviour != null)
movementBehaviour.renderInContraption(context, ms, msLocal, buffer); movementBehaviour.renderInContraption(context, renderWorld, matrices, buffer);
for (MatrixStack m : matrixStacks) m.pop();
m.pop();
} }
} }
private static SuperByteBuffer buildStructureBuffer(Contraption c, RenderType layer) { public static void renderStructure(World world, PlacementSimulationWorld renderWorld, Contraption c,
BufferBuilder builder = buildStructure(c, layer); ContraptionMatrices matrices, IRenderTypeBuffer buffer) {
SuperByteBufferCache bufferCache = CreateClient.bufferCache;
List<RenderType> blockLayers = RenderType.getBlockLayers();
buffer.getBuffer(RenderType.getSolid());
for (int i = 0; i < blockLayers.size(); i++) {
RenderType layer = blockLayers.get(i);
Pair<Contraption, Integer> key = Pair.of(c, i);
SuperByteBuffer contraptionBuffer = bufferCache.get(CONTRAPTION, key, () -> buildStructureBuffer(renderWorld, c, layer));
if (contraptionBuffer.isEmpty())
continue;
contraptionBuffer
.transform(matrices.contraptionStack)
.light(matrices.entityMatrix)
.hybridLight()
.renderInto(matrices.entityStack, buffer.getBuffer(layer));
}
}
private static SuperByteBuffer buildStructureBuffer(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
BufferBuilder builder = buildStructure(renderWorld, c, layer);
return new SuperByteBuffer(builder); return new SuperByteBuffer(builder);
} }
public static BufferBuilder buildStructure(Contraption c, RenderType layer) { public static BufferBuilder buildStructure(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
if (renderWorld == null || renderWorld.getWorld() != Minecraft.getInstance().world)
renderWorld = new PlacementSimulationWorld(Minecraft.getInstance().world);
ForgeHooksClient.setRenderLayer(layer);
MatrixStack ms = new MatrixStack(); MatrixStack ms = new MatrixStack();
BlockRendererDispatcher dispatcher = Minecraft.getInstance()
.getBlockRendererDispatcher();
BlockModelRenderer blockRenderer = dispatcher.getBlockModelRenderer();
Random random = new Random(); Random random = new Random();
BufferBuilder builder = new BufferBuilder(DefaultVertexFormats.BLOCK.getIntegerSize()); BufferBuilder builder = new BufferBuilder(DefaultVertexFormats.BLOCK.getIntegerSize());
builder.begin(GL_QUADS, DefaultVertexFormats.BLOCK); builder.begin(GL_QUADS, DefaultVertexFormats.BLOCK);
renderWorld.setTileEntities(c.presentTileEntities.values());
ForgeHooksClient.setRenderLayer(layer);
BlockModelRenderer.enableCache();
for (Template.BlockInfo info : c.getBlocks() for (Template.BlockInfo info : c.getBlocks()
.values()) .values()) {
renderWorld.setBlockState(info.pos, info.state);
for (Template.BlockInfo info : c.getBlocks()
.values()) {
BlockState state = info.state; BlockState state = info.state;
if (state.getRenderType() == BlockRenderType.ENTITYBLOCK_ANIMATED) if (state.getRenderType() != BlockRenderType.MODEL)
continue; continue;
if (!RenderTypeLookup.canRenderInLayer(state, layer)) if (!RenderTypeLookup.canRenderInLayer(state, layer))
continue; continue;
IBakedModel originalModel = dispatcher.getModelForState(state); BlockPos pos = info.pos;
ms.push();
ms.translate(info.pos.getX(), info.pos.getY(), info.pos.getZ());
blockRenderer.renderModel(renderWorld, originalModel, state, info.pos, ms, builder, true, random, 42,
OverlayTexture.DEFAULT_UV, EmptyModelData.INSTANCE);
ms.pop();
}
builder.finishDrawing(); ms.push();
renderWorld.clear(); ms.translate(pos.getX(), pos.getY(), pos.getZ());
renderWorld = null; MODEL_RENDERER.renderModel(renderWorld, BLOCK_MODELS.getModel(state), state, pos, ms, builder, true,
return builder; random, 42, OverlayTexture.DEFAULT_UV, EmptyModelData.INSTANCE);
} ms.pop();
public static int getLight(World world, float lx, float ly, float lz) {
BlockPos.Mutable pos = new BlockPos.Mutable();
float sky = 0, block = 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;
}
return ((int) sky) << 20 | ((int) block) << 4;
}
public static int getLightOnContraption(World world, PlacementSimulationWorld renderWorld, BlockPos pos, BlockPos lightPos) {
int worldLight = WorldRenderer.getLightmapCoordinates(world, lightPos);
if (renderWorld != null)
return getMaxBlockLight(worldLight, renderWorld.getLightLevel(LightType.BLOCK, pos));
return worldLight;
}
public static int getMaxBlockLight(int packedLight, int blockLightValue) {
int unpackedBlockLight = LightTexture.getBlockLightCoordinates(packedLight);
if (blockLightValue > unpackedBlockLight) {
packedLight = (packedLight & 0xFFFF0000) | (blockLightValue << 4);
}
return packedLight;
}
public static int getLightOnContraption(MovementContext context) {
int entityId = context.contraption.entity.getEntityId();
RenderedContraption renderedContraption = renderers.get(entityId);
if (renderedContraption != null) {
return renderedContraption.renderWorld.getLightLevel(LightType.BLOCK, context.localPos);
} else {
return -1;
} }
BlockModelRenderer.disableCache();
ForgeHooksClient.setRenderLayer(null);
builder.finishDrawing();
return builder;
}
public static int getLight(World world, float lx, float ly, float lz) {
BlockPos.Mutable pos = new BlockPos.Mutable();
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);
block += world.getLightLevel(LightType.BLOCK, pos) / 8f;
sky += world.getLightLevel(LightType.SKY, pos) / 8f;
}
return LightTexture.pack((int) block, (int) sky);
}
public static int getContraptionWorldLight(MovementContext context, PlacementSimulationWorld renderWorld) {
return WorldRenderer.getLightmapCoordinates(renderWorld, context.localPos);
} }
public static void invalidateAll() { public static void invalidateAll() {
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : RENDERERS.values()) {
renderer.invalidate(); renderer.invalidate();
} }
renderers.clear(); RENDERERS.clear();
WORLD_HOLDERS.clear();
} }
public static void removeDeadContraptions() { public static void removeDeadContraptions() {
renderers.values().removeIf(renderer -> { RENDERERS.values().removeIf(renderer -> {
if (renderer.isDead()) { if (renderer.isDead()) {
renderer.invalidate(); renderer.invalidate();
return true; return true;
@ -308,4 +313,8 @@ public class ContraptionRenderDispatcher {
return false; return false;
}); });
} }
public static void removeDeadHolders() {
WORLD_HOLDERS.values().removeIf(ContraptionWorldHolder::isDead);
}
} }

View File

@ -0,0 +1,22 @@
package com.simibubi.create.content.contraptions.components.structureMovement.render;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
public class ContraptionWorldHolder {
public final Contraption contraption;
public final PlacementSimulationWorld renderWorld;
public ContraptionWorldHolder(Contraption contraption, PlacementSimulationWorld renderWorld) {
this.contraption = contraption;
this.renderWorld = renderWorld;
}
public int getEntityId() {
return contraption.entity.getEntityId();
}
public boolean isDead() {
return !contraption.entity.isAlive();
}
}

View File

@ -11,7 +11,7 @@ import com.simibubi.create.foundation.utility.outliner.AABBOutline;
public class LightVolumeDebugger { public class LightVolumeDebugger {
public static void render(MatrixStack ms, SuperRenderTypeBuffer buffer) { public static void render(MatrixStack ms, SuperRenderTypeBuffer buffer) {
ContraptionRenderDispatcher.renderers.values() ContraptionRenderDispatcher.RENDERERS.values()
.stream() .stream()
.flatMap(r -> { .flatMap(r -> {
GridAlignedBB texture = r.getLighter().lightVolume.getTextureVolume(); GridAlignedBB texture = r.getLighter().lightVolume.getTextureVolume();

View File

@ -6,12 +6,9 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.lwjgl.opengl.GL11;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.core.IndexedModel; import com.jozufozu.flywheel.backend.core.IndexedModel;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
@ -25,147 +22,121 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Con
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo; import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.BlockModelRenderer;
import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Matrix4f; import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.gen.feature.template.Template;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.data.EmptyModelData;
public class RenderedContraption { public class RenderedContraption extends ContraptionWorldHolder {
public static final VertexFormat FORMAT = VertexFormat.builder() public static final VertexFormat FORMAT = VertexFormat.builder()
.addAttributes(ContraptionAttributes.class) .addAttributes(ContraptionAttributes.class)
.build(); .build();
private static final BlockModelRenderer MODEL_RENDERER = new BlockModelRenderer(Minecraft.getInstance().getBlockColors());
private static final BlockModelShapes BLOCK_MODELS = Minecraft.getInstance().getModelManager().getBlockModelShapes();
public Contraption contraption;
private final ContraptionLighter<?> lighter; private final ContraptionLighter<?> lighter;
public final ContraptionKineticRenderer kinetics; public final ContraptionKineticRenderer kinetics;
public final PlacementSimulationWorld renderWorld;
private final Map<RenderType, IndexedModel> renderLayers = new HashMap<>(); private final Map<RenderType, IndexedModel> renderLayers = new HashMap<>();
private Matrix4f model; private Matrix4f model;
private AxisAlignedBB lightBox; private AxisAlignedBB lightBox;
public RenderedContraption(World world, Contraption contraption) { public RenderedContraption(World world, PlacementSimulationWorld renderWorld, Contraption contraption) {
this.contraption = contraption; super(contraption, renderWorld);
this.lighter = contraption.makeLighter(); this.lighter = contraption.makeLighter();
this.kinetics = new ContraptionKineticRenderer(this); this.kinetics = new ContraptionKineticRenderer(this);
this.renderWorld = setupRenderWorld(world, contraption);
buildLayers(); buildLayers();
if (Backend.canUseInstancing()) { if (Backend.canUseInstancing()) {
buildInstancedTiles(); buildInstancedTiles();
buildActors(); buildActors();
} }
} }
public int getEntityId() { public ContraptionLighter<?> getLighter() {
return contraption.entity.getEntityId(); return lighter;
} }
public boolean isDead() { public void doRenderLayer(RenderType layer, ContraptionProgram shader) {
return !contraption.entity.isAlive();
}
public ContraptionLighter<?> getLighter() {
return lighter;
}
public void doRenderLayer(RenderType layer, ContraptionProgram shader) {
IndexedModel structure = renderLayers.get(layer); IndexedModel structure = renderLayers.get(layer);
if (structure != null) { if (structure != null) {
setup(shader); setup(shader);
structure.render(); structure.render();
teardown(); teardown();
} }
} }
public void beginFrame(ActiveRenderInfo info, double camX, double camY, double camZ) { public void beginFrame(ActiveRenderInfo info, double camX, double camY, double camZ) {
kinetics.beginFrame(info); kinetics.beginFrame(info);
AbstractContraptionEntity entity = contraption.entity; AbstractContraptionEntity entity = contraption.entity;
float pt = AnimationTickHolder.getPartialTicks(); float pt = AnimationTickHolder.getPartialTicks();
MatrixStack stack = new MatrixStack(); MatrixStack stack = new MatrixStack();
double x = MathHelper.lerp(pt, entity.lastTickPosX, entity.getX()) - camX; double x = MathHelper.lerp(pt, entity.lastTickPosX, entity.getX()) - camX;
double y = MathHelper.lerp(pt, entity.lastTickPosY, entity.getY()) - camY; double y = MathHelper.lerp(pt, entity.lastTickPosY, entity.getY()) - camY;
double z = MathHelper.lerp(pt, entity.lastTickPosZ, entity.getZ()) - camZ; double z = MathHelper.lerp(pt, entity.lastTickPosZ, entity.getZ()) - camZ;
stack.translate(x, y, z); stack.translate(x, y, z);
entity.doLocalTransforms(pt, new MatrixStack[]{ stack }); entity.doLocalTransforms(pt, new MatrixStack[] { stack });
model = stack.peek().getModel(); model = stack.peek().getModel();
AxisAlignedBB lightBox = GridAlignedBB.toAABB(lighter.lightVolume.getTextureVolume()); AxisAlignedBB lightBox = GridAlignedBB.toAABB(lighter.lightVolume.getTextureVolume());
this.lightBox = lightBox.offset(-camX, -camY, -camZ); this.lightBox = lightBox.offset(-camX, -camY, -camZ);
} }
void setup(ContraptionProgram shader) { void setup(ContraptionProgram shader) {
if (model == null || lightBox == null) return; if (model == null || lightBox == null) return;
shader.bind(model, lightBox); shader.bind(model, lightBox);
lighter.lightVolume.bind(); lighter.lightVolume.bind();
} }
void teardown() { void teardown() {
lighter.lightVolume.unbind(); lighter.lightVolume.unbind();
} }
void invalidate() { void invalidate() {
for (IndexedModel buffer : renderLayers.values()) { for (IndexedModel buffer : renderLayers.values()) {
buffer.delete(); buffer.delete();
} }
renderLayers.clear(); renderLayers.clear();
lighter.lightVolume.delete(); lighter.lightVolume.delete();
kinetics.invalidate(); kinetics.invalidate();
} }
private void buildLayers() { private void buildLayers() {
for (IndexedModel buffer : renderLayers.values()) { for (IndexedModel buffer : renderLayers.values()) {
buffer.delete(); buffer.delete();
} }
renderLayers.clear(); renderLayers.clear();
List<RenderType> blockLayers = RenderType.getBlockLayers(); List<RenderType> blockLayers = RenderType.getBlockLayers();
for (RenderType layer : blockLayers) { for (RenderType layer : blockLayers) {
IndexedModel layerModel = buildStructureModel(renderWorld, contraption, layer); IndexedModel layerModel = buildStructureModel(renderWorld, contraption, layer);
if (layerModel != null) renderLayers.put(layer, layerModel); if (layerModel != null) renderLayers.put(layer, layerModel);
} }
} }
private void buildInstancedTiles() { private void buildInstancedTiles() {
Collection<TileEntity> tileEntities = contraption.maybeInstancedTileEntities; Collection<TileEntity> tileEntities = contraption.maybeInstancedTileEntities;
if (!tileEntities.isEmpty()) { if (!tileEntities.isEmpty()) {
for (TileEntity te : tileEntities) { for (TileEntity te : tileEntities) {
if (te instanceof IInstanceRendered) { if (te instanceof IInstanceRendered) {
World world = te.getWorld(); World world = te.getWorld();
BlockPos pos = te.getPos(); BlockPos pos = te.getPos();
te.setLocation(renderWorld, pos); te.setLocation(renderWorld, pos);
kinetics.add(te); kinetics.add(te);
te.setLocation(world, pos); te.setLocation(world, pos);
} }
} }
@ -178,7 +149,7 @@ public class RenderedContraption {
@Nullable @Nullable
private static IndexedModel buildStructureModel(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) { private static IndexedModel buildStructureModel(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
BufferBuilderReader reader = new BufferBuilderReader(buildStructure(renderWorld, c, layer)); BufferBuilderReader reader = new BufferBuilderReader(ContraptionRenderDispatcher.buildStructure(renderWorld, c, layer));
int vertexCount = reader.getVertexCount(); int vertexCount = reader.getVertexCount();
if (vertexCount == 0) return null; if (vertexCount == 0) return null;
@ -218,52 +189,4 @@ public class RenderedContraption {
return new IndexedModel(format, to, vertexCount); return new IndexedModel(format, to, vertexCount);
} }
private static PlacementSimulationWorld setupRenderWorld(World world, Contraption c) {
PlacementSimulationWorld renderWorld = new PlacementSimulationWorld(world);
renderWorld.setTileEntities(c.presentTileEntities.values());
for (Template.BlockInfo info : c.getBlocks()
.values())
// Skip individual lighting updates to prevent lag with large contraptions
renderWorld.setBlockState(info.pos, info.state, 128);
renderWorld.updateLightSources();
renderWorld.lighter.tick(Integer.MAX_VALUE, false, false);
return renderWorld;
}
private static BufferBuilder buildStructure(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
MatrixStack ms = new MatrixStack();
Random random = new Random();
BufferBuilder builder = new BufferBuilder(DefaultVertexFormats.BLOCK.getIntegerSize());
builder.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
ForgeHooksClient.setRenderLayer(layer);
BlockModelRenderer.enableCache();
for (Template.BlockInfo info : c.getBlocks()
.values()) {
BlockState state = info.state;
if (state.getRenderType() != BlockRenderType.MODEL)
continue;
if (!RenderTypeLookup.canRenderInLayer(state, layer))
continue;
BlockPos pos = info.pos;
ms.push();
ms.translate(pos.getX(), pos.getY(), pos.getZ());
MODEL_RENDERER.renderModel(renderWorld, BLOCK_MODELS.getModel(state), state, pos, ms, builder, true,
random, 42, OverlayTexture.DEFAULT_UV, EmptyModelData.INSTANCE);
ms.pop();
}
BlockModelRenderer.disableCache();
ForgeHooksClient.setRenderLayer(null);
builder.finishDrawing();
return builder;
}
} }

View File

@ -78,8 +78,7 @@ public class SchematicRenderer {
SuperByteBuffer superByteBuffer = bufferCache.get(layer); SuperByteBuffer superByteBuffer = bufferCache.get(layer);
superByteBuffer.renderInto(ms, buffer.getBuffer(layer)); superByteBuffer.renderInto(ms, buffer.getBuffer(layer));
} }
TileEntityRenderHelper.renderTileEntities(schematic, schematic.getRenderedTileEntities(), ms, new MatrixStack(), TileEntityRenderHelper.renderTileEntities(schematic, schematic.getRenderedTileEntities(), ms, buffer);
buffer);
} }
protected void redraw(Minecraft minecraft) { protected void redraw(Minecraft minecraft) {

View File

@ -369,7 +369,7 @@ public class WorldSectionElement extends AnimatedSceneElement {
private void renderTileEntities(PonderWorld world, MatrixStack ms, IRenderTypeBuffer buffer, float pt) { private void renderTileEntities(PonderWorld world, MatrixStack ms, IRenderTypeBuffer buffer, float pt) {
loadTEsIfMissing(world); 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) { private SuperByteBuffer buildStructureBuffer(PonderWorld world, RenderType layer) {

View File

@ -3,14 +3,15 @@ package com.simibubi.create.foundation.render;
import com.jozufozu.flywheel.util.BufferBuilderReader; import com.jozufozu.flywheel.util.BufferBuilderReader;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder; import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.block.render.SpriteShiftEntry; import com.simibubi.create.foundation.block.render.SpriteShiftEntry;
import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.MatrixStacker;
import it.unimi.dsi.fastutil.longs.Long2DoubleMap; import it.unimi.dsi.fastutil.longs.Long2IntMap;
import it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder; 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.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -20,28 +21,29 @@ import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f; import net.minecraft.util.math.vector.Vector3f;
import net.minecraft.util.math.vector.Vector4f; import net.minecraft.util.math.vector.Vector4f;
import net.minecraft.world.LightType;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.client.model.pipeline.LightUtil; import net.minecraftforge.client.model.pipeline.LightUtil;
public class SuperByteBuffer { public class SuperByteBuffer {
private final BufferBuilderReader template; private final BufferBuilderReader template;
// Vertex Position // Vertex Position
private MatrixStack transforms; private MatrixStack transforms;
// Vertex Texture Coords // Vertex Texture Coords
private SpriteShiftFunc spriteShiftFunc; private SpriteShiftFunc spriteShiftFunc;
// Vertex Lighting
private boolean shouldLight;
private int packedLightCoords;
private int otherBlockLight;
private Matrix4f lightTransform;
// Vertex Coloring // Vertex Coloring
private boolean shouldColor; private boolean shouldColor;
private int r, g, b, a; 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) { public SuperByteBuffer(BufferBuilder buf) {
template = new BufferBuilderReader(buf); template = new BufferBuilderReader(buf);
@ -58,8 +60,7 @@ public class SuperByteBuffer {
return (v - sprite.getMinV()) / f * 16.0F; return (v - sprite.getMinV()) / f * 16.0F;
} }
private static final Long2DoubleMap skyLightCache = new Long2DoubleOpenHashMap(); private static final Long2IntMap WORLD_LIGHT_CACHE = new Long2IntOpenHashMap();
private static final Long2DoubleMap blockLightCache = new Long2DoubleOpenHashMap();
Vector4f pos = new Vector4f(); Vector4f pos = new Vector4f();
Vector3f normal = new Vector3f(); Vector3f normal = new Vector3f();
Vector4f lightPos = new Vector4f(); Vector4f lightPos = new Vector4f();
@ -71,7 +72,6 @@ public class SuperByteBuffer {
Matrix3f normalMat = transforms.peek() Matrix3f normalMat = transforms.peek()
.getNormal() .getNormal()
.copy(); .copy();
// normalMat.multiply(transforms.peek().getNormal());
Matrix4f modelMat = input.peek() Matrix4f modelMat = input.peek()
.getModel() .getModel()
@ -81,11 +81,11 @@ public class SuperByteBuffer {
.getModel(); .getModel();
modelMat.multiply(localTransforms); modelMat.multiply(localTransforms);
if (shouldLight && lightTransform != null) { if (useWorldLight) {
skyLightCache.clear(); WORLD_LIGHT_CACHE.clear();
blockLightCache.clear();
} }
boolean hasDefaultLight = packedLightCoords != 0;
float f = .5f; float f = .5f;
int vertexCount = template.getVertexCount(); int vertexCount = template.getVertexCount();
for (int i = 0; i < vertexCount; i++) { for (int i = 0; i < vertexCount; i++) {
@ -120,7 +120,12 @@ public class SuperByteBuffer {
int colorB = Math.min(255, (int) (((float) this.b) * instanceDiffuse)); int colorB = Math.min(255, (int) (((float) this.b) * instanceDiffuse));
builder.color(colorR, colorG, colorB, this.a); builder.color(colorR, colorG, colorB, this.a);
} else { } 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 colorR = Math.min(255, (int) (((float) Byte.toUnsignedInt(r)) * diffuseMult));
int colorG = Math.min(255, (int) (((float) Byte.toUnsignedInt(g)) * diffuseMult)); int colorG = Math.min(255, (int) (((float) Byte.toUnsignedInt(g)) * diffuseMult));
int colorB = Math.min(255, (int) (((float) Byte.toUnsignedInt(b)) * diffuseMult)); int colorB = Math.min(255, (int) (((float) Byte.toUnsignedInt(b)) * diffuseMult));
@ -135,32 +140,51 @@ public class SuperByteBuffer {
} else } else
builder.texture(u, v); builder.texture(u, v);
if (shouldLight) { int light;
int light = packedLightCoords; 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) { 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); lightPos.transform(lightTransform);
light = getLight(Minecraft.getInstance().world, lightPos);
if (otherBlockLight >= 0) {
light = ContraptionRenderDispatcher.getMaxBlockLight(light, otherBlockLight);
}
} }
light = getLight(Minecraft.getInstance().world, lightPos);
if (hasDefaultLight) {
light = maxLight(light, packedLightCoords);
}
} else if (hasDefaultLight) {
light = packedLightCoords;
} else {
light = template.getLight(i);
}
if (hybridLight) {
builder.light(maxLight(light, template.getLight(i)));
} else {
builder.light(light); builder.light(light);
} else }
builder.light(template.getLight(i));
builder.normal(nx, ny, nz) builder.normal(nx, ny, nz)
.endVertex(); .endVertex();
} }
transforms = new MatrixStack(); reset();
}
public SuperByteBuffer reset() {
transforms = new MatrixStack();
spriteShiftFunc = null; spriteShiftFunc = null;
shouldColor = false; shouldColor = false;
shouldLight = false; r = 0;
otherBlockLight = -1; g = 0;
b = 0;
a = 0;
disableDiffuseTransform = false;
useWorldLight = false;
hybridLight = false;
packedLightCoords = 0;
lightTransform = null;
return this;
} }
public MatrixStacker matrixStacker() { public MatrixStacker matrixStacker() {
@ -215,6 +239,20 @@ public class SuperByteBuffer {
.translate(-.5f, -.5f, -.5f); .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) { public SuperByteBuffer shiftUV(SpriteShiftEntry entry) {
this.spriteShiftFunc = (builder, u, v) -> { this.spriteShiftFunc = (builder, u, v) -> {
float targetU = entry.getTarget() float targetU = entry.getTarget()
@ -251,42 +289,45 @@ public class SuperByteBuffer {
return this; return this;
} }
public SuperByteBuffer light(int packedLightCoords) { public SuperByteBuffer light() {
shouldLight = true; useWorldLight = true;
lightTransform = null;
this.packedLightCoords = packedLightCoords;
return this; return this;
} }
public SuperByteBuffer light(Matrix4f lightTransform) { public SuperByteBuffer light(Matrix4f lightTransform) {
shouldLight = true; useWorldLight = true;
this.lightTransform = lightTransform; this.lightTransform = lightTransform;
return this; return this;
} }
public SuperByteBuffer light(Matrix4f lightTransform, int otherBlockLight) { public SuperByteBuffer light(int packedLightCoords) {
shouldLight = true; this.packedLightCoords = packedLightCoords;
this.lightTransform = lightTransform;
this.otherBlockLight = otherBlockLight;
return this; return this;
} }
public SuperByteBuffer color(int color) { public SuperByteBuffer light(Matrix4f lightTransform, int packedLightCoords) {
shouldColor = true; useWorldLight = true;
r = ((color >> 16) & 0xFF); this.lightTransform = lightTransform;
g = ((color >> 8) & 0xFF); this.packedLightCoords = packedLightCoords;
b = (color & 0xFF);
a = 255;
return this; return this;
} }
public SuperByteBuffer hybridLight() {
hybridLight = true;
return this;
}
public static int maxLight(int packedLight1, int packedLight2) {
int blockLight1 = LightTexture.getBlockLightCoordinates(packedLight1);
int skyLight1 = LightTexture.getSkyLightCoordinates(packedLight1);
int blockLight2 = LightTexture.getBlockLightCoordinates(packedLight2);
int skyLight2 = LightTexture.getSkyLightCoordinates(packedLight2);
return LightTexture.pack(Math.max(blockLight1, blockLight2), Math.max(skyLight1, skyLight2));
}
private static int getLight(World world, Vector4f lightPos) { private static int getLight(World world, Vector4f lightPos) {
BlockPos.Mutable pos = new BlockPos.Mutable(); BlockPos pos = new BlockPos(lightPos.getX(), lightPos.getY(), lightPos.getZ());
double sky = 0, block = 0; return WORLD_LIGHT_CACHE.computeIfAbsent(pos.toLong(), $ -> WorldRenderer.getLightmapCoordinates(world, pos));
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;
} }
public boolean isEmpty() { public boolean isEmpty() {
@ -298,4 +339,9 @@ public class SuperByteBuffer {
void shift(IVertexBuilder builder, float u, float v); void shift(IVertexBuilder builder, float u, float v);
} }
@FunctionalInterface
public interface IVertexLighter {
public int getPackedLight(float x, float y, float z);
}
} }

View File

@ -2,15 +2,17 @@ package com.simibubi.create.foundation.render;
import java.util.Iterator; import java.util.Iterator;
import javax.annotation.Nullable;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.Create; 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.config.AllConfigs;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer; import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
@ -23,28 +25,26 @@ import net.minecraft.world.World;
public class TileEntityRenderHelper { public class TileEntityRenderHelper {
public static void renderTileEntities(World world, Iterable<TileEntity> customRenderTEs, MatrixStack ms, public static void renderTileEntities(World world, Iterable<TileEntity> customRenderTEs, MatrixStack ms,
MatrixStack localTransform, IRenderTypeBuffer buffer) { IRenderTypeBuffer buffer) {
renderTileEntities(world, null, customRenderTEs, ms, localTransform, buffer); renderTileEntities(world, null, customRenderTEs, ms, null, buffer);
} }
public static void renderTileEntities(World world, Iterable<TileEntity> customRenderTEs, MatrixStack ms, public static void renderTileEntities(World world, Iterable<TileEntity> customRenderTEs, MatrixStack ms,
MatrixStack localTransform, IRenderTypeBuffer buffer, float pt) { IRenderTypeBuffer buffer, float pt) {
renderTileEntities(world, null, customRenderTEs, ms, localTransform, buffer, pt); renderTileEntities(world, null, customRenderTEs, ms, null, buffer, pt);
} }
public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, public static void renderTileEntities(World world, @Nullable PlacementSimulationWorld renderWorld,
Iterable<TileEntity> customRenderTEs, MatrixStack ms, MatrixStack localTransform, IRenderTypeBuffer buffer) { Iterable<TileEntity> customRenderTEs, MatrixStack ms, @Nullable Matrix4f lightTransform, IRenderTypeBuffer buffer) {
renderTileEntities(world, renderWorld, customRenderTEs, ms, localTransform, buffer, renderTileEntities(world, renderWorld, customRenderTEs, ms, lightTransform, buffer,
AnimationTickHolder.getPartialTicks()); AnimationTickHolder.getPartialTicks());
} }
public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, public static void renderTileEntities(World world, @Nullable PlacementSimulationWorld renderWorld,
Iterable<TileEntity> customRenderTEs, MatrixStack ms, MatrixStack localTransform, IRenderTypeBuffer buffer, Iterable<TileEntity> customRenderTEs, MatrixStack ms, @Nullable Matrix4f lightTransform, IRenderTypeBuffer buffer,
float pt) { float pt) {
Matrix4f matrix = localTransform.peek() Iterator<TileEntity> iterator = customRenderTEs.iterator();
.getModel(); while (iterator.hasNext()) {
for (Iterator<TileEntity> iterator = customRenderTEs.iterator(); iterator.hasNext();) {
TileEntity tileEntity = iterator.next(); TileEntity tileEntity = iterator.next();
// if (tileEntity instanceof IInstanceRendered) continue; // TODO: some things still need to render // if (tileEntity instanceof IInstanceRendered) continue; // TODO: some things still need to render
@ -60,10 +60,15 @@ public class TileEntityRenderHelper {
.translate(pos); .translate(pos);
try { try {
Vector4f vec = new Vector4f(pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, 1); BlockPos lightPos;
vec.transform(matrix); if (lightTransform != null) {
BlockPos lightPos = new BlockPos(vec.getX(), vec.getY(), vec.getZ()); Vector4f lightVec = new Vector4f(pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, 1);
int worldLight = ContraptionRenderDispatcher.getLightOnContraption(world, renderWorld, pos, lightPos); 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); renderer.render(tileEntity, pt, ms, buffer, worldLight, OverlayTexture.DEFAULT_UV);
} catch (Exception e) { } 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;
}
} }