Skip to content

Commit

Permalink
Update StringRenderer.java
Browse files Browse the repository at this point in the history
Fixed RenderingIssue
  • Loading branch information
hotpad100c authored Dec 6, 2024
1 parent 2e34681 commit 82babd1
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions src/client/java/mypals/ml/StringRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,45 @@ public static void renderTextList(MatrixStack matrixStack, BlockPos pos, float t
private static VertexConsumerProvider.Immediate getVertexConsumer() {
return MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers();
}
public static void drawStringList(MatrixStack matrices, BlockPos textPos, float tickDelta, float line, ArrayList<String> texts, ArrayList<Integer> colors, float size) {
private static void drawStringList(MatrixStack matrixStack, BlockPos pos,float tickDelta, float line, ArrayList<String> texts, ArrayList<Integer> colors, float size)
{
MinecraftClient client = MinecraftClient.getInstance();
Camera camera = client.gameRenderer.getCamera();
//modelViewMatrixStack modelViewMatrix = new modelViewMatrixStack(1);
//modelViewMatrix.identity();

if (camera.isReady() && client.getEntityRenderDispatcher().gameOptions != null && client.player != null) {
matrices.push();
float x = (float) (textPos.toCenterPos().getX() - MathHelper.lerp(tickDelta, lastTickPosX, camera.getPos().getX()));
float y = (float) (textPos.toCenterPos().getY() - MathHelper.lerp(tickDelta, lastTickPosY, camera.getPos().getY()));
float z = (float) (textPos.toCenterPos().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);
matrices.multiply(MinecraftClient.getInstance().gameRenderer.getCamera().getRotation());
matrices.scale(size, -size, 1);
Matrix4f modelViewMatrix = matrices.peek().getPositionMatrix();

TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;

RenderSystem.disableDepthTest();

float totalHeight = 0.0F;
for (String text : texts) {
totalHeight += textRenderer.getWrappedLinesHeight(text, Integer.MAX_VALUE) * 1.25F;
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;
matrixStack.push();
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 totalWidth = 0.0F;
for (String text: texts)
{
totalWidth += client.textRenderer.getWidth(text);
}

float renderYBase = -totalHeight / 2.0F; // 起始位置,从底部开始
for (int i = 0; i < texts.size(); i++) {
float renderX = -textRenderer.getWidth(texts.get(i)) * 0.5F; // 居中
float renderY = renderYBase + textRenderer.getWrappedLinesHeight(texts.get(i), Integer.MAX_VALUE) * 1.25F * i;
VertexConsumerProvider.Immediate immediate = getVertexConsumer();
textRenderer.draw(
texts.get(i), renderX, renderY, colors.get(i) != null? colors.get(i) : Color.white.getRGB(), shadow,
modelViewMatrix, immediate, TextRenderer.TextLayerType.SEE_THROUGH, background?backgroundColor.getRGB():0,
0xF000F0
);
float writtenWidth = 0.0F;
for (int i = 0; i < texts.size(); i++)
{
float renderX = -totalWidth * 0.5F + writtenWidth;
float renderY = client.textRenderer.getWrappedLinesHeight(texts.get(i), Integer.MAX_VALUE) * (-0.5F + 1.25F * line);

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();

writtenWidth += client.textRenderer.getWidth(texts.get(i));
}
matrices.pop();

// 恢复矩阵状态
RenderSystem.enableDepthTest();
matrixStack.pop();
}
}
public static void drawCube(MatrixStack matrices, BlockPos pos, float size, float tickDelta, Color color,float alpha) {
Expand Down Expand Up @@ -175,4 +169,4 @@ public static void drawString(MatrixStack matrixStack,float tickDelta, Camera ca

}

}
}

0 comments on commit 82babd1

Please sign in to comment.