2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.Billboard;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
13 * A text label rendered as a billboard texture that always faces the camera.
15 * <p>This shape renders a single line of text onto a {@link Texture} using the font metrics
16 * defined in {@link TextCanvas} ({@link TextCanvas#FONT}, {@link TextCanvas#FONT_CHAR_WIDTH_TEXTURE_PIXELS},
17 * {@link TextCanvas#FONT_CHAR_HEIGHT_TEXTURE_PIXELS}), then displays the texture as a
18 * forward-oriented billboard via its {@link Billboard} superclass. The result
19 * is a text label that remains readable from any viewing angle.</p>
21 * <p><b>Usage example:</b></p>
23 * // Create a red text label at position (0, -50, 300)
24 * ForwardOrientedTextBlock label = new ForwardOrientedTextBlock(
25 * new Point3D(0, -50, 300),
31 * shapeCollection.addShape(label);
38 public class ForwardOrientedTextBlock extends Billboard {
41 * Creates a new forward-oriented text block at the given 3D position.
43 * @param point the 3D position where the text label is placed
44 * @param scale the scale factor controlling the rendered size of the text
45 * @param maxUpscaleFactor the maximum mipmap upscale factor for the backing texture
46 * @param text the text string to render
47 * @param textColor the color of the rendered text
49 public ForwardOrientedTextBlock(final Point3D point, final double scale,
50 final int maxUpscaleFactor, final String text,
51 final eu.svjatoslav.sixth.e3d.renderer.raster.Color textColor) {
52 super(point, scale, getTexture(text, maxUpscaleFactor, textColor));
57 * Creates a {@link Texture} containing the rendered text string.
59 * <p>The texture dimensions are calculated from the text length and the font metrics
60 * defined in {@link TextCanvas}. Each character is drawn individually at the appropriate
61 * horizontal offset using {@link TextCanvas#FONT}.</p>
63 * @param text the text string to render into the texture
64 * @param maxUpscaleFactor the maximum mipmap upscale factor for the texture
65 * @param textColor the color of the rendered text
66 * @return a new {@link Texture} containing the rendered text
68 public static Texture getTexture(final String text,
69 final int maxUpscaleFactor,
70 final eu.svjatoslav.sixth.e3d.renderer.raster.Color textColor) {
72 final Texture texture = new Texture(text.length()
73 * TextCanvas.FONT_CHAR_WIDTH_TEXTURE_PIXELS, TextCanvas.FONT_CHAR_HEIGHT_TEXTURE_PIXELS,
76 // texture.graphics.setColor(Color.BLUE);
77 // texture.graphics.fillRect(0, 0, texture.primaryBitmap.width,
78 // texture.primaryBitmap.width);
80 texture.graphics.setFont(TextCanvas.FONT);
81 texture.graphics.setColor(textColor.toAwtColor());
83 for (int c = 0; c < text.length(); c++)
84 texture.graphics.drawChars(new char[]{text.charAt(c),}, 0, 1,
85 (c * TextCanvas.FONT_CHAR_WIDTH_TEXTURE_PIXELS),
86 (int) (TextCanvas.FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.45));