Names on players
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { flush_batch, get_current_texture, push_quad, set_current_texture, white_tex } from "./core.ts";
|
||||
import { flush_batch, get_current_texture, push_quad, push_vertex, set_current_texture, white_tex } from "./core.ts";
|
||||
|
||||
export function draw_rect(
|
||||
x: number,
|
||||
@@ -32,3 +32,63 @@ export function draw_rect_stroke(
|
||||
// right
|
||||
draw_rect(x + w - thickness, y, thickness, h, color);
|
||||
}
|
||||
|
||||
// a plane in the world to lay 2d drawing on: origin is its top left, x goes along right and y along -up, like the
|
||||
// screen. for text and rects facing a direction, like nameplates facing the camera
|
||||
export type Plane3d = {
|
||||
origin: [number, number, number];
|
||||
right: [number, number, number];
|
||||
up: [number, number, number];
|
||||
};
|
||||
|
||||
// a quad at x, y on the plane, in its units. wound counter clockwise seen from where right and up point the usual way,
|
||||
// so it's culled from behind
|
||||
export function push_plane_quad(
|
||||
plane: Plane3d,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
u0: number,
|
||||
v0: number,
|
||||
u1: number,
|
||||
v1: number,
|
||||
color: readonly number[],
|
||||
) {
|
||||
const { origin: o, right: rt, up } = plane;
|
||||
const corner = (cx: number, cy: number, u: number, v: number) =>
|
||||
push_vertex(
|
||||
o[0] + rt[0] * cx - up[0] * cy,
|
||||
o[1] + rt[1] * cx - up[1] * cy,
|
||||
o[2] + rt[2] * cx - up[2] * cy,
|
||||
u,
|
||||
v,
|
||||
color[0],
|
||||
color[1],
|
||||
color[2],
|
||||
color[3],
|
||||
);
|
||||
const x2 = x + w;
|
||||
const y2 = y + h;
|
||||
corner(x, y, u0, v0);
|
||||
corner(x, y2, u0, v1);
|
||||
corner(x2, y2, u1, v1);
|
||||
corner(x, y, u0, v0);
|
||||
corner(x2, y2, u1, v1);
|
||||
corner(x2, y, u1, v0);
|
||||
}
|
||||
|
||||
export function draw_rect_3d(
|
||||
plane: Plane3d,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
color: readonly number[] = [1, 1, 1, 1],
|
||||
) {
|
||||
if (get_current_texture() !== white_tex) {
|
||||
flush_batch();
|
||||
set_current_texture(white_tex!);
|
||||
}
|
||||
push_plane_quad(plane, x, y, w, h, 0, 0, 1, 1, color);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { flush_batch, get_current_texture, push_quad, set_current_texture } from "./core.ts";
|
||||
import { type Plane3d, push_plane_quad } from "./shapes.ts";
|
||||
import { FntChar, FntFont, Texture } from "./types.ts";
|
||||
|
||||
let font_texture: Texture;
|
||||
@@ -102,6 +103,52 @@ export function draw_text(str: string, x: number, y: number, scale: number = 1,
|
||||
}
|
||||
}
|
||||
|
||||
// text on a plane in the world (see Plane3d), x and y in the plane's units like draw_text's are in pixels. scale is
|
||||
// how big a font pixel is in them
|
||||
export function draw_text_3d(
|
||||
plane: Plane3d,
|
||||
str: string,
|
||||
x: number,
|
||||
y: number,
|
||||
scale: number,
|
||||
color: readonly number[] = [1, 1, 1, 1],
|
||||
) {
|
||||
if (!font_texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (get_current_texture() !== font_texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(font_texture.tex);
|
||||
}
|
||||
|
||||
for (const ch of str) {
|
||||
const glyph = font_fnt.chars[ch.charCodeAt(0)];
|
||||
if (!glyph) {
|
||||
continue;
|
||||
}
|
||||
|
||||
push_plane_quad(
|
||||
plane,
|
||||
x + glyph.xoffset * scale,
|
||||
y + glyph.yoffset * scale,
|
||||
glyph.width * scale,
|
||||
glyph.height * scale,
|
||||
glyph.x / font_texture.width,
|
||||
glyph.y / font_texture.height,
|
||||
(glyph.x + glyph.width) / font_texture.width,
|
||||
(glyph.y + glyph.height) / font_texture.height,
|
||||
color,
|
||||
);
|
||||
|
||||
x += glyph.xadvance * scale;
|
||||
}
|
||||
}
|
||||
|
||||
export function font_line_height() {
|
||||
return font_fnt?.line_height ?? 0;
|
||||
}
|
||||
|
||||
export function measure_text(str: string, scale: number = 1) {
|
||||
let x = 0;
|
||||
for (const ch of str) {
|
||||
|
||||
Reference in New Issue
Block a user