Add lighting to non-Flywheel contraptions

- Pass contraption world to movement behavior rendering
- Add hybrid lighting option to SuperByteBuffer (takes max of world light and vertex light)
- Create ContraptionWorldHolder for caching contraption worlds for contraptions that are not rendered with Flywheel
- Refactor some parts of contraption rendering
This commit is contained in:
PepperBell 2021-05-15 00:21:20 -07:00
parent 8efe8ed01e
commit 67052fd00c
18 changed files with 266 additions and 268 deletions

View file

@ -8,6 +8,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov
import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionKineticRenderer;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -34,10 +35,10 @@ public class DrillMovementBehaviour extends BlockBreakingMovementBehaviour {
@Override
@OnlyIn(value = Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
if (!Backend.canUseInstancing())
DrillRenderer.renderInContraption(context, ms, msLocal, buffer);
DrillRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffer);
}
@Override

View file

@ -5,12 +5,14 @@ import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -29,8 +31,8 @@ public class DrillRenderer extends KineticTileEntityRenderer {
return PartialBufferer.getFacing(AllBlockPartials.DRILL_HEAD, te.getBlockState());
}
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal};
BlockState state = context.state;
SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.DRILL_HEAD, state);
@ -51,8 +53,8 @@ public class DrillRenderer extends KineticTileEntityRenderer {
.unCentre();
superBuffer
.light(msLocal.peek()
.getModel())
.light(msLocal.peek().getModel(),
ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, buffer.getBuffer(RenderType.getSolid()));
}

View file

@ -14,6 +14,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.ren
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionKineticRenderer;
import com.simibubi.create.foundation.utility.BlockHelper;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
@ -52,10 +53,10 @@ public class HarvesterMovementBehaviour extends MovementBehaviour {
}
@Override
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffers) {
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) {
if (!Backend.canUseInstancing())
HarvesterRenderer.renderInContraption(context, ms, msLocal, buffers);
HarvesterRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffers);
}
@Override

View file

@ -12,6 +12,7 @@ import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -39,8 +40,8 @@ public class HarvesterRenderer extends SafeTileEntityRenderer<HarvesterTileEntit
.renderInto(ms, buffer.getBuffer(RenderType.getCutoutMipped()));
}
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffers) {
public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) {
BlockState blockState = context.state;
Direction facing = blockState.get(HORIZONTAL_FACING);
SuperByteBuffer superBuffer = PartialBufferer.get(AllBlockPartials.HARVESTER_BLADE, blockState);
@ -52,8 +53,9 @@ public class HarvesterRenderer extends SafeTileEntityRenderer<HarvesterTileEntit
transform(context.world, facing, superBuffer, speed);
superBuffer.light(msLocal.peek()
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context))
superBuffer
.light(msLocal.peek().getModel(),
ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, buffers.getBuffer(RenderType.getCutoutMipped()));
}

View file

@ -6,6 +6,7 @@ import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -31,9 +32,9 @@ public class PortableStorageInterfaceMovement extends MovementBehaviour {
@Override
@OnlyIn(Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
PortableStorageInterfaceRenderer.renderInContraption(context, ms, msLocal, buffer);
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
PortableStorageInterfaceRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffer);
}
@Override

View file

@ -15,6 +15,7 @@ import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -41,8 +42,8 @@ public class PortableStorageInterfaceRenderer extends SafeTileEntityRenderer<Por
.renderInto(ms, vb), ms);
}
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
BlockState blockState = context.state;
PortableStorageInterfaceTileEntity te = getTargetPSI(context);
IVertexBuilder vb = buffer.getBuffer(RenderType.getSolid());
@ -56,7 +57,7 @@ public class PortableStorageInterfaceRenderer extends SafeTileEntityRenderer<Por
}
render(blockState, progress, lit, sbb -> sbb.light(msLocal.peek()
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context))
.getModel(), ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, vb), ms, msLocal);
}

View file

@ -1,5 +1,6 @@
package com.simibubi.create.content.contraptions.components.actors;
import com.jozufozu.flywheel.backend.Backend;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.content.contraptions.components.saw.SawBlock;
import com.simibubi.create.content.contraptions.components.saw.SawRenderer;
@ -7,6 +8,7 @@ import com.simibubi.create.content.contraptions.components.saw.SawTileEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext;
import com.simibubi.create.foundation.utility.TreeCutter;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -75,9 +77,9 @@ public class SawMovementBehaviour extends BlockBreakingMovementBehaviour {
@Override
@OnlyIn(value = Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
SawRenderer.renderInContraption(context, ms, msLocal, buffer);
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
SawRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffer);
}
@Override

View file

@ -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.NBTHelper;
import com.simibubi.create.foundation.utility.NBTProcessors;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.IRenderTypeBuffer;
@ -254,10 +255,10 @@ public class DeployerMovementBehaviour extends MovementBehaviour {
}
@Override
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffers) {
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) {
if (!Backend.canUseInstancing())
DeployerRenderer.renderInContraption(context, ms, msLocal, buffers);
DeployerRenderer.renderInContraption(context, renderWorld, ms, msLocal, buffers);
}
@Override

View file

@ -21,6 +21,7 @@ import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
@ -154,8 +155,8 @@ public class DeployerRenderer extends SafeTileEntityRenderer<DeployerTileEntity>
return buffer;
}
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal};
IVertexBuilder builder = buffer.getBuffer(RenderType.getSolid());
BlockState blockState = context.state;
@ -187,9 +188,9 @@ public class DeployerRenderer extends SafeTileEntityRenderer<DeployerTileEntity>
.getModel();
for (MatrixStack m : matrixStacks)
m.translate(offset.x, offset.y, offset.z);
pole.light(lighting, ContraptionRenderDispatcher.getLightOnContraption(context))
pole.light(lighting, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, builder);
hand.light(lighting, ContraptionRenderDispatcher.getLightOnContraption(context))
hand.light(lighting, ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, builder);
}

View file

@ -18,6 +18,7 @@ import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
@ -151,8 +152,8 @@ public class SawRenderer extends SafeTileEntityRenderer<SawTileEntity> {
return KineticTileEntityRenderer.shaft(KineticTileEntityRenderer.getRotationAxisOf(te));
}
public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
public static void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[] { ms, msLocal };
BlockState state = context.state;
SuperByteBuffer superBuffer;
@ -196,8 +197,8 @@ public class SawRenderer extends SafeTileEntityRenderer<SawTileEntity> {
}
superBuffer
.light(msLocal.peek()
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context))
.light(msLocal.peek().getModel(),
ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, buffer.getBuffer(RenderType.getCutoutMipped()));
}

View file

@ -5,6 +5,7 @@ import javax.annotation.Nullable;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ActorInstance;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionKineticRenderer;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.entity.item.ItemEntity;
@ -60,8 +61,8 @@ public abstract class MovementBehaviour {
}
@OnlyIn(Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {}
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {}
@OnlyIn(Dist.CLIENT)
@Nullable

View file

@ -17,6 +17,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.ren
import com.simibubi.create.foundation.render.PartialBufferer;
import com.simibubi.create.foundation.render.SuperByteBuffer;
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.RenderType;
@ -31,8 +32,8 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour {
@Override
@OnlyIn(Dist.CLIENT)
public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
if (Backend.canUseInstancing()) return;
Direction facing = context.state.get(BlockStateProperties.FACING);
@ -55,9 +56,10 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour {
superBuffer.rotateCentered(orientation);
// render
superBuffer.light(msLocal.peek()
.getModel(), ContraptionRenderDispatcher.getLightOnContraption(context));
superBuffer.renderInto(ms, buffer.getBuffer(RenderType.getSolid()));
superBuffer
.light(msLocal.peek().getModel(),
ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld))
.renderInto(ms, buffer.getBuffer(RenderType.getSolid()));
}
@Override

View file

@ -1,17 +1,17 @@
package com.simibubi.create.content.contraptions.components.structureMovement.render;
import static org.lwjgl.opengl.GL13.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_TEXTURE4;
import static org.lwjgl.opengl.GL13.GL_TEXTURE_3D;
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.Random;
import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.opengl.GL11;
import com.jozufozu.flywheel.backend.Backend;
import com.mojang.blaze3d.matrix.MatrixStack;
@ -36,7 +36,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo;
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.GameRenderer;
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.RenderTypeLookup;
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.vertex.DefaultVertexFormats;
import net.minecraft.client.world.ClientWorld;
@ -57,25 +56,34 @@ import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.data.EmptyModelData;
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 ticksUntilHolderRefresh;
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<>();
protected static PlacementSimulationWorld renderWorld;
public static void tick() {
if (Minecraft.getInstance().isGamePaused()) return;
for (RenderedContraption contraption : renderers.values()) {
for (RenderedContraption contraption : RENDERERS.values()) {
contraption.getLighter().tick(contraption);
contraption.kinetics.tick();
}
if (ticksUntilHolderRefresh <= 0) {
removeDeadHolders();
ticksUntilHolderRefresh = 20;
}
}
public static void beginFrame(ClientWorld world, MatrixStack stack, ActiveRenderInfo info, GameRenderer gameRenderer, LightTexture lightTexture) {
double camX = info.getProjectedView().x;
double camY = info.getProjectedView().y;
double camZ = info.getProjectedView().z;
for (RenderedContraption renderer : renderers.values()) {
for (RenderedContraption renderer : RENDERERS.values()) {
renderer.beginFrame(info, camX, camY, camZ);
}
}
@ -83,7 +91,7 @@ public class ContraptionRenderDispatcher {
public static void renderLayer(ClientWorld world, RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ) {
removeDeadContraptions();
if (renderers.isEmpty()) return;
if (RENDERERS.isEmpty()) return;
layer.startDrawing();
glEnable(GL_TEXTURE_3D);
@ -96,13 +104,13 @@ public class ContraptionRenderDispatcher {
structureShader.uploadViewProjection(viewProjection);
structureShader.uploadCameraPos(camX, camY, camZ);
for (RenderedContraption renderer : renderers.values()) {
for (RenderedContraption renderer : RENDERERS.values()) {
renderer.doRenderLayer(layer, structureShader);
}
}
if (Backend.canUseInstancing()) {
for (RenderedContraption renderer : renderers.values()) {
for (RenderedContraption renderer : RENDERERS.values()) {
renderer.kinetics.render(layer, viewProjection, camX, camY, camZ, renderer::setup);
renderer.teardown();
}
@ -113,70 +121,81 @@ public class ContraptionRenderDispatcher {
glActiveTexture(GL_TEXTURE0);
}
public static void render(AbstractContraptionEntity entity, MatrixStack ms, IRenderTypeBuffer buffers,
MatrixStack msLocal, Contraption contraption) {
World world = entity.world;
if (Backend.canUseVBOs() && Backend.isFlywheelWorld(world)) {
RenderedContraption renderer = getRenderer(world, contraption);
PlacementSimulationWorld renderWorld = renderer.renderWorld;
ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, ms, msLocal, buffers);
} else {
ContraptionWorldHolder holder = getWorldHolder(world, contraption);
PlacementSimulationWorld renderWorld = holder.renderWorld;
ContraptionRenderDispatcher.renderDynamic(world, renderWorld, contraption, ms, msLocal, buffers);
ContraptionRenderDispatcher.renderStructure(world, renderWorld, contraption, ms, msLocal, buffers);
}
}
private static RenderedContraption getRenderer(World world, Contraption c) {
int entityId = c.entity.getEntityId();
RenderedContraption contraption = renderers.get(entityId);
RenderedContraption contraption = RENDERERS.get(entityId);
if (contraption == null) {
contraption = new RenderedContraption(world, c);
renderers.put(entityId, contraption);
PlacementSimulationWorld renderWorld = setupRenderWorld(world, c);
contraption = new RenderedContraption(world, renderWorld, c);
RENDERERS.put(entityId, contraption);
}
return contraption;
}
public static void render(AbstractContraptionEntity entity, MatrixStack ms, IRenderTypeBuffer buffers,
MatrixStack msLocal, Contraption contraption) {
if (Backend.canUseVBOs() && Backend.isFlywheelWorld(entity.world)) {
ContraptionRenderDispatcher.renderDynamic(entity.world, contraption, ms, msLocal, buffers);
} else {
ContraptionRenderDispatcher.renderDynamic(entity.world, contraption, ms, msLocal, buffers);
ContraptionRenderDispatcher.renderStructure(entity.world, contraption, ms, msLocal, buffers);
}
private static ContraptionWorldHolder getWorldHolder(World world, Contraption c) {
int entityId = c.entity.getEntityId();
ContraptionWorldHolder holder = WORLD_HOLDERS.get(entityId);
if (holder == null) {
PlacementSimulationWorld renderWorld = setupRenderWorld(world, c);
holder = new ContraptionWorldHolder(c, renderWorld);
WORLD_HOLDERS.put(entityId, holder);
}
public static void renderStructure(World world, Contraption c, MatrixStack ms, MatrixStack msLocal,
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(c, layer));
if (contraptionBuffer.isEmpty())
continue;
Matrix4f model = msLocal.peek()
.getModel();
contraptionBuffer.light(model)
.renderInto(ms, buffer.getBuffer(layer));
}
return holder;
}
public static void renderDynamic(World world, Contraption c, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
renderTileEntities(world, c, ms, msLocal, buffer);
public 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;
}
public static void renderDynamic(World world, PlacementSimulationWorld renderWorld, Contraption c,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
renderTileEntities(world, renderWorld, c, ms, msLocal, buffer);
if (buffer instanceof IRenderTypeBuffer.Impl)
((IRenderTypeBuffer.Impl) buffer).draw();
renderActors(world, c, ms, msLocal, buffer);
renderActors(world, renderWorld, c, ms, msLocal, buffer);
}
public static void renderTileEntities(World world, Contraption c, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
PlacementSimulationWorld renderWorld = null;
if (Backend.canUseVBOs() && Backend.isFlywheelWorld(world)) {
RenderedContraption renderer = getRenderer(world, c);
renderWorld = renderer.renderWorld;
}
public static void renderTileEntities(World world, PlacementSimulationWorld renderWorld, Contraption c,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
TileEntityRenderHelper.renderTileEntities(world, renderWorld, c.specialRenderedTileEntities, ms, msLocal, buffer);
}
protected static void renderActors(World world, Contraption c, MatrixStack ms, MatrixStack msLocal,
IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[]{ms, msLocal};
protected static void renderActors(World world, PlacementSimulationWorld renderWorld, Contraption c,
MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) {
MatrixStack[] matrixStacks = new MatrixStack[] {ms, msLocal};
for (Pair<Template.BlockInfo, MovementContext> actor : c.getActors()) {
MovementContext context = actor.getRight();
if (context == null)
@ -192,56 +211,67 @@ public class ContraptionRenderDispatcher {
MovementBehaviour movementBehaviour = AllMovementBehaviours.of(blockInfo.state);
if (movementBehaviour != null)
movementBehaviour.renderInContraption(context, ms, msLocal, buffer);
movementBehaviour.renderInContraption(context, renderWorld, ms, msLocal, buffer);
for (MatrixStack m : matrixStacks)
m.pop();
}
}
private static SuperByteBuffer buildStructureBuffer(Contraption c, RenderType layer) {
BufferBuilder builder = buildStructure(c, layer);
public static void renderStructure(World world, PlacementSimulationWorld renderWorld, Contraption c,
MatrixStack ms, MatrixStack msLocal, 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;
Matrix4f model = msLocal.peek()
.getModel();
contraptionBuffer.light(model)
.hybridLight()
.renderInto(ms, buffer.getBuffer(layer));
}
}
private static SuperByteBuffer buildStructureBuffer(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
BufferBuilder builder = buildStructure(renderWorld, c, layer);
return new SuperByteBuffer(builder);
}
public static BufferBuilder buildStructure(Contraption c, RenderType layer) {
if (renderWorld == null || renderWorld.getWorld() != Minecraft.getInstance().world)
renderWorld = new PlacementSimulationWorld(Minecraft.getInstance().world);
ForgeHooksClient.setRenderLayer(layer);
public static BufferBuilder buildStructure(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
MatrixStack ms = new MatrixStack();
BlockRendererDispatcher dispatcher = Minecraft.getInstance()
.getBlockRendererDispatcher();
BlockModelRenderer blockRenderer = dispatcher.getBlockModelRenderer();
Random random = new Random();
BufferBuilder builder = new BufferBuilder(DefaultVertexFormats.BLOCK.getIntegerSize());
builder.begin(GL_QUADS, DefaultVertexFormats.BLOCK);
renderWorld.setTileEntities(c.presentTileEntities.values());
for (Template.BlockInfo info : c.getBlocks()
.values())
renderWorld.setBlockState(info.pos, info.state);
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.ENTITYBLOCK_ANIMATED)
if (state.getRenderType() != BlockRenderType.MODEL)
continue;
if (!RenderTypeLookup.canRenderInLayer(state, layer))
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.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();
renderWorld.clear();
renderWorld = null;
return builder;
}
@ -261,46 +291,32 @@ public class ContraptionRenderDispatcher {
return ((int) sky) << 20 | ((int) block) << 4;
}
public static int getLightOnContraption(World world, PlacementSimulationWorld renderWorld, BlockPos pos, BlockPos lightPos) {
public static int getContraptionLightAt(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));
if (renderWorld != null) {
int renderWorldLight = WorldRenderer.getLightmapCoordinates(renderWorld, pos);
return SuperByteBuffer.maxLight(worldLight, renderWorldLight);
}
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;
}
public static int getContraptionWorldLight(MovementContext context, PlacementSimulationWorld renderWorld) {
return WorldRenderer.getLightmapCoordinates(renderWorld, context.localPos);
}
public static void invalidateAll() {
for (RenderedContraption renderer : renderers.values()) {
for (RenderedContraption renderer : RENDERERS.values()) {
renderer.invalidate();
}
renderers.clear();
RENDERERS.clear();
WORLD_HOLDERS.clear();
}
public static void removeDeadContraptions() {
renderers.values().removeIf(renderer -> {
RENDERERS.values().removeIf(renderer -> {
if (renderer.isDead()) {
renderer.invalidate();
return true;
@ -308,4 +324,8 @@ public class ContraptionRenderDispatcher {
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 static void render(MatrixStack ms, SuperRenderTypeBuffer buffer) {
ContraptionRenderDispatcher.renderers.values()
ContraptionRenderDispatcher.RENDERERS.values()
.stream()
.flatMap(r -> {
GridAlignedBB texture = r.getLighter().lightVolume.getTextureVolume();

View file

@ -4,9 +4,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.instancing.IInstanceRendered;
@ -18,46 +15,29 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Con
import com.simibubi.create.foundation.utility.AnimationTickHolder;
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.BlockModelRenderer;
import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.BufferBuilder;
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.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Matrix4f;
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 {
private static final BlockModelRenderer MODEL_RENDERER = new BlockModelRenderer(Minecraft.getInstance().getBlockColors());
private static final BlockModelShapes BLOCK_MODELS = Minecraft.getInstance().getModelManager().getBlockModelShapes();
public Contraption contraption;
public class RenderedContraption extends ContraptionWorldHolder {
private final ContraptionLighter<?> lighter;
public final ContraptionKineticRenderer kinetics;
public final PlacementSimulationWorld renderWorld;
private final Map<RenderType, ContraptionModel> renderLayers = new HashMap<>();
private Matrix4f model;
private AxisAlignedBB lightBox;
public RenderedContraption(World world, Contraption contraption) {
this.contraption = contraption;
public RenderedContraption(World world, PlacementSimulationWorld renderWorld, Contraption contraption) {
super(contraption, renderWorld);
this.lighter = contraption.makeLighter();
this.kinetics = new ContraptionKineticRenderer(this);
this.renderWorld = setupRenderWorld(world, contraption);
buildLayers();
if (Backend.canUseInstancing()) {
@ -66,14 +46,6 @@ public class RenderedContraption {
}
}
public int getEntityId() {
return contraption.entity.getEntityId();
}
public boolean isDead() {
return !contraption.entity.isAlive();
}
public ContraptionLighter<?> getLighter() {
return lighter;
}
@ -164,55 +136,7 @@ public class RenderedContraption {
}
private static ContraptionModel buildStructureModel(PlacementSimulationWorld renderWorld, Contraption c, RenderType layer) {
BufferBuilder builder = buildStructure(renderWorld, c, layer);
BufferBuilder builder = ContraptionRenderDispatcher.buildStructure(renderWorld, c, layer);
return new ContraptionModel(builder);
}
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

@ -5,7 +5,6 @@ import java.nio.ByteBuffer;
import com.mojang.blaze3d.matrix.MatrixStack;
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.utility.MatrixStacker;
@ -13,6 +12,7 @@ import it.unimi.dsi.fastutil.longs.Long2DoubleMap;
import it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
@ -40,8 +40,8 @@ public class SuperByteBuffer extends TemplateBuffer {
// Vertex Lighting
private boolean shouldLight;
private boolean hybridLight;
private int packedLightCoords;
private int otherBlockLight;
private Matrix4f lightTransform;
// Vertex Coloring
@ -149,11 +149,14 @@ public class SuperByteBuffer extends TemplateBuffer {
lightPos.transform(localTransforms);
lightPos.transform(lightTransform);
light = getLight(Minecraft.getInstance().world, lightPos);
if (otherBlockLight >= 0) {
light = ContraptionRenderDispatcher.getMaxBlockLight(light, otherBlockLight);
int worldLight = getLight(Minecraft.getInstance().world, lightPos);
if (light >= 0) {
light = maxLight(worldLight, light);
}
}
if (hybridLight)
builder.light(maxLight(light, getLight(buffer, i)));
else
builder.light(light);
} else
builder.light(getLight(buffer, i));
@ -167,7 +170,7 @@ public class SuperByteBuffer extends TemplateBuffer {
spriteShiftFunc = null;
shouldColor = false;
shouldLight = false;
otherBlockLight = -1;
hybridLight = false;
}
public MatrixStacker matrixStacker() {
@ -271,10 +274,15 @@ public class SuperByteBuffer extends TemplateBuffer {
return this;
}
public SuperByteBuffer light(Matrix4f lightTransform, int otherBlockLight) {
public SuperByteBuffer light(Matrix4f lightTransform, int packedLightCoords) {
shouldLight = true;
this.lightTransform = lightTransform;
this.otherBlockLight = otherBlockLight;
this.packedLightCoords = packedLightCoords;
return this;
}
public SuperByteBuffer hybridLight() {
hybridLight = true;
return this;
}
@ -287,6 +295,14 @@ public class SuperByteBuffer extends TemplateBuffer {
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) {
BlockPos.Mutable pos = new BlockPos.Mutable();
double sky = 0, block = 0;

View file

@ -63,7 +63,7 @@ public class TileEntityRenderHelper {
Vector4f vec = new Vector4f(pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, 1);
vec.transform(matrix);
BlockPos lightPos = new BlockPos(vec.getX(), vec.getY(), vec.getZ());
int worldLight = ContraptionRenderDispatcher.getLightOnContraption(world, renderWorld, pos, lightPos);
int worldLight = ContraptionRenderDispatcher.getContraptionLightAt(world, renderWorld, pos, lightPos);
renderer.render(tileEntity, pt, ms, buffer, worldLight, OverlayTexture.DEFAULT_UV);
} catch (Exception e) {