From e18cd583636fadf8ad2a18817b155ff430736041 Mon Sep 17 00:00:00 2001 From: XyperCode Date: Sun, 21 Jan 2024 01:25:11 +0100 Subject: [PATCH] "Tried" fixing printer stuff some more. --- .gitignore | 3 + .../com/ultreon/devices/api/print/IPrint.java | 8 ++- .../entity/renderer/PrinterRenderer.java | 62 +++++++++++-------- .../devices/programs/PixelPainterApp.java | 30 +++++++-- 4 files changed, 70 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 04ad9a62..93b272be 100644 --- a/.gitignore +++ b/.gitignore @@ -135,3 +135,6 @@ run/ # Ignore datagen cache. /common/src/main/generated/resources/.cache/ + +/*/bin/ +/.vscode/ diff --git a/common/src/main/java/com/ultreon/devices/api/print/IPrint.java b/common/src/main/java/com/ultreon/devices/api/print/IPrint.java index 12c1387c..8801d91b 100644 --- a/common/src/main/java/com/ultreon/devices/api/print/IPrint.java +++ b/common/src/main/java/com/ultreon/devices/api/print/IPrint.java @@ -1,9 +1,11 @@ package com.ultreon.devices.api.print; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; import com.ultreon.devices.init.DeviceBlocks; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.world.item.ItemStack; @@ -78,6 +80,10 @@ static ItemStack generateItem(IPrint print) { Class getRenderer(); interface Renderer { - boolean render(PoseStack pose, CompoundTag data); + default boolean render(PoseStack pose, CompoundTag data) { + return render(pose, null, data, 0); + } + + boolean render(PoseStack pose, VertexConsumer buffer, CompoundTag data, int packedLight); } } diff --git a/common/src/main/java/com/ultreon/devices/block/entity/renderer/PrinterRenderer.java b/common/src/main/java/com/ultreon/devices/block/entity/renderer/PrinterRenderer.java index f80d04c3..ac1ad754 100644 --- a/common/src/main/java/com/ultreon/devices/block/entity/renderer/PrinterRenderer.java +++ b/common/src/main/java/com/ultreon/devices/block/entity/renderer/PrinterRenderer.java @@ -36,6 +36,10 @@ * @author MrCrayfish */ public record PrinterRenderer(BlockEntityRendererProvider.Context context) implements BlockEntityRenderer { + private static final Quaternionf tmpQ = new Quaternionf(); + public static final float DEG2RAD = 0.017453292519943295f; + public static final double PIXEL_SIZE = 0.015625; + @Override public void render(PrinterBlockEntity blockEntity, float partialTick, @NotNull PoseStack pose, @NotNull MultiBufferSource bufferSource, int packedLight, int packedOverlay) { PaperModel paperModel = new PaperModel(Minecraft.getInstance().getEntityModels().bakeLayer(PaperModel.LAYER_LOCATION)); @@ -55,7 +59,7 @@ public void render(PrinterBlockEntity blockEntity, float partialTick, @NotNull P } pose.pushPose(); - renderPrint(blockEntity, pose, state, bufferSource, paperModel); + renderPrint(blockEntity, pose, state, bufferSource, packedLight, paperModel); pose.popPose(); pose.pushPose(); @@ -69,70 +73,74 @@ public void render(PrinterBlockEntity blockEntity, float partialTick, @NotNull P private static void renderPaper(@NotNull PoseStack pose, @NotNull MultiBufferSource bufferSource, int packedLight, BlockState state, PaperModel paperModel) { pose.translate(0.5, 0.5, 0.5); pose.mulPose(state.getValue(PrinterBlock.FACING).getRotation()); - pose.mulPose(new Quaternionf().rotateX(67.5f * 0.017453292519943295f)); + pose.mulPose(new Quaternionf().rotateX(67.5f * DEG2RAD)); pose.translate(0, 0, 0.4); - pose.translate(-13 * 0.015625, -13 * 0.015625, -1 * 0.015625); + pose.translate(-13 * PIXEL_SIZE, -13 * PIXEL_SIZE, -1 * PIXEL_SIZE); pose.scale(0.3f, 0.3f, 0.3f); - //region - VertexConsumer vertexconsumer = bufferSource.getBuffer(paperModel.renderType(PaperModel.TEXTURE)); - paperModel.renderToBuffer(pose, vertexconsumer, packedLight, OverlayTexture.NO_OVERLAY, 1f, 1f, 1f, 1f); - //endregion + drawBuffer(pose, bufferSource, packedLight, paperModel); } - private static void renderPrint(PrinterBlockEntity blockEntity, @NotNull PoseStack pose, BlockState state, @NotNull MultiBufferSource bufferSource, PaperModel paperModel) { + private static void renderPrint(PrinterBlockEntity blockEntity, @NotNull PoseStack pose, BlockState state, @NotNull MultiBufferSource bufferSource, int packedLight, PaperModel paperModel) { if (blockEntity.isLoading()) { pose.translate(0.5, 0.5, 0.5); pose.mulPose(state.getValue(PrinterBlock.FACING).getRotation()); - pose.mulPose(new Quaternionf().rotateX(67.5f * 0.017453292519943295f)); + pose.mulPose(tmpQ.identity().rotateX(67.5f * DEG2RAD)); double progress = Math.max(-0.4, -0.4 + (0.4 * ((double) (blockEntity.getRemainingPrintTime() - 10) / 20))); pose.translate(0, -progress, 0.4); - pose.translate(-13 * 0.015625, -13 * 0.015625, -1 * 0.015625); + pose.translate(-13 * PIXEL_SIZE, -13 * PIXEL_SIZE, -1 * PIXEL_SIZE); pose.scale(0.3f, 0.3f, 0.3f); - // region - VertexConsumer vertexconsumer = bufferSource.getBuffer(paperModel.renderType(PaperModel.TEXTURE)); - paperModel.renderToBuffer(pose, vertexconsumer, 0xf000f0, OverlayTexture.NO_OVERLAY, 1f, 1f, 1f, 1f); - // endregion - } else if (true /*blockEntity.isPrinting()*/) { + drawBuffer(pose, bufferSource, packedLight, paperModel); + } else if (blockEntity.isPrinting()) { pose.translate(0.5, 0.078125, 0.5); pose.mulPose(state.getValue(PrinterBlock.FACING).getRotation()); - pose.mulPose(new Quaternionf(1, 0, 0, 90f)); - double progress = -0.35 + (0.50 * 0.5/*((double) (blockEntity.getRemainingPrintTime() - 20) / blockEntity.getTotalPrintTime())*/); + double progress = -0.35 + (((double) (blockEntity.getRemainingPrintTime() - 20) / blockEntity.getTotalPrintTime())); pose.translate(0, -progress, 0); - pose.translate(-13 * 0.015625, -13 * 0.015625, -0.5 * 0.015625); + pose.translate(-13 * PIXEL_SIZE, -13 * PIXEL_SIZE, -0.5 * PIXEL_SIZE); pose.scale(0.3f, 0.3f, 0.3f); - // region - VertexConsumer vertexconsumer = bufferSource.getBuffer(paperModel.renderType(PaperModel.TEXTURE)); - paperModel.renderToBuffer(pose, vertexconsumer, 0xf000f0, OverlayTexture.NO_OVERLAY, 1f, 1f, 1f, 1f); - // endregion + drawBuffer(pose, bufferSource, packedLight, paperModel); - pose.translate(0.3225, 0.085, -0.001); - pose.mulPose(new Quaternionf(0, 1, 0, 180f)); + pose.translate(0.4, 0.085, -0.001); + pose.mulPose(tmpQ.identity().rotateY(180f * DEG2RAD)); + //region IPrint print = blockEntity.getPrint(); if (print != null) { + pose.pushPose(); + pose.translate(-15 * 0.0625, 7.5 * 0.0625, 0); + pose.scale(1 / 16384f, 1 / 16384f, 1 / 16384f); + pose.scale(1 / 1.5f, 1 / 1.5f, 1 / 1.5f); + IPrint.Renderer renderer = PrintingManager.getRenderer(print); - renderer.render(pose, print.toTag()); + VertexConsumer buffer = bufferSource.getBuffer(paperModel.renderType(PaperModel.TEXTURE)); + renderer.render(pose, buffer, print.toTag(), packedLight); + pose.popPose(); } + //endregion } } + private static void drawBuffer(@NotNull PoseStack pose, @NotNull MultiBufferSource bufferSource, int packedLight, PaperModel paperModel) { + VertexConsumer buffer = bufferSource.getBuffer(paperModel.renderType(PaperModel.TEXTURE)); + paperModel.renderToBuffer(pose, buffer, packedLight, OverlayTexture.NO_OVERLAY, 1f, 1f, 1f, 1f); + } + private static void renderDisplay(PrinterBlockEntity blockEntity, @NotNull PoseStack pose, @NotNull MultiBufferSource bufferSource, BlockState state) { RenderSystem.depthMask(false); // region pose.translate(0.5, 0.5, 0.5); pose.mulPose(state.getValue(PrinterBlock.FACING).getRotation()); - pose.mulPose(new Quaternionf().rotateY(180f * 0.017453292519943295f)); + pose.mulPose(tmpQ.identity().rotateY(180f * DEG2RAD)); pose.translate(0.0675, 0.005, -0.032); pose.translate((8 -5.85) * 0.0625, (8 -5) * 0.0625, (-4.25) * 0.0625); pose.pushPose(); pose.scale(-0.010416667f, -0.010416667f, -0.010416667f); - pose.mulPose(new Quaternionf().rotateX((90 + 22.5f) * 0.017453292519943295f)); + pose.mulPose(tmpQ.identity().rotateX((90 + 22.5f) * DEG2RAD)); // endregion Minecraft.getInstance().font.drawInBatch(Integer.toString(blockEntity.getPaperCount()), -Minecraft.getInstance().font.width(Integer.toString(blockEntity.getPaperCount())), -Minecraft.getInstance().font.lineHeight, Color.WHITE.getRGB(), false, pose.last().pose(), bufferSource, Font.DisplayMode.NORMAL, 0x00000000, 15728880); diff --git a/common/src/main/java/com/ultreon/devices/programs/PixelPainterApp.java b/common/src/main/java/com/ultreon/devices/programs/PixelPainterApp.java index 34d826e4..045fa652 100644 --- a/common/src/main/java/com/ultreon/devices/programs/PixelPainterApp.java +++ b/common/src/main/java/com/ultreon/devices/programs/PixelPainterApp.java @@ -5,6 +5,7 @@ import com.mojang.blaze3d.platform.TextureUtil; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; import com.ultreon.devices.Reference; import com.ultreon.devices.api.app.Component; import com.ultreon.devices.api.app.Dialog; @@ -25,18 +26,23 @@ import com.ultreon.devices.object.Picture; import com.ultreon.devices.programs.system.layout.StandardLayout; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; +import org.joml.Matrix3f; import org.joml.Quaternionf; import org.jetbrains.annotations.Nullable; +import org.joml.Vector3f; + import java.awt.*; -import java.lang.System; import java.util.Objects; +import static org.lwjgl.opengl.GL11.*; + @SuppressWarnings({"unused", "FieldCanBeLocal"}) public class PixelPainterApp extends Application { private static final ResourceLocation PIXEL_PAINTER_ICONS = new ResourceLocation("devices:textures/gui/pixel_painter.png"); @@ -492,7 +498,7 @@ public static class PictureRenderer implements IPrint.Renderer { @SuppressWarnings("resource") @Override - public boolean render(PoseStack pose, CompoundTag data) { + public boolean render(PoseStack pose, VertexConsumer buffer, CompoundTag data, int packedLight) { if (data.contains("pixels", Tag.TAG_INT_ARRAY) && data.contains("resolution", Tag.TAG_INT)) { int[] pixels = data.getIntArray("pixels"); int resolution = data.getInt("resolution"); @@ -502,7 +508,8 @@ public boolean render(PoseStack pose, CompoundTag data) { return false; RenderSystem.enableBlend(); - RenderSystem.blendFuncSeparate(770, 771, 1, 0); + RenderSystem.enableDepthTest(); + RenderSystem.blendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO); // GlStateManager.disableLighting(); pose.mulPose(new Quaternionf(0, 0, 0, 180)); @@ -514,6 +521,7 @@ public boolean render(PoseStack pose, CompoundTag data) { // This creates a flipped copy of the pixel array // as it otherwise would be mirrored + // TODO This is not the best way to do it, causes performance issues. Consider caching native images. NativeImage image = new NativeImage(resolution, resolution, false); for (int i = 0; i < resolution; i++) { for (int j = 0; j < resolution; j++) { @@ -531,7 +539,19 @@ public boolean render(PoseStack pose, CompoundTag data) { image.upload(0, 0, 0, false); RenderSystem.setShaderTexture(0, textureId); - RenderUtil.drawRectWithTexture2(null, pose, 0, 0, 0, 0, 1, 1, resolution, resolution, resolution, resolution); + if (buffer != null) { + Matrix3f poseNormal = pose.last().normal(); + Vector3f transformedNor = poseNormal.transform(new Vector3f(0, 0, 0)); + float norX = transformedNor.x(); + float norY = transformedNor.y(); + float norZ = transformedNor.z(); + buffer.vertex(0, 0, 0, 1, 1, 1, 1, 0, 0, 0, packedLight, norX, norY, norZ); + buffer.vertex(0, resolution, 0, 1, 1, 1, 1, 0, 1, 0, packedLight, norX, norY, norZ); + buffer.vertex(resolution, resolution, 0, 1, 1, 1, 1, 1, 1, 0, packedLight, norX, norY, norZ); + buffer.vertex(resolution, 0, 0, 1, 1, 1, 1, 1, 0, 0, packedLight, norX, norY, norZ); + } else { + RenderUtil.drawRectWithTexture2(null, pose, 0, 0, 0, 0, 1, 1, resolution, resolution, resolution, resolution); + } RenderSystem.deleteTexture(textureId); // RenderSystem.disableRescaleNormal();