Method to invalidate contraption renderers

- Call ContraptionRenderDispatcher#invalidate, at some point during a tick, and the structure will be reset.
 - Move contraption compartment to SBBContraptionManager
 - Contraption compartment uses custom pair with better hash function instead of apache commons pair
This commit is contained in:
Jozufozu 2021-08-15 15:10:35 -07:00
parent 5555f9e6bb
commit f1701ae784
7 changed files with 46 additions and 14 deletions

View file

@ -7,6 +7,7 @@ import java.util.function.Function;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer; import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher;
import com.simibubi.create.content.contraptions.components.structureMovement.render.SBBContraptionManager;
import com.simibubi.create.content.contraptions.relays.encased.CasingConnectivity; import com.simibubi.create.content.contraptions.relays.encased.CasingConnectivity;
import com.simibubi.create.content.curiosities.armor.CopperBacktankArmorLayer; import com.simibubi.create.content.curiosities.armor.CopperBacktankArmorLayer;
import com.simibubi.create.content.curiosities.bell.SoulPulseEffectHandler; import com.simibubi.create.content.curiosities.bell.SoulPulseEffectHandler;
@ -94,7 +95,7 @@ public class CreateClient {
public static void clientInit(FMLClientSetupEvent event) { public static void clientInit(FMLClientSetupEvent event) {
BUFFER_CACHE.registerCompartment(KineticTileEntityRenderer.KINETIC_TILE); BUFFER_CACHE.registerCompartment(KineticTileEntityRenderer.KINETIC_TILE);
BUFFER_CACHE.registerCompartment(ContraptionRenderDispatcher.CONTRAPTION, 20); BUFFER_CACHE.registerCompartment(SBBContraptionManager.CONTRAPTION, 20);
BUFFER_CACHE.registerCompartment(WorldSectionElement.DOC_WORLD_SECTION, 20); BUFFER_CACHE.registerCompartment(WorldSectionElement.DOC_WORLD_SECTION, 20);
AllKeys.register(); AllKeys.register();

View file

@ -44,7 +44,16 @@ public class ContraptionRenderDispatcher {
private static WorldAttached<ContraptionRenderManager<?>> WORLDS = new WorldAttached<>(SBBContraptionManager::new); private static WorldAttached<ContraptionRenderManager<?>> WORLDS = new WorldAttached<>(SBBContraptionManager::new);
public static final Compartment<Pair<Contraption, RenderType>> CONTRAPTION = new Compartment<>(); /**
* Reset a contraption's renderer.
* @param contraption The contraption to invalidate.
* @return true if there was a renderer associated with the given contraption.
*/
public static boolean invalidate(Contraption contraption) {
World level = contraption.entity.level;
return WORLDS.get(level).invalidate(contraption);
}
public static void tick(World world) { public static void tick(World world) {
if (Minecraft.getInstance().isPaused()) return; if (Minecraft.getInstance().isPaused()) return;

View file

@ -68,4 +68,8 @@ public class ContraptionRenderInfo {
public ContraptionMatrices getMatrices() { public ContraptionMatrices getMatrices() {
return matrices; return matrices;
} }
public void invalidate() {
}
} }

View file

@ -29,6 +29,20 @@ public abstract class ContraptionRenderManager<C extends ContraptionRenderInfo>
this.world = (World) world; this.world = (World) world;
} }
public boolean invalidate(Contraption contraption) {
int entityId = contraption.entity.getId();
C removed = renderInfos.remove(entityId);
if (removed != null) {
removed.invalidate();
visible.remove(removed);
return true;
}
return false;
}
public void renderLayer(RenderLayerEvent event) { public void renderLayer(RenderLayerEvent event) {
for (C c : visible) { for (C c : visible) {
c.setupMatrices(event.stack, event.camX, event.camY, event.camZ); c.setupMatrices(event.stack, event.camX, event.camY, event.camZ);
@ -85,6 +99,9 @@ public abstract class ContraptionRenderManager<C extends ContraptionRenderInfo>
} }
public void delete() { public void delete() {
for (C renderer : renderInfos.values()) {
renderer.invalidate();
}
renderInfos.clear(); renderInfos.clear();
} }

View file

@ -94,12 +94,4 @@ public class FlwContraptionManager extends ContraptionRenderManager<RenderedCont
// we use visible in #tick() so we have to re-evaluate it if any were removed // we use visible in #tick() so we have to re-evaluate it if any were removed
if (removed) collectVisible(); if (removed) collectVisible();
} }
@Override
public void delete() {
for (RenderedContraption renderer : renderInfos.values()) {
renderer.invalidate();
}
renderInfos.clear();
}
} }

View file

@ -105,7 +105,7 @@ public class RenderedContraption extends ContraptionRenderInfo {
lighter.lightVolume.bind(); lighter.lightVolume.bind();
} }
void invalidate() { public void invalidate() {
for (ModelRenderer buffer : renderLayers.values()) { for (ModelRenderer buffer : renderLayers.values()) {
buffer.delete(); buffer.delete();
} }

View file

@ -1,20 +1,21 @@
package com.simibubi.create.content.contraptions.components.structureMovement.render; package com.simibubi.create.content.contraptions.components.structureMovement.render;
import static com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher.CONTRAPTION;
import static com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher.buildStructureBuffer; import static com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher.buildStructureBuffer;
import org.apache.commons.lang3.tuple.Pair;
import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.event.RenderLayerEvent;
import com.simibubi.create.CreateClient; import com.simibubi.create.CreateClient;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption;
import com.simibubi.create.foundation.render.Compartment;
import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBuffer;
import com.simibubi.create.foundation.utility.Pair;
import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.IWorld; import net.minecraft.world.IWorld;
public class SBBContraptionManager extends ContraptionRenderManager<ContraptionRenderInfo> { public class SBBContraptionManager extends ContraptionRenderManager<ContraptionRenderInfo> {
public static final Compartment<Pair<Contraption, RenderType>> CONTRAPTION = new Compartment<>();
public SBBContraptionManager(IWorld world) { public SBBContraptionManager(IWorld world) {
super(world); super(world);
} }
@ -25,6 +26,14 @@ public class SBBContraptionManager extends ContraptionRenderManager<ContraptionR
visible.forEach(info -> renderContraptionLayerSBB(event, info)); visible.forEach(info -> renderContraptionLayerSBB(event, info));
} }
@Override
public boolean invalidate(Contraption contraption) {
for (RenderType chunkBufferLayer : RenderType.chunkBufferLayers()) {
CreateClient.BUFFER_CACHE.invalidate(CONTRAPTION, Pair.of(contraption, chunkBufferLayer));
}
return super.invalidate(contraption);
}
@Override @Override
protected ContraptionRenderInfo create(Contraption c) { protected ContraptionRenderInfo create(Contraption c) {
PlacementSimulationWorld renderWorld = ContraptionRenderDispatcher.setupRenderWorld(world, c); PlacementSimulationWorld renderWorld = ContraptionRenderDispatcher.setupRenderWorld(world, c);