2026-07-18 14:03:23 +02:00
|
|
|
/* @jsxImportSource @diffusionstudio/jsx */
|
2026-07-20 14:15:45 +02:00
|
|
|
/* Three.js owning a <surface>: a glTF helmet spun by the playhead.
|
2026-07-18 14:03:23 +02:00
|
|
|
*
|
2026-08-25 14:32:48 +02:00
|
|
|
* cp examples/06-three.tsx ~/Projects/three/index.tsx
|
|
|
|
|
* dapi open ~/Projects/three
|
2026-07-18 14:03:23 +02:00
|
|
|
*
|
2026-08-25 14:32:48 +02:00
|
|
|
* `ref={surfaceRef}` assigns the surface's node, whose `element` is its
|
|
|
|
|
* detached canvas; onMount three.js takes it over and renders into it;
|
2026-07-18 14:03:23 +02:00
|
|
|
* the engine samples that bitmap into the node's box every frame, so a WebGL
|
|
|
|
|
* scene composites like any other paint (needs preserveDrawingBuffer so the
|
|
|
|
|
* per-frame readback doesn't come back blank). One clock drives everything:
|
|
|
|
|
* useTicker's composition time seeks the helmet's rotation inside a
|
|
|
|
|
* createEffect, so scrubbing and exports stay frame-accurate, not wall-clock.
|
|
|
|
|
*
|
2026-08-25 16:31:49 +02:00
|
|
|
* An export mounts the module again in a world of its own, so the .glb is
|
|
|
|
|
* fetched from scratch there and races the first frames — which is what
|
|
|
|
|
* `hold` is for: the encoder waits for the model before it samples anything.
|
|
|
|
|
*
|
2026-07-18 14:03:23 +02:00
|
|
|
* The "Battle Damaged Sci-Fi Helmet" is the Khronos DamagedHelmet sample, a
|
2026-08-21 23:05:45 +02:00
|
|
|
* single-file .glb GLTFLoader fetches directly over the network.
|
|
|
|
|
* RoomEnvironment bakes an in-memory PBR reflection map, so the metal reads
|
|
|
|
|
* without shipping an HDR.
|
2026-07-18 14:03:23 +02:00
|
|
|
*/
|
|
|
|
|
|
2026-08-25 14:32:48 +02:00
|
|
|
import { createEffect, createSignal, onCleanup, onMount } from "solid-js";
|
2026-07-20 14:15:45 +02:00
|
|
|
import { useTicker } from "@diffusionstudio/jsx";
|
2026-08-25 14:32:48 +02:00
|
|
|
import type { SceneNode } from "@diffusionstudio/jsx";
|
2026-07-18 14:03:23 +02:00
|
|
|
import * as THREE from "three";
|
|
|
|
|
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
|
|
|
import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment.js";
|
|
|
|
|
|
|
|
|
|
const MODEL_URL =
|
|
|
|
|
"https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/DamagedHelmet/glTF-Binary/DamagedHelmet.glb";
|
|
|
|
|
|
|
|
|
|
const TURN_SECONDS = 6; // one full revolution per 6s of composition time
|
|
|
|
|
|
2026-07-20 15:53:30 +02:00
|
|
|
export default function ThreeHelmet() {
|
2026-08-25 16:31:49 +02:00
|
|
|
const { time, hold } = useTicker();
|
2026-07-18 14:03:23 +02:00
|
|
|
const [loaded, setLoaded] = createSignal(false);
|
|
|
|
|
|
2026-08-25 14:32:48 +02:00
|
|
|
let surfaceRef: SceneNode | undefined;
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
const el = surfaceRef!.element;
|
|
|
|
|
if (!el) return;
|
2026-07-18 14:03:23 +02:00
|
|
|
const renderer = new THREE.WebGLRenderer({
|
|
|
|
|
canvas: el,
|
|
|
|
|
antialias: true,
|
|
|
|
|
alpha: true,
|
|
|
|
|
preserveDrawingBuffer: true,
|
|
|
|
|
});
|
|
|
|
|
renderer.setSize(el.width, el.height, false);
|
|
|
|
|
renderer.outputColorSpace = THREE.SRGBColorSpace;
|
|
|
|
|
renderer.toneMapping = THREE.ACESFilmicToneMapping;
|
|
|
|
|
renderer.toneMappingExposure = 1.1;
|
|
|
|
|
|
|
|
|
|
const scene = new THREE.Scene();
|
|
|
|
|
const camera = new THREE.PerspectiveCamera(45, el.width / el.height, 0.1, 100);
|
|
|
|
|
camera.position.set(0, 0.35, 4);
|
|
|
|
|
camera.lookAt(0, 0, 0);
|
|
|
|
|
|
|
|
|
|
// In-memory image-based lighting: gives the PBR metal real reflections
|
|
|
|
|
// without fetching an external HDR.
|
|
|
|
|
const pmrem = new THREE.PMREMGenerator(renderer);
|
|
|
|
|
scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
|
|
|
|
|
|
|
|
|
|
// Key + hemisphere fill, so the helmet reads with shape and highlights.
|
|
|
|
|
const key = new THREE.DirectionalLight(0xffffff, 2.6);
|
|
|
|
|
key.position.set(5, 8, 6);
|
|
|
|
|
scene.add(key);
|
|
|
|
|
scene.add(new THREE.HemisphereLight(0xbad6ff, 0x1a1a22, 1.1));
|
|
|
|
|
|
|
|
|
|
// Rotating around the pivot keeps the model centred as it spins.
|
|
|
|
|
const pivot = new THREE.Group();
|
|
|
|
|
scene.add(pivot);
|
|
|
|
|
|
|
|
|
|
let disposed = false;
|
2026-08-25 16:31:49 +02:00
|
|
|
// Held, so export and capture wait for the helmet rather than writing the
|
|
|
|
|
// frames it hasn't arrived for; live playback collects nothing.
|
|
|
|
|
const ready = new GLTFLoader()
|
2026-07-18 14:03:23 +02:00
|
|
|
.loadAsync(MODEL_URL)
|
|
|
|
|
.then((gltf) => {
|
|
|
|
|
if (disposed) return;
|
|
|
|
|
const model = gltf.scene;
|
|
|
|
|
// Centre on the pivot and normalise scale so the helmet fills the frame.
|
|
|
|
|
const box = new THREE.Box3().setFromObject(model);
|
|
|
|
|
const size = box.getSize(new THREE.Vector3()).length();
|
|
|
|
|
const center = box.getCenter(new THREE.Vector3());
|
|
|
|
|
model.position.sub(center);
|
|
|
|
|
pivot.scale.setScalar(2.6 / size);
|
|
|
|
|
pivot.add(model);
|
|
|
|
|
setLoaded(true); // wake the effect so the first frame renders even when paused
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => console.error("Helmet model load failed:", err));
|
|
|
|
|
|
2026-08-25 16:31:49 +02:00
|
|
|
hold(ready);
|
|
|
|
|
|
2026-07-18 14:03:23 +02:00
|
|
|
// The playhead is the only clock. Reading time() (and loaded()) subscribes
|
|
|
|
|
// the effect, so it re-renders every tick and once the model streams in.
|
|
|
|
|
createEffect(() => {
|
|
|
|
|
loaded();
|
|
|
|
|
pivot.rotation.y = time() * ((Math.PI * 2) / TURN_SECONDS);
|
|
|
|
|
renderer.render(scene, camera);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onCleanup(() => {
|
|
|
|
|
disposed = true;
|
|
|
|
|
pmrem.dispose();
|
|
|
|
|
renderer.dispose();
|
|
|
|
|
});
|
2026-08-25 14:32:48 +02:00
|
|
|
});
|
2026-07-18 14:03:23 +02:00
|
|
|
|
|
|
|
|
return (
|
2026-08-25 16:43:40 +02:00
|
|
|
<stage camera={[0.6, 0, 0, 0.6, 85, 150]}>
|
|
|
|
|
<scene name="Three helmet" width={960} height={540} fill="#0b0d12" active>
|
2026-08-25 14:32:48 +02:00
|
|
|
<surface x={0} y={0} width={960} height={540} ref={surfaceRef} />
|
|
|
|
|
</scene>
|
|
|
|
|
</stage>
|
2026-07-18 14:03:23 +02:00
|
|
|
);
|
2026-07-20 15:53:30 +02:00
|
|
|
}
|