feat: baked ao

This commit is contained in:
TrèsAbhi committed 2025-09-06 00:48:39 -05:00
1 parent 335cd4d699
commit 66cf1fd223
11 files changed
+75 -93

No files matched your search

@@ -12,7 +12,6 @@ import { dirname } from "path";
import { readBaseColor } from "../readBaseColor";
import { readDVPLFile } from "../readDVPLFile";
import { readNormal } from "../readNormal";
import { readOcclusion } from "../readOcclusion";
import { readRoughnessMetallic } from "../readRoughnessMetallic";
import {
vertexAttributeGLTFName,
@@ -70,7 +69,10 @@ export async function extractModel(data: string, path: string) {
.setMimeType("image/jpeg")
.setImage(
await readBaseColor(
`${data}/3d/${dirname(path)}/${textures.baseColorMap ?? textures.albedo}`
`${data}/3d/${dirname(path)}/${textures.baseColorMap ?? textures.albedo}`,
textures.miscMap
? `${data}/3d/${dirname(path)}/${textures.miscMap}`
: undefined
)
)
);
@@ -88,19 +90,6 @@ export async function extractModel(data: string, path: string) {
);
}
if (textures.miscMap) {
material.setOcclusionTexture(
document
.createTexture(node.materialName)
.setMimeType("image/jpeg")
.setImage(
await readOcclusion(
`${data}/3d/${dirname(path)}/${textures.miscMap}`
)
)
);
}
if (textures.baseNormalMap ?? textures.normalmap) {
const isBase = textures.baseNormalMap !== undefined;
@@ -236,10 +225,6 @@ export async function extractModel(data: string, path: string) {
.setBuffer(buffer);
primitive.setAttribute(name, attributeAccessor);
if (attribute === VertexAttribute.TEXCOORD0) {
primitive.setAttribute("TEXCOORD_2", attributeAccessor);
}
});
const mesh = document
@@ -1,7 +1,48 @@
import sharp from "sharp";
import { readTexture } from "./readTexture";
export async function readBaseColor(path: string) {
const raw = await readTexture(path);
return await sharp(raw.data, { raw }).removeAlpha().jpeg().toBuffer();
export 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 })
.removeAlpha()
.raw()
.toBuffer();
const occlusion = occlusionRaw
? await sharp(occlusionRaw.data, { raw: occlusionRaw })
.extractChannel(3)
.raw()
.toBuffer()
: undefined;
const combined = Buffer.alloc(baseRaw.width * baseRaw.height * 3);
for (let i = 0; i < baseRaw.width * baseRaw.height; i++) {
let c = 1;
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;
}
combined[i * 3 + 0] = Math.round(base[i * 3 + 0] * c);
combined[i * 3 + 1] = Math.round(base[i * 3 + 1] * c);
combined[i * 3 + 2] = Math.round(base[i * 3 + 2] * c);
}
const image = await sharp(combined, {
raw: { width: baseRaw.width, height: baseRaw.height, channels: 3 },
})
.jpeg()
.toBuffer();
return image;
}
@@ -17,8 +17,7 @@ export async function readNormal(path: string, isBase: boolean) {
*/
let x = raw.data[index + 3] * (2 / 255) - 1;
let y = raw.data[index + 1] * (2 / 255) - 1;
const underSqrt = 1 - x ** 2 - y ** 2;
let z = underSqrt > 0 ? Math.sqrt(underSqrt) : -Math.sqrt(-underSqrt);
let z = Math.sqrt(Math.max(0, 1 - x ** 2 - y ** 2));
raw.data[index] = Math.round((x + 1) * (255 / 2));
raw.data[index + 1] = Math.round((y + 1) * (255 / 2));
@@ -1,25 +0,0 @@
import { writeFile } from "fs/promises";
import sharp from "sharp";
import { readTexture } from "./readTexture";
export async function readOcclusion(path: string) {
const raw = await readTexture(path);
const blitz = sharp(raw.data, { raw: raw });
const occlusion = await blitz?.extractChannel(3).raw().toBuffer();
const combined = Buffer.alloc(raw.width * raw.height * 3);
for (let i = 0; i < raw.width * raw.height; i++) {
combined[i * 3 + 0] = occlusion?.[i] ?? 0;
}
const image = await sharp(combined, {
raw: { width: raw.width, height: raw.height, channels: 3 },
})
.jpeg()
.toBuffer();
writeFile("test.ao.jpg", image);
return image;
}
@@ -1,4 +1,3 @@
import { writeFile } from "fs/promises";
import sharp from "sharp";
import { readTexture } from "./readTexture";
@@ -11,8 +10,8 @@ export async function readRoughnessMetallic(path: string) {
const combined = Buffer.alloc(raw.width * raw.height * 3);
for (let i = 0; i < raw.width * raw.height; i++) {
combined[i * 3 + 1] = roughness?.[i] ?? 0;
combined[i * 3 + 2] = metallicness?.[i] ?? 0;
combined[i * 3 + 1] = roughness[i];
combined[i * 3 + 2] = metallicness[i];
}
const image = await sharp(combined, {
@@ -21,7 +20,5 @@ export async function readRoughnessMetallic(path: string) {
.jpeg()
.toBuffer();
writeFile("test.rm.jpg", image);
return image;
}