Some optimization

This commit is contained in:
2026-10-25 21:31:32 -03:00
parent c955d8d143
commit f46058f9b5
5 changed files with 92 additions and 25 deletions
+31 -5
View File
@@ -97,7 +97,7 @@ export function draw_item(item: ItemStack, x: number, y: number, show_count = it
const model = item_model_of(item);
const block_info = item_block(item);
if (model) {
draw_item_model(model, icon_matrix(x, y, model.display.gui));
draw_item_model(model, x, y);
} else if (block_info) {
draw_item_block(block_info, x, y);
} else {
@@ -177,10 +177,36 @@ function icon_matrix(x: number, y: number, gui?: DisplayTransform): mat4 {
return mat4.fromValues(size, -size / 2, -1, 0, 0, -size, 1, 0, -size, -size / 2, -1, 0, cx, cy + size * 1.5, 0, 1);
}
// each model's icon in a slot at 0, 0, as x y u v r g b per vertex. a slot's place only moves the icon (see
// icon_matrix), so it's worked out once per model and moved to each slot, rather than every slot every frame
const model_icons = new WeakMap<ItemModel, Float32Array>();
// an item's model in a slot: its faces that face the viewer, back to front
function draw_item_model(model: ItemModel, matrix: mat4) {
function draw_item_model(model: ItemModel, x: number, y: number) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
use_texture(atlas.tex);
let icon = model_icons.get(model);
if (!icon) {
icon = model_icon(model, atlas);
model_icons.set(model, icon);
}
for (let i = 0; i < icon.length; i += 7) {
push_vertex(
x + icon[i],
y + icon[i + 1],
0,
icon[i + 2],
icon[i + 3],
icon[i + 4],
icon[i + 5],
icon[i + 6],
1,
);
}
}
function model_icon(model: ItemModel, atlas: Texture): Float32Array {
const matrix = icon_matrix(0, 0, model.display.gui);
// a mirroring matrix (like the y flip to the screen's y down) turns which way a face's corners go round
const flip = Math.sign(mat4.determinant(matrix));
const visible = model.quads
@@ -197,24 +223,24 @@ function draw_item_model(model: ItemModel, matrix: mat4) {
.filter(({ facing }) => facing > 1e-6)
.sort((a, b) => a.depth - b.depth);
const vertices: number[] = [];
for (const { quad, corners } of visible) {
const region = get_sprite_region(quad.texture);
const [r, g, b] = model.block ? item_tint(model.block, quad.variables) : [1, 1, 1];
const shade = quad.shade ? ITEM_FACE_SHADE[quad.face] : 1;
for (const i of [0, 1, 2, 0, 2, 3]) {
push_vertex(
vertices.push(
corners[i][0],
corners[i][1],
0,
sprite_u(atlas, region, quad.uvs[i * 2]),
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
shade * r,
shade * g,
shade * b,
1,
);
}
}
return new Float32Array(vertices);
}
function draw_item_block(block_info: BlockRegistry, x: number, y: number) {