#!/usr/bin/env node /** * Bake the five `studio/*.sl` presets into a TypeScript module. * * The desktop parser lives in * `blender-5.2.0/source/blender/blenkernel/intern/studiolight.cc:183-226` and is * deliberately dumb: for every key it scans all lines with * `strstr(line, " ")` — the key must be followed by exactly one space — * and runs `atof` on the remainder. There is no version check, no comment * syntax, no missing-key error, and a later duplicate overwrites an earlier one. * We reproduce that literally so a preset cannot diverge from the desktop. * * Output is generated rather than fetched at runtime so * `createSolidStudioMaterial()` can stay synchronous, and so the app does not * need filesystem access to `blender-5.2.0/` outside the vite root. */ import { readFile, writeFile } from "node:fs/promises"; import path from "node:path"; const ROOT = "/home/mes123456/workinf_Blender_Wasm"; const SOURCE_DIR = path.join(ROOT, "blender-5.2.0/release/datafiles/studiolights/studio"); const OUTPUT = path.join(ROOT, "web/app/src/three-adapter/studio-light-presets.ts"); /** Mirrors READ_VAL: substring match on `" "`, last occurrence wins. */ function readValue(lines, id, parse) { const needle = `${id} `; let value = null; for (const line of lines) { const at = line.indexOf(needle); if (at < 0) continue; value = parse(line.slice(at + needle.length)); } return value; } const readFloat = (lines, id) => readValue(lines, id, (text) => Number.parseFloat(text) || 0); const readInt = (lines, id) => readValue(lines, id, (text) => Number.parseInt(text, 10) || 0); const readVec3 = (lines, id) => [ readFloat(lines, `${id}.x`) ?? 0, readFloat(lines, `${id}.y`) ?? 0, readFloat(lines, `${id}.z`) ?? 0, ]; /* SolidLight defaults, DNA_userdef_types.h:727-733 (used when a key is absent). */ const LIGHT_DEFAULTS = { smooth: 0, col: [0.8, 0.8, 0.8], spec: [0.8, 0.8, 0.8], vec: [0, 0, 1] }; function parseSolidLight(text) { const lines = text.split("\n"); const lights = []; for (let index = 0; index < 4; index += 1) { const flag = readInt(lines, `light[${index}].flag`) ?? 0; const light = { smooth: readFloat(lines, `light[${index}].smooth`) ?? LIGHT_DEFAULTS.smooth, col: readVec3(lines, `light[${index}].col`), spec: readVec3(lines, `light[${index}].spec`), vec: readVec3(lines, `light[${index}].vec`), }; /* workbench_resources.cc:68 skips a light whose flag is clear. The shader * keeps four slots, so a disabled light becomes an exact no-op instead of * shifting the array (zero diffuse and zero specular contribute nothing). */ lights.push(flag ? light : { smooth: 0, col: [0, 0, 0], spec: [0, 0, 0], vec: light.vec }); } return { ambient: readVec3(lines, "light_ambient"), lights }; } const files = ["basic.sl", "outdoor.sl", "paint.sl", "rim.sl", "studio.sl"]; const entries = []; for (const file of files) { const text = await readFile(path.join(SOURCE_DIR, file), "utf8"); entries.push([file, parseSolidLight(text)]); } const fmt = (value) => (Number.isInteger(value) ? String(value) : value.toFixed(6)); const vec = (value) => `[${value.map(fmt).join(", ")}]`; const rig = (entry) => `{ ambient: ${vec(entry.ambient)}, lights: [ ${entry.lights.map((light) => ` { smooth: ${fmt(light.smooth)}, col: ${vec(light.col)}, spec: ${vec(light.spec)}, vec: ${vec(light.vec)} },`).join("\n")} ], }`; const body = `import type { StudioLightRig } from "./studio-light"; /** * GENERATED by /home/mes123456/workinf_Blender_Wasm/tools/web/generate-studiolight-presets.mjs * Source: blender-5.2.0/release/datafiles/studiolights/studio/*.sl * Do not edit by hand; re-run the generator instead. * * Keys match the desktop registry, which stores the basename *with* its * extension (studiolight.cc:600-618) — so the UI shows "studio.sl", not "studio". */ export const STUDIO_LIGHT_PRESETS: Record = { ${entries.map(([name, entry]) => ` "${name}": ${rig(entry)},`).join("\n")} }; /** The internal, non-file light that desktop always lists first. */ export const STUDIO_LIGHT_PRESET_NAMES: string[] = ["Default", ...Object.keys(STUDIO_LIGHT_PRESETS)]; `; await writeFile(OUTPUT, body, "utf8"); console.log(`wrote ${OUTPUT} (${entries.length} presets, ${body.length} bytes)`);