| 1 | package net.minecraft.client.gui.font.glyphs;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 6 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.gui.Font;
|
| 10 | import net.minecraft.client.gui.font.GlyphRenderTypes;
|
| 11 | import net.minecraft.client.gui.font.TextRenderable;
|
| 12 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 13 | import net.minecraft.network.chat.Style;
|
| 14 | import org.joml.Matrix4fc;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class BakedSheetGlyph implements EffectGlyph, BakedGlyph {
|
| 18 | public static final float Z_FIGHTER = 0.001F;
|
| 19 | private final GlyphInfo info;
|
| 20 | private final GlyphRenderTypes renderTypes;
|
| 21 | private final GpuTextureView textureView;
|
| 22 | private final float u0;
|
| 23 | private final float u1;
|
| 24 | private final float v0;
|
| 25 | private final float v1;
|
| 26 | private final float left;
|
| 27 | private final float right;
|
| 28 | private final float up;
|
| 29 | private final float down;
|
| 30 |
|
| 31 | public BakedSheetGlyph(
|
| 32 | final GlyphInfo info,
|
| 33 | final GlyphRenderTypes renderTypes,
|
| 34 | final GpuTextureView textureView,
|
| 35 | final float u0,
|
| 36 | final float u1,
|
| 37 | final float v0,
|
| 38 | final float v1,
|
| 39 | final float left,
|
| 40 | final float right,
|
| 41 | final float up,
|
| 42 | final float down
|
| 43 | ) {
|
| 44 | this.info = info;
|
| 45 | this.renderTypes = renderTypes;
|
| 46 | this.textureView = textureView;
|
| 47 | this.u0 = u0;
|
| 48 | this.u1 = u1;
|
| 49 | this.v0 = v0;
|
| 50 | this.v1 = v1;
|
| 51 | this.left = left;
|
| 52 | this.right = right;
|
| 53 | this.up = up;
|
| 54 | this.down = down;
|
| 55 | }
|
| 56 |
|
| 57 | private float left(final BakedSheetGlyph.GlyphInstance instance) {
|
| 58 | return instance.x + this.left + (instance.style.isItalic() ? Math.min(this.shearTop(), this.shearBottom()) : 0.0F) - extraThickness(instance.style.isBold());
|
| 59 | }
|
| 60 |
|
| 61 | private float top(final BakedSheetGlyph.GlyphInstance instance) {
|
| 62 | return instance.y + this.up - extraThickness(instance.style.isBold());
|
| 63 | }
|
| 64 |
|
| 65 | private float right(final BakedSheetGlyph.GlyphInstance instance) {
|
| 66 | return instance.x
|
| 67 | + this.right
|
| 68 | + (instance.hasShadow() ? instance.shadowOffset : 0.0F)
|
| 69 | + (instance.style.isItalic() ? Math.max(this.shearTop(), this.shearBottom()) : 0.0F)
|
| 70 | + extraThickness(instance.style.isBold());
|
| 71 | }
|
| 72 |
|
| 73 | private float bottom(final BakedSheetGlyph.GlyphInstance instance) {
|
| 74 | return instance.y + this.down + (instance.hasShadow() ? instance.shadowOffset : 0.0F) + extraThickness(instance.style.isBold());
|
| 75 | }
|
| 76 |
|
| 77 | private void renderChar(
|
| 78 | final BakedSheetGlyph.GlyphInstance glyphInstance, final Matrix4fc pose, final VertexConsumer buffer, final int packedLightCoords, final boolean flat
|
| 79 | ) {
|
| 80 | Style style = glyphInstance.style();
|
| 81 | boolean italic = style.isItalic();
|
| 82 | float x = glyphInstance.x();
|
| 83 | float y = glyphInstance.y();
|
| 84 | int color = glyphInstance.color();
|
| 85 | boolean bold = style.isBold();
|
| 86 | float zFighter = flat ? 0.0F : 0.001F;
|
| 87 | float depth;
|
| 88 | if (glyphInstance.hasShadow()) {
|
| 89 | int shadowColor = glyphInstance.shadowColor();
|
| 90 | this.render(italic, x + glyphInstance.shadowOffset(), y + glyphInstance.shadowOffset(), 0.0F, pose, buffer, shadowColor, bold, packedLightCoords);
|
| 91 | if (bold) {
|
| 92 | this.render(
|
| 93 | italic,
|
| 94 | x + glyphInstance.boldOffset() + glyphInstance.shadowOffset(),
|
| 95 | y + glyphInstance.shadowOffset(),
|
| 96 | zFighter,
|
| 97 | pose,
|
| 98 | buffer,
|
| 99 | shadowColor,
|
| 100 | true,
|
| 101 | packedLightCoords
|
| 102 | );
|
| 103 | }
|
| 104 |
|
| 105 | depth = flat ? 0.0F : 0.03F;
|
| 106 | } else {
|
| 107 | depth = 0.0F;
|
| 108 | }
|
| 109 |
|
| 110 | this.render(italic, x, y, depth, pose, buffer, color, bold, packedLightCoords);
|
| 111 | if (bold) {
|
| 112 | this.render(italic, x + glyphInstance.boldOffset(), y, depth + zFighter, pose, buffer, color, true, packedLightCoords);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private void render(
|
| 117 | final boolean italic,
|
| 118 | final float x,
|
| 119 | final float y,
|
| 120 | final float z,
|
| 121 | final Matrix4fc pose,
|
| 122 | final VertexConsumer builder,
|
| 123 | final int color,
|
| 124 | final boolean bold,
|
| 125 | final int packedLightCoords
|
| 126 | ) {
|
| 127 | float x0 = x + this.left;
|
| 128 | float x1 = x + this.right;
|
| 129 | float y0 = y + this.up;
|
| 130 | float y1 = y + this.down;
|
| 131 | float shearY0 = italic ? this.shearTop() : 0.0F;
|
| 132 | float shearY1 = italic ? this.shearBottom() : 0.0F;
|
| 133 | float extraThickness = extraThickness(bold);
|
| 134 | builder.addVertex(pose, x0 + shearY0 - extraThickness, y0 - extraThickness, z).setColor(color).setUv(this.u0, this.v0).setLight(packedLightCoords);
|
| 135 | builder.addVertex(pose, x0 + shearY1 - extraThickness, y1 + extraThickness, z).setColor(color).setUv(this.u0, this.v1).setLight(packedLightCoords);
|
| 136 | builder.addVertex(pose, x1 + shearY1 + extraThickness, y1 + extraThickness, z).setColor(color).setUv(this.u1, this.v1).setLight(packedLightCoords);
|
| 137 | builder.addVertex(pose, x1 + shearY0 + extraThickness, y0 - extraThickness, z).setColor(color).setUv(this.u1, this.v0).setLight(packedLightCoords);
|
| 138 | }
|
| 139 |
|
| 140 | private static float extraThickness(final boolean bold) {
|
| 141 | return bold ? 0.1F : 0.0F;
|
| 142 | }
|
| 143 |
|
| 144 | private float shearBottom() {
|
| 145 | return 1.0F - 0.25F * this.down;
|
| 146 | }
|
| 147 |
|
| 148 | private float shearTop() {
|
| 149 | return 1.0F - 0.25F * this.up;
|
| 150 | }
|
| 151 |
|
| 152 | private void renderEffect(
|
| 153 | final BakedSheetGlyph.EffectInstance effect, final Matrix4fc pose, final VertexConsumer buffer, final int packedLightCoords, final boolean flat
|
| 154 | ) {
|
| 155 | float depth = flat ? 0.0F : effect.depth;
|
| 156 | if (effect.hasShadow()) {
|
| 157 | this.buildEffect(effect, effect.shadowOffset(), depth, effect.shadowColor(), buffer, packedLightCoords, pose);
|
| 158 | depth += flat ? 0.0F : 0.03F;
|
| 159 | }
|
| 160 |
|
| 161 | this.buildEffect(effect, 0.0F, depth, effect.color, buffer, packedLightCoords, pose);
|
| 162 | }
|
| 163 |
|
| 164 | private void buildEffect(
|
| 165 | final BakedSheetGlyph.EffectInstance effect,
|
| 166 | final float offset,
|
| 167 | final float z,
|
| 168 | final int color,
|
| 169 | final VertexConsumer buffer,
|
| 170 | final int packedLightCoords,
|
| 171 | final Matrix4fc pose
|
| 172 | ) {
|
| 173 | buffer.addVertex(pose, effect.x0 + offset, effect.y1 + offset, z).setColor(color).setUv(this.u0, this.v0).setLight(packedLightCoords);
|
| 174 | buffer.addVertex(pose, effect.x1 + offset, effect.y1 + offset, z).setColor(color).setUv(this.u0, this.v1).setLight(packedLightCoords);
|
| 175 | buffer.addVertex(pose, effect.x1 + offset, effect.y0 + offset, z).setColor(color).setUv(this.u1, this.v1).setLight(packedLightCoords);
|
| 176 | buffer.addVertex(pose, effect.x0 + offset, effect.y0 + offset, z).setColor(color).setUv(this.u1, this.v0).setLight(packedLightCoords);
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | public GlyphInfo info() {
|
| 181 | return this.info;
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public TextRenderable.Styled createGlyph(
|
| 186 | final float x, final float y, final int color, final int shadowColor, final Style style, final float boldOffset, final float shadowOffset
|
| 187 | ) {
|
| 188 | return new BakedSheetGlyph.GlyphInstance(x, y, color, shadowColor, this, style, boldOffset, shadowOffset);
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public TextRenderable createEffect(
|
| 193 | final float x0, final float y0, final float x1, final float y1, final float depth, final int color, final int shadowColor, final float shadowOffset
|
| 194 | ) {
|
| 195 | return new BakedSheetGlyph.EffectInstance(this, x0, y0, x1, y1, depth, color, shadowColor, shadowOffset);
|
| 196 | }
|
| 197 |
|
| 198 | @Environment(EnvType.CLIENT)
|
| 199 | private record EffectInstance(BakedSheetGlyph glyph, float x0, float y0, float x1, float y1, float depth, int color, int shadowColor, float shadowOffset)
|
| 200 | implements TextRenderable {
|
| 201 | @Override
|
| 202 | public float left() {
|
| 203 | return this.x0;
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public float top() {
|
| 208 | return this.y0;
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public float right() {
|
| 213 | return this.x1 + (this.hasShadow() ? this.shadowOffset : 0.0F);
|
| 214 | }
|
| 215 |
|
| 216 | @Override
|
| 217 | public float bottom() {
|
| 218 | return this.y1 + (this.hasShadow() ? this.shadowOffset : 0.0F);
|
| 219 | }
|
| 220 |
|
| 221 | private boolean hasShadow() {
|
| 222 | return this.shadowColor() != 0;
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | public void render(final Matrix4fc pose, final VertexConsumer buffer, final int packedLightCoords, final boolean flat) {
|
| 227 | this.glyph.renderEffect(this, pose, buffer, packedLightCoords, false);
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public RenderType renderType(final Font.DisplayMode displayMode) {
|
| 232 | return this.glyph.renderTypes.select(displayMode);
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public GpuTextureView textureView() {
|
| 237 | return this.glyph.textureView;
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public RenderPipeline guiPipeline() {
|
| 242 | return this.glyph.renderTypes.guiPipeline();
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | @Environment(EnvType.CLIENT)
|
| 247 | private record GlyphInstance(float x, float y, int color, int shadowColor, BakedSheetGlyph glyph, Style style, float boldOffset, float shadowOffset)
|
| 248 | implements TextRenderable.Styled {
|
| 249 | @Override
|
| 250 | public float left() {
|
| 251 | return this.glyph.left(this);
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public float top() {
|
| 256 | return this.glyph.top(this);
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | public float right() {
|
| 261 | return this.glyph.right(this);
|
| 262 | }
|
| 263 |
|
| 264 | @Override
|
| 265 | public float activeRight() {
|
| 266 | return this.x + this.glyph.info.getAdvance(this.style.isBold());
|
| 267 | }
|
| 268 |
|
| 269 | @Override
|
| 270 | public float bottom() {
|
| 271 | return this.glyph.bottom(this);
|
| 272 | }
|
| 273 |
|
| 274 | private boolean hasShadow() {
|
| 275 | return this.shadowColor() != 0;
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public void render(final Matrix4fc pose, final VertexConsumer buffer, final int packedLightCoords, final boolean flat) {
|
| 280 | this.glyph.renderChar(this, pose, buffer, packedLightCoords, flat);
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public RenderType renderType(final Font.DisplayMode displayMode) {
|
| 285 | return this.glyph.renderTypes.select(displayMode);
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public GpuTextureView textureView() {
|
| 290 | return this.glyph.textureView;
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public RenderPipeline guiPipeline() {
|
| 295 | return this.glyph.renderTypes.guiPipeline();
|
| 296 | }
|
| 297 | }
|
| 298 | }
|
| 299 |
|