| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import com.google.common.cache.CacheBuilder;
|
| 4 | import com.google.common.cache.CacheLoader;
|
| 5 | import com.google.common.cache.LoadingCache;
|
| 6 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 7 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 8 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 9 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 10 | import java.util.function.Supplier;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.client.gui.Font;
|
| 14 | import net.minecraft.client.gui.GlyphSource;
|
| 15 | import net.minecraft.client.gui.font.PlayerGlyphProvider.1.1;
|
| 16 | import net.minecraft.client.renderer.PlayerSkinRenderCache;
|
| 17 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 18 | import net.minecraft.network.chat.Style;
|
| 19 | import net.minecraft.network.chat.FontDescription.PlayerSprite;
|
| 20 | import org.joml.Matrix4fc;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class PlayerGlyphProvider {
|
| 24 | private static final GlyphInfo GLYPH_INFO = GlyphInfo.simple(8.0F);
|
| 25 | private final PlayerSkinRenderCache playerSkinRenderCache;
|
| 26 | private final LoadingCache<PlayerSprite, GlyphSource> wrapperCache = CacheBuilder.newBuilder()
|
| 27 | .expireAfterAccess(PlayerSkinRenderCache.CACHE_DURATION)
|
| 28 | .build(new CacheLoader<PlayerSprite, GlyphSource>() {
|
| 29 | public GlyphSource load(final PlayerSprite playerInfo) {
|
| 30 | Supplier<PlayerSkinRenderCache.RenderInfo> skin = PlayerGlyphProvider.this.playerSkinRenderCache.createLookup(playerInfo.profile());
|
| 31 | boolean hat = playerInfo.hat();
|
| 32 | return new SingleSpriteSource(new 1(this, skin, hat));
|
| 33 | }
|
| 34 | });
|
| 35 |
|
| 36 | public PlayerGlyphProvider(final PlayerSkinRenderCache playerSkinRenderCache) {
|
| 37 | this.playerSkinRenderCache = playerSkinRenderCache;
|
| 38 | }
|
| 39 |
|
| 40 | public GlyphSource sourceForPlayer(final PlayerSprite playerInfo) {
|
| 41 | return this.wrapperCache.getUnchecked(playerInfo);
|
| 42 | }
|
| 43 |
|
| 44 | @Environment(EnvType.CLIENT)
|
| 45 | private record Instance(
|
| 46 | Supplier<PlayerSkinRenderCache.RenderInfo> skin, boolean hat, float x, float y, int color, int shadowColor, float shadowOffset, Style style
|
| 47 | ) implements PlainTextRenderable {
|
| 48 | @Override
|
| 49 | public void renderSprite(
|
| 50 | final Matrix4fc pose, final VertexConsumer buffer, final int packedLightCoords, final float offsetX, final float offsetY, final float z, final int color
|
| 51 | ) {
|
| 52 | float x0 = offsetX + this.left();
|
| 53 | float x1 = offsetX + this.right();
|
| 54 | float y0 = offsetY + this.top();
|
| 55 | float y1 = offsetY + this.bottom();
|
| 56 | renderQuad(pose, buffer, packedLightCoords, x0, x1, y0, y1, z, color, 8.0F, 8.0F, 8, 8, 64, 64);
|
| 57 | if (this.hat) {
|
| 58 | renderQuad(pose, buffer, packedLightCoords, x0, x1, y0, y1, z, color, 40.0F, 8.0F, 8, 8, 64, 64);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private static void renderQuad(
|
| 63 | final Matrix4fc pose,
|
| 64 | final VertexConsumer buffer,
|
| 65 | final int packedLightCoords,
|
| 66 | final float x0,
|
| 67 | final float x1,
|
| 68 | final float y0,
|
| 69 | final float y1,
|
| 70 | final float z,
|
| 71 | final int color,
|
| 72 | final float u,
|
| 73 | final float v,
|
| 74 | final int srcWidth,
|
| 75 | final int srcHeight,
|
| 76 | final int textureWidth,
|
| 77 | final int textureHeight
|
| 78 | ) {
|
| 79 | float u0 = (u + 0.0F) / textureWidth;
|
| 80 | float u1 = (u + srcWidth) / textureWidth;
|
| 81 | float v0 = (v + 0.0F) / textureHeight;
|
| 82 | float v1 = (v + srcHeight) / textureHeight;
|
| 83 | buffer.addVertex(pose, x0, y0, z).setUv(u0, v0).setColor(color).setLight(packedLightCoords);
|
| 84 | buffer.addVertex(pose, x0, y1, z).setUv(u0, v1).setColor(color).setLight(packedLightCoords);
|
| 85 | buffer.addVertex(pose, x1, y1, z).setUv(u1, v1).setColor(color).setLight(packedLightCoords);
|
| 86 | buffer.addVertex(pose, x1, y0, z).setUv(u1, v0).setColor(color).setLight(packedLightCoords);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public RenderType renderType(final Font.DisplayMode displayMode) {
|
| 91 | return this.skin.get().glyphRenderTypes().select(displayMode);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public RenderPipeline guiPipeline() {
|
| 96 | return this.skin.get().glyphRenderTypes().guiPipeline();
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public GpuTextureView textureView() {
|
| 101 | return this.skin.get().textureView();
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|