TextFeatureRenderer .java

net.minecraft.client.renderer.feature.TextFeatureRenderer (class): members: GlyphRenderer, Content, Text, buildGroup, Submit, TYPE. imports: Text.

net.minecraft.client.renderer.featureclass91 linesclientextends RenderTypeFeatureRenderer<TextFeatureRenderer.Submit>3.5 KB

API Surface (4)

void buildGroup(final FeatureFrameContext context, final List<TextFeatureRenderer.Submit> submits)
void acceptRenderable(final TextRenderable renderable)
float distanceToCameraSq()
FeatureRendererType<TextFeatureRenderer.Submit> featureType()

Fields & Constants (2)

TYPE
Text

Full Source

TextFeatureRenderer.java91 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer.feature;
2
3
import com.mojang.blaze3d.vertex.VertexConsumer;
4
import java.util.List;
5
import net.fabricmc.api.EnvType;
6
import net.fabricmc.api.Environment;
7
import net.minecraft.client.gui.Font;
8
import net.minecraft.client.gui.font.TextRenderable;
9
import net.minecraft.client.renderer.feature.TextFeatureRenderer.Content.StandaloneBackground;
10
import net.minecraft.client.renderer.feature.TextFeatureRenderer.Content.Text;
11
import net.minecraft.client.renderer.feature.submit.TranslucentSubmit;
12
import org.joml.Matrix4f;
13
import org.joml.Matrix4fc;
14
15
@Environment(EnvType.CLIENT)
16
public class TextFeatureRenderer extends RenderTypeFeatureRenderer<TextFeatureRenderer.Submit> {
17
	public static final FeatureRendererType<TextFeatureRenderer.Submit> TYPE = FeatureRendererType.create("Text");
18
19
	@Override
20
	protected void buildGroup(final FeatureFrameContext context, final List<TextFeatureRenderer.Submit> submits) {
21
		Font font = context.font();
22
		TextFeatureRenderer.GlyphRenderer glyphRenderer = new TextFeatureRenderer.GlyphRenderer();
23
24
		for (TextFeatureRenderer.Submit submit : submits) {
25
			glyphRenderer.pose.set(submit.pose());
26
			glyphRenderer.lightCoords = submit.lightCoords();
27
			glyphRenderer.displayMode = submit.displayMode();
28
			switch (submit.content()) {
29
				case Text text:
30
					renderText(font, glyphRenderer, text);
31
					break;
32
				case StandaloneBackground standaloneBackground:
33
					glyphRenderer.acceptRenderable(
34
						font.prepareBackground(
35
							standaloneBackground.x0(), standaloneBackground.y0(), standaloneBackground.x1(), standaloneBackground.y1(), standaloneBackground.color()
36
						)
37
					);
38
					break;
39
				default:
40
					throw new MatchException(null, null);
41
			}
42
		}
43
	}
44
45
	private static void renderText(final Font font, final TextFeatureRenderer.GlyphRenderer glyphRenderer, final Text content) {
46
		if (content.outlineColor() == 0) {
47
			Font.PreparedText text = font.prepareText(
48
				content.string(), content.x(), content.y(), content.color(), content.dropShadow(), false, content.backgroundColor()
49
			);
50
			text.visit(glyphRenderer);
51
		} else {
52
			Font.PreparedText outline = font.prepare8xTextOutline(content.string(), content.x(), content.y(), content.outlineColor());
53
			Font.PreparedText text = font.prepareText(content.string(), content.x(), content.y(), content.color(), false, false, 0);
54
			glyphRenderer.displayMode = Font.DisplayMode.NORMAL;
55
			outline.visit(glyphRenderer);
56
			glyphRenderer.displayMode = Font.DisplayMode.POLYGON_OFFSET;
57
			text.visit(glyphRenderer);
58
		}
59
	}
60
61
	@Environment(EnvType.CLIENT)
62
	public sealed interface Content permits Text, StandaloneBackground {
63
	}
64
65
	@Environment(EnvType.CLIENT)
66
	private class GlyphRenderer implements Font.GlyphVisitor {
67
		private final Matrix4f pose = new Matrix4f();
68
		private int lightCoords = 15728880;
69
		private Font.DisplayMode displayMode = Font.DisplayMode.NORMAL;
70
71
		@Override
72
		public void acceptRenderable(final TextRenderable renderable) {
73
			VertexConsumer builder = TextFeatureRenderer.this.getVertexBuilder(renderable.renderType(this.displayMode));
74
			renderable.render(this.pose, builder, this.lightCoords, false);
75
		}
76
	}
77
78
	@Environment(EnvType.CLIENT)
79
	public record Submit(Matrix4fc pose, Font.DisplayMode displayMode, int lightCoords, TextFeatureRenderer.Content content) implements TranslucentSubmit {
80
		@Override
81
		public float distanceToCameraSq() {
82
			return TranslucentSubmit.computeDistanceToCameraSq(this.pose);
83
		}
84
85
		@Override
86
		public FeatureRendererType<TextFeatureRenderer.Submit> featureType() {
87
			return TextFeatureRenderer.TYPE;
88
		}
89
	}
90
}
91

Decompiled Minecraft 26.3 source of TextFeatureRenderer (net/minecraft/client/renderer/feature/TextFeatureRenderer.java) with Mojang official mappings, stored verbatim on the site.