Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class DisplayWallOverlay implements IChestOverlay {
private static final float ITEM_Y = 9.25F / 16F;
private static final float ITEM_SIZE = 7.5F / 16F;
private static final float INDICATOR_SIZE = 2.5F / 16F;
private static final float LAYER = 0.0005F;

@Override
public void render(IChest chest, BlockPos pos, Direction face, float partialTick,
Expand All @@ -74,20 +73,22 @@ public void render(IChest chest, BlockPos pos, Direction face, float partialTick
}
return;
}
// Like the overlay offset, so layers do not z-fight far away.
float layer = RenderChestCore.getOverlayOffset(pos) / 2;
poseStack.pushPose();
renderPanel(chest, atlas, poseStack, buffers, light, overlay);
renderPanel(chest, atlas, layer, poseStack, buffers, light, overlay);
ItemStack displayed = wall.getDisplayed(face);
if (!displayed.isEmpty()) {
DisplayStats stats = wall.getDisplayStats(face);
poseStack.translate(0, 0, LAYER);
poseStack.translate(0, 0, layer);
renderDisplayedItem(poseStack, buffers, level, displayed, light);
poseStack.translate(0, 0, LAYER);
poseStack.translate(0, 0, layer);
if (wall.isEnabled(face, DisplayOption.COUNT)) {
String count = IModHelpers.get().getGuiHelpers().quantityToScaledString(stats.count());
ChestOverlayHelpers.renderText(poseStack, buffers, count, 0.5F, TEXT_Y0, TEXT_HEIGHT, COLOR_TEXT, light);
}
if (wall.isEnabled(face, DisplayOption.FILL_LEVEL)) {
renderFillBar(atlas.getSprite(BAR_TEXTURE), stats.getFillLevel(), poseStack, buffers, light, overlay);
renderFillBar(atlas.getSprite(BAR_TEXTURE), stats.getFillLevel(), layer, poseStack, buffers, light, overlay);
}
if (wall.isEnabled(face, DisplayOption.UPGRADE_INDICATORS)) {
renderIndicators(atlas, stats, poseStack, buffers, light, overlay);
Expand All @@ -96,16 +97,15 @@ public void render(IChest chest, BlockPos pos, Direction face, float partialTick
poseStack.popPose();
}

private void renderPanel(IChest chest, TextureAtlas atlas, PoseStack poseStack, MultiBufferSource buffers,
private void renderPanel(IChest chest, TextureAtlas atlas, float layer, PoseStack poseStack, MultiBufferSource buffers,
int light, int overlay) {
// A frame of the chest's material, so the panel looks built into it.
ChestMaterial material = chest.getChestMaterial();
if (material != null) {
ResourceLocation frame = material.id().withPrefix("block/chest_wall_");
TextureAtlasSprite frameSprite = atlas.getSprite(frame);
ChestOverlayHelpers.renderSprite(poseStack, buffers, frameSprite, 0, 0, 1, 1, light, overlay);
ChestOverlayHelpers.renderSides(poseStack, buffers, frameSprite, RenderChestCore.OVERLAY_OFFSET, light, overlay);
poseStack.translate(0, 0, LAYER);
poseStack.translate(0, 0, layer);
}
// The panel texture has a transparent rim, so the frame shows around it.
ChestOverlayHelpers.renderSprite(poseStack, buffers, atlas.getSprite(PANEL_TEXTURE), 0, 0, 1, 1, light, overlay);
Expand All @@ -129,11 +129,11 @@ private void renderDisplayedItem(PoseStack poseStack, MultiBufferSource buffers,
poseStack.popPose();
}

private void renderFillBar(TextureAtlasSprite bar, float fill, PoseStack poseStack, MultiBufferSource buffers, int light, int overlay) {
private void renderFillBar(TextureAtlasSprite bar, float fill, float layer, PoseStack poseStack, MultiBufferSource buffers, int light, int overlay) {
ChestOverlayHelpers.renderSprite(poseStack, buffers, bar, BAR_X0, BAR_Y0, BAR_X1, BAR_Y1, light, overlay, COLOR_BAR_BACKGROUND);
if (fill > 0) {
poseStack.pushPose();
poseStack.translate(0, 0, LAYER);
poseStack.translate(0, 0, layer);
ChestOverlayHelpers.renderSprite(poseStack, buffers, bar, BAR_X0, BAR_Y0, BAR_X0 + (BAR_X1 - BAR_X0) * fill, BAR_Y1,
light, overlay, fill >= 1 ? COLOR_BAR_FULL : COLOR_BAR);
poseStack.popPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.renderer.LevelRenderer;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.Vec3;
import org.cyclops.colossalchests2.api.ChestMaterial;
import org.cyclops.colossalchests2.api.client.ChestOverlays;
import org.cyclops.colossalchests2.api.client.IChestOverlay;
Expand All @@ -34,10 +36,13 @@
public class RenderChestCore implements BlockEntityRenderer<BlockEntityChestCore> {

/**
* How far overlays float in front of the chest surface, far enough to avoid z-fighting.
* How far overlays float in front of the chest surface, per block of camera distance.
* Like vanilla's view offset layering, this keeps them flush up close while avoiding z-fighting far away,
* where depth precision drops.
* The lock sticks out further, so it hides the part of an overlay right behind it, like any object in front.
*/
public static final float OVERLAY_OFFSET = 1F / 32F;
public static final float OVERLAY_OFFSET_PER_DISTANCE = 1F / 2048F;
public static final float OVERLAY_OFFSET_MIN = 1F / 1024F;

private static final float MODEL_HEIGHT = 14F / 16F;
private static final float LID_PIVOT_Y = 9F / 16F;
Expand Down Expand Up @@ -122,7 +127,7 @@ protected void renderOverlays(BlockEntityChestCore core, ChestStructure structur
applyLidTransform(poseStack, origin, structure, facing, lidRotation);
}
poseStack.translate(pos.getX() - origin.getX(), pos.getY() - origin.getY(), pos.getZ() - origin.getZ());
applyFaceTransform(poseStack, face, facing);
applyFaceTransform(poseStack, face, facing, getOverlayOffset(pos));
int light = LevelRenderer.getLightColor(level, pos.relative(face));
chestOverlay.render(core, pos, face, partialTick, poseStack, buffers, light, overlay);
poseStack.popPose();
Expand Down Expand Up @@ -150,17 +155,33 @@ public static void applyLidTransform(PoseStack poseStack, BlockPos origin, Chest
}

/**
* Map a block's unit cube onto the face-local unit square of {@link IChestOverlay}.
* @param pos A member position.
* @return How far overlays on it float in front of the chest surface.
*/
public static void applyFaceTransform(PoseStack poseStack, Direction face, Direction facing) {
public static float getOverlayOffset(BlockPos pos) {
return getOverlayOffset(Vec3.atCenterOf(pos).distanceTo(Minecraft.getInstance().gameRenderer.getMainCamera().getPosition()));
}

/**
* @param distance The camera distance.
* @return How far overlays float in front of the chest surface.
*/
public static float getOverlayOffset(double distance) {
return Math.max(OVERLAY_OFFSET_MIN, (float) distance * OVERLAY_OFFSET_PER_DISTANCE);
}

/**
* Map a block's unit cube onto the face-local unit square of {@link IChestOverlay}, offset in front of it.
*/
public static void applyFaceTransform(PoseStack poseStack, Direction face, Direction facing, float offset) {
poseStack.translate(0.5F, 0.5F, 0.5F);
if (face.getAxis().isHorizontal()) {
poseStack.mulPose(Axis.YP.rotationDegrees(-face.toYRot()));
} else {
poseStack.mulPose(Axis.YP.rotationDegrees(-facing.toYRot()));
poseStack.mulPose(Axis.XP.rotationDegrees(face == Direction.UP ? -90 : 90));
}
poseStack.translate(-0.5F, -0.5F, 0.5F + OVERLAY_OFFSET);
poseStack.translate(-0.5F, -0.5F, 0.5F + offset);
}

protected Material getMaterial(ChestMaterial material) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class TestRenderChestCoreTransforms {

private static final float EPSILON = 0.0001F;
private static final float OUT = RenderChestCore.OVERLAY_OFFSET;
private static final float OUT = 0.01F;
private static final ChestStructure STRUCTURE = new ChestStructure(BlockPos.ZERO, 3);

private static Vector3f transform(PoseStack poseStack, float x, float y, float z) {
Expand All @@ -32,7 +32,7 @@ private static void assertPoint(float x, float y, float z, Vector3f actual) {

private static PoseStack face(Direction face, Direction facing) {
PoseStack poseStack = new PoseStack();
RenderChestCore.applyFaceTransform(poseStack, face, facing);
RenderChestCore.applyFaceTransform(poseStack, face, facing, OUT);
return poseStack;
}

Expand Down Expand Up @@ -78,6 +78,12 @@ public void testFaceDownPointsToFront() {
assertPoint(0, -OUT, 1, transform(poseStack, 0, 1, 0));
}

@Test
public void testOverlayOffsetGrowsWithDistance() {
assertEquals(RenderChestCore.OVERLAY_OFFSET_MIN, RenderChestCore.getOverlayOffset(0), 0);
assertEquals(64 * RenderChestCore.OVERLAY_OFFSET_PER_DISTANCE, RenderChestCore.getOverlayOffset(64), EPSILON);
}

@Test
public void testChestModelFillsStructure() {
for (Direction facing : Direction.Plane.HORIZONTAL) {
Expand Down
Loading