Stop relying on IDynamicInstance#beginFrame to setup instance state.

- Gantry carriages no longer update when not necessary.
This commit is contained in:
JozsefA 2021-03-26 22:59:47 -07:00
parent d3d338e64b
commit 4675e0ad15
10 changed files with 88 additions and 48 deletions

View file

@ -14,11 +14,13 @@ import net.minecraft.util.Direction;
public class HandCrankInstance extends SingleRotatingInstance implements IDynamicInstance { public class HandCrankInstance extends SingleRotatingInstance implements IDynamicInstance {
private final HandCrankTileEntity tile;
private InstanceKey<ModelData> crank; private InstanceKey<ModelData> crank;
private Direction facing; private Direction facing;
public HandCrankInstance(InstancedTileRenderer<?> modelManager, KineticTileEntity tile) { public HandCrankInstance(InstancedTileRenderer<?> modelManager, HandCrankTileEntity tile) {
super(modelManager, tile); super(modelManager, tile);
this.tile = tile;
Block block = blockState.getBlock(); Block block = blockState.getBlock();
AllBlockPartials renderedHandle = null; AllBlockPartials renderedHandle = null;
@ -30,16 +32,20 @@ public class HandCrankInstance extends SingleRotatingInstance implements IDynami
facing = blockState.get(BlockStateProperties.FACING); facing = blockState.get(BlockStateProperties.FACING);
InstancedModel<ModelData> model = renderedHandle.renderOnDirectionalSouthModel(modelManager, blockState, facing.getOpposite()); InstancedModel<ModelData> model = renderedHandle.renderOnDirectionalSouthModel(modelManager, blockState, facing.getOpposite());
crank = model.createInstance(); crank = model.createInstance();
rotateCrank();
} }
@Override @Override
public void beginFrame() { public void beginFrame() {
if (crank == null) return; if (crank == null) return;
HandCrankTileEntity crankTile = (HandCrankTileEntity) tile; rotateCrank();
}
private void rotateCrank() {
Direction.Axis axis = facing.getAxis(); Direction.Axis axis = facing.getAxis();
float angle = (crankTile.independentAngle + AnimationTickHolder.getPartialTicks() * crankTile.chasingVelocity) / 360; float angle = (tile.independentAngle + AnimationTickHolder.getPartialTicks() * tile.chasingVelocity) / 360;
MatrixStack ms = new MatrixStack(); MatrixStack ms = new MatrixStack();
MatrixStacker.of(ms) MatrixStacker.of(ms)

View file

@ -37,11 +37,8 @@ public class FlyWheelInstance extends KineticTileInstance<FlywheelTileEntity> im
protected InstanceKey<ModelData> upperSliding; protected InstanceKey<ModelData> upperSliding;
protected InstanceKey<ModelData> lowerSliding; protected InstanceKey<ModelData> lowerSliding;
protected float lastAngle = Float.NaN; protected float lastAngle = Float.NaN;
protected boolean firstFrame = true;
public FlyWheelInstance(InstancedTileRenderer<?> modelManager, FlywheelTileEntity tile) { public FlyWheelInstance(InstancedTileRenderer<?> modelManager, FlywheelTileEntity tile) {
super(modelManager, tile); super(modelManager, tile);
@ -70,6 +67,8 @@ public class FlyWheelInstance extends KineticTileInstance<FlywheelTileEntity> im
} else { } else {
connectors = Collections.emptyList(); connectors = Collections.emptyList();
} }
animate(tile.angle);
} }
@Override @Override
@ -80,8 +79,14 @@ public class FlyWheelInstance extends KineticTileInstance<FlywheelTileEntity> im
float speed = tile.visualSpeed.get(partialTicks) * 3 / 10f; float speed = tile.visualSpeed.get(partialTicks) * 3 / 10f;
float angle = tile.angle + speed * partialTicks; float angle = tile.angle + speed * partialTicks;
if (!firstFrame && Math.abs(angle - lastAngle) < 0.001) return; if (Math.abs(angle - lastAngle) < 0.001) return;
animate(angle);
lastAngle = angle;
}
private void animate(float angle) {
MatrixStack ms = new MatrixStack(); MatrixStack ms = new MatrixStack();
MatrixStacker msr = MatrixStacker.of(ms); MatrixStacker msr = MatrixStacker.of(ms);
@ -121,9 +126,6 @@ public class FlyWheelInstance extends KineticTileInstance<FlywheelTileEntity> im
.unCentre(); .unCentre();
wheel.getInstance().setTransform(ms); wheel.getInstance().setTransform(ms);
lastAngle = angle;
firstFrame = false;
} }
@Override @Override

View file

@ -14,25 +14,25 @@ import com.simibubi.create.foundation.utility.MatrixStacker;
public class PressInstance extends ShaftInstance implements IDynamicInstance { public class PressInstance extends ShaftInstance implements IDynamicInstance {
private final InstanceKey<ModelData> pressHead; private final InstanceKey<ModelData> pressHead;
private final MechanicalPressTileEntity press;
public PressInstance(InstancedTileRenderer<?> dispatcher, KineticTileEntity tile) { public PressInstance(InstancedTileRenderer<?> dispatcher, MechanicalPressTileEntity tile) {
super(dispatcher, tile); super(dispatcher, tile);
press = tile;
pressHead = AllBlockPartials.MECHANICAL_PRESS_HEAD.renderOnHorizontalModel(dispatcher, blockState).createInstance(); pressHead = AllBlockPartials.MECHANICAL_PRESS_HEAD.renderOnHorizontalModel(dispatcher, blockState).createInstance();
transformModels();
transformModels((MechanicalPressTileEntity) tile);
} }
@Override @Override
public void beginFrame() { public void beginFrame() {
MechanicalPressTileEntity press = (MechanicalPressTileEntity) tile;
if (!press.running) if (!press.running)
return; return;
transformModels(press); transformModels();
} }
private void transformModels(MechanicalPressTileEntity press) { private void transformModels() {
float renderedHeadOffset = getRenderedHeadOffset(press); float renderedHeadOffset = getRenderedHeadOffset(press);
MatrixStack ms = new MatrixStack(); MatrixStack ms = new MatrixStack();

View file

@ -28,6 +28,8 @@ public class StickerInstance extends TileEntityInstance<StickerTileEntity> imple
fakeWorld = tile.getWorld() != Minecraft.getInstance().world; fakeWorld = tile.getWorld() != Minecraft.getInstance().world;
facing = blockState.get(StickerBlock.FACING); facing = blockState.get(StickerBlock.FACING);
offset = blockState.get(StickerBlock.EXTENDED) ? 1 : 0; offset = blockState.get(StickerBlock.EXTENDED) ? 1 : 0;
animateHead(offset);
} }
@Override @Override
@ -40,6 +42,12 @@ public class StickerInstance extends TileEntityInstance<StickerTileEntity> imple
if (MathHelper.epsilonEquals(offset, lastOffset)) if (MathHelper.epsilonEquals(offset, lastOffset))
return; return;
animateHead(offset);
lastOffset = offset;
}
private void animateHead(float offset) {
MatrixStack stack = new MatrixStack(); MatrixStack stack = new MatrixStack();
MatrixStacker.of(stack) MatrixStacker.of(stack)
.translate(getInstancePosition()) .translate(getInstancePosition())
@ -52,8 +60,6 @@ public class StickerInstance extends TileEntityInstance<StickerTileEntity> imple
head.getInstance() head.getInstance()
.setTransform(stack); .setTransform(stack);
lastOffset = offset;
} }
@Override @Override

View file

@ -15,6 +15,7 @@ import com.simibubi.create.foundation.utility.MatrixStacker;
import net.minecraft.client.renderer.Vector3f; import net.minecraft.client.renderer.Vector3f;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
public class GantryCarriageInstance extends ShaftInstance implements IDynamicInstance { public class GantryCarriageInstance extends ShaftInstance implements IDynamicInstance {
@ -23,8 +24,11 @@ public class GantryCarriageInstance extends ShaftInstance implements IDynamicIns
final Direction facing; final Direction facing;
final Boolean alongFirst; final Boolean alongFirst;
final Direction.Axis rotationAxis; final Direction.Axis rotationAxis;
final float rotationMult;
final BlockPos visualPos; final BlockPos visualPos;
private float lastAngle = Float.NaN;
public GantryCarriageInstance(InstancedTileRenderer<?> dispatcher, KineticTileEntity tile) { public GantryCarriageInstance(InstancedTileRenderer<?> dispatcher, KineticTileEntity tile) {
super(dispatcher, tile); super(dispatcher, tile);
@ -36,27 +40,29 @@ public class GantryCarriageInstance extends ShaftInstance implements IDynamicIns
alongFirst = blockState.get(GantryCarriageBlock.AXIS_ALONG_FIRST_COORDINATE); alongFirst = blockState.get(GantryCarriageBlock.AXIS_ALONG_FIRST_COORDINATE);
rotationAxis = KineticTileEntityRenderer.getRotationAxisOf(tile); rotationAxis = KineticTileEntityRenderer.getRotationAxisOf(tile);
rotationMult = getRotationMultiplier(getGantryAxis(), facing);
visualPos = facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? tile.getPos() visualPos = facing.getAxisDirection() == Direction.AxisDirection.POSITIVE ? tile.getPos()
: tile.getPos() : tile.getPos()
.offset(facing.getOpposite()); .offset(facing.getOpposite());
animateCogs(getCogAngle());
} }
@Override @Override
public void beginFrame() { public void beginFrame() {
float angleForTe = GantryCarriageRenderer.getAngleForTe(tile, visualPos, rotationAxis); float cogAngle = getCogAngle();
Direction.Axis gantryAxis = Direction.Axis.X; if (MathHelper.epsilonEquals(cogAngle, lastAngle)) return;
for (Direction.Axis axis : Iterate.axes)
if (axis != rotationAxis && axis != facing.getAxis())
gantryAxis = axis;
if (gantryAxis == Direction.Axis.Z) animateCogs(cogAngle);
if (facing == Direction.DOWN) }
angleForTe *= -1;
if (gantryAxis == Direction.Axis.Y)
if (facing == Direction.NORTH || facing == Direction.EAST)
angleForTe *= -1;
private float getCogAngle() {
return GantryCarriageRenderer.getAngleForTe(tile, visualPos, rotationAxis) * rotationMult;
}
private void animateCogs(float cogAngle) {
MatrixStack ms = new MatrixStack(); MatrixStack ms = new MatrixStack();
MatrixStacker.of(ms) MatrixStacker.of(ms)
.translate(getInstancePosition()) .translate(getInstancePosition())
@ -65,13 +71,33 @@ public class GantryCarriageInstance extends ShaftInstance implements IDynamicIns
.rotateX(facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90) .rotateX(facing == Direction.UP ? 0 : facing == Direction.DOWN ? 180 : 90)
.rotateY(alongFirst ^ facing.getAxis() == Direction.Axis.Z ? 90 : 0) .rotateY(alongFirst ^ facing.getAxis() == Direction.Axis.Z ? 90 : 0)
.translate(0, -9 / 16f, 0) .translate(0, -9 / 16f, 0)
.multiply(Vector3f.POSITIVE_X.getRadialQuaternion(-angleForTe)) .multiply(Vector3f.POSITIVE_X.getRadialQuaternion(-cogAngle))
.translate(0, 9 / 16f, 0) .translate(0, 9 / 16f, 0)
.unCentre(); .unCentre();
gantryCogs.getInstance().setTransform(ms); gantryCogs.getInstance().setTransform(ms);
} }
static float getRotationMultiplier(Direction.Axis gantryAxis, Direction facing) {
float multiplier = 1;
if (gantryAxis == Direction.Axis.Z)
if (facing == Direction.DOWN)
multiplier *= -1;
if (gantryAxis == Direction.Axis.Y)
if (facing == Direction.NORTH || facing == Direction.EAST)
multiplier *= -1;
return multiplier;
}
private Direction.Axis getGantryAxis() {
Direction.Axis gantryAxis = Direction.Axis.X;
for (Direction.Axis axis : Iterate.axes)
if (axis != rotationAxis && axis != facing.getAxis())
gantryAxis = axis;
return gantryAxis;
}
@Override @Override
public void updateLight() { public void updateLight() {
relight(pos, gantryCogs.getInstance(), rotatingModel.getInstance()); relight(pos, gantryCogs.getInstance(), rotatingModel.getInstance());

View file

@ -34,8 +34,6 @@ public class SplitShaftInstance extends KineticTileInstance<SplitShaftTileEntity
keys.add(setup(half.createInstance(), splitSpeed)); keys.add(setup(half.createInstance(), splitSpeed));
} }
updateLight();
} }
@Override @Override

View file

@ -28,7 +28,6 @@ public class EjectorInstance extends ShaftInstance implements IDynamicInstance {
plate = getTransformMaterial().getModel(AllBlockPartials.EJECTOR_TOP, blockState).createInstance(); plate = getTransformMaterial().getModel(AllBlockPartials.EJECTOR_TOP, blockState).createInstance();
pivotPlate(); pivotPlate();
updateLight();
} }
@Override @Override

View file

@ -3,7 +3,6 @@ package com.simibubi.create.content.logistics.block.mechanicalArm;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
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.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.RotatingData; import com.simibubi.create.content.contraptions.base.RotatingData;
import com.simibubi.create.content.contraptions.base.SingleRotatingInstance; import com.simibubi.create.content.contraptions.base.SingleRotatingInstance;
import com.simibubi.create.foundation.render.backend.instancing.*; import com.simibubi.create.foundation.render.backend.instancing.*;
@ -31,10 +30,11 @@ public class ArmInstance extends SingleRotatingInstance implements IDynamicInsta
private final ArrayList<InstanceKey<ModelData>> clawGrips; private final ArrayList<InstanceKey<ModelData>> clawGrips;
private final ArrayList<InstanceKey<ModelData>> models; private final ArrayList<InstanceKey<ModelData>> models;
private final ArmTileEntity arm;
private boolean firstTick = true; private boolean firstTick = true;
public ArmInstance(InstancedTileRenderer<?> modelManager, KineticTileEntity tile) { public ArmInstance(InstancedTileRenderer<?> modelManager, ArmTileEntity tile) {
super(modelManager, tile); super(modelManager, tile);
RenderMaterial<?, InstancedModel<ModelData>> mat = getTransformMaterial(); RenderMaterial<?, InstancedModel<ModelData>> mat = getTransformMaterial();
@ -51,33 +51,35 @@ public class ArmInstance extends SingleRotatingInstance implements IDynamicInsta
clawGrips = Lists.newArrayList(clawGrip1, clawGrip2); clawGrips = Lists.newArrayList(clawGrip1, clawGrip2);
models = Lists.newArrayList(base, lowerBody, upperBody, head, claw, clawGrip1, clawGrip2); models = Lists.newArrayList(base, lowerBody, upperBody, head, claw, clawGrip1, clawGrip2);
arm = tile;
animateArm(false);
} }
@Override @Override
public void beginFrame() { public void beginFrame() {
ArmTileEntity arm = (ArmTileEntity) tile;
boolean settled = arm.baseAngle.settled() && arm.lowerArmAngle.settled() && arm.upperArmAngle.settled() && arm.headAngle.settled(); boolean settled = arm.baseAngle.settled() && arm.lowerArmAngle.settled() && arm.upperArmAngle.settled() && arm.headAngle.settled();
boolean rave = arm.phase == ArmTileEntity.Phase.DANCING; boolean rave = arm.phase == ArmTileEntity.Phase.DANCING;
if (!settled || rave || firstTick) if (!settled || rave || firstTick)
transformModels(arm, rave); animateArm(rave);
if (settled) if (settled)
firstTick = false; firstTick = false;
} }
private void transformModels(ArmTileEntity arm, boolean rave) { private void animateArm(boolean rave) {
float pt = AnimationTickHolder.getPartialTicks(); float pt = AnimationTickHolder.getPartialTicks();
int color = 0xFFFFFF; int color = 0xFFFFFF;
float baseAngle = arm.baseAngle.get(pt); float baseAngle = this.arm.baseAngle.get(pt);
float lowerArmAngle = arm.lowerArmAngle.get(pt) - 135; float lowerArmAngle = this.arm.lowerArmAngle.get(pt) - 135;
float upperArmAngle = arm.upperArmAngle.get(pt) - 90; float upperArmAngle = this.arm.upperArmAngle.get(pt) - 90;
float headAngle = arm.headAngle.get(pt); float headAngle = this.arm.headAngle.get(pt);
if (rave) { if (rave) {
float renderTick = AnimationTickHolder.getRenderTime(arm.getWorld()) + (tile.hashCode() % 64); float renderTick = AnimationTickHolder.getRenderTime(this.arm.getWorld()) + (tile.hashCode() % 64);
baseAngle = (renderTick * 10) % 360; baseAngle = (renderTick * 10) % 360;
lowerArmAngle = MathHelper.lerp((MathHelper.sin(renderTick / 4) + 1) / 2, -45, 15); lowerArmAngle = MathHelper.lerp((MathHelper.sin(renderTick / 4) + 1) / 2, -45, 15);
upperArmAngle = MathHelper.lerp((MathHelper.sin(renderTick / 8) + 1) / 4, -45, 95); upperArmAngle = MathHelper.lerp((MathHelper.sin(renderTick / 8) + 1) / 4, -45, 95);
@ -85,7 +87,6 @@ public class ArmInstance extends SingleRotatingInstance implements IDynamicInsta
color = ColorHelper.rainbowColor(AnimationTickHolder.getTicks() * 100); color = ColorHelper.rainbowColor(AnimationTickHolder.getTicks() * 100);
} }
MatrixStack msLocal = new MatrixStack(); MatrixStack msLocal = new MatrixStack();
MatrixStacker msr = MatrixStacker.of(msLocal); MatrixStacker msr = MatrixStacker.of(msLocal);
msr.translate(getInstancePosition()); msr.translate(getInstancePosition());
@ -116,7 +117,7 @@ public class ArmInstance extends SingleRotatingInstance implements IDynamicInsta
claw.getInstance() claw.getInstance()
.setTransform(msLocal); .setTransform(msLocal);
ItemStack item = arm.heldItem; ItemStack item = this.arm.heldItem;
ItemRenderer itemRenderer = Minecraft.getInstance() ItemRenderer itemRenderer = Minecraft.getInstance()
.getItemRenderer(); .getItemRenderer();
boolean hasItem = !item.isEmpty(); boolean hasItem = !item.isEmpty();

View file

@ -31,16 +31,16 @@ public class AnalogLeverInstance extends TileEntityInstance<AnalogLeverTileEntit
rX = face == AttachFace.FLOOR ? 0 : face == AttachFace.WALL ? 90 : 180; rX = face == AttachFace.FLOOR ? 0 : face == AttachFace.WALL ? 90 : 180;
rY = AngleHelper.horizontalAngle(blockState.get(AnalogLeverBlock.HORIZONTAL_FACING)); rY = AngleHelper.horizontalAngle(blockState.get(AnalogLeverBlock.HORIZONTAL_FACING));
setupModel(); animateLever();
} }
@Override @Override
public void beginFrame() { public void beginFrame() {
if (!tile.clientState.settled()) if (!tile.clientState.settled())
setupModel(); animateLever();
} }
protected void setupModel() { protected void animateLever() {
MatrixStack ms = new MatrixStack(); MatrixStack ms = new MatrixStack();
MatrixStacker msr = MatrixStacker.of(ms); MatrixStacker msr = MatrixStacker.of(ms);

View file

@ -20,6 +20,8 @@ public class SchematicannonInstance extends TileEntityInstance<SchematicannonTil
connector = mat.getModel(AllBlockPartials.SCHEMATICANNON_CONNECTOR, blockState).createInstance(); connector = mat.getModel(AllBlockPartials.SCHEMATICANNON_CONNECTOR, blockState).createInstance();
pipe = mat.getModel(AllBlockPartials.SCHEMATICANNON_PIPE, blockState).createInstance(); pipe = mat.getModel(AllBlockPartials.SCHEMATICANNON_PIPE, blockState).createInstance();
beginFrame();
} }
@Override @Override