Skip to content

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.

2.1 KB
Raw Blame
// Bundle the shared Three.js WebGPU Earth wallpaper demo into `out/webgpu/wallpaper-demo.js`
// and stage 4K planet textures into `out/webgpu/assets/` (used identically by ThreeRT & Chromium).
// Run with: bun webgpu/wallpaper/demo/build.mjs
import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";

const demoDir = import.meta.dirname;
const root = join(demoDir, "..", "..", "..");
const outDir = join(root, "out", "webgpu");
const outAssets = join(outDir, "assets");
mkdirSync(outAssets, { recursive: true });

const threeDir = join(root, "node_modules", "three");

const result = await Bun.build({
  entrypoints: [join(demoDir, "scene.ts")],
  outdir: outDir,
  naming: "wallpaper-demo.js",
  target: "browser",
  format: "iife",
  minify: true,
  plugins: [
    {
      name: "resolve-three",
      setup(build) {
        build.onResolve({ filter: /^three(\/.*)?$/ }, (args) => {
          if (args.path === "three/webgpu") return { path: join(threeDir, "build", "three.webgpu.js") };
          if (args.path === "three/tsl") return { path: join(threeDir, "build", "three.tsl.js") };
          if (args.path === "three/core") return { path: join(threeDir, "build", "three.core.js") };
          if (args.path === "three") return { path: join(threeDir, "build", "three.module.js") };
          return { path: join(threeDir, args.path.slice("three/".length)) };
        });
      },
    },
  ],
});

if (!result.success) {
  for (const log of result.logs) console.error(log);
  process.exit(1);
}

copyFileSync(join(demoDir, "index.html"), join(outDir, "wallpaper-demo.html"));
const srcAssets = join(demoDir, "assets");
if (existsSync(srcAssets)) {
  for (const f of readdirSync(srcAssets)) {
    copyFileSync(join(srcAssets, f), join(outAssets, f));
  }
} else {
  console.warn(`[wallpaper-demo] ${srcAssets} is missing: the 4K textures are not committed (README.md, Assets)`);
}

const outJs = join(outDir, "wallpaper-demo.js");
const kb = (statSync(outJs).size / 1024).toFixed(1);
console.log(`[wallpaper-demo] built ${outJs} (${kb} KiB) + wallpaper-demo.html + assets/`);