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.8 KB
Raw Blame
// Minimal native three.js r187 WebGPURenderer + TSL sample scene for ThreeRT `--renderer webgpu`.
// Renders a lit studio scene with TSL node materials into `globalThis.__threertGpuCanvas` over
// native `navigator.gpu` (wgpu), with zero path-tracer code.
import * as THREE from "three/webgpu";
import { color, mix, normalWorld, positionWorld, sin } from "three/tsl";

declare const __threertGpuCanvas: HTMLCanvasElement;
declare const __native: { sys: { exit(code?: number): void } };

async function main() {
  const canvas = __threertGpuCanvas;
  const renderer = new THREE.WebGPURenderer({ canvas, antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio || 1);
  renderer.setSize(window.innerWidth, window.innerHeight, false);
  renderer.toneMapping = THREE.ACESFilmicToneMapping;
  renderer.toneMappingExposure = 1.1;
  await renderer.init();

  const scene = new THREE.Scene();
  scene.background = new THREE.Color(0x10141d);

  const camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 100);
  camera.position.set(0, 2.1, 6.4);
  camera.lookAt(0, 1.35, 0);

  // Studio lighting
  const hemi = new THREE.HemisphereLight(0xdbe8ff, 0x262936, 1.1);
  scene.add(hemi);

  const key = new THREE.DirectionalLight(0xfff1dc, 2.8);
  key.position.set(4, 6, 4);
  scene.add(key);

  const rim = new THREE.DirectionalLight(0x52a8ff, 1.4);
  rim.position.set(-4, 3, -3);
  scene.add(rim);

  // TSL node material: subtle vertical/normal color gradient on a metallic torus knot
  const knotMat = new THREE.MeshStandardNodeMaterial({ metalness: 0.35, roughness: 0.28 });
  const rimMix = normalWorld.y.mul(0.5).add(0.5);
  knotMat.colorNode = mix(color(0x2d6bff), color(0xff9f43), rimMix);

  const knot = new THREE.Mesh(new THREE.TorusKnotGeometry(0.95, 0.32, 128, 32), knotMat);
  knot.position.set(0, 1.55, 0);
  scene.add(knot);

  // Ground plane with a procedural TSL checkerboard-like wave pattern
  const floorMat = new THREE.MeshStandardNodeMaterial({ roughness: 0.72, metalness: 0.08 });
  const wave = sin(positionWorld.x.mul(3.0)).mul(sin(positionWorld.z.mul(3.0))).mul(0.5).add(0.5);
  floorMat.colorNode = mix(color(0x1c212e), color(0x283042), wave);

  const floor = new THREE.Mesh(new THREE.PlaneGeometry(18, 18), floorMat);
  floor.rotation.x = -Math.PI / 2;
  scene.add(floor);

  // Render a few frames so pipelines compile and steady-state draws complete cleanly.
  let frame = 0;
  const totalFrames = 4;
  const step = () => {
    knot.rotation.y = 0.45 + frame * 0.15;
    knot.rotation.x = 0.25 + frame * 0.05;
    renderer.render(scene, camera);
    frame++;
    if (frame >= totalFrames) {
      __native.sys.exit(0);
      return;
    }
    requestAnimationFrame(step);
  };
  requestAnimationFrame(step);
}

main().catch((err) => {
  console.error("webgpu sample failed:", err);
  __native.sys.exit(1);
});