depthview-mcp 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ # Clouds, smoke, fog and light shafts
2
+
3
+ Volumes are the most expensive thing a site can draw. Choose the cheapest technique that
4
+ sells the shot.
5
+
6
+ | Technique | Cost | Looks like | Use for |
7
+ | ---------------------------------------------- | --------------------- | --------------------------------------------- | --------------------------------------------------- |
8
+ | `scene.fog` / `FogExp2` | Free | Distance haze | Depth in every scene. Do this first |
9
+ | Height fog in a patched shader | Nearly free | Fog pooling near the ground | Landscapes, floors |
10
+ | Sprite stack (billboards with a noise texture) | Low–medium (overdraw) | Smoke, steam, mist, distant clouds | Most websites. drei `<Cloud>` is this |
11
+ | Ray-marched volume in a box | High | True volumetric clouds, smoke you fly through | Hero shots only |
12
+ | Baked volume (VDB → 3D texture) | Medium | Sculpted, art-directed smoke and clouds | igloo's approach: simulate offline, ship compressed |
13
+ | Screen-space light shafts | Low | God rays from a bright source | A sun behind objects |
14
+
15
+ ## Live with Depthview
16
+
17
+ - `add_effect kind:"smoke"`: sprite puffs (`smoke`, `steam`, `ground-mist`, `fire`).
18
+ - `add_effect kind:"clouds"`: ray-marched box (`cumulus`, `overcast`, `wisps`, `sunset`).
19
+ - `add_effect kind:"fog"`: the same volume, thin and low (`ground-fog`, `haze`).
20
+
21
+ Tune with `set_effect`; the box is the object's `scale`. Volume uniforms: `uSteps` (cost),
22
+ `uDensity`, `uCoverage`, `uScale` (puff size), `uDetail`, `uEdge` / `uBottom` / `uTop` (how it
23
+ fades inside its box), `uAbsorption`, `uPowder`, `uSilver`, `uLightDir`, `uLightColor`,
24
+ `uShadowColor`, `uWind`. Point `uLightDir` at the scene's key light (`describe_scene` gives
25
+ its position) or the cloud is lit from somewhere else than everything around it.
26
+
27
+ The live version reads the depth of the solid scene (the result says `sceneDepth: true`), so a
28
+ cloud goes round the objects inside it and stops at the floor, and its noise comes from a
29
+ texture (`uNoise`), not arithmetic. The code `get_effect_code` writes does the same when you
30
+ pass it a depth texture. What is left for the source: marching large volumes at half
31
+ resolution (below). When `sceneDepth` is false (WebGL 1, a depth buffer that cannot be
32
+ copied) keep volumes clear of solid objects.
33
+
34
+ ## Fog (do this in every scene)
35
+
36
+ ```js
37
+ scene.fog = new THREE.FogExp2('#0b1220', 0.06); // same colour as the background
38
+ ```
39
+
40
+ Fog only works if its colour **is** the background colour (or the sky at the horizon).
41
+ Density so the far end of the scene is 60–90% fogged. It hides the far clipping plane, gives
42
+ scale, and costs nothing. ShaderMaterials need `fog: true` and the fog chunks
43
+ (`fog_pars_vertex`, `fog_vertex`, `fog_pars_fragment`, `fog_fragment`) to take part.
44
+
45
+ Height fog, patched into materials after `#include <fog_fragment>`:
46
+
47
+ ```glsl
48
+ float h = exp(-max(vWorldPosition.y - uFogBase, 0.0) * uFogFalloff);
49
+ float d = 1.0 - exp(-distance(cameraPosition, vWorldPosition) * uFogDensity);
50
+ gl_FragColor.rgb = mix(gl_FragColor.rgb, uFogColor, h * d);
51
+ ```
52
+
53
+ ## Sprite smoke
54
+
55
+ Twenty to a hundred large camera-facing quads, each with a soft noise-shaped alpha, rising,
56
+ growing, turning slowly and fading. What sells it:
57
+
58
+ - A **texture with real smoke shapes** (a baked 2×2 or 8×8 flipbook from Houdini/EmberGen
59
+ beats procedural noise), each sprite a different frame and rotation.
60
+ - Life cycle: fade in fast (first 8%), grow 2–4×, fade out slowly. Never pop.
61
+ - Fake lighting: darker at the bottom, or a normal-mapped sprite lit by the key light.
62
+ - Low alpha (0.1–0.3) and many overlaps, never a few opaque puffs.
63
+ - Soft particles (depth fade) where it meets the ground: see particles.
64
+ - Overdraw is the cost: big sprites near the camera fill the screen many times. Fewer,
65
+ larger, lower-alpha sprites; or render them into a half-resolution target.
66
+
67
+ ## Ray-marched volumes
68
+
69
+ Draw a box; for each pixel march a ray through it, sampling density and accumulating light.
70
+
71
+ ```glsl
72
+ vec2 hitBox(vec3 ro, vec3 rd) { // unit box, object space
73
+ vec3 inv = 1.0 / rd;
74
+ vec3 t0 = (vec3(-0.5) - ro) * inv, t1 = (vec3(0.5) - ro) * inv;
75
+ vec3 mn = min(t0, t1), mx = max(t0, t1);
76
+ return vec2(max(mn.x, max(mn.y, mn.z)), min(mx.x, min(mx.y, mx.z)));
77
+ }
78
+ // ro = camera in object space (mesh.worldToLocal), rd = normalize(vertexPosition - ro)
79
+ vec2 hit = hitBox(ro, rd);
80
+ if (hit.x > hit.y) discard;
81
+ hit.x = max(hit.x, 0.0);
82
+ float dt = (hit.y - hit.x) / STEPS;
83
+ float t = hit.x + dt * jitter; // jitter: see below
84
+ float T = 1.0; vec3 L = vec3(0.0); // transmittance, light
85
+ for (int i = 0; i < STEPS; i++) {
86
+ if (T < 0.02) break; // early exit: nothing behind is visible
87
+ vec3 p = ro + rd * t;
88
+ float d = density(p);
89
+ if (d > 0.001) {
90
+ float sun = exp(-lightMarch(p) * absorption); // Beer–Lambert towards the light
91
+ float a = 1.0 - exp(-d * dt * absorption);
92
+ L += T * a * (ambient + sunColor * sun);
93
+ T *= 1.0 - a;
94
+ }
95
+ t += dt;
96
+ }
97
+ gl_FragColor = vec4(L / max(1.0 - T, 1e-4), 1.0 - T);
98
+ ```
99
+
100
+ **Density.** Low-frequency noise for the shape, remapped by coverage, minus high-frequency
101
+ noise to erode the edges; multiplied by a height profile (flat bottom, round top for cumulus)
102
+ and a fade towards the box so the box never shows. Animate by offsetting the noise with wind.
103
+
104
+ **Lighting.** For each sample take 3–6 steps towards the light and sum the density:
105
+ `exp(-sum * absorption)` is how much sun arrives. Add:
106
+
107
+ - _Powder_: `1 - exp(-d * 2)` darkens thin edges facing the light: the creases of cumulus.
108
+ - _Phase function_ (Henyey–Greenstein, g ≈ 0.6, blended with a back lobe): the silver lining
109
+ when looking towards the sun.
110
+ - _Ambient_ from the sky colour, stronger at the top.
111
+ Brightness must not change when the step count changes: if it does, the `dt` factor is
112
+ missing somewhere.
113
+
114
+ **The optimisations that make it shippable** (the first three matter most):
115
+
116
+ 1. **Noise from textures, not arithmetic.** A tiling 3D texture (`Data3DTexture`, 64³–128³,
117
+ one channel is 0.26–2 MB) with Perlin–Worley in it. One read replaces ~30 hash
118
+ calls. Generate once at start-up or bake offline. This alone is often 5× faster.
119
+ 2. **Half or quarter resolution.** Clouds have no sharp edges. Render the volume into a target
120
+ at half size (a quarter of the pixels) and composite with a depth-aware (bilateral)
121
+ upsample so it does not bleed over object edges.
122
+ 3. **Jitter the ray start** with blue noise (a 64² or 128² tiling texture; interleaved gradient
123
+ noise is the texture-free fallback). It turns the visible slices of a 24-step march into
124
+ fine grain. Animate the jitter per frame only if a temporal filter accumulates it;
125
+ otherwise keep it static, or it crawls.
126
+ 4. **Bound the march**: only inside the box, stop at scene depth (read the depth texture and
127
+ clamp `hit.y`), exit when transmittance is under 0.02, skip empty space with a cheap
128
+ low-frequency density test before the detailed one.
129
+ 5. **Fewer light steps** with growing step length; a cheaper density for them (two octaves).
130
+ 6. **Temporal reprojection** for big skies: update a quarter of the pixels per frame and
131
+ reproject the rest. Worth it only for a full-screen cloudscape.
132
+ 7. Step counts: 24–40 for a contained cloud with jitter; 64+ only for close fly-throughs.
133
+
134
+ **Depth.** To sit correctly among objects the volume needs the scene's depth: render opaque
135
+ objects with a depth texture, pass it in, convert to a distance along the ray and stop there.
136
+ Draw the box with `depthTest: false` and `side: BackSide` then, and write the composite
137
+ yourself.
138
+
139
+ **Camera inside the box.** Front faces vanish when the camera enters. Switch to `BackSide`
140
+ while inside, with `depthTest: false` (a floor in front of the far faces would otherwise cut
141
+ the volume away), and clamp the start of the ray to 0. Depthview's effect and the code it
142
+ writes do this.
143
+
144
+ ## Baked volumes
145
+
146
+ When the shape must be designed (a plume, a specific cloud, igloo's VDB shapes): simulate in
147
+ Houdini/Blender/EmberGen, export the density grid, ship it as a 3D texture. A 64³ single-
148
+ channel grid is 262 KB raw and compresses well; pack frames or channels for animation. Ray
149
+ march it exactly as above with `density(p) = texture(uVolume, p + 0.5).r`, and add scrolling
150
+ detail noise so it lives.
151
+
152
+ ## Light shafts (god rays)
153
+
154
+ - **Screen-space radial blur** from the light's screen position over an occlusion mask
155
+ (pmndrs `GodRaysEffect`): cheap, works only when the light is on screen.
156
+ - **Cone meshes** with a fresnel-faded additive shader along a spotlight: cheapest, great for
157
+ stage lights and windows. Fade by depth and by angle to the view.
158
+ - **True volumetric** shafts march the shadow map: expensive; rarely worth it on the web.
159
+
160
+ ## Checks before shipping a volume
161
+
162
+ `get_performance` with and without it (`remove_object`, measure, `reset_changes`), at pixel
163
+ ratio 2 and at phone size (`set_viewport`). More than a fifth of the frame: fewer steps, lower
164
+ resolution, smaller box on screen, or fall back to sprites on slow devices.
165
+ `audit_visuals` for banding in the soft gradients: add dither.