From 9c8439fe0824907acd1821ae04a03802d918ea9c Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sun, 3 Apr 2022 15:08:14 -0700 Subject: [PATCH] Better bogey brightness - Deduplicate bogey translation code --- .../trains/entity/BogeyInstance.java | 21 ++++++++ .../CarriageContraptionEntityRenderer.java | 50 +++++++++++++------ .../entity/CarriageContraptionInstance.java | 18 +------ 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java index 92ff2c913..fde1ee81e 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyInstance.java @@ -1,9 +1,12 @@ package com.simibubi.create.content.logistics.trains.entity; +import org.jetbrains.annotations.Nullable; + 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.jozufozu.flywheel.util.AnimationTickHolder; import com.mojang.blaze3d.vertex.PoseStack; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.AllBlocks; @@ -12,7 +15,11 @@ 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.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.world.level.BlockAndTintGetter; +import net.minecraft.world.level.LightLayer; +import net.minecraft.world.phys.Vec3; public sealed class BogeyInstance { @@ -47,6 +54,20 @@ public sealed class BogeyInstance { .unCentre(); } + public void updateLight(BlockAndTintGetter world, CarriageContraptionEntity entity) { + var lightPos = new BlockPos(getLightPos(entity)); + + updateLight(world.getBrightness(LightLayer.BLOCK, lightPos), world.getBrightness(LightLayer.SKY, lightPos)); + } + + private Vec3 getLightPos(CarriageContraptionEntity entity) { + if (bogey.getAnchorPosition() != null) { + return bogey.getAnchorPosition(); + } else { + return entity.getLightProbePosition(AnimationTickHolder.getPartialTicks()); + } + } + public void updateLight(int blockLight, int skyLight) { for (ModelData shaft : shafts) { shaft.setBlockLight(blockLight).setSkyLight(skyLight); diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionEntityRenderer.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionEntityRenderer.java index b8dd14784..2f91c04b1 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionEntityRenderer.java @@ -1,13 +1,18 @@ package com.simibubi.create.content.logistics.trains.entity; +import java.util.Objects; + 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; +import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.culling.Frustum; import net.minecraft.client.renderer.entity.EntityRendererProvider; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.LightLayer; import net.minecraft.world.phys.Vec3; public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer { @@ -34,24 +39,41 @@ 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; + boolean usingFlywheel = Backend.isOn(); + Vec3 position = entity.getPosition(partialTicks); - ms.pushPose(); + float viewYRot = entity.getViewYRot(partialTicks); + float viewXRot = entity.getViewXRot(partialTicks); + int bogeySpacing = carriage.bogeySpacing; + carriage.bogeys.forEach(bogey -> { if (bogey == null) return; - ms.pushPose(); - float viewYRot = entity.getViewYRot(partialTicks); - float viewXRot = entity.getViewXRot(partialTicks); - int bogeySpacing = carriage.bogeySpacing; - TransformStack.cast(ms) + if (!usingFlywheel) { + ms.pushPose(); + translateBogey(ms, bogey, bogeySpacing, viewYRot, viewXRot, partialTicks); + + int light = getBogeyLightCoords(entity, bogey, partialTicks); + bogey.type.render(null, bogey.wheelAngle.getValue(partialTicks), ms, partialTicks, buffers, light, overlay); + + ms.popPose(); + } + + bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, bogey.isLeading); + if (!carriage.isOnTwoBogeys()) + bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, !bogey.isLeading); + + }); + } + + public static void translateBogey(PoseStack ms, CarriageBogey bogey, int bogeySpacing, float viewYRot, float viewXRot, float partialTicks) { + TransformStack.cast(ms) .rotateY(viewYRot + 90) .rotateX(-viewXRot) .rotateY(180) @@ -62,17 +84,13 @@ public class CarriageContraptionEntityRenderer extends ContraptionEntityRenderer .rotateY(bogey.yaw.getValue(partialTicks)) .rotateX(bogey.pitch.getValue(partialTicks)) .translate(0, .5f, 0); + } - bogey.type.render(null, bogey.wheelAngle.getValue(partialTicks), ms, partialTicks, buffers, - getPackedLightCoords(entity, partialTicks), overlay); - bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, bogey.isLeading); - if (!carriage.isOnTwoBogeys()) - bogey.updateCouplingAnchor(position, viewXRot, viewYRot, bogeySpacing, partialTicks, !bogey.isLeading); + public static int getBogeyLightCoords(CarriageContraptionEntity entity, CarriageBogey bogey, float partialTicks) { - ms.popPose(); - }); - ms.popPose(); + var lightPos = new BlockPos(Objects.requireNonNullElseGet(bogey.getAnchorPosition(), () -> entity.getLightProbePosition(partialTicks))); + return LightTexture.pack(entity.level.getBrightness(LightLayer.BLOCK, lightPos), entity.level.getBrightness(LightLayer.SKY, lightPos)); } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionInstance.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionInstance.java index 7f47c8280..77fee5118 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/CarriageContraptionInstance.java @@ -58,17 +58,7 @@ public class CarriageContraptionInstance extends EntityInstance { if (instance != null) - instance.updateLight(block, sky); + instance.updateLight(world, entity); }); }