# Deterministic XPBD Cloth/Ropes & WebGPU Mood Lighting Presets in Pulse

> Curated WebGPU area environment lighting presets (AgX, volumetric fog, contact shadows, SSR, RT GI) paired with a 100% tick-deterministic cosmetic XPBD rope/banner solver stored in Pulse components.

- **Canonical page:** https://gamecrew.ai/techniques/pulse/deterministic-xpbd-cloth-lighting-recipes
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Pulse
- **Category:** rendering
- **Tags:** `cloth`, `rope`, `xpbd`, `verlet`, `lighting`, `tonemap`, `agx`, `ssr`, `contact-shadows`, `volumetric`, `deterministic`, `webgpu`
- **Verification:** Compile-verified pattern
- **Revision:** 1 (2026-11-13)
- **Demo:** No demo

## When to Use & Field Best Practices

Pulse gives every game a built-in WebGPU renderer with native **`agx` tone mapping**, **physical sky**, **volumetric clouds & fog**, **contact shadows**, **screen-space reflections (`ssr_intensity`)**, **RT global illumination (`rt_gi_intensity`)**, and **depth of field** — while strictly enforcing tick determinism (`systems/*.ts` forbids module-level mutable state, `Math.random`, `Date`, or `Promise`).

Two reliable craft patterns give Pulse worlds AAA-level visual richness without breaking witnessed scenarios:

1. **Flat-Key WebGPU Mood Presets (`world/*.area.jsonc`)**:
   - Always pair `tonemap: "agx"` (hue-faithful bright highlights) with `contact_shadows: 0.65` (`0..1` float strength — grounds characters and props cleanly) and `taa: true`.
   - Use restrained `ssr_intensity` (`0.25–0.45`) on rainy/polished stone areas and moderate `rt_gi_intensity` (`0.25–0.50`) so shadow sides pick up natural bounce color without blowing brightness caps.
   - Keep `fog_density` inversely proportional to level width (`0.002–0.005`) and never invent nested or undocumented environment keys (Pulse warns at `compile`).
2. **Tick-Deterministic Cosmetic XPBD Rope / Banner Physics (`contracts/` + `systems/`)**:
   - For standard capes, flags, and banners driven by area `environment.wind`, Pulse ships a built-in `Cloth` component (`setCloth` / `clothImpulse`). When you need custom wind fields, arbitrary tethers, or multi-point hanging chains, store each node's position and previous position inside a **`cosmetic` component** (`cosmetic` keeps pure visual motion outside gameplay save hashes while remaining 100% deterministic across replays!).
   - Integrate fixed-tick Verlet velocity + `3` distance-constraint passes directly in a pure `system(...)` using entity `pos` as the moving anchor and deterministic `Math.sin` wind — zero module-level arrays or scratch objects!

## Mathematical & Algorithmic Core

- **Fixed-Tick Cosmetic Verlet Step** ($\Delta t = 1 / \text{hz}$, damping $d \in [0.02, 0.08]$):
  $$\mathbf{p}_{k}' = \mathbf{p}_k + (1 - d)(\mathbf{p}_k - \mathbf{p}_{k,\text{prev}}) + (\mathbf{g} + \mathbf{w}(t))\Delta t^2$$
- **Segment Length Projection** for adjacent nodes $(\mathbf{a}, \mathbf{b})$ with rest length $L$:
  $$\boldsymbol{\delta} = \frac{\|\mathbf{b} - \mathbf{a}\| - L}{\max(10^{-4},\, \|\mathbf{b} - \mathbf{a}\|)}(\mathbf{b} - \mathbf{a}), \qquad \mathbf{b} \leftarrow \mathbf{b} - \boldsymbol{\delta}$$

## Engine Implementation

### 1. Calibrated WebGPU Mood Presets (`world/*.area.jsonc` excerpts)

Copy one flat `environment` block matching your brief's mood (all keys verified against Pulse's area schema):

```jsonc
// Preset A — Rain-Slick Neon Undercity (SSR + AgX + Contact Shadows + Readable Rim)
{
  "environment": {
    "physical_sky": false,
    "sky_zenith": "#12182c",
    "sky_horizon": "#243554",
    "sky_ground": "#0c101b",
    "bg_color": "#12182c",
    "sun_dir": [-0.35, -0.78, -0.52],
    "sun_color": "#94c4ff",
    "sun_intensity": 1.35,
    "sun_shadows": true,
    "ambient_color": "#384c78",
    "ambient_intensity": 0.65,
    "ibl_intensity": 0.65,
    "fog_density": 0.0045,
    "volumetric_fog_density": 0.006,
    "volumetric_fog_rgba": "#2c4668",
    "tonemap": "agx",
    "exposure": 1.12,
    "bloom_intensity": 0.32,
    "bloom_threshold": 0.96,
    "contrast": 1.08,
    "saturation": 1.14,
    "vignette": 0.22,
    "contact_shadows": 0.65,
    "ssr_intensity": 0.42,
    "rt_gi_intensity": 0.35,
    "taa": true
  }
}
```

### 2. Deterministic Cosmetic XPBD Tail / Banner System (`contracts/core.decl.jsonc` + `systems/cloth.ts`)

Declare the pure cosmetic component and tuning in one `pulse_declare` call (which writes both into `contracts/core.ts`):
- `"component ClothChain { x1: num=0, y1: num=0, z1: num=0, px1: num=0, py1: num=0, pz1: num=0, x2: num=0, y2: num=0, z2: num=0, px2: num=0, py2: num=0, pz2: num=0, seg_len: num=0.45, tick: int(0..1000000)=0 } cosmetic"`
- `"tuning cloth { gravity_y: -14.0, damping: 0.05, wind_amp: 3.2, wind_freq: 2.1 }"`

Then update it inside a strictly pure, deterministic system (`systems/cloth.ts`) with **zero module-level mutable variables**:

```ts
import { query, system } from "pulse";
import { ClothChain, cloth } from "../contracts/core";

export const clothSystem = system("cloth", { after: "physics" }, (dt) => {
  const dt2 = dt * dt;
  const keep = 1.0 - cloth.damping;

  for (const e of query(ClothChain)) {
    const c = e.clothChain;
    const ax = e.pos.x;
    const ay = e.pos.y;
    const az = e.pos.z;

    // Initialize chain behind anchor on first tick
    if (c.tick === 0) {
      c.x1 = ax; c.y1 = ay - c.seg_len; c.z1 = az;
      c.px1 = c.x1; c.py1 = c.y1; c.pz1 = c.z1;
      c.x2 = ax; c.y2 = ay - c.seg_len * 2.0; c.z2 = az;
      c.px2 = c.x2; c.py2 = c.y2; c.pz2 = c.z2;
    }
    c.tick = (c.tick + 1) % 1000000;

    const phase = c.tick * dt * cloth.wind_freq;
    const windX = Math.sin(phase + ax * 0.5) * cloth.wind_amp;
    const windZ = Math.cos(phase * 0.8 + az * 0.5) * (cloth.wind_amp * 0.65);

    // Verlet integration for node 1
    const nx1 = c.x1 + (c.x1 - c.px1) * keep + windX * dt2;
    const ny1 = c.y1 + (c.y1 - c.py1) * keep + cloth.gravity_y * dt2;
    const nz1 = c.z1 + (c.z1 - c.pz1) * keep + windZ * dt2;
    c.px1 = c.x1; c.py1 = c.y1; c.pz1 = c.z1;
    c.x1 = nx1; c.y1 = ny1; c.z1 = nz1;

    // Verlet integration for node 2
    const nx2 = c.x2 + (c.x2 - c.px2) * keep + windX * 1.3 * dt2;
    const ny2 = c.y2 + (c.y2 - c.py2) * keep + cloth.gravity_y * dt2;
    const nz2 = c.z2 + (c.z2 - c.pz2) * keep + windZ * 1.3 * dt2;
    c.px2 = c.x2; c.py2 = c.y2; c.pz2 = c.z2;
    c.x2 = nx2; c.y2 = ny2; c.z2 = nz2;

    // 3 XPBD distance relaxation passes (exact anchor at entity pos)
    for (let it = 0; it < 3; it++) {
      const dx1 = c.x1 - ax;
      const dy1 = c.y1 - ay;
      const dz1 = c.z1 - az;
      const d1 = Math.sqrt(dx1 * dx1 + dy1 * dy1 + dz1 * dz1);
      if (d1 > 0.0001) {
        const s1 = c.seg_len / d1;
        c.x1 = ax + dx1 * s1;
        c.y1 = ay + dy1 * s1;
        c.z1 = az + dz1 * s1;
      }

      const dx2 = c.x2 - c.x1;
      const dy2 = c.y2 - c.y1;
      const dz2 = c.z2 - c.z1;
      const d2 = Math.sqrt(dx2 * dx2 + dy2 * dy2 + dz2 * dz2);
      if (d2 > 0.0001) {
        const diff = (d2 - c.seg_len) / d2;
        c.x1 += dx2 * (diff * 0.35);
        c.y1 += dy2 * (diff * 0.35);
        c.z1 += dz2 * (diff * 0.35);
        c.x2 -= dx2 * (diff * 0.65);
        c.y2 -= dy2 * (diff * 0.65);
        c.z2 -= dz2 * (diff * 0.65);
      }
    }
  }
});
```

## Browser, Mobile & Software-Renderer Budgets

- **Zero Determinism Leaks**: Because every node coordinate lives on `ClothChain` (`cosmetic`) with zero module-level `let`/`var`/arrays, `pulse compile` accepts the system cleanly and all `spec/scenarios/` replays stay bit-for-bit reproducible.
- **Pair with Native `ProceduralChain` / `ProceduralMotion`**: Use Pulse's built-in `ProceduralChain` for simple character tails/banners and `ClothChain` when you want custom wind fields or interactive gameplay-coupled sway.
- **Night Readability**: Always keep `ambient_intensity >= 0.55` and a non-black `sky_horizon` (`#243554`) so dark neon scenes stay above the `0.10` brightness floor in captures.

## Verification Checklist

1. Run `compile` and confirm zero determinism violations and zero unknown environment-key warnings.
2. Run `pulse_look` on `"views play"` and inspect the PNG to confirm crisp contact shadows, readable SSR floor sheen, and non-clipped `agx` highlights.
3. Run `test` to confirm every kept scenario remains witnessed.

## Official references

- Matthias Müller / matthias-research — Ten Minute Physics XPBD Constraints (MIT)
- KhronosGroup — ToneMapping AgX & PBR Neutral Reference (Apache-2.0)
- Pulse Deterministic ECS & WebGPU Area Environment Reference

## Open-source sources

- Takes an idea from Ten Minute Physics by Matthias Müller (MIT License), https://github.com/matthias-research/pages (fixed-step XPBD Verlet distance constraint formulation)
- Takes an idea from ToneMapping by Khronos Group (Apache License 2.0), https://github.com/KhronosGroup/ToneMapping (AgX and hue-preserving display transform calibration)

Games built with this guide carry these credits, and the licence notices they ask for, in their third-party notices.
