Universal Svelte Scene Graph One Declarative Tree. Three Engines.
Write declarative 2D + 3D graphics once in Svelte 5 and hot-swap between Babylon.js 9.0+, PixiJS v8.23+, and Three.js—with shared-device hybrid 2D HUD layers, WGSL compute pipelines, deterministic HMR disposal, and SSR-safe SvelteKit asset streaming.
Click any 3D mesh or the left-side PixiJS 2D HUD cards inside the canvas to inspect unified hit-testing and 2D-over-3D pointer blocking.
Waiting for elastic rigid-body impacts, tether rebounds, or submerged wave tank motion.
Probing navigator.gpu...
Two-Layer Universal Scene Graph
Instead of maintaining three separate wrapper libraries, USSG separates cross-engine
semantic primitives (Layer 2) from dynamic engine proxies (Layer 1) behind a single
pluggable RenderAdapter interface.
Automatic Scene Injection
Babylon requires passing scene into constructors like MeshBuilder.CreateBox(name, opts, scene). <Babylon.*> pulls the active Scene from Svelte Context via injectBabylonScene() automatically.
Hybrid 2D HUD & @pixi/3d
Composes crisp 2D vector UI and sprites as Pass 2 over a 3D Babylon or Three world on the
exact same canvas & GPUDevice, blocking 3D raycasts behind 2D HUD hits.
TSL + Threlte Drop-In
Compiles Three Shading Language (TSL) to WGSL/GLSL automatically and includes a
deterministic codemod that converts <Canvas> and <T.*> components into USSG.
<script lang="ts"> import { Stage, Group, Camera, Light, Mesh, Compute, babylonAdapter } from 'svelte-brush';</script><!-- Swap babylonAdapter / pixiAdapter / threeAdapter with zero scene rewrites --><Stage adapter={babylonAdapter} backend="auto"> {#snippet fallback({ reason })} <p>Running in WebGL2 fallback: {reason}</p> {/snippet} <Camera type="orbit" fov={48} position={[0, 2.2, 6.4]} /> <Light type="hemispheric" intensity={1.1} /> <Compute mode="galaxy" particles={1024} /> <Group autoRotate> <Mesh geometry="icosahedron" color="#ff6b35" shader={HOLOGRAM_SHADER} /> </Group></Stage>Unified WebGPU Init, Compute & Dual-Target Shaders
navigator.gpu.requestAdapter() and engine.initAsync() are always
asynchronous. <Stage> provides a reactive context immediately at mount
where sceneReady flips from false to true once ready—so
child meshes attach with zero race conditions.
| Engine | WebGPU Initialization | Fallback & USSG Abstraction |
|---|---|---|
| Babylon.js 9.0 | const e = new WebGPUEngine(canvas); await e.initAsync(); | USSG probes WebGPUEngine.IsSupportedAsync before instantiation and swaps
to Engine (WebGL2) transparently. |
| PixiJS v8.23+ | await app.init({ preference: 'webgpu', gpuDevice }) | Forces preference: 'webgpu', shares parent GPUDevice, and
maps shaders to dual gpu (WGSL) / gl (GLSL). |
| Three.js | const r = new WebGPURenderer(); await r.init(); | Falls back to WebGL2 internally and routes defineShader() through TSL when
compiling node materials. |
Engine-Agnostic Shader Compiler
Adapts output dynamically to the active Studio engine & backend (Babylon WebGPU ShaderMaterial).
struct Uniforms { time: f32, color: vec3f };@group(0) @binding(0) var<uniform> u: Uniforms;@fragment fn fs_main(@location(0) normal: vec3f, @location(1) uv: vec2f) -> @location(0) vec4f { let fresnel = pow(1.0 - abs(normal.z), 2.4); let scan = 0.5 + 0.5 * sin(uv.y * 42.0 - u.time * 4.5); return vec4f(u.color * (0.45 + fresnel * 0.9) * (0.75 + 0.25 * scan), 0.92);}Declarative <Compute> Pipeline
Binds WGSL compute kernels directly into the unified useTick scheduler.
struct Particle { pos: vec4f, vel: vec4f,};@group(0) @binding(0) var<storage, read_write> particles: array<Particle>;@group(0) @binding(1) var<uniform> dt: f32;@compute @workgroup_size(64)fn cs_main(@builtin(global_invocation_id) id: vec3u) { let i = id.x; if (i >= arrayLength(&particles)) { return; } var p = particles[i]; let swirl = vec3f(-p.pos.z, sin(p.pos.x + dt) * 0.4, p.pos.x); p.vel = vec4f(mix(p.vel.xyz, normalize(swirl) * 1.4, 0.08), 1.0); p.pos = vec4f(p.pos.xyz + p.vel.xyz * dt, 1.0); particles[i] = p;}Interactive Threlte-to-USSG Codemod
Edit or load a Threlte component on the left to compile it into Svelte 5 USSG code with upgraded event handlers, semantic primitives, and automatic HMR disposal.
1<script lang="ts">2 import { Stage, threeAdapter, Camera, Light, Group, Mesh, useTick, Three } from 'svelte-brush';3let rotation = 0;45 useTick((delta) => {6 rotation += delta;7 });8</script>910<Stage adapter={threeAdapter} backend="auto">11 <Camera type="orbit" makeDefault position={[0, 2, 6]} />12 <Light type="directional" position={[4, 6, 4]} intensity={1.2} />13 <Group rotation={[0, rotation, 0]}>14 <Mesh geometry="box" onclick={() => console.log('clicked')} onpointerover={() => {}} color="#ff3e00" />15 </Group>16</Stage>| Capability | svelte-brush (USSG) | Threlte | svelte-pixi |
|---|---|---|---|
| Supported Engines | Babylon 9.0, Pixi v8, Three.js | Three.js only | PixiJS only |
| Hybrid 2D HUD + 3D World | Shared GPUDevice + 2D Pointer Block | DOM overlay / separate canvas | 2D focused |
| WebGPU + Compute Shaders | <Stage backend="auto"> + <Compute> | Manual WebGPURenderer setup | Manual shader setup |
| Zero-Leak HMR Disposal | Automatic ResourceRegistry + live canvas | Manual dispose rules | Manual destroy |
| Built-in DevTools Overlay | Universal <Inspector /> across engines | External browser extension | External browser extension |
SvelteKit-Native Asset Pipeline
Preloaded directly inside src/routes/+page.ts with ssr = true. Cached entries survive client-side goto() route navigations
without re-fetching.
Server-Safe Scene Serialization
No export const ssr = false or manual {#if browser} wrappers required. serializeSceneForSSR() produces deterministic metadata on the
server while client hydration initializes WebGPU asynchronously.
{ "schema": "ussg/v1", "ssrSafe": true, "nodeCount": 5, "engines": [ "babylon", "pixi" ], "nodes": [ { "id": "camera-main", "type": "camera", "name": "Orbit Camera", "engine": "babylon", "position": [0, 2.2, 6.4] }, { "id": "mesh-core", "type": "mesh", "name": "Quantum Core", "engine": "babylon", "position": [0, 0.2, 0] }, { "id": "mesh-ring", "type": "mesh", "name": "Orbital Torus", "engine": "babylon", "position": [0, 0.2, 0] }, { "id": "compute-galaxy", "type": "compute", "name": "WebGPU Particle Field", "engine": "babylon", "position": [0, 0, 0] }, { "id": "hud-layer", "type": "layer", "name": "Pixi v8 2D HUD", "engine": "pixi", "position": [16, 16, 0] } ]}4-Phase Execution Roadmap
Phase 1 — @svelte-brush/core (Adapter & Runes Engine)
ImplementedRenderAdapter contract, async WebGPU-safe StageController context, priority-ordered FrameScheduler (useTick, useSpring, useTransform), and zero-leak HMR ResourceRegistry.
const id = stage.registerNode({ type: 'mesh', name: 'Core', engine: 'babylon', ... });// Guaranteed .dispose() / .destroy() on Svelte 5 $effect teardownPhase 2 — @svelte-brush/babylon & @svelte-brush/pixi
ImplementedBabylon.js 9.0+ WebGPUEngine adapter with automatic Svelte Context scene injection plus PixiJS v8.23+ hybrid 2D/@pixi/3d adapter with Pass-2 HUD pointer occlusion.
injectBabylonScene(BABYLON.MeshBuilder.CreateBox, ['box', opts], activeScene);// + Pixi v8 dual { gpu: wgsl, gl: glsl } shader compilerPhase 3 — @svelte-brush/three & Threlte Migration Codemod
ImplementedThree.js WebGPURenderer + TSL compilation alongside a deterministic codemod converting <Canvas> and <T.*> components into cross-engine USSG primitives.
const { code, stats } = migrateThrelteCode(threlteSource);Phase 4 — @svelte-brush/kit (SvelteKit 3 Asset & SSR Pipeline)
ImplementedRoute-aware preloadSceneAssets() inside +page.ts load functions, reactive asset.progress runes surviving goto(), WebGPU capability detection, and SSR-safe scene serialization.
export const load = async () => ({ preloadedBundle: await preloadSceneAssets(manifest)});