Skip to content

Commit

Permalink
"Tried" fixing printer stuff some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Jan 21, 2024
1 parent b4cf7d1 commit e18cd58
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ run/

# Ignore datagen cache.
/common/src/main/generated/resources/.cache/

/*/bin/
/.vscode/
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -78,6 +80,10 @@ static ItemStack generateItem(IPrint print) {
Class<? extends Renderer> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
* @author MrCrayfish
*/
public record PrinterRenderer(BlockEntityRendererProvider.Context context) implements BlockEntityRenderer<PrinterBlockEntity> {
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));
Expand All @@ -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();
Expand All @@ -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 <DrawBuffer()>
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 <DrawBuffer()>
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 <DrawBuffer()>
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 <RenderPrint()>
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 <Prepare()>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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));

Expand All @@ -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++) {
Expand All @@ -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();
Expand Down

0 comments on commit e18cd58

Please sign in to comment.