Svelte 5 Runes + SvelteKit 3 WebGPU-First + Auto WebGL2 Fallback Zero-Leak HMR

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.

Active Render Adapter
Compute Pipeline
Pass-2 Pixi 2D HUD
Simulate WebGL2 Fallback
RigidBody Physics
Buoyancy Wave Tank
Elastic 3D Tether
Elasticity 0.82
Buoyancy 1.25
Fluid Level -0.35
Babylon.js 9.0 · webgpu
Unified Pointer & 2D Occlusion Log Click 2D HUD vs 3D Mesh

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.

RigidBody Collisions & Buoyancy
0 hits 0 buoyant

Waiting for elastic rigid-body impacts, tether rebounds, or submerged wave tank motion.

Browser WebGPU Hardware Probe DETECTING

Probing navigator.gpu...

Architecture

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.

@svelte-brush/babylon v9.0+

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.

@svelte-brush/pixi v8.23+

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.

@svelte-brush/three WebGPURenderer

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.

svelte
<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>
WebGPU-First

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.

EngineWebGPU InitializationFallback & USSG Abstraction
Babylon.js 9.0const 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.jsconst 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).

WGSL
Babylon WebGPU ShaderMaterial
rust
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.

@workgroup_size(64)
boids-compute.wgsl
rust
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;
}
Migration & Comparison

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.

8 Tags Migrated 2 Events Upgraded 1 dispose() Removed
Input: Legacy Threlte (@threlte/core)
MigratedUSSG.svelte
svelte
1<script lang="ts">
2 import { Stage, threeAdapter, Camera, Light, Group, Mesh, useTick, Three } from 'svelte-brush';
3let rotation = 0;
4
5 useTick((delta) => {
6 rotation += delta;
7 });
8</script>
9
10<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>
Capabilitysvelte-brush (USSG)Threltesvelte-pixi
Supported EnginesBabylon 9.0, Pixi v8, Three.jsThree.js onlyPixiJS only
Hybrid 2D HUD + 3D WorldShared GPUDevice + 2D Pointer BlockDOM overlay / separate canvas2D focused
WebGPU + Compute Shaders<Stage backend="auto"> + <Compute>Manual WebGPURenderer setupManual shader setup
Zero-Leak HMR DisposalAutomatic ResourceRegistry + live canvasManual dispose rulesManual destroy
Built-in DevTools OverlayUniversal <Inspector /> across enginesExternal browser extensionExternal browser extension
@svelte-brush/kit

SvelteKit-Native Asset Pipeline

100% · Ready

Preloaded directly inside src/routes/+page.ts with ssr = true. Cached entries survive client-side goto() route navigations without re-fetching.

core-icosahedron
/assets/models/core-reactor.glb · 420 KB · GLB v2 · 1 mesh(es) (256 B)
ready (100%)
studio-hdri
/assets/env/nebula-2k.hdr · 1180 KB · Radiance HDR (1024x512)
ready (100%)
pixi-hud-atlas
/assets/hud/controls-v8.json · 165 KB · Spritesheet (3 frames)
ready (100%)
boids-compute
/assets/shaders/boids-field.wgsl · 28 KB · WGSL (1 entry point)
ready (100%)
Zero SSR Friction

Server-Safe Scene Serialization

ssr: true

No export const ssr = false or manual {#if browser} wrappers required. serializeSceneForSSR() produces deterministic metadata on the server while client hydration initializes WebGPU asynchronously.

SSR Hydration Snapshot (+page.ts)
json
{
"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]
}
]
}
Architecture Plan

4-Phase Execution Roadmap

1

Phase 1 — @svelte-brush/core (Adapter & Runes Engine)

Implemented

RenderAdapter contract, async WebGPU-safe StageController context, priority-ordered FrameScheduler (useTick, useSpring, useTransform), and zero-leak HMR ResourceRegistry.

typescript
const id = stage.registerNode({ type: 'mesh', name: 'Core', engine: 'babylon', ... });
// Guaranteed .dispose() / .destroy() on Svelte 5 $effect teardown
2

Phase 2 — @svelte-brush/babylon & @svelte-brush/pixi

Implemented

Babylon.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.

typescript
injectBabylonScene(BABYLON.MeshBuilder.CreateBox, ['box', opts], activeScene);
// + Pixi v8 dual { gpu: wgsl, gl: glsl } shader compiler
3

Phase 3 — @svelte-brush/three & Threlte Migration Codemod

Implemented

Three.js WebGPURenderer + TSL compilation alongside a deterministic codemod converting <Canvas> and <T.*> components into cross-engine USSG primitives.

typescript
const { code, stats } = migrateThrelteCode(threlteSource);
4

Phase 4 — @svelte-brush/kit (SvelteKit 3 Asset & SSR Pipeline)

Implemented

Route-aware preloadSceneAssets() inside +page.ts load functions, reactive asset.progress runes surviving goto(), WebGPU capability detection, and SSR-safe scene serialization.

typescript
export const load = async () => ({
preloadedBundle: await preloadSceneAssets(manifest)
});