From 37d8f19ea89e33ed604458395a6b7e472754f495 Mon Sep 17 00:00:00 2001 From: hotpad100c <46704388+hotpad100c@users.noreply.github.com> Date: Sat, 7 Dec 2024 13:38:35 +0900 Subject: [PATCH] Update StringRenderer.java --- src/client/java/mypals/ml/StringRenderer.java | 356 +++++++++++------- 1 file changed, 227 insertions(+), 129 deletions(-) diff --git a/src/client/java/mypals/ml/StringRenderer.java b/src/client/java/mypals/ml/StringRenderer.java index 31f43b1..35407a3 100644 --- a/src/client/java/mypals/ml/StringRenderer.java +++ b/src/client/java/mypals/ml/StringRenderer.java @@ -1,146 +1,244 @@ package mypals.ml; -import net.minecraft.client.render.RenderTickCounter; +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.render.*; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Text; +import net.minecraft.util.Colors; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec3d; +import org.joml.Matrix4f; +import org.joml.Matrix4fStack; import java.awt.*; import java.util.ArrayList; -import java.util.List; -import static mypals.ml.ScheduledTickVisualizerClient.textSize; -import static mypals.ml.StringRenderer.*; import static mypals.ml.config.ScheduledTickVisualizerConfig.*; -public class InfoRender { - - public static List scheduledTicksFluid = new ArrayList<>(); - public static List scheduledTicksBlock = new ArrayList<>(); - public static int blockTickDataClearTimer = timeOutDelay; - public static int fluidTickDataClearTimer = timeOutDelay; - public static List reorderedBlockTicks = new ArrayList<>(); - public static List reorderedFluidTicks = new ArrayList<>(); - public static void setScheduledTicksBlock(List scheduledTicks){ - scheduledTicksBlock = scheduledTicks; - blockTickDataClearTimer = timeOutDelay; - reorderedBlockTicks = TickOrderResolver.resolveTickOrder(scheduledTicks); - } - public static void setScheduledTicksFluid(List scheduledTicks){ - scheduledTicksFluid = scheduledTicks; - fluidTickDataClearTimer = timeOutDelay; - reorderedFluidTicks = TickOrderResolver.resolveTickOrder(scheduledTicks); - } - @SuppressWarnings("ConstantConditions") - public static void render(MatrixStack matrixStack, Float tickDelta) { - if(showInfo) { - - if (!reorderedBlockTicks.isEmpty() || !reorderedFluidTicks.isEmpty()) { - int totalSize = reorderedBlockTicks.size() + reorderedFluidTicks.size(); - - if (ScheduledTickVisualizerClient.orderViewerIndex < 0 || ScheduledTickVisualizerClient.orderViewerIndex >= totalSize) { - ScheduledTickVisualizerClient.orderViewerIndex = 0; - } - - BlockPos pos; - if (ScheduledTickVisualizerClient.orderViewerIndex < reorderedBlockTicks.size()) { - pos = reorderedBlockTicks.get(ScheduledTickVisualizerClient.orderViewerIndex); - } else { - int fluidIndex = ScheduledTickVisualizerClient.orderViewerIndex - reorderedBlockTicks.size(); - pos = reorderedFluidTicks.get(fluidIndex); - } - - drawCube2(matrixStack, pos, 0.02f, tickDelta, - new Color(225, 225, 225), Math.min(boxAlpha+0.3f,1f)); - } +public class StringRenderer { + public static double lastTickPosX = 0; + public static double lastTickPosY = 0; + public static double lastTickPosZ = 0; + public static void renderTextList(MatrixStack matrixStack, BlockPos pos, float tickDelta, float line, ArrayList texts, ArrayList colors, float size) { + drawStringList(matrixStack, pos, tickDelta, line, texts, colors, size) ; - for (SchedulTickObject tick : scheduledTicksBlock) { - ArrayList colors = new ArrayList<>(); - ArrayList text = new ArrayList<>(); - if(simplify){ - text.add( - tick.subTick + "@" + - tick.time + - (tick.priority != 0 ? ("[" + tick.priority +"]"):"") - ); - colors.add(blockTickColor.getRGB()); - } - else{ - if(showTickTypeInfo){ - if(showAccurateBlockType) - text.add(tick.name); - else - text.add(Text.translatable("text.scheduledtick.block").getString()); - colors.add(blockTickColor.getRGB()); - } - if(showSubOrderInfo){ - text.add( Text.translatable("text.scheduledtick.sub_order").getString() + ": " + tick.subTick); - colors.add(subOrderColor.getRGB()); - } - if(showTriggerInfo){ - text.add( Text.translatable("text.scheduledtick.trigger").getString() + ": " + tick.time); - colors.add(triggerColor.getRGB()); - } - if(showPriorityInfo){ - text.add( Text.translatable("text.scheduledtick.priority").getString() + ": " + tick.priority); - colors.add(priorityColor.getRGB()); - } - } - renderTextList(matrixStack, tick.pos, tickDelta, 5, text, colors, textSize); - if(showInfoBox) - drawCube(matrixStack,tick.pos,0f,tickDelta, - new Color(blockTickColor.getRed(),blockTickColor.getGreen(),blockTickColor.getBlue()),boxAlpha); + } + private static void drawStringList(MatrixStack matrixStack, BlockPos pos,float tickDelta, float line, ArrayList texts, ArrayList colors, float size) + { + MinecraftClient client = MinecraftClient.getInstance(); + Camera camera = client.gameRenderer.getCamera(); + if (camera.isReady() && client.getEntityRenderDispatcher().gameOptions != null && client.player != null) + { + /*double x = (double)pos.toCenterPos().getX(); + double y = (double)pos.toCenterPos().getY(); + double z = (double)pos.toCenterPos().getZ(); + double camX = camera.getPos().x; + double camY = camera.getPos().y; + double camZ = camera.getPos().z;*/ + float x = (float) (pos.toCenterPos().getX() - MathHelper.lerp(tickDelta, lastTickPosX, camera.getPos().getX())); + float y = (float) (pos.toCenterPos().getY() - MathHelper.lerp(tickDelta, lastTickPosY, camera.getPos().getY())); + float z = (float) (pos.toCenterPos().getZ() - MathHelper.lerp(tickDelta, lastTickPosZ, camera.getPos().getZ())); + lastTickPosX = camera.getPos().getX(); + lastTickPosY = camera.getPos().getY(); + lastTickPosZ = camera.getPos().getZ(); + + matrixStack.push(); + matrixStack.translate(x, y, z); + //matrixStack.translate((float)(x - camX), (float)(y - camY), (float)(z - camZ)); + matrixStack.multiplyPositionMatrix(new Matrix4f().rotation(camera.getRotation())); + matrixStack.scale(-size, -size, 1); + RenderSystem.disableDepthTest(); // visibleThroughObjects + + float totalHeight = 0.0F; + for (String text : texts) { + totalHeight += client.textRenderer.getWrappedLinesHeight(text, Integer.MAX_VALUE) * 1.25F; } - for (SchedulTickObject tick : scheduledTicksFluid) { - ArrayList colors = new ArrayList<>(); - ArrayList text = new ArrayList<>(); - if(simplify){ - text.add( - tick.subTick + "@" + - tick.time + - (tick.priority != 0 ? ("[" + tick.priority +"]"):"") - ); - colors.add(fluidTickColor.getRGB()); - } - else { - if (showTickTypeInfo) { - if (showAccurateBlockType) - text.add(tick.name); - else - text.add(Text.translatable("text.scheduledtick.fluid").getString()); - colors.add(fluidTickColor.getRGB()); - } - if (showSubOrderInfo) { - text.add(Text.translatable("text.scheduledtick.sub_order").getString() + ": " + tick.subTick); - colors.add(subOrderColor.getRGB()); - } - if (showTriggerInfo) { - text.add(Text.translatable("text.scheduledtick.trigger").getString() + ": " + tick.time); - colors.add(triggerColor.getRGB()); - } - if (showPriorityInfo) { - text.add(Text.translatable("text.scheduledtick.priority").getString() + ": " + tick.priority); - colors.add(priorityColor.getRGB()); - } - } - renderTextList(matrixStack, tick.pos, tickDelta, 5, text, colors, textSize); - if (showInfoBox) - drawCube(matrixStack, tick.pos, 0f, tickDelta, - new Color(fluidTickColor.getRed(), fluidTickColor.getGreen(), fluidTickColor.getBlue()), boxAlpha); - } - if(fluidTickDataClearTimer>0) - fluidTickDataClearTimer--; - if(blockTickDataClearTimer>0) - blockTickDataClearTimer--; - if(blockTickDataClearTimer<=0){ - reorderedBlockTicks.clear(); - scheduledTicksBlock.clear(); - } - if(fluidTickDataClearTimer<=0){ - reorderedFluidTicks.clear(); - scheduledTicksFluid.clear(); + float renderYBase = -totalHeight / 2.0F; // 起始位置,从底部开始 + for (int i = 0; i < texts.size(); i++) { + float renderX = -client.textRenderer.getWidth(texts.get(i)) * 0.5F; // 居中 + float renderY = renderYBase + client.textRenderer.getWrappedLinesHeight(texts.get(i), Integer.MAX_VALUE) * 1.25F * i; + VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer()); + + client.textRenderer.draw(texts.get(i), renderX, renderY, colors.get(i), false, matrixStack.peek().getPositionMatrix(), immediate, TextRenderer.TextLayerType.SEE_THROUGH, 0, 0xF000F0); + immediate.draw(); } + + RenderSystem.enableDepthTest(); + matrixStack.pop(); } } + public static void drawCube(MatrixStack matrices, BlockPos pos, float size, float tickDelta, Color color, float alpha) { + MinecraftClient client = MinecraftClient.getInstance(); + Camera camera = client.gameRenderer.getCamera(); + + if (camera.isReady() && client.player != null) { + matrices.push(); + + // 计算相对坐标 + float x = (float) (pos.getX() - MathHelper.lerp(tickDelta, lastTickPosX, camera.getPos().getX())); + float y = (float) (pos.getY() - MathHelper.lerp(tickDelta, lastTickPosY, camera.getPos().getY())); + float z = (float) (pos.getZ() - MathHelper.lerp(tickDelta, lastTickPosZ, camera.getPos().getZ())); + lastTickPosX = camera.getPos().getX(); + lastTickPosY = camera.getPos().getY(); + lastTickPosZ = camera.getPos().getZ(); + + matrices.translate(x, y, z); + Matrix4f modelViewMatrix = matrices.peek().getPositionMatrix(); + + RenderSystem.disableDepthTest(); + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + + BufferBuilder bufferBuilder = Tessellator.getInstance().getBuffer(); + bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); + + float minOffset = -0.001F - size; + float maxOffset = 1.001F + size; + + float red = ((color.getRGB() >> 16) & 0xFF) / 255.0f; + float green = ((color.getRGB() >> 8) & 0xFF) / 255.0f; + float blue = (color.getRGB() & 0xFF) / 255.0f; + + bufferBuilder.vertex(modelViewMatrix, minOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + + bufferBuilder.vertex(modelViewMatrix, minOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + + bufferBuilder.vertex(modelViewMatrix, minOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + + bufferBuilder.vertex(modelViewMatrix, maxOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + + bufferBuilder.vertex(modelViewMatrix, minOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + + bufferBuilder.vertex(modelViewMatrix, minOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, maxOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + bufferBuilder.vertex(modelViewMatrix, minOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + + + BufferRenderer.drawWithGlobalProgram(bufferBuilder.end()); + + RenderSystem.enableDepthTest(); + RenderSystem.disableBlend(); + + matrices.pop(); + } + } + + public static void drawCube2(MatrixStack matrices, BlockPos pos, float size, float tickDelta, Color color,float alpha) { + MinecraftClient client = MinecraftClient.getInstance(); + Camera camera = client.gameRenderer.getCamera(); + if (camera.isReady() && client.getEntityRenderDispatcher().gameOptions != null && client.player != null) { + matrices.push(); + float x = (float) (pos.getX() - MathHelper.lerp(tickDelta, lastTickPosX, camera.getPos().getX())); + float y = (float) (pos.getY() - MathHelper.lerp(tickDelta, lastTickPosY, camera.getPos().getY())); + float z = (float) (pos.getZ() - MathHelper.lerp(tickDelta, lastTickPosZ, camera.getPos().getZ())); + lastTickPosX = camera.getPos().getX(); + lastTickPosY = camera.getPos().getY(); + lastTickPosZ = camera.getPos().getZ(); + + matrices.translate(x, y, z); + Matrix4f modelViewMatrix = matrices.peek().getPositionMatrix(); + RenderSystem.disableDepthTest(); + + VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer()); + + VertexConsumer vertexConsumer = immediate.getBuffer(RenderLayer.getDebugQuads()); + + float minOffset = -0.001F - size; + float maxOffset = 1.001F + size; + + float red = ((color.getRGB() >> 16) & 0xFF) / 255.0f; + float green = ((color.getRGB() >> 8) & 0xFF) / 255.0f; + float blue = (color.getRGB() & 0xFF) / 255.0f; + + vertexConsumer.vertex(modelViewMatrix, minOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + + vertexConsumer.vertex(modelViewMatrix, minOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + + vertexConsumer.vertex(modelViewMatrix, minOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + + vertexConsumer.vertex(modelViewMatrix, maxOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + + vertexConsumer.vertex(modelViewMatrix, minOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, minOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, maxOffset, minOffset).color(red, green, blue, alpha).next(); + + vertexConsumer.vertex(modelViewMatrix, minOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, maxOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, maxOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + vertexConsumer.vertex(modelViewMatrix, minOffset, minOffset, maxOffset).color(red, green, blue, alpha).next(); + + + immediate.draw(); + matrices.pop(); + + RenderSystem.enableDepthTest(); + } + } + + public static void drawString(MatrixStack matrixStack,float tickDelta, Camera camera, Vec3d textPos, String text, int color, float SIZE, boolean seeThrow) { + + Matrix4fStack modelViewMatrix = new Matrix4fStack(1); + modelViewMatrix.identity(); + + float x = (float) (textPos.x - MathHelper.lerp(tickDelta, lastTickPosX, camera.getPos().getX())); + float y = (float) (textPos.y - MathHelper.lerp(tickDelta, lastTickPosY, camera.getPos().getY())); + float z = (float) (textPos.z - MathHelper.lerp(tickDelta, lastTickPosZ, camera.getPos().getZ())); + lastTickPosX = camera.getPos().getX(); + lastTickPosY = camera.getPos().getY(); + lastTickPosZ = camera.getPos().getZ(); + modelViewMatrix.translate(x, y, z); + modelViewMatrix.rotate(MinecraftClient.getInstance().gameRenderer.getCamera().getRotation()); + modelViewMatrix.scale(SIZE, -SIZE, 1); + + TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; + float totalWidth = textRenderer.getWidth(text); + float writtenWidth = 1; + float renderX = -totalWidth * 0.5F + writtenWidth; + + VertexConsumerProvider.Immediate immediate = MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers(); + RenderSystem.disableDepthTest(); + + if(seeThrow) + textRenderer.draw(text, renderX, 0, color, false, modelViewMatrix + , immediate, TextRenderer.TextLayerType.SEE_THROUGH, 0, 0xF000F0); + else + textRenderer.draw(text, renderX, 0, color, false, modelViewMatrix + , immediate, TextRenderer.TextLayerType.NORMAL, 0, 0xF000F0); + immediate.draw(); + RenderSystem.enableDepthTest(); + + } + }