Instanced bogeys

This commit is contained in:
Jozufozu 2022-03-16 19:03:10 -07:00
parent dc44c7aa68
commit eb74505ce8
11 changed files with 368 additions and 37 deletions

View file

@ -16,8 +16,10 @@ import com.simibubi.create.content.curiosities.weapons.PotatoProjectileEntity;
import com.simibubi.create.content.curiosities.weapons.PotatoProjectileRenderer;
import com.simibubi.create.content.logistics.trains.entity.CarriageContraptionEntity;
import com.simibubi.create.content.logistics.trains.entity.CarriageContraptionEntityRenderer;
import com.simibubi.create.content.logistics.trains.entity.CarriageContraptionInstance;
import com.simibubi.create.foundation.data.CreateEntityBuilder;
import com.simibubi.create.foundation.utility.Lang;
import com.tterrag.registrate.builders.EntityBuilder;
import com.tterrag.registrate.util.entry.EntityEntry;
import com.tterrag.registrate.util.nullness.NonNullConsumer;
import com.tterrag.registrate.util.nullness.NonNullFunction;
@ -33,15 +35,20 @@ import net.minecraft.world.entity.MobCategory;
public class AllEntityTypes {
public static final EntityEntry<OrientedContraptionEntity> ORIENTED_CONTRAPTION = contraption("contraption",
OrientedContraptionEntity::new, () -> OrientedContraptionEntityRenderer::new, 5, 3, true);
OrientedContraptionEntity::new, () -> OrientedContraptionEntityRenderer::new, 5, 3, true)
.register();
public static final EntityEntry<ControlledContraptionEntity> CONTROLLED_CONTRAPTION =
contraption("stationary_contraption", ControlledContraptionEntity::new, () -> ContraptionEntityRenderer::new,
20, 40, false);
20, 40, false)
.register();
public static final EntityEntry<GantryContraptionEntity> GANTRY_CONTRAPTION = contraption("gantry_contraption",
GantryContraptionEntity::new, () -> ContraptionEntityRenderer::new, 10, 40, false);
GantryContraptionEntity::new, () -> ContraptionEntityRenderer::new, 10, 40, false)
.register();
public static final EntityEntry<CarriageContraptionEntity> CARRIAGE_CONTRAPTION =
contraption("carriage_contraption", CarriageContraptionEntity::new,
() -> CarriageContraptionEntityRenderer::new, 15, 3, true);
() -> CarriageContraptionEntityRenderer::new, 15, 3, true)
.instance(() -> CarriageContraptionInstance::new)
.register();
public static final EntityEntry<SuperGlueEntity> SUPER_GLUE =
register("super_glue", SuperGlueEntity::new, () -> SuperGlueRenderer::new, MobCategory.MISC, 10,
@ -61,11 +68,11 @@ public class AllEntityTypes {
//
private static <T extends Entity> EntityEntry<T> contraption(String name, EntityFactory<T> factory,
private static <T extends Entity> CreateEntityBuilder<T, ?> contraption(String name, EntityFactory<T> factory,
NonNullSupplier<NonNullFunction<EntityRendererProvider.Context, EntityRenderer<? super T>>> renderer, int range,
int updateFrequency, boolean sendVelocity) {
return register(name, factory, renderer, MobCategory.MISC, range, updateFrequency, sendVelocity, true,
AbstractContraptionEntity::build).register();
AbstractContraptionEntity::build);
}
private static <T extends Entity> CreateEntityBuilder<T, ?> register(String name, EntityFactory<T> factory,

View file

@ -16,7 +16,7 @@ public class ContraptionGroup<P extends ContraptionProgram> extends InstancedMat
}
@Override
public void setup(P program) {
protected void setup(P program) {
contraption.setup(program);
}

View file

@ -6,8 +6,11 @@ import java.util.List;
import javax.annotation.Nullable;
import com.jozufozu.flywheel.api.MaterialManager;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.content.logistics.trains.entity.BogeyInstance;
import com.simibubi.create.content.logistics.trains.entity.CarriageBogey;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.core.BlockPos;
@ -43,6 +46,9 @@ public interface IBogeyBlock extends IWrenchable {
public void render(@Nullable BlockState state, float wheelAngle, PoseStack ms, float partialTicks,
MultiBufferSource buffers, int light, int overlay);
@OnlyIn(Dist.CLIENT)
public BogeyInstance createInstance(MaterialManager materialManager, CarriageBogey bogey);
public default Direction getBogeyUpDirection() {
return Direction.UP;
}

View file

@ -0,0 +1,183 @@
package com.simibubi.create.content.logistics.trains.entity;
import com.jozufozu.flywheel.api.Material;
import com.jozufozu.flywheel.api.MaterialManager;
import com.jozufozu.flywheel.core.Materials;
import com.jozufozu.flywheel.core.materials.model.ModelData;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.content.contraptions.relays.elementary.ShaftBlock;
import com.simibubi.create.foundation.render.CachedBufferer;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.Iterate;
import net.minecraft.core.Direction;
public sealed class BogeyInstance {
public final CarriageBogey bogey;
private final ModelData[] shafts;
protected BogeyInstance(CarriageBogey bogey, MaterialManager materialManager) {
this.bogey = bogey;
shafts = new ModelData[2];
materialManager.defaultSolid()
.material(Materials.TRANSFORMED)
.getModel(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.Z))
.createInstances(shafts);
}
public void remove() {
for (ModelData shaft : shafts)
shaft.delete();
}
public void beginFrame(float wheelAngle, PoseStack ms) {
for (int i : Iterate.zeroAndOne)
shafts[i].setTransform(ms)
.translate(-.5f, .25f, i * -1)
.centre()
.rotateZ(wheelAngle)
.unCentre();
}
public void updateLight(int blockLight, int skyLight) {
for (ModelData shaft : shafts) {
shaft.setBlockLight(blockLight).setSkyLight(skyLight);
}
}
public static final class Frame extends BogeyInstance {
private final ModelData frame;
private final ModelData[] wheels;
public Frame(CarriageBogey bogey, MaterialManager materialManager) {
super(bogey, materialManager);
frame = materialManager.defaultSolid()
.material(Materials.TRANSFORMED)
.getModel(AllBlockPartials.BOGEY_FRAME)
.createInstance();
wheels = new ModelData[2];
materialManager.defaultSolid()
.material(Materials.TRANSFORMED)
.getModel(AllBlockPartials.SMALL_BOGEY_WHEELS)
.createInstances(wheels);
}
@Override
public void beginFrame(float wheelAngle, PoseStack ms) {
super.beginFrame(wheelAngle, ms);
frame.setTransform(ms);
for (int side : Iterate.positiveAndNegative) {
wheels[(side + 1) / 2].setTransform(ms)
.translate(0, 12 / 16f, side)
.rotateX(wheelAngle);
}
}
@Override
public void updateLight(int blockLight, int skyLight) {
super.updateLight(blockLight, skyLight);
frame.setBlockLight(blockLight).setSkyLight(skyLight);
for (ModelData wheel : wheels)
wheel.setBlockLight(blockLight).setSkyLight(skyLight);
}
@Override
public void remove() {
super.remove();
frame.delete();
for (ModelData wheel : wheels)
wheel.delete();
}
}
public static final class Drive extends BogeyInstance {
private final ModelData[] secondShaft;
private final ModelData drive;
private final ModelData piston;
private final ModelData wheels;
private final ModelData pin;
public Drive(CarriageBogey bogey, MaterialManager materialManager) {
super(bogey, materialManager);
Material<ModelData> mat = materialManager.defaultSolid()
.material(Materials.TRANSFORMED);
secondShaft = new ModelData[2];
mat.getModel(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Direction.Axis.X))
.createInstances(secondShaft);
drive = mat.getModel(AllBlockPartials.BOGEY_DRIVE)
.createInstance();
piston = mat.getModel(AllBlockPartials.BOGEY_PISTON)
.createInstance();
wheels = mat.getModel(AllBlockPartials.LARGE_BOGEY_WHEELS)
.createInstance();
pin = mat.getModel(AllBlockPartials.BOGEY_PIN)
.createInstance();
}
@Override
public void beginFrame(float wheelAngle, PoseStack ms) {
super.beginFrame(wheelAngle, ms);
for (int i : Iterate.zeroAndOne)
secondShaft[i].setTransform(ms)
.translate(-.5f, .25f, .5f + i * -2)
.centre()
.rotateX(wheelAngle)
.unCentre();
drive.setTransform(ms);
piston.setTransform(ms)
.translate(0, 0, 1 / 4f * Math.sin(AngleHelper.rad(wheelAngle)));
wheels.setTransform(ms)
.translate(0, 1, 0)
.rotateX(wheelAngle);
pin.setTransform(ms)
.translate(0, 1, 0)
.rotateX(wheelAngle)
.translate(0, 1 / 4f, 0)
.rotateX(-wheelAngle);
}
@Override
public void updateLight(int blockLight, int skyLight) {
super.updateLight(blockLight, skyLight);
for (ModelData shaft : secondShaft)
shaft.setBlockLight(blockLight).setSkyLight(skyLight);
drive.setBlockLight(blockLight).setSkyLight(skyLight);
piston.setBlockLight(blockLight).setSkyLight(skyLight);
wheels.setBlockLight(blockLight).setSkyLight(skyLight);
pin.setBlockLight(blockLight).setSkyLight(skyLight);
}
@Override
public void remove() {
super.remove();
for (ModelData shaft : secondShaft)
shaft.delete();
drive.delete();
piston.delete();
wheels.delete();
pin.delete();
}
}
}

View file

@ -63,6 +63,7 @@ public class Carriage {
updateContraptionAnchors();
bogey1.setLeading();
bogey1.carriage = this;
if (bogey2 != null)
bogey2.carriage = this;

View file

@ -2,6 +2,7 @@ package com.simibubi.create.content.logistics.trains.entity;
import javax.annotation.Nullable;
import com.jozufozu.flywheel.api.MaterialManager;
import com.simibubi.create.Create;
import com.simibubi.create.content.logistics.trains.IBogeyBlock;
import com.simibubi.create.content.logistics.trains.TrackGraph;
@ -24,6 +25,8 @@ public class CarriageBogey {
public Carriage carriage;
boolean isLeading;
IBogeyBlock type;
Couple<TravellingPoint> points;
@ -133,4 +136,11 @@ public class CarriageBogey {
return carriageBogey;
}
public BogeyInstance createInstance(MaterialManager materialManager) {
return type.createInstance(materialManager, this);
}
void setLeading() {
isLeading = true;
}
}

View file

@ -1,5 +1,6 @@
package com.simibubi.create.content.logistics.trains.entity;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.util.transform.TransformStack;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionEntityRenderer;
@ -33,6 +34,8 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer
MultiBufferSource buffers, int overlay) {
super.render(entity, yaw, partialTicks, ms, buffers, overlay);
if (Backend.isOn()) return;
Carriage carriage = entity.getCarriage();
if (carriage == null)
return;
@ -40,8 +43,8 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer
Vec3 position = entity.getPosition(partialTicks);
ms.pushPose();
carriage.bogeys.forEachWithContext((bogey, first) -> {
if (!first && !carriage.isOnTwoBogeys())
carriage.bogeys.forEach(bogey -> {
if (bogey == null)
return;
ms.pushPose();
@ -52,7 +55,7 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer
.rotateY(viewYRot + 90)
.rotateX(-viewXRot)
.rotateY(180)
.translate(0, 0, first ? 0 : -bogeySpacing)
.translate(0, 0, bogey.isLeading ? 0 : -bogeySpacing)
.rotateY(-180)
.rotateX(viewXRot)
.rotateY(-viewYRot - 90)
@ -62,9 +65,9 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer
bogey.type.render(null, bogey.wheelAngle.getValue(partialTicks), ms, partialTicks, buffers,
getPackedLightCoords(entity, partialTicks), overlay);
bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, first);
bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, bogey.isLeading);
if (!carriage.isOnTwoBogeys())
bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, !first);
bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, !bogey.isLeading);
ms.popPose();
});

View file

@ -0,0 +1,101 @@
package com.simibubi.create.content.logistics.trains.entity;
import com.jozufozu.flywheel.api.MaterialManager;
import com.jozufozu.flywheel.api.instance.DynamicInstance;
import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance;
import com.jozufozu.flywheel.util.AnimationTickHolder;
import com.jozufozu.flywheel.util.transform.TransformStack;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.foundation.utility.Couple;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.phys.Vec3;
public class CarriageContraptionInstance extends EntityInstance<CarriageContraptionEntity> implements DynamicInstance {
private final PoseStack ms = new PoseStack();
private Carriage carriage;
private Couple<BogeyInstance> bogeys;
public CarriageContraptionInstance(MaterialManager materialManager, CarriageContraptionEntity entity) {
super(materialManager, entity);
}
@Override
public void init() {
carriage = entity.getCarriage();
if (carriage == null) return;
bogeys = carriage.bogeys.mapNotNullWithParam(CarriageBogey::createInstance, materialManager);
updateLight();
}
@Override
public void beginFrame() {
if (bogeys == null) {
init();
return;
}
float partialTicks = AnimationTickHolder.getPartialTicks();
float viewYRot = entity.getViewYRot(partialTicks);
float viewXRot = entity.getViewXRot(partialTicks);
int bogeySpacing = carriage.bogeySpacing;
ms.pushPose();
TransformStack.cast(ms)
.translate(getInstancePosition(partialTicks))
.translate(0, -1.5 - 1 / 128f, 0);;
for (BogeyInstance instance : bogeys) {
if (instance != null) {
ms.pushPose();
CarriageBogey bogey = instance.bogey;
TransformStack.cast(ms)
.rotateY(viewYRot + 90)
.rotateX(-viewXRot)
.rotateY(180)
.translate(0, 0, bogey.isLeading ? 0 : -bogeySpacing)
.rotateY(-180)
.rotateX(viewXRot)
.rotateY(-viewYRot - 90)
.rotateY(bogey.yaw.getValue(partialTicks))
.rotateX(bogey.pitch.getValue(partialTicks))
.translate(0, .5f, 0);
instance.beginFrame(bogey.wheelAngle.getValue(partialTicks), ms);
ms.popPose();
}
}
ms.popPose();
}
@Override
public void updateLight() {
if (bogeys == null) return;
var pos = new BlockPos(entity.getLightProbePosition(AnimationTickHolder.getPartialTicks()));
int block = world.getBrightness(LightLayer.BLOCK, pos);
int sky = world.getBrightness(LightLayer.SKY, pos);
bogeys.forEach(instance -> {
if (instance != null)
instance.updateLight(block, sky);
});
}
@Override
public void remove() {
bogeys.forEach(BogeyInstance::remove);
}
}

View file

@ -2,6 +2,7 @@ package com.simibubi.create.content.logistics.trains.track;
import java.util.EnumSet;
import com.jozufozu.flywheel.api.MaterialManager;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;
@ -10,6 +11,8 @@ import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllTileEntities;
import com.simibubi.create.content.contraptions.relays.elementary.ShaftBlock;
import com.simibubi.create.content.logistics.trains.IBogeyBlock;
import com.simibubi.create.content.logistics.trains.entity.BogeyInstance;
import com.simibubi.create.content.logistics.trains.entity.CarriageBogey;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.render.CachedBufferer;
import com.simibubi.create.foundation.utility.AngleHelper;
@ -36,7 +39,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
public class StandardBogeyBlock extends Block implements IBogeyBlock, ITE<StandardBogeyTileEntity> {
public static final EnumProperty<Axis> AXIS = BlockStateProperties.HORIZONTAL_AXIS;
private boolean large;
private final boolean large;
public StandardBogeyBlock(Properties p_i48440_1_, boolean large) {
super(p_i48440_1_);
@ -114,23 +117,30 @@ public class StandardBogeyBlock extends Block implements IBogeyBlock, ITE<Standa
.light(light)
.renderInto(ms, vb);
if (!large) {
CachedBufferer.partial(AllBlockPartials.BOGEY_FRAME, air)
if (large) {
renderLargeBogey(wheelAngle, ms, light, vb, air);
} else {
renderBogey(wheelAngle, ms, light, vb, air);
}
}
private void renderBogey(float wheelAngle, PoseStack ms, int light, VertexConsumer vb, BlockState air) {
CachedBufferer.partial(AllBlockPartials.BOGEY_FRAME, air)
.light(light)
.renderInto(ms, vb);
for (int side : Iterate.positiveAndNegative) {
ms.pushPose();
CachedBufferer.partial(AllBlockPartials.SMALL_BOGEY_WHEELS, air)
.translate(0, 12 / 16f, side)
.rotateX(wheelAngle)
.light(light)
.renderInto(ms, vb);
for (int side : Iterate.positiveAndNegative) {
ms.pushPose();
CachedBufferer.partial(AllBlockPartials.SMALL_BOGEY_WHEELS, air)
.translate(0, 12 / 16f, side)
.rotateX(wheelAngle)
.light(light)
.renderInto(ms, vb);
ms.popPose();
}
return;
ms.popPose();
}
}
private void renderLargeBogey(float wheelAngle, PoseStack ms, int light, VertexConsumer vb, BlockState air) {
for (int i : Iterate.zeroAndOne)
CachedBufferer.block(AllBlocks.SHAFT.getDefaultState()
.setValue(ShaftBlock.AXIS, Axis.X))
@ -163,18 +173,23 @@ public class StandardBogeyBlock extends Block implements IBogeyBlock, ITE<Standa
.light(light)
.renderInto(ms, vb);
ms.popPose();
}
@Override
public BogeyInstance createInstance(MaterialManager materialManager, CarriageBogey bogey) {
if (large) {
return new BogeyInstance.Drive(bogey, materialManager);
} else {
return new BogeyInstance.Frame(bogey, materialManager);
}
}
@Override
public BlockState rotate(BlockState pState, Rotation pRotation) {
switch (pRotation) {
case COUNTERCLOCKWISE_90:
case CLOCKWISE_90:
return pState.cycle(AXIS);
default:
return pState;
}
return switch (pRotation) {
case COUNTERCLOCKWISE_90, CLOCKWISE_90 -> pState.cycle(AXIS);
default -> pState;
};
}
@Override

View file

@ -42,7 +42,7 @@ public class CreateEntityBuilder<T extends Entity, P> extends EntityBuilder<T, P
}
public CreateEntityBuilder<T, P> instance(NonNullSupplier<BiFunction<MaterialManager, T, EntityInstance<? super T>>> instanceFactory, boolean renderNormally) {
return instance(instanceFactory, be -> true);
return instance(instanceFactory, be -> renderNormally);
}
public CreateEntityBuilder<T, P> instance(NonNullSupplier<BiFunction<MaterialManager, T, EntityInstance<? super T>>> instanceFactory, NonNullPredicate<T> renderNormally) {

View file

@ -63,6 +63,11 @@ public class Couple<T> extends Pair<T, T> implements Iterable<T> {
return Couple.create(function.apply(first, values.first), function.apply(second, values.second));
}
public <S, R> Couple<S> mapNotNullWithParam(BiFunction<T, R, S> function, R value) {
return Couple.create(first != null ? function.apply(first, value) : null,
second != null ? function.apply(second, value) : null);
}
public boolean both(Predicate<T> test) {
return test.test(getFirst()) && test.test(getSecond());
}
@ -125,7 +130,7 @@ public class Couple<T> extends Pair<T, T> implements Iterable<T> {
private static class Couplerator<T> implements Iterator<T> {
int state;
private Couple<T> couple;
private final Couple<T> couple;
public Couplerator(Couple<T> couple) {
this.couple = couple;