Add Flywheel visuals for bogey block entities

This commit is contained in:
PepperCode1 2024-10-24 21:52:07 -07:00
parent 1935b790dd
commit d48a504486
9 changed files with 179 additions and 40 deletions

View file

@ -199,6 +199,7 @@ import com.simibubi.create.content.schematics.cannon.SchematicannonRenderer;
import com.simibubi.create.content.schematics.cannon.SchematicannonVisual;
import com.simibubi.create.content.schematics.table.SchematicTableBlockEntity;
import com.simibubi.create.content.trains.bogey.BogeyBlockEntityRenderer;
import com.simibubi.create.content.trains.bogey.BogeyBlockEntityVisual;
import com.simibubi.create.content.trains.bogey.StandardBogeyBlockEntity;
import com.simibubi.create.content.trains.display.FlapDisplayBlockEntity;
import com.simibubi.create.content.trains.display.FlapDisplayRenderer;
@ -844,6 +845,7 @@ public class AllBlockEntityTypes {
public static final BlockEntityEntry<StandardBogeyBlockEntity> BOGEY = REGISTRATE
.blockEntity("bogey", StandardBogeyBlockEntity::new)
.visual(() -> BogeyBlockEntityVisual::new, false)
.renderer(() -> BogeyBlockEntityRenderer::new)
.validBlocks(AllBlocks.SMALL_BOGEY, AllBlocks.LARGE_BOGEY)
.register();

View file

@ -113,6 +113,6 @@ public abstract class AbstractBogeyBlockEntity extends CachedRenderBBBlockEntity
setChanged();
Level level = getLevel();
if (level != null)
getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}
}

View file

@ -7,10 +7,9 @@ import com.simibubi.create.foundation.blockEntity.renderer.SafeBlockEntityRender
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
public class BogeyBlockEntityRenderer<T extends BlockEntity> extends SafeBlockEntityRenderer<T> {
public class BogeyBlockEntityRenderer<T extends AbstractBogeyBlockEntity> extends SafeBlockEntityRenderer<T> {
public BogeyBlockEntityRenderer(BlockEntityRendererProvider.Context context) {
}
@ -18,14 +17,16 @@ public class BogeyBlockEntityRenderer<T extends BlockEntity> extends SafeBlockEn
protected void renderSafe(T be, float partialTicks, PoseStack ms, MultiBufferSource buffer, int light,
int overlay) {
BlockState blockState = be.getBlockState();
if (be instanceof AbstractBogeyBlockEntity sbbe) {
float angle = sbbe.getVirtualAngle(partialTicks);
if (blockState.getBlock() instanceof AbstractBogeyBlock<?> bogey) {
ms.translate(.5f, .5f, .5f);
if (blockState.getValue(AbstractBogeyBlock.AXIS) == Direction.Axis.X)
ms.mulPose(Axis.YP.rotationDegrees(90));
sbbe.getStyle().render(bogey.getSize(), partialTicks, ms, buffer, light, overlay, angle, sbbe.getBogeyData(), false);
}
if (!(blockState.getBlock() instanceof AbstractBogeyBlock<?> bogey)) {
return;
}
float angle = be.getVirtualAngle(partialTicks);
ms.pushPose();
ms.translate(.5f, .5f, .5f);
if (blockState.getValue(AbstractBogeyBlock.AXIS) == Direction.Axis.X)
ms.mulPose(Axis.YP.rotationDegrees(90));
be.getStyle().render(bogey.getSize(), partialTicks, ms, buffer, light, overlay, angle, be.getBogeyData(), false);
ms.popPose();
}
}

View file

@ -0,0 +1,95 @@
package com.simibubi.create.content.trains.bogey;
import java.util.function.Consumer;
import org.jetbrains.annotations.Nullable;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import com.simibubi.create.content.trains.bogey.BogeySizes.BogeySize;
import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
import dev.engine_room.flywheel.lib.visual.AbstractBlockEntityVisual;
import dev.engine_room.flywheel.lib.visual.SimpleDynamicVisual;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
public class BogeyBlockEntityVisual extends AbstractBlockEntityVisual<AbstractBogeyBlockEntity> implements SimpleDynamicVisual {
private final PoseStack poseStack = new PoseStack();
@Nullable
private final BogeySize bogeySize;
private BogeyStyle lastStyle;
@Nullable
private BogeyVisual bogey;
public BogeyBlockEntityVisual(VisualizationContext ctx, AbstractBogeyBlockEntity blockEntity, float partialTick) {
super(ctx, blockEntity, partialTick);
lastStyle = blockEntity.getStyle();
if (!(blockState.getBlock() instanceof AbstractBogeyBlock<?> block)) {
bogeySize = null;
return;
}
bogeySize = block.getSize();
BlockPos visualPos = getVisualPosition();
poseStack.translate(visualPos.getX(), visualPos.getY(), visualPos.getZ());
poseStack.translate(.5f, .5f, .5f);
if (blockState.getValue(AbstractBogeyBlock.AXIS) == Direction.Axis.X)
poseStack.mulPose(Axis.YP.rotationDegrees(90));
poseStack.translate(0, -1.5 - 1 / 128f, 0);
bogey = lastStyle.createVisual(bogeySize, visualizationContext, partialTick, false);
}
@Override
public void beginFrame(Context context) {
if (bogeySize == null) {
return;
}
BogeyStyle style = blockEntity.getStyle();
if (style != lastStyle) {
if (bogey != null) {
bogey.delete();
bogey = null;
}
lastStyle = style;
bogey = lastStyle.createVisual(bogeySize, visualizationContext, context.partialTick(), false);
}
if (bogey == null) {
return;
}
CompoundTag bogeyData = blockEntity.getBogeyData();
float angle = blockEntity.getVirtualAngle(context.partialTick());
bogey.update(bogeyData, angle, poseStack);
}
@Override
public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) {
if (bogey != null) {
bogey.collectCrumblingInstances(consumer);
}
}
@Override
public void updateLight(float partialTick) {
if (bogey != null) {
bogey.updateLight(computePackedLight());
}
}
@Override
protected void _delete() {
if (bogey != null) {
bogey.delete();
}
}
}

View file

@ -12,7 +12,6 @@ import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.AllBogeyStyles;
import com.simibubi.create.AllSoundEvents;
import com.simibubi.create.content.trains.bogey.BogeySizes.BogeySize;
import com.simibubi.create.content.trains.entity.CarriageBogey;
import com.simibubi.create.foundation.utility.Lang;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
@ -96,10 +95,10 @@ public class BogeyStyle {
@OnlyIn(Dist.CLIENT)
@Nullable
public BogeyVisual createVisual(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
SizeRenderer renderer = sizeRenderers.get(bogey.getSize());
public BogeyVisual createVisual(BogeySize size, VisualizationContext ctx, float partialTick, boolean inContraption) {
SizeRenderer renderer = sizeRenderers.get(size);
if (renderer != null) {
return renderer.visualizer.createVisual(ctx, bogey, partialTick);
return renderer.visualizer.createVisual(ctx, partialTick, inContraption);
}
return null;
}

View file

@ -1,13 +1,22 @@
package com.simibubi.create.content.trains.bogey;
import java.util.function.Consumer;
import org.jetbrains.annotations.Nullable;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.engine_room.flywheel.api.instance.Instance;
import net.minecraft.nbt.CompoundTag;
public interface BogeyVisual {
void update(float wheelAngle, PoseStack poseStack);
void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack);
void hide();
void updateLight(int packedLight);
void collectCrumblingInstances(Consumer<@Nullable Instance> consumer);
void delete();
}

View file

@ -1,10 +1,8 @@
package com.simibubi.create.content.trains.bogey;
import com.simibubi.create.content.trains.entity.CarriageBogey;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
@FunctionalInterface
public interface BogeyVisualizer {
BogeyVisual createVisual(VisualizationContext ctx, CarriageBogey bogey, float partialTick);
BogeyVisual createVisual(VisualizationContext ctx, float partialTick, boolean inContraption);
}

View file

@ -1,24 +1,29 @@
package com.simibubi.create.content.trains.bogey;
import java.util.function.Consumer;
import org.jetbrains.annotations.Nullable;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllPartialModels;
import com.simibubi.create.content.kinetics.simpleRelays.ShaftBlock;
import com.simibubi.create.content.trains.entity.CarriageBogey;
import com.simibubi.create.foundation.render.VirtualRenderHelper;
import com.simibubi.create.foundation.utility.AngleHelper;
import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
import dev.engine_room.flywheel.lib.instance.InstanceTypes;
import dev.engine_room.flywheel.lib.instance.TransformedInstance;
import dev.engine_room.flywheel.lib.model.Models;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
public class StandardBogeyVisual implements BogeyVisual {
private final TransformedInstance shaft1;
private final TransformedInstance shaft2;
public StandardBogeyVisual(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
public StandardBogeyVisual(VisualizationContext ctx, float partialTick, boolean inContraption) {
var shaftInstancer = ctx.instancerProvider()
.instancer(InstanceTypes.TRANSFORMED, VirtualRenderHelper.blockModel(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.Z)));
@ -27,7 +32,7 @@ public class StandardBogeyVisual implements BogeyVisual {
}
@Override
public void update(float wheelAngle, PoseStack poseStack) {
public void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack) {
shaft1.setTransform(poseStack)
.translate(-.5f, .25f, 0)
.center()
@ -54,6 +59,12 @@ public class StandardBogeyVisual implements BogeyVisual {
shaft2.light(packedLight).setChanged();
}
@Override
public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) {
consumer.accept(shaft1);
consumer.accept(shaft2);
}
@Override
public void delete() {
shaft1.delete();
@ -65,8 +76,8 @@ public class StandardBogeyVisual implements BogeyVisual {
private final TransformedInstance wheel1;
private final TransformedInstance wheel2;
public Small(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
super(ctx, bogey, partialTick);
public Small(VisualizationContext ctx, float partialTick, boolean inContraption) {
super(ctx, partialTick, inContraption);
var wheelInstancer = ctx.instancerProvider()
.instancer(InstanceTypes.TRANSFORMED, Models.partial(AllPartialModels.SMALL_BOGEY_WHEELS));
frame = ctx.instancerProvider()
@ -77,8 +88,8 @@ public class StandardBogeyVisual implements BogeyVisual {
}
@Override
public void update(float wheelAngle, PoseStack poseStack) {
super.update(wheelAngle, poseStack);
public void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack) {
super.update(bogeyData, wheelAngle, poseStack);
wheel1.setTransform(poseStack)
.translate(0, 12 / 16f, -1)
.rotateXDegrees(wheelAngle)
@ -108,6 +119,14 @@ public class StandardBogeyVisual implements BogeyVisual {
wheel2.light(packedLight).setChanged();
}
@Override
public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) {
super.collectCrumblingInstances(consumer);
consumer.accept(frame);
consumer.accept(wheel1);
consumer.accept(wheel2);
}
@Override
public void delete() {
super.delete();
@ -125,8 +144,8 @@ public class StandardBogeyVisual implements BogeyVisual {
private final TransformedInstance wheels;
private final TransformedInstance pin;
public Large(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
super(ctx, bogey, partialTick);
public Large(VisualizationContext ctx, float partialTick, boolean inContraption) {
super(ctx, partialTick, inContraption);
var secondaryShaftInstancer = ctx.instancerProvider()
.instancer(InstanceTypes.TRANSFORMED, VirtualRenderHelper.blockModel(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.X)));
@ -147,8 +166,8 @@ public class StandardBogeyVisual implements BogeyVisual {
}
@Override
public void update(float wheelAngle, PoseStack poseStack) {
super.update(wheelAngle, poseStack);
public void update(CompoundTag bogeyData, float wheelAngle, PoseStack poseStack) {
super.update(bogeyData, wheelAngle, poseStack);
secondaryShaft1.setTransform(poseStack)
.translate(-.5f, .25f, .5f)
.center()
@ -201,6 +220,17 @@ public class StandardBogeyVisual implements BogeyVisual {
pin.light(packedLight).setChanged();
}
@Override
public void collectCrumblingInstances(Consumer<@Nullable Instance> consumer) {
super.collectCrumblingInstances(consumer);
consumer.accept(secondaryShaft1);
consumer.accept(secondaryShaft2);
consumer.accept(wheels);
consumer.accept(drive);
consumer.accept(piston);
consumer.accept(pin);
}
@Override
public void delete() {
super.delete();

View file

@ -12,9 +12,10 @@ import com.simibubi.create.foundation.utility.Iterate;
import dev.engine_room.flywheel.api.visual.DynamicVisual;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
import dev.engine_room.flywheel.lib.transform.TransformStack;
import net.minecraft.nbt.CompoundTag;
public class CarriageContraptionVisual extends ContraptionVisual<CarriageContraptionEntity> {
private final PoseStack ms = new PoseStack();
private final PoseStack poseStack = new PoseStack();
@Nullable
private Carriage carriage;
@ -59,10 +60,10 @@ public class CarriageContraptionVisual extends ContraptionVisual<CarriageContrap
float viewXRot = entity.getViewXRot(partialTick);
int bogeySpacing = carriage.bogeySpacing;
ms.pushPose();
poseStack.pushPose();
Vector3f visualPosition = getVisualPosition(partialTick);
TransformStack.of(ms)
TransformStack.of(poseStack)
.translate(visualPosition);
for (boolean current : Iterate.trueAndFalse) {
@ -75,17 +76,21 @@ public class CarriageContraptionVisual extends ContraptionVisual<CarriageContrap
continue;
}
ms.pushPose();
poseStack.pushPose();
CarriageBogey bogey = visualizedBogey.bogey;
CarriageContraptionEntityRenderer.translateBogey(ms, bogey, bogeySpacing, viewYRot, viewXRot, partialTick);
ms.translate(0, -1.5 - 1 / 128f, 0);
CarriageContraptionEntityRenderer.translateBogey(poseStack, bogey, bogeySpacing, viewYRot, viewXRot, partialTick);
poseStack.translate(0, -1.5 - 1 / 128f, 0);
visualizedBogey.visual.update(bogey.wheelAngle.getValue(partialTick), ms);
ms.popPose();
CompoundTag bogeyData = bogey.bogeyData;
if (bogeyData == null) {
bogeyData = new CompoundTag();
}
visualizedBogey.visual.update(bogeyData, bogey.wheelAngle.getValue(partialTick), poseStack);
poseStack.popPose();
}
ms.popPose();
poseStack.popPose();
}
@Override
@ -120,7 +125,7 @@ public class CarriageContraptionVisual extends ContraptionVisual<CarriageContrap
private record VisualizedBogey(CarriageBogey bogey, BogeyVisual visual) {
@Nullable
static VisualizedBogey of(VisualizationContext ctx, CarriageBogey bogey, float partialTick) {
BogeyVisual visual = bogey.getStyle().createVisual(ctx, bogey, partialTick);
BogeyVisual visual = bogey.getStyle().createVisual(bogey.getSize(), ctx, partialTick, true);
if (visual == null) {
return null;
}