# noniv/snowflow_demo architecture

> Architecture diagram of the GitHub repository noniv/snowflow_demo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/noniv/snowflow_demo
- Repository: https://github.com/noniv/snowflow_demo

## Overview

#### Purpose and runtime
SNOWFLOW is a browser-only real-time snow-rendering tech demo. It uses Babylon.js on WebGPU with custom WGSL; there is intentionally no WebGL fallback and no authored runtime assets. Vite serves/builds the application from `index.html` into the JavaScript entry point `src/main.js`.

#### Frame orchestration and core services
`src/main.js` owns initialization, loading/readiness gating, and per-frame ordering. Core systems provide camera/input motion, GPU resource helpers, live settings, loading state, and frame instrumentation:
- `src/core/camera.js`
- `src/core/input.js`
- `src/core/loading.js`
- `src/core/settings.js`
- `src/core/perf.js`

The browser/WebGPU device is the primary platform boundary; Babylon.js supplies engine/resource abstractions while application rendering behavior remains in custom shaders and passes.

#### Procedural terrain and persistent snow state
Terrain is a static nested-ring clipmap whose vertex shaders place, morph, and displace geometry; CPU code does not rebuild terrain each frame. Load-time terrain generation creates the procedural heightfield and supports CPU-side grounding samples. Persistent snow deformation is a ping-ponged GPU state updated every frame, receiving brushes from character, wake, and spells.
- `src/terrain/terrain.js`
- `src/terrain/clipmapMesh.js`
- `src/terrain/heightfield.js`
- `src/terrain/deformation.js`
- `src/shaders/deformSim.fragment.wgsl`

This is a key shared boundary: writers produce deformation inputs, while terrain beauty and shadow shaders consume the resulting state.

#### Scene rendering, sky, and shadows
The render layer builds custom depth/prepass and three-cascade shadow workflows compatible with GPU-displaced terrain and procedural casters. Sky/IBL is generated from atmospheric shaders rather than external HDR assets.
- `src/render/depthPass.js`
- `src/render/shadows.js`
- `src/render/sky.js`
- `src/shaders/lib/terrain.wgsl`
- `src/shaders/lib/shadowLookup.wgsl`

Shader registration and include composition are centralized in `src/shaders/registry.js`; shared WGSL libraries encode terrain, atmosphere, shading, deformation, water, and dynamic-light contracts.

#### Character simulation and contact
The character is generated from a procedural skeleton/figure definition, driven by controller motion and foot placement, with cloth simulation and snow-contact output. Its GPU-facing character/cloth state is rendered through dedicated WGSL variants for beauty, depth, and prepass.
- `src/character/build.js`
- `src/character/controller.js`
- `src/character/cloth.js`
- `src/character/snowContact.js`
- `src/shaders/char.vertex.wgsl`

Character contact feeds the terrain deformation boundary rather than maintaining a separate footprint system.

#### Dynamic interactions: wake, particles, and spells
Snow-surf creates a swept wake mesh plus spray; spells are coordinated by a shared system. Water-like spells reuse a common swept-body representation, write into deformation, and populate a pooled dynamic-light interface consumed by common shading.
- `src/vfx/surfWake.js`
- `src/vfx/particles.js`
- `src/spells/spellSystem.js`
- `src/spells/waterBody.js`
- `src/spells/spellLights.js`

Individual spell modules (`src/spells/sweep.js`, `src/spells/ribbon.js`, etc.) are behavior producers, while shared water/light/deformation paths keep rendering draw/resource usage bounded.

#### Post-processing and presentation
A depth/specular prepass feeds a post chain implementing temporal and screen-space effects before final presentation. UI exposes live art/system controls and performance/debug views.
- `src/post/postChain.js`
- `src/shaders/prepass.fragment.wgsl`
- `src/shaders/post/taa.fragment.wgsl`
- `src/shaders/post/tonemap.fragment.wgsl`
- `src/ui/overlay.js`

The major control flow is: input/camera → simulation and interaction writers → deformation update → shadow/depth/beauty rendering → post chain → overlay/performance reporting.

Main entry points: [`src/main.js`](https://github.com/noniv/snowflow_demo/blob/main/src/main.js) (Frame orchestrator), [`src/terrain/terrain.js`](https://github.com/noniv/snowflow_demo/blob/main/src/terrain/terrain.js) (Clipmap terrain), [`src/terrain/deformation.js`](https://github.com/noniv/snowflow_demo/blob/main/src/terrain/deformation.js) (Snow deformation state), [`src/shaders/lib/terrain.wgsl`](https://github.com/noniv/snowflow_demo/blob/main/src/shaders/lib/terrain.wgsl) (Beauty rendering)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App runtime"]
  node_vite["Vite entry<br/>build entry<br/>[index.html]"]
  node_main["Frame orchestrator<br/>application loop<br/>[main.js]"]
  node_platform{{"Browser WebGPU + Babylon<br/>platform boundary"}}
  node_camera_input["Camera and input<br/>motion controls<br/>[camera.js]"]
  node_runtime_services["Settings, loading, perf<br/>runtime services<br/>[settings.js]"]
  node_overlay["Control and debug overlay<br/>UI presentation<br/>[overlay.js]"]
end

subgraph group_world["Terrain and simulation"]
  node_terrain["Clipmap terrain<br/>procedural terrain<br/>[terrain.js]"]
  node_heightfield["Procedural heightfield<br/>terrain data<br/>[heightfield.js]"]
  node_deformation[("Snow deformation state<br/>ping-pong GPU state<br/>[deformation.js]")]
  node_deform_shader["Deformation simulation<br/>WGSL simulation shader"]
end

subgraph group_interaction["Character and interactions"]
  node_character["Procedural character<br/>character simulation<br/>[build.js]"]
  node_contact["Snow contact<br/>deformation writer<br/>[snowContact.js]"]
  node_wake_particles["Surf wake and spray<br/>interaction effects<br/>[surfWake.js]"]
  node_spells["Spell system<br/>interaction coordinator<br/>[spellSystem.js]"]
  node_water_lights["Water bodies and spell lights<br/>shared spell resources<br/>[waterBody.js]"]
end

subgraph group_render["Rendering pipeline"]
  node_shader_registry["WGSL registry and libraries<br/>shader composition<br/>[registry.js]"]
  node_sky["Atmospheric sky and IBL<br/>procedural environment<br/>[sky.js]"]
  node_shadows["Cascaded shadows<br/>shadow pipeline<br/>[shadows.js]"]
  node_depth_prepass["Depth and specular prepass<br/>render pass<br/>[depthPass.js]"]
  node_beauty["Beauty rendering<br/>scene render<br/>[terrain.wgsl]"]
  node_post["Post chain<br/>screen-space pipeline<br/>[postChain.js]"]
end

node_vite -->|"loads"| node_main
node_main -->|"initializes"| node_platform
node_main -->|"updates"| node_camera_input
node_main -->|"uses"| node_runtime_services
node_main -->|"initializes"| node_terrain
node_heightfield -->|"grounds and shapes"| node_terrain
node_camera_input -->|"drives motion"| node_character
node_character -->|"produces foot contact"| node_contact
node_contact -->|"writes brushes"| node_deformation
node_wake_particles -->|"writes wake"| node_deformation
node_spells -->|"coordinates"| node_water_lights
node_water_lights -->|"writes spells"| node_deformation
node_deform_shader -->|"updates ping-pong state"| node_deformation
node_deformation -->|"displaces snow"| node_terrain
node_shader_registry -->|"registers"| node_deform_shader
node_shader_registry -->|"composes shaders"| node_beauty
node_terrain -->|"casts displaced terrain"| node_shadows
node_character -->|"casts procedural figure"| node_shadows
node_shadows -->|"shadow lookup"| node_beauty
node_sky -->|"provides IBL"| node_beauty
node_water_lights -->|"dynamic lights"| node_beauty
node_terrain -->|"renders depth"| node_depth_prepass
node_character -->|"renders depth"| node_depth_prepass
node_depth_prepass -->|"depth and specular inputs"| node_post
node_beauty -->|"HDR scene color"| node_post
node_post -->|"final presentation"| node_overlay
node_runtime_services -->|"settings and metrics"| node_overlay

click node_vite "https://github.com/noniv/snowflow_demo/blob/main/index.html"
click node_main "https://github.com/noniv/snowflow_demo/blob/main/src/main.js"
click node_camera_input "https://github.com/noniv/snowflow_demo/blob/main/src/core/camera.js"
click node_runtime_services "https://github.com/noniv/snowflow_demo/blob/main/src/core/settings.js"
click node_terrain "https://github.com/noniv/snowflow_demo/blob/main/src/terrain/terrain.js"
click node_heightfield "https://github.com/noniv/snowflow_demo/blob/main/src/terrain/heightfield.js"
click node_deformation "https://github.com/noniv/snowflow_demo/blob/main/src/terrain/deformation.js"
click node_deform_shader "https://github.com/noniv/snowflow_demo/blob/main/src/shaders/deformSim.fragment.wgsl"
click node_character "https://github.com/noniv/snowflow_demo/blob/main/src/character/build.js"
click node_contact "https://github.com/noniv/snowflow_demo/blob/main/src/character/snowContact.js"
click node_wake_particles "https://github.com/noniv/snowflow_demo/blob/main/src/vfx/surfWake.js"
click node_spells "https://github.com/noniv/snowflow_demo/blob/main/src/spells/spellSystem.js"
click node_water_lights "https://github.com/noniv/snowflow_demo/blob/main/src/spells/waterBody.js"
click node_shader_registry "https://github.com/noniv/snowflow_demo/blob/main/src/shaders/registry.js"
click node_sky "https://github.com/noniv/snowflow_demo/blob/main/src/render/sky.js"
click node_shadows "https://github.com/noniv/snowflow_demo/blob/main/src/render/shadows.js"
click node_depth_prepass "https://github.com/noniv/snowflow_demo/blob/main/src/render/depthPass.js"
click node_beauty "https://github.com/noniv/snowflow_demo/blob/main/src/shaders/lib/terrain.wgsl"
click node_post "https://github.com/noniv/snowflow_demo/blob/main/src/post/postChain.js"
click node_overlay "https://github.com/noniv/snowflow_demo/blob/main/src/ui/overlay.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_vite,node_main,node_platform,node_camera_input,node_runtime_services,node_overlay toneBlue
class node_terrain,node_heightfield,node_deformation,node_deform_shader toneAmber
class node_character,node_contact,node_wake_particles,node_spells,node_water_lights toneMint
class node_shader_registry,node_sky,node_shadows,node_depth_prepass,node_beauty,node_post toneRose
```

## Components

### App runtime

- [Vite entry](https://github.com/noniv/snowflow_demo/blob/main/index.html): build entry · Serves and builds the browser application. · `index.html`
- [Frame orchestrator](https://github.com/noniv/snowflow_demo/blob/main/src/main.js): application loop · Initializes systems, gates readiness, and orders each frame. · `src/main.js`
- **Browser WebGPU + Babylon**: platform boundary · WebGPU-only device and Babylon resource abstractions.
- [Camera and input](https://github.com/noniv/snowflow_demo/blob/main/src/core/camera.js): motion controls · `src/core/camera.js`
- [Settings, loading, perf](https://github.com/noniv/snowflow_demo/blob/main/src/core/settings.js): runtime services · `src/core/settings.js`
- [Control and debug overlay](https://github.com/noniv/snowflow_demo/blob/main/src/ui/overlay.js): UI presentation · `src/ui/overlay.js`

### Terrain and simulation

- [Clipmap terrain](https://github.com/noniv/snowflow_demo/blob/main/src/terrain/terrain.js): procedural terrain · Static nested-ring terrain with GPU vertex displacement. · `src/terrain/terrain.js`
- [Procedural heightfield](https://github.com/noniv/snowflow_demo/blob/main/src/terrain/heightfield.js): terrain data · Load-time terrain generation and CPU grounding samples. · `src/terrain/heightfield.js`
- [Snow deformation state](https://github.com/noniv/snowflow_demo/blob/main/src/terrain/deformation.js): ping-pong GPU state · Shared persistent snow state updated every frame. · `src/terrain/deformation.js`
- [Deformation simulation](https://github.com/noniv/snowflow_demo/blob/main/src/shaders/deformSim.fragment.wgsl): WGSL simulation shader · `src/shaders/deformSim.fragment.wgsl`

### Character and interactions

- [Procedural character](https://github.com/noniv/snowflow_demo/blob/main/src/character/build.js): character simulation · Controller-driven figure, foot placement, and cloth. · `src/character/build.js`
- [Snow contact](https://github.com/noniv/snowflow_demo/blob/main/src/character/snowContact.js): deformation writer · `src/character/snowContact.js`
- [Surf wake and spray](https://github.com/noniv/snowflow_demo/blob/main/src/vfx/surfWake.js): interaction effects · Swept wake mesh and snow spray. · `src/vfx/surfWake.js`
- [Spell system](https://github.com/noniv/snowflow_demo/blob/main/src/spells/spellSystem.js): interaction coordinator · Coordinates individual spell behavior producers. · `src/spells/spellSystem.js`
- [Water bodies and spell lights](https://github.com/noniv/snowflow_demo/blob/main/src/spells/waterBody.js): shared spell resources · Swept spell bodies write snow and populate pooled lights. · `src/spells/waterBody.js`

### Rendering pipeline

- [WGSL registry and libraries](https://github.com/noniv/snowflow_demo/blob/main/src/shaders/registry.js): shader composition · Central registration and shared rendering contracts. · `src/shaders/registry.js`
- [Atmospheric sky and IBL](https://github.com/noniv/snowflow_demo/blob/main/src/render/sky.js): procedural environment · Generated from atmospheric shaders without HDR assets. · `src/render/sky.js`
- [Cascaded shadows](https://github.com/noniv/snowflow_demo/blob/main/src/render/shadows.js): shadow pipeline · Three cascades support displaced terrain and procedural casters. · `src/render/shadows.js`
- [Depth and specular prepass](https://github.com/noniv/snowflow_demo/blob/main/src/render/depthPass.js): render pass · `src/render/depthPass.js`
- [Beauty rendering](https://github.com/noniv/snowflow_demo/blob/main/src/shaders/lib/terrain.wgsl): scene render · Renders terrain, character, and dynamic effects. · `src/shaders/lib/terrain.wgsl`
- [Post chain](https://github.com/noniv/snowflow_demo/blob/main/src/post/postChain.js): screen-space pipeline · Temporal and screen-space effects followed by tone mapping. · `src/post/postChain.js`

## Connections

- Vite entry → Frame orchestrator: loads
- Frame orchestrator → Browser WebGPU + Babylon: initializes
- Frame orchestrator → Camera and input: updates
- Frame orchestrator → Settings, loading, perf: uses
- Frame orchestrator → Clipmap terrain: initializes
- Procedural heightfield → Clipmap terrain: grounds and shapes
- Camera and input → Procedural character: drives motion
- Procedural character → Snow contact: produces foot contact
- Snow contact → Snow deformation state: writes brushes
- Surf wake and spray → Snow deformation state: writes wake
- Spell system → Water bodies and spell lights: coordinates
- Water bodies and spell lights → Snow deformation state: writes spells
- Deformation simulation → Snow deformation state: updates ping-pong state
- Snow deformation state → Clipmap terrain: displaces snow
- WGSL registry and libraries → Deformation simulation: registers
- WGSL registry and libraries → Beauty rendering: composes shaders
- Clipmap terrain → Cascaded shadows: casts displaced terrain
- Procedural character → Cascaded shadows: casts procedural figure
- Cascaded shadows → Beauty rendering: shadow lookup
- Atmospheric sky and IBL → Beauty rendering: provides IBL
- Water bodies and spell lights → Beauty rendering: dynamic lights
- Clipmap terrain → Depth and specular prepass: renders depth
- Procedural character → Depth and specular prepass: renders depth
- Depth and specular prepass → Post chain: depth and specular inputs
- Beauty rendering → Post chain: HDR scene color
- Post chain → Control and debug overlay: final presentation
- Settings, loading, perf → Control and debug overlay: settings and metrics
