Skip to content

Commit

Permalink
进一步适应 1.21.3 的文本渲染,修复一些崩溃问题(其他分支需要部分参考)
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidBlock-cn committed Nov 5, 2024
1 parent 660c282 commit 1d0fbb6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/main/java/pers/solid/mishang/uc/MishangUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ private static int toSignOutlineColor(DyeColor color) {
* @return 发光后颜色的整数值。
*/
public static int toSignOutlineColor(int color) {
int j = (int) ((double) (color & 0xFF) * 0.4);
int k = (int) ((double) (color >> 8 & 0xFF) * 0.4);
int l = (int) ((double) (color >> 16 & 0xFF) * 0.4);
if (color == 0) {
return 0xf0ebcc;
if ((color & 0xffffff) == 0) {
return (color & 0xff000000) | 0xf0ebcc;
}
return 0 << 24 | (l & 0xFF) << 16 | (k & 0xFF) << 8 | j & 0xFF;
int j = (int) ((double) ColorHelper.getRed(color) * 0.4);
int k = (int) ((double) ColorHelper.getGreen(color) * 0.4);
int l = (int) ((double) ColorHelper.getBlue(color) * 0.4);
return ColorHelper.getArgb(ColorHelper.getAlpha(color), j, k, l);
}

@ApiStatus.AvailableSince("0.2.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import pers.solid.mishang.uc.MishangUtils;
import pers.solid.mishang.uc.mixin.TextRendererAccessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public record PatternSpecialDrawable(TextContext textContext, String shapeName, @Unmodifiable float[][] rectangles) implements SpecialDrawable {
Expand Down Expand Up @@ -224,42 +226,51 @@ public boolean isEmpty() {
@Override
public void drawExtra(TextRenderer textRenderer, MatrixStack matrixStack, VertexConsumerProvider vertexConsumers, int light, float x, float y) {
int color = textContext.color;
final int alpha = ((color & 0xFC000000) == 0) ? 1 : (color >> 24 & 0xFF);
final int alpha = ((color & 0xFC000000) == 0) ? 255 : (color >> 24 & 0xFF);
//noinspection resource
BakedGlyph glyphRenderer = ((TextRendererAccessor) textRenderer).invokeGetFontStorage(Style.DEFAULT_FONT_ID).getRectangleBakedGlyph();
BakedGlyph bakedGlyph = ((TextRendererAccessor) textRenderer).invokeGetFontStorage(Style.DEFAULT_FONT_ID).getRectangleBakedGlyph();
final float sizeMultiplier = 1;
final RenderLayer layer = glyphRenderer.getLayer(textContext.outlineColor != -2 ? TextRenderer.TextLayerType.POLYGON_OFFSET : textContext.seeThrough ? TextRenderer.TextLayerType.SEE_THROUGH : TextRenderer.TextLayerType.NORMAL);
final Matrix4f matrix4f = matrixStack.peek().getPositionMatrix();
final VertexConsumer vertexConsumer = vertexConsumers.getBuffer(layer);
final RenderLayer layer = bakedGlyph.getLayer(textContext.outlineColor != -2 ? TextRenderer.TextLayerType.POLYGON_OFFSET : textContext.seeThrough ? TextRenderer.TextLayerType.SEE_THROUGH : TextRenderer.TextLayerType.NORMAL);

// 文本是否存在阴影。
final boolean shadow = textContext.shadow;
// 用于文本渲染的矩阵。当存在阴影时,文本渲染需要适当调整。
final List<BakedGlyph.Rectangle> rectanglesToDraw = new ArrayList<>();
final List<BakedGlyph.Rectangle> outlineRectangles = textContext.outlineColor == -2 ? null : new ArrayList<>();
for (float[] rectangle : rectangles) {
final float minX = (rectangle[0] + x) * sizeMultiplier;
final float minY = (rectangle[3] + y) * sizeMultiplier;
final float maxX = (rectangle[2] + x) * sizeMultiplier;
final float maxY = (rectangle[1] + y) * sizeMultiplier;
if (shadow) {
glyphRenderer.drawRectangle(
new BakedGlyph.Rectangle(minX + 1, minY + 1, maxX + 1, maxY + 1, 0, ColorHelper.scaleRgb(color, 0.25f)),
matrix4f, vertexConsumer, light
rectanglesToDraw.add(
new BakedGlyph.Rectangle(minX + 1, minY + 1, maxX + 1, maxY + 1, 0, ColorHelper.withAlpha(alpha, ColorHelper.scaleRgb(color, 0.25f)))
);
}
if (textContext.outlineColor != -2) {
final VertexConsumer vertexConsumerOutline = vertexConsumers.getBuffer(glyphRenderer.getLayer(TextRenderer.TextLayerType.NORMAL));
if (outlineRectangles != null) {
int outlineColor = textContext.outlineColor == -1 ? MishangUtils.toSignOutlineColor(color) : textContext.outlineColor;
glyphRenderer.drawRectangle(
new BakedGlyph.Rectangle(minX - 1, minY + 1, maxX + 1, maxY - 1, 0, ColorHelper.withAlpha(alpha, outlineColor)),
matrix4f, vertexConsumerOutline, light
final int outlineAlpha = ((outlineColor & 0xFC000000) == 0) ? 255 : (outlineColor >> 24 & 0xFF);
outlineRectangles.add(
new BakedGlyph.Rectangle(minX - 1, minY + 1, maxX + 1, maxY - 1, 0, ColorHelper.withAlpha(outlineAlpha, outlineColor))
);

}
glyphRenderer.drawRectangle(
new BakedGlyph.Rectangle(minX, minY, maxX, maxY, shadow ? 0.24f : textContext.outlineColor != -2 ? 0.02f : 0, ColorHelper.withAlpha(alpha, color)),
matrix4f, vertexConsumer, light
rectanglesToDraw.add(
new BakedGlyph.Rectangle(minX, minY, maxX, maxY, shadow ? 0.03f : textContext.outlineColor != -2 ? 0.02f : 0, ColorHelper.withAlpha(alpha, color))
);
}

final Matrix4f matrix4f = matrixStack.peek().getPositionMatrix();
final VertexConsumer vertexConsumer = vertexConsumers.getBuffer(layer);
for (BakedGlyph.Rectangle rectangle : rectanglesToDraw) {
bakedGlyph.drawRectangle(rectangle, matrix4f, vertexConsumer, light);
}
if (outlineRectangles != null) {
final VertexConsumer vertexConsumerOutline = vertexConsumers.getBuffer(bakedGlyph.getLayer(TextRenderer.TextLayerType.NORMAL));
for (BakedGlyph.Rectangle outlineRectangle : outlineRectangles) {
bakedGlyph.drawRectangle(outlineRectangle, matrix4f, vertexConsumerOutline, light);
}
}
}

@Override
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/pers/solid/mishang/uc/text/RectSpecialDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.fabricmc.api.Environment;
import net.minecraft.client.font.BakedGlyph;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.util.math.MatrixStack;
Expand Down Expand Up @@ -31,23 +32,27 @@ public record RectSpecialDrawable(float width, float height, @NotNull TextContex
@Override
public void drawExtra(TextRenderer textRenderer, MatrixStack matrixStack, VertexConsumerProvider vertexConsumers, int light, float x, float y) {
int color = textContext.color;
final int alpha = ((color & 0xFC000000) == 0) ? 1 : (color >> 24 & 0xFF);
BakedGlyph glyphRenderer = ((TextRendererAccessor) textRenderer).invokeGetFontStorage(Style.DEFAULT_FONT_ID).getRectangleBakedGlyph();
final int alpha = ((color & 0xFC000000) == 0) ? 255 : (color >> 24 & 0xFF);
BakedGlyph bakedGlyph = ((TextRendererAccessor) textRenderer).invokeGetFontStorage(Style.DEFAULT_FONT_ID).getRectangleBakedGlyph();
final Matrix4f matrix4f = matrixStack.peek().getPositionMatrix();
final VertexConsumer vertexConsumer = vertexConsumers.getBuffer(glyphRenderer.getLayer(textContext.outlineColor != -2 ? TextRenderer.TextLayerType.POLYGON_OFFSET : textContext.seeThrough ? TextRenderer.TextLayerType.SEE_THROUGH : TextRenderer.TextLayerType.NORMAL));
final RenderLayer layer = bakedGlyph.getLayer(textContext.outlineColor != -2 ? TextRenderer.TextLayerType.POLYGON_OFFSET : textContext.seeThrough ? TextRenderer.TextLayerType.SEE_THROUGH : TextRenderer.TextLayerType.NORMAL);
final VertexConsumer vertexConsumer = vertexConsumers.getBuffer(layer);
if (textContext.shadow) {
// 绘制阴影
BakedGlyph.Rectangle shadowRectangle = new BakedGlyph.Rectangle(x + 1, (height + y) + 1, (width + x) + 1, y + 1, 0, ColorHelper.withAlpha(alpha, ColorHelper.scaleRgb(color, 0.25f)));
glyphRenderer.drawRectangle(shadowRectangle, matrix4f, vertexConsumer, light);
bakedGlyph.drawRectangle(shadowRectangle, matrix4f, vertexConsumer, light);
}
if (textContext.outlineColor != -2) {
// 绘制轮廓
int outlineColor = textContext.outlineColor == -1 ? MishangUtils.toSignOutlineColor(color) : textContext.outlineColor;
BakedGlyph.Rectangle rectangle = new BakedGlyph.Rectangle(x - 1, (height + y) + 1, (width + x) + 1, y - 1, 0, ColorHelper.withAlpha(alpha, outlineColor));
glyphRenderer.drawRectangle(rectangle, matrix4f, vertexConsumers.getBuffer(glyphRenderer.getLayer(TextRenderer.TextLayerType.NORMAL)), light);
final int outlineAlpha = ((outlineColor & 0xfc000000) == 0) ? 255 : (outlineColor >> 24 & 0xFF);
BakedGlyph.Rectangle outlineRectangle = new BakedGlyph.Rectangle(x - 1, (height + y) + 1, (width + x) + 1, y - 1, 0, ColorHelper.withAlpha(outlineAlpha, outlineColor));
bakedGlyph.drawRectangle(outlineRectangle, matrix4f, vertexConsumers.getBuffer(bakedGlyph.getLayer(TextRenderer.TextLayerType.NORMAL)), light);
}
BakedGlyph.Rectangle rectangle = new BakedGlyph.Rectangle(x, (height + y), (width + x), y, textContext.shadow ? 0.24f : textContext.outlineColor != -2 ? 0.02f : 0, ColorHelper.withAlpha(alpha, color));
glyphRenderer.drawRectangle(rectangle, matrix4f, vertexConsumer, light);

final VertexConsumer vertexConsumer2 = vertexConsumers.getBuffer(layer);
BakedGlyph.Rectangle rectangle = new BakedGlyph.Rectangle(x, (height + y), (width + x), y, textContext.shadow ? 0.03f : textContext.outlineColor != -2 ? 0.02f : 0, ColorHelper.withAlpha(alpha, color));
bakedGlyph.drawRectangle(rectangle, matrix4f, vertexConsumer2, light);
}

@Override
Expand Down

0 comments on commit 1d0fbb6

Please sign in to comment.