From 55ea7e3e55c9be19b0e4b94c854f862385213198 Mon Sep 17 00:00:00 2001 From: JozsefA Date: Sat, 23 Jan 2021 21:02:11 -0800 Subject: [PATCH] cart contraptions are lit --- .../bearing/StabilizedLighter.java | 40 ++++++------------- .../mounted/MountedContraption.java | 6 +++ .../mounted/MountedLighter.java | 30 ++++++++++++++ .../simibubi/create/events/ClientEvents.java | 2 + .../ContraptionRenderDispatcher.java | 7 +++- .../render/light/ContraptionLighter.java | 21 +++++++--- .../render/light/GridAlignedBB.java | 39 ++++++++++++++++++ .../foundation/render/light/LightVolume.java | 18 ++++++--- .../resources/assets/create/shader/belt.vert | 4 +- 9 files changed, 126 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedLighter.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedLighter.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedLighter.java index 556d22462..b92901b77 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedLighter.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedLighter.java @@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.be import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; import com.simibubi.create.foundation.render.light.ContraptionLighter; import com.simibubi.create.foundation.render.light.GridAlignedBB; import net.minecraft.client.world.ClientWorld; @@ -15,38 +16,21 @@ public class StabilizedLighter extends ContraptionLighter super(contraption); } + @Override + public void tick(RenderedContraption owner) { + GridAlignedBB contraptionBounds = getContraptionBounds(); + + if (!contraptionBounds.sameAs(bounds)) { + lightVolume.move(contraption.entity.world, contraptionBoundsToVolume(contraptionBounds)); + bounds = contraptionBounds; + } + } + @Override public GridAlignedBB getContraptionBounds() { GridAlignedBB bb = GridAlignedBB.fromAABB(contraption.bounds); - // TODO: this whole thing is a hack and is not generalizable - Iterable allEntities = ((ClientWorld) contraption.entity.world).getAllEntities(); - - for (Entity entity : allEntities) { - - if (entity.getUniqueID() == contraption.parentID && entity instanceof AbstractContraptionEntity) { - Contraption mountedOn = ((AbstractContraptionEntity) entity).getContraption(); - - GridAlignedBB mountedBounds = GridAlignedBB.fromAABB(mountedOn.bounds); - - Vec3i dir = contraption.getFacing().getDirectionVec(); - - int mulX = 1 - Math.abs(dir.getX()); - int mulY = 1 - Math.abs(dir.getY()); - int mulZ = 1 - Math.abs(dir.getZ()); - - bb.minX -= mulX * mountedBounds.sizeX(); - bb.minY -= mulY * mountedBounds.sizeY(); - bb.minZ -= mulZ * mountedBounds.sizeZ(); - bb.maxX += mulX * mountedBounds.sizeX(); - bb.maxY += mulY * mountedBounds.sizeY(); - bb.maxZ += mulZ * mountedBounds.sizeZ(); - - break; - } - } - - bb.translate(contraption.anchor); + bb.translate(contraption.entity.getPosition()); return bb; } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java index 3d6a28dd8..38406550a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java @@ -4,6 +4,7 @@ import static com.simibubi.create.content.contraptions.components.structureMovem import java.util.List; +import com.simibubi.create.foundation.render.light.ContraptionLighter; import org.apache.commons.lang3.tuple.Pair; import com.simibubi.create.AllBlocks; @@ -160,4 +161,9 @@ public class MountedContraption extends Contraption { IItemHandlerModifiable handlerFromInv = new InvWrapper((IInventory) cart); inventory = new CombinedInvWrapper(handlerFromInv, inventory); } + + @Override + public ContraptionLighter makeLighter() { + return new MountedLighter(this); + } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedLighter.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedLighter.java new file mode 100644 index 000000000..8523a9c13 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedLighter.java @@ -0,0 +1,30 @@ +package com.simibubi.create.content.contraptions.components.structureMovement.mounted; + +import com.simibubi.create.foundation.render.contraption.RenderedContraption; +import com.simibubi.create.foundation.render.light.ContraptionLighter; +import com.simibubi.create.foundation.render.light.GridAlignedBB; + +public class MountedLighter extends ContraptionLighter { + public MountedLighter(MountedContraption contraption) { + super(contraption); + } + + @Override + public void tick(RenderedContraption owner) { + GridAlignedBB contraptionBounds = getContraptionBounds(); + + if (!contraptionBounds.sameAs(bounds)) { + lightVolume.move(contraption.entity.world, contraptionBoundsToVolume(contraptionBounds)); + bounds = contraptionBounds; + } + } + + @Override + public GridAlignedBB getContraptionBounds() { + GridAlignedBB bb = GridAlignedBB.fromAABB(contraption.bounds); + + bb.translate(contraption.entity.getPosition()); + + return bb; + } +} diff --git a/src/main/java/com/simibubi/create/events/ClientEvents.java b/src/main/java/com/simibubi/create/events/ClientEvents.java index 39a622a52..7824ea87c 100644 --- a/src/main/java/com/simibubi/create/events/ClientEvents.java +++ b/src/main/java/com/simibubi/create/events/ClientEvents.java @@ -26,6 +26,7 @@ import com.simibubi.create.foundation.networking.AllPackets; import com.simibubi.create.foundation.networking.LeftClickPacket; import com.simibubi.create.foundation.render.FastRenderDispatcher; import com.simibubi.create.foundation.render.RenderWork; +import com.simibubi.create.foundation.render.contraption.ContraptionRenderDispatcher; import com.simibubi.create.foundation.render.light.LightVolumeDebugger; import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer; import com.simibubi.create.foundation.tileEntity.behaviour.edgeInteraction.EdgeInteractionRenderer; @@ -106,6 +107,7 @@ public class ClientEvents { ArmInteractionPointHandler.tick(); PlacementHelpers.tick(); CreateClient.outliner.tickOutlines(); + ContraptionRenderDispatcher.tick(); } @SubscribeEvent diff --git a/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionRenderDispatcher.java b/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionRenderDispatcher.java index 5f55119fd..6795420dc 100644 --- a/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionRenderDispatcher.java +++ b/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionRenderDispatcher.java @@ -2,7 +2,6 @@ package com.simibubi.create.foundation.render.contraption; import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; -import com.simibubi.create.foundation.render.FastKineticRenderer; import com.simibubi.create.foundation.render.shader.Shader; import com.simibubi.create.foundation.render.shader.ShaderCallback; import com.simibubi.create.foundation.render.shader.ShaderHelper; @@ -30,6 +29,12 @@ public class ContraptionRenderDispatcher { } } + public static void tick() { + for (RenderedContraption contraption : renderers.values()) { + contraption.getLighter().tick(contraption); + } + } + private static RenderedContraption getRenderer(World world, Contraption c) { RenderedContraption renderer; int entityId = c.entity.getEntityId(); diff --git a/src/main/java/com/simibubi/create/foundation/render/light/ContraptionLighter.java b/src/main/java/com/simibubi/create/foundation/render/light/ContraptionLighter.java index 110113283..68b4342cc 100644 --- a/src/main/java/com/simibubi/create/foundation/render/light/ContraptionLighter.java +++ b/src/main/java/com/simibubi/create/foundation/render/light/ContraptionLighter.java @@ -1,6 +1,7 @@ package com.simibubi.create.foundation.render.light; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; import net.minecraft.util.math.SectionPos; import net.minecraft.world.ILightReader; import net.minecraft.world.LightType; @@ -11,18 +12,28 @@ public abstract class ContraptionLighter { protected final C contraption; public final LightVolume lightVolume; + protected GridAlignedBB bounds; + protected ContraptionLighter(C contraption) { this.contraption = contraption; - GridAlignedBB bounds = getContraptionBounds(); - bounds.grow(1); // so we have at least enough data on the edges to avoid artifacts and have smooth lighting - bounds.minY = Math.max(bounds.minY, 0); - bounds.maxY = Math.min(bounds.maxY, 255); + bounds = getContraptionBounds(); - lightVolume = new LightVolume(bounds); + lightVolume = new LightVolume(contraptionBoundsToVolume(bounds)); lightVolume.initialize(contraption.entity.world); } + protected GridAlignedBB contraptionBoundsToVolume(GridAlignedBB bounds) { + bounds = bounds.copy(); + bounds.grow(1); // so we have at least enough data on the edges to avoid artifacts and have smooth lighting + bounds.minY = Math.max(bounds.minY, 0); + bounds.maxY = Math.min(bounds.maxY, 255); + + return bounds; + } + + public void tick(RenderedContraption owner) {} + public abstract GridAlignedBB getContraptionBounds(); } diff --git a/src/main/java/com/simibubi/create/foundation/render/light/GridAlignedBB.java b/src/main/java/com/simibubi/create/foundation/render/light/GridAlignedBB.java index f47889b11..210a74031 100644 --- a/src/main/java/com/simibubi/create/foundation/render/light/GridAlignedBB.java +++ b/src/main/java/com/simibubi/create/foundation/render/light/GridAlignedBB.java @@ -55,6 +55,19 @@ public class GridAlignedBB { return new AxisAlignedBB(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.maxY, bb.maxZ); } + public GridAlignedBB copy() { + return copy(this); + } + + public boolean sameAs(GridAlignedBB other) { + return minX == other.minX && + minY == other.minY && + minZ == other.minZ && + maxX == other.maxX && + maxY == other.maxY && + maxZ == other.maxZ; + } + public int sizeX() { return maxX - minX; } @@ -272,4 +285,30 @@ public class GridAlignedBB { } } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + GridAlignedBB that = (GridAlignedBB) o; + + if (minX != that.minX) return false; + if (minY != that.minY) return false; + if (minZ != that.minZ) return false; + if (maxX != that.maxX) return false; + if (maxY != that.maxY) return false; + return maxZ == that.maxZ; + } + + @Override + public int hashCode() { + int result = minX; + result = 31 * result + minY; + result = 31 * result + minZ; + result = 31 * result + maxX; + result = 31 * result + maxY; + result = 31 * result + maxZ; + return result; + } + } diff --git a/src/main/java/com/simibubi/create/foundation/render/light/LightVolume.java b/src/main/java/com/simibubi/create/foundation/render/light/LightVolume.java index 7b180c52b..e0f251bcf 100644 --- a/src/main/java/com/simibubi/create/foundation/render/light/LightVolume.java +++ b/src/main/java/com/simibubi/create/foundation/render/light/LightVolume.java @@ -17,8 +17,8 @@ import java.util.concurrent.atomic.AtomicBoolean; // to reread all the lighting data in those cases. public class LightVolume { - private final GridAlignedBB sampleVolume; - private final GridAlignedBB textureVolume; + private GridAlignedBB sampleVolume; + private GridAlignedBB textureVolume; private ByteBuffer lightData; private boolean bufferDirty; @@ -27,14 +27,18 @@ public class LightVolume { private int glTexture; public LightVolume(GridAlignedBB sampleVolume) { - this.sampleVolume = GridAlignedBB.copy(sampleVolume); - sampleVolume.nextPowerOf2Centered(); - this.textureVolume = sampleVolume; + setSampleVolume(sampleVolume); this.glTexture = GL11.glGenTextures(); this.lightData = MemoryUtil.memAlloc(this.textureVolume.volume() * 2); // TODO: maybe figure out how to pack light coords into a single byte } + private void setSampleVolume(GridAlignedBB sampleVolume) { + this.sampleVolume = sampleVolume; + this.textureVolume = sampleVolume.copy(); + this.textureVolume.nextPowerOf2Centered(); + } + public GridAlignedBB getTextureVolume() { return GridAlignedBB.copy(textureVolume); } @@ -79,6 +83,10 @@ public class LightVolume { return textureVolume.sizeZ(); } + public void move(ILightReader world, GridAlignedBB newSampleVolume) { + setSampleVolume(newSampleVolume); + initialize(world); + } public void notifyLightUpdate(ILightReader world, LightType type, SectionPos location) { GridAlignedBB changedVolume = GridAlignedBB.fromSection(location); diff --git a/src/main/resources/assets/create/shader/belt.vert b/src/main/resources/assets/create/shader/belt.vert index 6e4ba0fbf..8a9c50380 100644 --- a/src/main/resources/assets/create/shader/belt.vert +++ b/src/main/resources/assets/create/shader/belt.vert @@ -53,9 +53,9 @@ void main() { float scrollSize = scrollTexture.w - scrollTexture.y; - float scroll = fract(speed * time / (36 * 16.)) * scrollSize * scrollMult; + float scroll = fract(speed * time / (36 * 16)) * scrollSize * scrollMult; - vec3 norm = (rotation * vec4(aNormal, 0.)).xyz; + vec3 norm = (rotation * vec4(aNormal, 0)).xyz; Diffuse = diffuse(norm); Light = light;