From 9b7aa13d61ebf9ee5a9f377ef97a706d1d5a4317 Mon Sep 17 00:00:00 2001 From: tresabhi <43380238+tresabhi@users.noreply.github.com> Date: Mon, 10 Aug 2026 10:39:31 -0500 Subject: [PATCH] fix occlusion baking --- .../[...chunk_api]/tanks/[id]/model.glb.ts | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/packages/website/src/pages/[...chunk_api]/tanks/[id]/model.glb.ts b/packages/website/src/pages/[...chunk_api]/tanks/[id]/model.glb.ts index 684f4b375..0aa75db99 100644 --- a/packages/website/src/pages/[...chunk_api]/tanks/[id]/model.glb.ts +++ b/packages/website/src/pages/[...chunk_api]/tanks/[id]/model.glb.ts @@ -107,9 +107,6 @@ async function extractModel(vfs: AbstractVFS, path: string) { if (textures) { const baseColor = await readBaseColor( `Data/3d/${dirname(path)}/${textures.baseColorMap ?? textures.albedo}`, - textures.miscMap - ? `Data/3d/${dirname(path)}/${textures.miscMap}` - : undefined, ); material.setBaseColorTexture( document @@ -180,6 +177,19 @@ async function extractModel(vfs: AbstractVFS, path: string) { } } + if (textures.miscMap) { + material.setOcclusionTexture( + document + .createTexture(node.materialName) + .setMimeType("image/webp") + .setImage( + await readOcclusion( + `Data/3d/${dirname(path)}/${textures.miscMap}`, + ), + ), + ); + } + if (textures.baseRMMap) { material.setMetallicRoughnessTexture( document @@ -368,46 +378,24 @@ async function extractModel(vfs: AbstractVFS, path: string) { return document; } -async function readBaseColor(path: string, occlusionPath?: string) { - const baseRaw = await readTexture(path); - const occlusionRaw = occlusionPath - ? await readTexture(occlusionPath) - : undefined; - const base = await sharp(baseRaw.data, { raw: baseRaw }).raw().toBuffer(); - const occlusion = occlusionRaw - ? await sharp(occlusionRaw.data, { raw: occlusionRaw }) - .extractChannel(3) - .raw() - .toBuffer() - : undefined; +async function readBaseColor(path: string) { + const raw = await readTexture(path); + const image = await sharp(raw.data, { raw: raw }).webp().toBuffer(); - const combined = Buffer.alloc(baseRaw.width * baseRaw.height * 4); + return image; +} - for (let i = 0; i < baseRaw.width * baseRaw.height; i++) { - let c = 1; +async function readOcclusion(path: string) { + const raw = await readTexture(path); - if (occlusionRaw) { - const x = i % baseRaw.width; - const y = Math.floor(i / baseRaw.width); - const u = Math.floor(x * (occlusionRaw.width / baseRaw.width)); - const v = Math.floor(y * (occlusionRaw.height / baseRaw.height)); - - const occlusionI = u + v * occlusionRaw.width; - - c = occlusion![occlusionI] / 255; - } - - const alpha = base[i * 4 + 3]; - - combined[i * 4 + 0] = Math.round(base[i * 4 + 0] * c); - combined[i * 4 + 1] = Math.round(base[i * 4 + 1] * c); - combined[i * 4 + 2] = Math.round(base[i * 4 + 2] * c); - combined[i * 4 + 3] = alpha; - } - - const image = await sharp(combined, { - raw: { width: baseRaw.width, height: baseRaw.height, channels: 4 }, + const image = await sharp(raw.data, { + raw: { + width: raw.width, + height: raw.height, + channels: raw.channels, + }, }) + .extractChannel(3) .webp() .toBuffer();