fix occlusion baking

This commit is contained in:
tresabhi committed 2026-08-10 10:39:31 -05:00
1 parent 84ecaec5d3
commit 9b7aa13d61
1 file changed
+27 -39
@@ -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();