cart contraptions are lit

This commit is contained in:
JozsefA 2021-01-23 21:02:11 -08:00
parent dea3f05364
commit 55ea7e3e55
9 changed files with 126 additions and 41 deletions

View file

@ -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.AbstractContraptionEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; 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.ContraptionLighter;
import com.simibubi.create.foundation.render.light.GridAlignedBB; import com.simibubi.create.foundation.render.light.GridAlignedBB;
import net.minecraft.client.world.ClientWorld; import net.minecraft.client.world.ClientWorld;
@ -15,38 +16,21 @@ public class StabilizedLighter extends ContraptionLighter<StabilizedContraption>
super(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 @Override
public GridAlignedBB getContraptionBounds() { public GridAlignedBB getContraptionBounds() {
GridAlignedBB bb = GridAlignedBB.fromAABB(contraption.bounds); GridAlignedBB bb = GridAlignedBB.fromAABB(contraption.bounds);
// TODO: this whole thing is a hack and is not generalizable bb.translate(contraption.entity.getPosition());
Iterable<Entity> 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);
return bb; return bb;
} }

View file

@ -4,6 +4,7 @@ import static com.simibubi.create.content.contraptions.components.structureMovem
import java.util.List; import java.util.List;
import com.simibubi.create.foundation.render.light.ContraptionLighter;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
@ -160,4 +161,9 @@ public class MountedContraption extends Contraption {
IItemHandlerModifiable handlerFromInv = new InvWrapper((IInventory) cart); IItemHandlerModifiable handlerFromInv = new InvWrapper((IInventory) cart);
inventory = new CombinedInvWrapper(handlerFromInv, inventory); inventory = new CombinedInvWrapper(handlerFromInv, inventory);
} }
@Override
public ContraptionLighter<?> makeLighter() {
return new MountedLighter(this);
}
} }

View file

@ -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<MountedContraption> {
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;
}
}

View file

@ -26,6 +26,7 @@ import com.simibubi.create.foundation.networking.AllPackets;
import com.simibubi.create.foundation.networking.LeftClickPacket; import com.simibubi.create.foundation.networking.LeftClickPacket;
import com.simibubi.create.foundation.render.FastRenderDispatcher; import com.simibubi.create.foundation.render.FastRenderDispatcher;
import com.simibubi.create.foundation.render.RenderWork; 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.render.light.LightVolumeDebugger;
import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer; import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer;
import com.simibubi.create.foundation.tileEntity.behaviour.edgeInteraction.EdgeInteractionRenderer; import com.simibubi.create.foundation.tileEntity.behaviour.edgeInteraction.EdgeInteractionRenderer;
@ -106,6 +107,7 @@ public class ClientEvents {
ArmInteractionPointHandler.tick(); ArmInteractionPointHandler.tick();
PlacementHelpers.tick(); PlacementHelpers.tick();
CreateClient.outliner.tickOutlines(); CreateClient.outliner.tickOutlines();
ContraptionRenderDispatcher.tick();
} }
@SubscribeEvent @SubscribeEvent

View file

@ -2,7 +2,6 @@ package com.simibubi.create.foundation.render.contraption;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; 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.Shader;
import com.simibubi.create.foundation.render.shader.ShaderCallback; import com.simibubi.create.foundation.render.shader.ShaderCallback;
import com.simibubi.create.foundation.render.shader.ShaderHelper; 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) { private static RenderedContraption getRenderer(World world, Contraption c) {
RenderedContraption renderer; RenderedContraption renderer;
int entityId = c.entity.getEntityId(); int entityId = c.entity.getEntityId();

View file

@ -1,6 +1,7 @@
package com.simibubi.create.foundation.render.light; package com.simibubi.create.foundation.render.light;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; 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.util.math.SectionPos;
import net.minecraft.world.ILightReader; import net.minecraft.world.ILightReader;
import net.minecraft.world.LightType; import net.minecraft.world.LightType;
@ -11,18 +12,28 @@ public abstract class ContraptionLighter<C extends Contraption> {
protected final C contraption; protected final C contraption;
public final LightVolume lightVolume; public final LightVolume lightVolume;
protected GridAlignedBB bounds;
protected ContraptionLighter(C contraption) { protected ContraptionLighter(C contraption) {
this.contraption = contraption; this.contraption = contraption;
GridAlignedBB bounds = getContraptionBounds(); 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);
lightVolume = new LightVolume(bounds); lightVolume = new LightVolume(contraptionBoundsToVolume(bounds));
lightVolume.initialize(contraption.entity.world); 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(); public abstract GridAlignedBB getContraptionBounds();
} }

View file

@ -55,6 +55,19 @@ public class GridAlignedBB {
return new AxisAlignedBB(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.maxY, bb.maxZ); 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() { public int sizeX() {
return maxX - minX; 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;
}
} }

View file

@ -17,8 +17,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
// to reread all the lighting data in those cases. // to reread all the lighting data in those cases.
public class LightVolume { public class LightVolume {
private final GridAlignedBB sampleVolume; private GridAlignedBB sampleVolume;
private final GridAlignedBB textureVolume; private GridAlignedBB textureVolume;
private ByteBuffer lightData; private ByteBuffer lightData;
private boolean bufferDirty; private boolean bufferDirty;
@ -27,14 +27,18 @@ public class LightVolume {
private int glTexture; private int glTexture;
public LightVolume(GridAlignedBB sampleVolume) { public LightVolume(GridAlignedBB sampleVolume) {
this.sampleVolume = GridAlignedBB.copy(sampleVolume); setSampleVolume(sampleVolume);
sampleVolume.nextPowerOf2Centered();
this.textureVolume = sampleVolume;
this.glTexture = GL11.glGenTextures(); 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 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() { public GridAlignedBB getTextureVolume() {
return GridAlignedBB.copy(textureVolume); return GridAlignedBB.copy(textureVolume);
} }
@ -79,6 +83,10 @@ public class LightVolume {
return textureVolume.sizeZ(); return textureVolume.sizeZ();
} }
public void move(ILightReader world, GridAlignedBB newSampleVolume) {
setSampleVolume(newSampleVolume);
initialize(world);
}
public void notifyLightUpdate(ILightReader world, LightType type, SectionPos location) { public void notifyLightUpdate(ILightReader world, LightType type, SectionPos location) {
GridAlignedBB changedVolume = GridAlignedBB.fromSection(location); GridAlignedBB changedVolume = GridAlignedBB.fromSection(location);

View file

@ -53,9 +53,9 @@ void main() {
float scrollSize = scrollTexture.w - scrollTexture.y; 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); Diffuse = diffuse(norm);
Light = light; Light = light;