The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
// Unified ThreeRT build CLI: selects one renderer (`path` or `webgpu`).
// node tools/build.ts --renderer webgpu [--debug] [--js-only]
// node tools/build.ts --renderer path [--debug]
//
// `--renderer webgpu` builds the clean native runtime + WebGPU renderer (linking zero path-tracer code).
// `--renderer path` builds the hardware path tracer (`renderer/`, today's default).
import { spawnSync } from "node:child_process";
import { mkdirSync, statSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { build } from "esbuild";
const root = join(import.meta.dirname, "..");
function parseArgs(argv: string[]) {
let renderer: "path" | "webgpu" = "path";
let debug = false;
let jsOnly = false;
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === "--renderer") {
const v = argv[++i];
if (v !== "path" && v !== "webgpu") {
throw new Error(`unknown --renderer value: ${String(v)} (expected "path" or "webgpu")`);
}
renderer = v;
} else if (a.startsWith("--renderer=")) {
const v = a.slice("--renderer=".length);
if (v !== "path" && v !== "webgpu") {
throw new Error(`unknown --renderer value: ${v} (expected "path" or "webgpu")`);
}
renderer = v;
} else if (a === "--debug") {
debug = true;
} else if (a === "--js-only") {
jsOnly = true;
} else if (a === "-h" || a === "--help") {
console.log("Usage: node tools/build.ts --renderer <path|webgpu> [--debug] [--js-only]");
process.exit(0);
} else {
throw new Error(`unknown argument: ${a}`);
}
}
return { renderer, debug, jsOnly };
}
function run(cmd: string, args: string[], cwd: string) {
const res = spawnSync(cmd, args, { cwd, stdio: "inherit" });
if (res.status !== 0) {
process.exit(res.status ?? 1);
}
}
function formatMiB(bytes: number): string {
return `${(bytes / (1024 * 1024)).toFixed(2)} MiB`;
}
async function bundleWebGpuJs() {
const outDir = join(root, "out", "webgpu");
mkdirSync(outDir, { recursive: true });
const rtRes = await build({
entryPoints: [join(root, "webgpu", "js", "index.ts")],
bundle: true,
format: "iife",
platform: "neutral",
target: "es2022",
minify: true,
legalComments: "none",
write: false,
logLevel: "warning",
});
const rtPath = join(outDir, "runtime.js");
writeFileSync(rtPath, rtRes.outputFiles[0].text);
const sampleRes = await build({
entryPoints: [join(root, "webgpu", "sample", "scene.ts")],
bundle: true,
format: "iife",
platform: "browser",
target: "es2022",
mainFields: ["module", "main"],
minify: true,
legalComments: "none",
write: false,
logLevel: "warning",
});
const samplePath = join(outDir, "sample.js");
writeFileSync(samplePath, sampleRes.outputFiles[0].text);
console.log(
`[build --renderer webgpu] JS bundles: runtime.js (${(rtRes.outputFiles[0].contents.length / 1024).toFixed(1)} KiB), ` +
`sample.js (${(sampleRes.outputFiles[0].contents.length / 1024).toFixed(1)} KiB)`
);
}
async function buildWebGpu(debug: boolean, jsOnly: boolean) {
await bundleWebGpuJs();
if (jsOnly) return;
if (process.platform !== "darwin" && process.platform !== "linux") {
throw new Error(`native --renderer webgpu is supported on macOS and Linux (got ${process.platform})`);
}
const cargoArgs = ["build", "--workspace", "--locked"];
if (!debug) cargoArgs.push("--release");
run("cargo", cargoArgs, join(root, "webgpu"));
const profileDir = debug ? "debug" : "release";
const sampleBin = join(root, "webgpu", "target", profileDir, "threert-webgpu-sample");
const shotBin = join(root, "webgpu", "target", profileDir, "webgpu-shot");
console.log(
`[build --renderer webgpu] binaries (${profileDir}): ` +
`threert-webgpu-sample=${formatMiB(statSync(sampleBin).size)}, ` +
`webgpu-shot=${formatMiB(statSync(shotBin).size)}`
);
}
function buildPath(debug: boolean) {
run("node", [join(root, "tools", "build-runtime.ts")], root);
const cargoArgs = ["build", "--locked"];
if (!debug) cargoArgs.push("--release");
run("cargo", cargoArgs, join(root, "renderer"));
const profileDir = debug ? "debug" : "release";
const bin = join(root, "renderer", "target", profileDir, process.platform === "win32" ? "threert-render.exe" : "threert-render");
console.log(`[build --renderer path] binary (${profileDir}): threert-render=${formatMiB(statSync(bin).size)}`);
}
const opts = parseArgs(process.argv.slice(2));
if (opts.renderer === "webgpu") {
await buildWebGpu(opts.debug, opts.jsOnly);
} else {
buildPath(opts.debug);
}