roamwright 0.1.0 → 0.2.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.
- package/CONTRIBUTING.md +39 -8
- package/README.md +53 -24
- package/examples/viaduct-camera.js +40 -0
- package/examples/viaduct-landscape.js +81 -0
- package/examples/viaduct.css +75 -0
- package/examples/viaduct.html +47 -0
- package/examples/viaduct.js +332 -0
- package/geometry/builder.js +11 -14
- package/geometry/creased-sheet.js +127 -0
- package/geometry/ear.js +97 -0
- package/geometry/erosion.js +106 -0
- package/geometry/face-grid.js +143 -0
- package/geometry/garment-tags.js +11 -0
- package/geometry/hair-gathers.js +174 -0
- package/geometry/hair-locks.js +156 -0
- package/geometry/hair-patch.js +42 -0
- package/geometry/hair-shell.js +186 -0
- package/geometry/hair-tags.js +6 -0
- package/geometry/head-profile.js +138 -0
- package/geometry/head-shape.js +499 -0
- package/geometry/heightfield.js +68 -0
- package/geometry/outline-grid.js +46 -0
- package/geometry/path-field.js +65 -0
- package/geometry/sagging-tube.js +58 -0
- package/geometry/sampled-surface.js +101 -0
- package/geometry/scatter-grid.js +32 -0
- package/geometry/sculpt.js +263 -0
- package/geometry/shore-grid.js +67 -0
- package/geometry/strata-field.js +339 -0
- package/geometry/strata-tiles.js +215 -0
- package/geometry/stream.js +94 -0
- package/geometry/terrain-chunks.js +300 -0
- package/geometry/terrain-extension.js +44 -0
- package/math/cell-edge.js +44 -0
- package/math/cloth-textures.js +83 -0
- package/math/cloud-textures.js +112 -0
- package/math/color.js +29 -0
- package/math/curve.js +3 -4
- package/math/mountain-ranges.js +463 -0
- package/math/particle-noise.js +48 -0
- package/math/periodic.js +20 -0
- package/math/rock-texture.js +56 -0
- package/math/surface-textures.js +241 -0
- package/motion/arm-reach.js +74 -0
- package/motion/biped-stride.js +276 -0
- package/motion/crowd-alarms.js +127 -0
- package/motion/crowd-routine.js +183 -0
- package/motion/daily-clock.js +59 -0
- package/motion/instance-sight.js +84 -0
- package/motion/quadruped.js +332 -0
- package/motion/weather.js +59 -0
- package/motion/wind.js +27 -0
- package/package.json +6 -4
- package/rendering/caster-motion.js +79 -0
- package/rendering/cell-lod.js +175 -0
- package/rendering/cloud-dome.js +371 -0
- package/rendering/cloud-field.js +83 -0
- package/rendering/compilation-schedule.js +34 -0
- package/rendering/contact-shadows.js +79 -0
- package/rendering/crowd-skinning.js +133 -0
- package/rendering/crust.js +28 -0
- package/rendering/daylight.js +50 -0
- package/rendering/far-shadow.js +186 -0
- package/rendering/flocks.js +323 -0
- package/rendering/garment-material.js +110 -0
- package/rendering/glow-lines.js +74 -0
- package/rendering/glow-shell.js +90 -0
- package/rendering/hair-strands.js +67 -0
- package/rendering/height-haze.js +129 -0
- package/rendering/height-probe.js +91 -0
- package/rendering/interior-mapping.js +19 -0
- package/rendering/light-budget.js +222 -0
- package/rendering/light-points.js +51 -0
- package/rendering/light-shafts.js +131 -0
- package/rendering/local-lights.js +28 -0
- package/rendering/low-light.js +13 -0
- package/rendering/material-lod.js +110 -0
- package/rendering/particle-emitters.js +223 -0
- package/rendering/particles.js +297 -0
- package/rendering/planar-reflection.js +175 -0
- package/rendering/post-shaders.js +21 -1
- package/rendering/post.js +8 -5
- package/rendering/rain-rings.js +22 -0
- package/rendering/road-surface.js +63 -0
- package/rendering/sea-shaders.js +230 -0
- package/rendering/shader-warmup.js +197 -0
- package/rendering/shadow-alignment.js +28 -0
- package/rendering/shadow-depth.js +129 -0
- package/rendering/sky-occlusion.js +187 -0
- package/rendering/sky.js +169 -0
- package/rendering/stage-lights.js +209 -0
- package/rendering/static-point-shadow.js +119 -0
- package/rendering/still-casters.js +6 -11
- package/rendering/strata-material.js +93 -0
- package/rendering/stream-flow.js +36 -0
- package/rendering/surface-light.js +22 -0
- package/rendering/surface-projection.js +69 -0
- package/rendering/talus.js +26 -0
- package/rendering/terrain-rock.js +48 -0
- package/rendering/texture-fills.js +68 -0
- package/rendering/viewport.js +9 -0
- package/rendering/wakes.js +99 -0
- package/rendering/water-material.js +288 -0
- package/rendering/world-matrices.js +4 -6
- package/runtime/device-quality.js +27 -0
- package/runtime/diagnostic-log.js +80 -0
- package/runtime/events.js +14 -0
- package/runtime/first-use.js +3 -1
- package/runtime/frame-yield.js +14 -0
- package/runtime/grid-cache.js +88 -0
- package/runtime/grid-rows.js +9 -0
- package/runtime/module-preload.js +15 -0
- package/runtime/quality-fit.js +5 -9
- package/runtime/search.js +69 -0
- package/runtime/spatial-audio.js +57 -0
- package/scripts/serve.py +26 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Shader functions for diffuse reflectance under dim illumination. Both take linear RGB.
|
|
2
|
+
// Vary adaptation with illumination so a surface close to a bright source keeps its color.
|
|
3
|
+
export const LOW_LIGHT_REFLECTANCE = /* glsl */ `
|
|
4
|
+
vec3 lowLightReflectance(vec3 albedo, float adaptation) {
|
|
5
|
+
float neutral = dot(albedo, vec3(0.2126, 0.7152, 0.0722));
|
|
6
|
+
return mix(albedo, vec3(neutral), clamp(adaptation, 0.0, 1.0));
|
|
7
|
+
}
|
|
8
|
+
vec3 diffuseTextileBounce(vec3 albedo, vec3 irradiance, float fiberScatter) {
|
|
9
|
+
// Neutral scattering at the yarn surface remains visible even when the dye absorbs a narrow-band light.
|
|
10
|
+
float fiber = sqrt(max(dot(albedo, vec3(0.2126, 0.7152, 0.0722)), 0.0));
|
|
11
|
+
return irradiance * mix(albedo, vec3(fiber), clamp(fiberScatter, 0.0, 1.0));
|
|
12
|
+
}
|
|
13
|
+
`;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
const SCATTER = 'fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715))))';
|
|
4
|
+
// A copy of a material that draws only a share of its pixels (share.value, from 0 to 1), scattered evenly: a house built in detail dissolves in over its far version instead of jumping into place, and out again
|
|
5
|
+
// when it is let go. One copy to a material and share (a land's own stone has its own); all share one program.
|
|
6
|
+
const fadings = new WeakMap();
|
|
7
|
+
export function fadingOf(base, share) {
|
|
8
|
+
let of = fadings.get(base);
|
|
9
|
+
if (!of) fadings.set(base, (of = new Map()));
|
|
10
|
+
let m = of.get(share);
|
|
11
|
+
if (m) return m;
|
|
12
|
+
of.set(share, (m = base.clone()));
|
|
13
|
+
m.onBeforeCompile = (sh, renderer) => {
|
|
14
|
+
base.onBeforeCompile(sh, renderer);
|
|
15
|
+
sh.uniforms.uShare = share;
|
|
16
|
+
sh.fragmentShader = sh.fragmentShader
|
|
17
|
+
.replace('#include <common>', '#include <common>\nuniform float uShare;')
|
|
18
|
+
.replace('#include <clipping_planes_fragment>', `#include <clipping_planes_fragment>\nif (${SCATTER} > uShare) discard;`);
|
|
19
|
+
};
|
|
20
|
+
m.customProgramCacheKey = () => base.customProgramCacheKey() + ':dissolving';
|
|
21
|
+
return m;
|
|
22
|
+
}
|
|
23
|
+
// A copy of a material that draws only what lies nearer the eye than `reach.value` metres or, with `beyond`, only what lies
|
|
24
|
+
// further: a thing's near version and its far one hand over pixel by pixel across a band `band` metres deep about that distance,
|
|
25
|
+
// each drawing the pixels of the screen-fixed scatter (SCATTER) the other leaves, a share that follows the pixel's distance. No
|
|
26
|
+
// pixel is drawn by both, none by neither, and no stretch of the thing changes all at once. One copy to a material, reach, side
|
|
27
|
+
// and band; the two sides share one program each.
|
|
28
|
+
const reaching = new WeakMap();
|
|
29
|
+
export function reachOf(base, reach, beyond = false, band = 1) {
|
|
30
|
+
let of = reaching.get(base);
|
|
31
|
+
if (!of) reaching.set(base, (of = new Map()));
|
|
32
|
+
const key = `${beyond}:${band}`;
|
|
33
|
+
let sides = of.get(reach);
|
|
34
|
+
if (!sides) of.set(reach, (sides = {}));
|
|
35
|
+
if (sides[key]) return sides[key];
|
|
36
|
+
const m = (sides[key] = base.clone());
|
|
37
|
+
m.onBeforeCompile = (sh, renderer) => {
|
|
38
|
+
base.onBeforeCompile(sh, renderer);
|
|
39
|
+
sh.uniforms.uReach = reach;
|
|
40
|
+
sh.fragmentShader = sh.fragmentShader
|
|
41
|
+
.replace('#include <common>', '#include <common>\nuniform float uReach;')
|
|
42
|
+
.replace(
|
|
43
|
+
'#include <clipping_planes_fragment>',
|
|
44
|
+
`#include <clipping_planes_fragment>\n${handedOver(beyond, nearShare('length(vViewPosition)', band))}`,
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
m.customProgramCacheKey = () => base.customProgramCacheKey() + (beyond ? ':beyond-reach:' : ':within-reach:') + band;
|
|
48
|
+
return m;
|
|
49
|
+
}
|
|
50
|
+
// The same for the instances of an InstancedMesh, each handed over whole and in its own time, using its geometry's `aNear` (an instanced attribute, 0 to 1) is the near version's share of the instance's
|
|
51
|
+
// pixels, which its builder moves from the one end to the other over a moment once the viewer comes within or goes beyond its
|
|
52
|
+
// reach. An instance its version does not draw at all has its vertices put out of sight. handOverShadow gives the near or far
|
|
53
|
+
// version's shadow body, which casts wherever its version draws anything: while an instance is handed over both cast, and their
|
|
54
|
+
// shadows, of one shape in two levels of detail, lie over each other.
|
|
55
|
+
const handing = new WeakMap();
|
|
56
|
+
export function handOverOf(base, beyond = false) {
|
|
57
|
+
let of = handing.get(base);
|
|
58
|
+
if (!of) handing.set(base, (of = {}));
|
|
59
|
+
if (of[beyond]) return of[beyond];
|
|
60
|
+
const m = (of[beyond] = base.clone());
|
|
61
|
+
m.onBeforeCompile = (sh, renderer) => {
|
|
62
|
+
base.onBeforeCompile(sh, renderer);
|
|
63
|
+
handOverVertices(sh, beyond);
|
|
64
|
+
sh.fragmentShader = sh.fragmentShader
|
|
65
|
+
.replace('#include <common>', '#include <common>\nvarying float vNearShare;')
|
|
66
|
+
.replace('#include <clipping_planes_fragment>', `#include <clipping_planes_fragment>\n${handedOver(beyond, 'vNearShare')}`);
|
|
67
|
+
};
|
|
68
|
+
m.customProgramCacheKey = () => base.customProgramCacheKey() + (beyond ? ':handed-far' : ':handed-near');
|
|
69
|
+
return m;
|
|
70
|
+
}
|
|
71
|
+
export function handOverShadow(beyond = false) {
|
|
72
|
+
const m = new THREE.MeshDepthMaterial({ depthPacking: THREE.RGBADepthPacking });
|
|
73
|
+
m.onBeforeCompile = sh => handOverVertices(sh, beyond);
|
|
74
|
+
m.customProgramCacheKey = () => (beyond ? 'handed-far-depth' : 'handed-near-depth');
|
|
75
|
+
return m;
|
|
76
|
+
}
|
|
77
|
+
// The near version's share, 1 short of the band about uReach and 0 past it, at a distance (a GLSL expression) from the eye
|
|
78
|
+
const nearShare = (distance, band) => `1.0 - smoothstep(uReach - ${(band / 2).toFixed(2)}, uReach + ${(band / 2).toFixed(2)}, ${distance})`;
|
|
79
|
+
// The fragment's test: the near version keeps the scatter's pixels under its share, the far version the rest
|
|
80
|
+
const handedOver = (beyond, share) => `if (${SCATTER} ${beyond ? '<' : '>='} ${share}) discard;`;
|
|
81
|
+
// Hands an instance's share (aNear) to the fragments (vNearShare), and puts the vertices of an instance its version does not draw
|
|
82
|
+
// at all out of sight.
|
|
83
|
+
function handOverVertices(sh, beyond) {
|
|
84
|
+
sh.vertexShader = sh.vertexShader
|
|
85
|
+
.replace('#include <common>', '#include <common>\nattribute float aNear;\nvarying float vNearShare;')
|
|
86
|
+
.replace(
|
|
87
|
+
'#include <project_vertex>',
|
|
88
|
+
`#include <project_vertex>\nvNearShare = aNear;\nif (aNear ${beyond ? '>= 1.0' : '<= 0.0'}) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
// The same material for meshes of another kind (kind: 'plain', 'instanced', or 'tinted' for instances with a colour each), drawn as
|
|
92
|
+
// the base is, its shader patch and cache key read from the base whenever it compiles. The renderer keeps one program state to a
|
|
93
|
+
// material: a material shared by meshes of different kinds has its program's parameters worked out anew each time it passes from
|
|
94
|
+
// one kind to the other, a few times every frame.
|
|
95
|
+
const twins = new WeakMap();
|
|
96
|
+
export function twinMaterial(base, kind) {
|
|
97
|
+
let of = twins.get(base);
|
|
98
|
+
if (!of) twins.set(base, (of = {}));
|
|
99
|
+
if (!of[kind]) {
|
|
100
|
+
const m = (of[kind] = base.clone());
|
|
101
|
+
m.onBeforeCompile = (sh, renderer) => base.onBeforeCompile(sh, renderer);
|
|
102
|
+
m.customProgramCacheKey = () => base.customProgramCacheKey();
|
|
103
|
+
}
|
|
104
|
+
return of[kind];
|
|
105
|
+
}
|
|
106
|
+
export function glowMesh(geo, hex, intensity, parent) {
|
|
107
|
+
const m = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ color: new THREE.Color(hex).multiplyScalar(intensity) }));
|
|
108
|
+
if (parent) parent.add(m);
|
|
109
|
+
return m;
|
|
110
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { TAU, clamp } from '../math/scalar.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Recipes for particles that sites send up, for a particle system such as `createParticleSystem`: ground mist, columns of smoke and
|
|
5
|
+
* fires with their sparks, glow and soot. Each fills the system's `spawn` record and hands it over with `put(pool)`, once per
|
|
6
|
+
* particle; nothing is allocated.
|
|
7
|
+
*
|
|
8
|
+
* `spawn` fields written: x, y, z (metres), vx, vy, vz (metres a second), life (seconds), size (metres), r, g, b (linear, may
|
|
9
|
+
* exceed 1 for light), drag, grav, and e1, e2, e3: the shader's opacity, heat and growth for this particle.
|
|
10
|
+
*
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {object} options.spawn The particle system's spawn record.
|
|
13
|
+
* @param {(pool: object) => void} options.put Hands the spawn record to a pool.
|
|
14
|
+
* @param {() => number} [options.random=Math.random]
|
|
15
|
+
* @param {number} [options.hearth=4] Metres: a column of smoke narrower than this is a hearth's or a campfire's (thin, pale, brief),
|
|
16
|
+
* not a burning building's.
|
|
17
|
+
*/
|
|
18
|
+
export function createParticleEmitters({ spawn, put, random = Math.random, hearth = 4 }) {
|
|
19
|
+
const rnd = random;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* One wisp of mist lying low over a disc of radius `site.r` about (site.x, site.z): broad pale veils, thickest at night (`day`
|
|
23
|
+
* 0 at night, 1 by day), drifting slowly. `groundAt(x, z)` is the ground's height there. site.size: the wisp's size.
|
|
24
|
+
*/
|
|
25
|
+
function mist(site, groundAt, day, pool) {
|
|
26
|
+
const a = rnd() * TAU,
|
|
27
|
+
d = Math.sqrt(rnd()) * site.r,
|
|
28
|
+
x = site.x + Math.cos(a) * d,
|
|
29
|
+
z = site.z + Math.sin(a) * d,
|
|
30
|
+
g = 0.62 + rnd() * 0.12,
|
|
31
|
+
thick = 0.45 + 0.55 * (1 - day);
|
|
32
|
+
spawn.x = x;
|
|
33
|
+
spawn.y = groundAt(x, z) + 0.25 + rnd() * 0.6;
|
|
34
|
+
spawn.z = z;
|
|
35
|
+
spawn.vx = (rnd() - 0.5) * 0.25;
|
|
36
|
+
spawn.vy = 0.02 + rnd() * 0.04;
|
|
37
|
+
spawn.vz = (rnd() - 0.5) * 0.25;
|
|
38
|
+
spawn.life = 12 + rnd() * 8;
|
|
39
|
+
spawn.size = site.size * (0.7 + rnd() * 0.6);
|
|
40
|
+
spawn.r = g;
|
|
41
|
+
spawn.g = g * 1.02;
|
|
42
|
+
spawn.b = g * 1.06;
|
|
43
|
+
spawn.drag = 0.02;
|
|
44
|
+
spawn.grav = 0;
|
|
45
|
+
spawn.e1 = (0.1 + rnd() * 0.08) * thick;
|
|
46
|
+
spawn.e2 = 0;
|
|
47
|
+
spawn.e3 = 0.12;
|
|
48
|
+
put(pool);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* One puff of a column of smoke at (site.x, site.y, site.z), `site.size` metres across: dense dark puffs at the root, and now and
|
|
53
|
+
* then a big slow one that carries it high and downwind. A narrow column (under `hearth`) is paler and briefer, as a cooking
|
|
54
|
+
* fire's is. Optional: site.dark (darkness, 1), site.heat (how it rises, 0.08), site.thin (opacity, 1).
|
|
55
|
+
*/
|
|
56
|
+
function smoke(site, pool) {
|
|
57
|
+
const S = site.size,
|
|
58
|
+
small = 1 - clamp(S / hearth, 0, 1),
|
|
59
|
+
g = (0.1 + rnd() * 0.17) * (site.dark ?? 1) * (1 + 1.8 * small),
|
|
60
|
+
brief = 1 - 0.6 * small,
|
|
61
|
+
j = S * 0.16,
|
|
62
|
+
heat = site.heat ?? 0.08,
|
|
63
|
+
up = Math.sqrt(S / 8),
|
|
64
|
+
thin = site.thin ?? 1;
|
|
65
|
+
if (rnd() < 0.24) {
|
|
66
|
+
spawn.x = site.x + (rnd() - 0.5) * j;
|
|
67
|
+
spawn.y = site.y + S * 0.2;
|
|
68
|
+
spawn.z = site.z + (rnd() - 0.5) * j;
|
|
69
|
+
spawn.vx = (rnd() - 0.5) * 0.6;
|
|
70
|
+
spawn.vy = (2.3 + rnd() * 1.5) * up;
|
|
71
|
+
spawn.vz = (rnd() - 0.5) * 0.6;
|
|
72
|
+
spawn.life = (14 + rnd() * 9) * brief;
|
|
73
|
+
spawn.size = S * (0.4 + rnd() * 0.3);
|
|
74
|
+
spawn.r = g * 1.3;
|
|
75
|
+
spawn.g = g * 1.3;
|
|
76
|
+
spawn.b = g * 1.36;
|
|
77
|
+
spawn.drag = 0.1;
|
|
78
|
+
spawn.grav = 0.1;
|
|
79
|
+
spawn.e1 = (0.34 + rnd() * 0.2) * thin * brief;
|
|
80
|
+
spawn.e2 = heat * 0.5;
|
|
81
|
+
spawn.e3 = 1.1 + rnd() * 0.4;
|
|
82
|
+
} else {
|
|
83
|
+
spawn.x = site.x + (rnd() - 0.5) * j;
|
|
84
|
+
spawn.y = site.y;
|
|
85
|
+
spawn.z = site.z + (rnd() - 0.5) * j;
|
|
86
|
+
spawn.vx = (rnd() - 0.5) * 0.9;
|
|
87
|
+
spawn.vy = (2.0 + rnd() * 1.7) * up;
|
|
88
|
+
spawn.vz = (rnd() - 0.5) * 0.9;
|
|
89
|
+
spawn.life = 5.5 + rnd() * 4.5;
|
|
90
|
+
spawn.size = S * (0.22 + rnd() * 0.2);
|
|
91
|
+
spawn.r = g;
|
|
92
|
+
spawn.g = g;
|
|
93
|
+
spawn.b = g * 1.05;
|
|
94
|
+
spawn.drag = 0.12;
|
|
95
|
+
spawn.grav = 0.12;
|
|
96
|
+
spawn.e1 = (0.5 + rnd() * 0.3) * thin;
|
|
97
|
+
spawn.e2 = heat;
|
|
98
|
+
spawn.e3 = 0.9 + rnd() * 0.6;
|
|
99
|
+
}
|
|
100
|
+
put(pool);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One tongue of a fire at (site.x, site.y, site.z), `site.size` metres across, into the additive pool `add`; with it, by chance,
|
|
105
|
+
* a spark (site.sparks, 0.32), a glow in the air above (site.halo, 0.1) and soot into the blended pool `alpha` (site.soot, 0.14):
|
|
106
|
+
* thin pale wisps for a wood fire burning well (site.wood), black soot otherwise. site.hot scales its light (1).
|
|
107
|
+
*/
|
|
108
|
+
function fire(site, add, alpha) {
|
|
109
|
+
const z = site.size,
|
|
110
|
+
sq = Math.sqrt(z),
|
|
111
|
+
hot = site.hot ?? 1;
|
|
112
|
+
spawn.x = site.x + (rnd() - 0.5) * z * 0.9;
|
|
113
|
+
spawn.y = site.y + rnd() * 0.12 * z;
|
|
114
|
+
spawn.z = site.z + (rnd() - 0.5) * z * 0.9;
|
|
115
|
+
spawn.vx = (rnd() - 0.5) * 0.35;
|
|
116
|
+
spawn.vy = (0.5 + rnd() * 0.9) * sq;
|
|
117
|
+
spawn.vz = (rnd() - 0.5) * 0.35;
|
|
118
|
+
spawn.life = 0.45 + rnd() * 0.5;
|
|
119
|
+
spawn.size = z * (0.6 + rnd() * 0.6);
|
|
120
|
+
spawn.r = 1.3 * hot;
|
|
121
|
+
spawn.g = (0.42 + rnd() * 0.2) * hot;
|
|
122
|
+
spawn.b = 0.065 * hot;
|
|
123
|
+
spawn.drag = 0.6;
|
|
124
|
+
spawn.grav = 0.8;
|
|
125
|
+
spawn.e1 = 1;
|
|
126
|
+
spawn.e2 = 0;
|
|
127
|
+
spawn.e3 = 0.12;
|
|
128
|
+
put(add);
|
|
129
|
+
if (rnd() < (site.sparks ?? 0.32)) {
|
|
130
|
+
spawn.x = site.x + (rnd() - 0.5) * z * 0.6;
|
|
131
|
+
spawn.y = site.y + 0.5 * z;
|
|
132
|
+
spawn.z = site.z + (rnd() - 0.5) * z * 0.6;
|
|
133
|
+
spawn.vx = (rnd() - 0.5) * 1.6 * sq;
|
|
134
|
+
spawn.vy = (1.8 + rnd() * 2.6) * sq;
|
|
135
|
+
spawn.vz = (rnd() - 0.5) * 1.6 * sq;
|
|
136
|
+
spawn.life = 1.4 + rnd() * 2.6;
|
|
137
|
+
spawn.size = (0.035 + rnd() * 0.04) * sq;
|
|
138
|
+
spawn.r = 4.6;
|
|
139
|
+
spawn.g = 1.7;
|
|
140
|
+
spawn.b = 0.36;
|
|
141
|
+
spawn.drag = 0.35;
|
|
142
|
+
spawn.grav = 0.3;
|
|
143
|
+
spawn.e1 = 2;
|
|
144
|
+
spawn.e2 = 0;
|
|
145
|
+
spawn.e3 = 0.7;
|
|
146
|
+
put(add);
|
|
147
|
+
}
|
|
148
|
+
if (rnd() < (site.halo ?? 0.1)) {
|
|
149
|
+
spawn.x = site.x;
|
|
150
|
+
spawn.y = site.y + z * 1.7;
|
|
151
|
+
spawn.z = site.z;
|
|
152
|
+
spawn.vx = 0;
|
|
153
|
+
spawn.vy = 0.4;
|
|
154
|
+
spawn.vz = 0;
|
|
155
|
+
spawn.life = 0.5 + rnd() * 0.5;
|
|
156
|
+
spawn.size = z * 3.2;
|
|
157
|
+
spawn.r = 0.3 * hot;
|
|
158
|
+
spawn.g = 0.09 * hot;
|
|
159
|
+
spawn.b = 0.018 * hot;
|
|
160
|
+
spawn.drag = 0.5;
|
|
161
|
+
spawn.grav = 0;
|
|
162
|
+
put(add);
|
|
163
|
+
}
|
|
164
|
+
if (rnd() < (site.soot ?? 0.14)) {
|
|
165
|
+
if (site.wood) {
|
|
166
|
+
const g = 0.36 + rnd() * 0.16;
|
|
167
|
+
spawn.x = site.x + (rnd() - 0.5) * z * 0.4;
|
|
168
|
+
spawn.y = site.y + z * 1.3;
|
|
169
|
+
spawn.z = site.z + (rnd() - 0.5) * z * 0.4;
|
|
170
|
+
spawn.vx = (rnd() - 0.5) * 0.3;
|
|
171
|
+
spawn.vy = (1.3 + rnd() * 0.9) * sq;
|
|
172
|
+
spawn.vz = (rnd() - 0.5) * 0.3;
|
|
173
|
+
spawn.life = (6 + rnd() * 5) * Math.sqrt(sq);
|
|
174
|
+
spawn.size = z * (0.9 + rnd() * 0.8);
|
|
175
|
+
spawn.r = g;
|
|
176
|
+
spawn.g = g * 1.02;
|
|
177
|
+
spawn.b = g * 1.08;
|
|
178
|
+
spawn.drag = 0.15;
|
|
179
|
+
spawn.grav = 0.15;
|
|
180
|
+
spawn.e1 = 0.13 + rnd() * 0.11;
|
|
181
|
+
spawn.e2 = 0.35;
|
|
182
|
+
spawn.e3 = 1.3;
|
|
183
|
+
} else {
|
|
184
|
+
const g = 0.04 + rnd() * 0.04;
|
|
185
|
+
spawn.x = site.x + (rnd() - 0.5) * z * 0.5;
|
|
186
|
+
spawn.y = site.y + z * 1.5;
|
|
187
|
+
spawn.z = site.z + (rnd() - 0.5) * z * 0.5;
|
|
188
|
+
spawn.vx = (rnd() - 0.5) * 0.4;
|
|
189
|
+
spawn.vy = (1.6 + rnd() * 1.2) * sq;
|
|
190
|
+
spawn.vz = (rnd() - 0.5) * 0.4;
|
|
191
|
+
spawn.life = (4 + rnd() * 3.5) * Math.sqrt(sq);
|
|
192
|
+
spawn.size = z * (0.8 + rnd() * 0.7);
|
|
193
|
+
spawn.r = g;
|
|
194
|
+
spawn.g = g * 0.95;
|
|
195
|
+
spawn.b = g * 0.9;
|
|
196
|
+
spawn.drag = 0.2;
|
|
197
|
+
spawn.grav = 0.2;
|
|
198
|
+
spawn.e1 = 0.42 + rnd() * 0.28;
|
|
199
|
+
spawn.e2 = 0.6;
|
|
200
|
+
spawn.e3 = 1.2;
|
|
201
|
+
}
|
|
202
|
+
put(alpha);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { mist, smoke, fire };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* A camera's shake as a tremor that rolls through the ground: a few slow, smooth waves (two to three a second) that die away, never
|
|
211
|
+
* a new random offset every frame (which makes the whole picture jitter). Writes the offset into `out` ({ set(x, y, z) }, such as
|
|
212
|
+
* a Vector3) for a shake of `strength` with `left` of its `duration` seconds remaining; `reach` is the metres moved per unit of
|
|
213
|
+
* strength.
|
|
214
|
+
*/
|
|
215
|
+
export function shakeOffset(out, strength, left, duration, reach = 0.18) {
|
|
216
|
+
const a = strength * reach * Math.max(0, left / duration) ** 1.5,
|
|
217
|
+
u = left;
|
|
218
|
+
return out.set(
|
|
219
|
+
a * (Math.sin(u * 13.1) * 0.6 + Math.sin(u * 19.7 + 1.3) * 0.4),
|
|
220
|
+
a * (Math.sin(u * 11.3 + 2.1) * 0.6 + Math.sin(u * 21.9) * 0.4) * 0.7,
|
|
221
|
+
a * (Math.sin(u * 15.5 + 0.7) * 0.6 + Math.sin(u * 9.7 + 2.9) * 0.4),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
export function createParticleSystem({ scene, pointScale, sceneDepth, elapsed, noiseTexture, random = Math.random, layer = 0, alphaOrder = 0, additiveOrder = 1 }) {
|
|
4
|
+
const fxU = {
|
|
5
|
+
uTime: { value: 0 },
|
|
6
|
+
uScale: pointScale,
|
|
7
|
+
uFog: { value: new THREE.Color() },
|
|
8
|
+
uFogD: { value: 0.001 },
|
|
9
|
+
uLight: { value: 1 },
|
|
10
|
+
uNoise: { value: null },
|
|
11
|
+
uSunW: { value: new THREE.Vector3(0, 1, 0) },
|
|
12
|
+
uSunC: { value: new THREE.Color() },
|
|
13
|
+
uSkyC: { value: new THREE.Color() },
|
|
14
|
+
uGndC: { value: new THREE.Color() },
|
|
15
|
+
uWind: { value: new THREE.Vector2(1.6, 0.6) },
|
|
16
|
+
};
|
|
17
|
+
// How far a particle stands in front of what the scene drew under it, as a share of its soft length (vSoft: its own distance, that
|
|
18
|
+
// length): it fades where it meets the ground or a wall instead of cutting a line through it. The distance is the occlusion pass's,
|
|
19
|
+
// at half size: of the four half-size pixels round this one the farthest is taken, so a puff behind a near roof is not thinned
|
|
20
|
+
// along the roof's edge (the depth test still hides it there).
|
|
21
|
+
const MEET = /* glsl */ `uniform sampler2D uDepth; uniform vec2 uDepthPx; uniform float uSoft; varying vec2 vSoft;
|
|
22
|
+
float meet(){
|
|
23
|
+
if (uSoft < 0.5 || vSoft.y <= 0.0) return 1.0;
|
|
24
|
+
ivec2 b = ivec2(floor(gl_FragCoord.xy * 0.5 - 0.5)), hi = ivec2(1.0 / uDepthPx) - 1;
|
|
25
|
+
float d = max(max(texelFetch(uDepth, clamp(b, ivec2(0), hi), 0).g, texelFetch(uDepth, clamp(b + ivec2(1, 0), ivec2(0), hi), 0).g),
|
|
26
|
+
max(texelFetch(uDepth, clamp(b + ivec2(0, 1), ivec2(0), hi), 0).g, texelFetch(uDepth, clamp(b + 1, ivec2(0), hi), 0).g));
|
|
27
|
+
return clamp((d - vSoft.x) / vSoft.y, 0.0, 1.0);
|
|
28
|
+
}`;
|
|
29
|
+
// Embers and coals: bright and dark crawling slowly over a surface. The mesh's colour attribute carries (phase, intensity, -).
|
|
30
|
+
// over: laid as a glow over a surface of its own (a heap of rubble) rather than as the surface itself.
|
|
31
|
+
function emberMaterial(scale, over = false) {
|
|
32
|
+
return new THREE.ShaderMaterial({
|
|
33
|
+
// over the ash the glow is added to the picture, so in haze it fades rather than taking the haze's colour
|
|
34
|
+
...(over && { transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, defines: { EMBER_OVER: 1, FOG_FADES: '' } }),
|
|
35
|
+
fog: true,
|
|
36
|
+
uniforms: {
|
|
37
|
+
...THREE.UniformsUtils.clone(THREE.UniformsLib.fog),
|
|
38
|
+
uTime: fxU.uTime,
|
|
39
|
+
uNoise: fxU.uNoise,
|
|
40
|
+
uLight: fxU.uLight,
|
|
41
|
+
uS: { value: scale },
|
|
42
|
+
},
|
|
43
|
+
vertexShader: `/* glsl */ uniform float uTime, uS; attribute vec3 color; varying vec2 vW; varying vec3 vE;
|
|
44
|
+
#include <fog_pars_vertex>
|
|
45
|
+
void main(){ vec4 w = modelMatrix * vec4(position, 1.0); vW = (w.xz + w.y * 0.71) * uS; vE = vec3(color.x, color.y * (0.78 + 0.22 * sin(uTime * (2.0 + color.x * 3.0) + color.x * 40.0)), fract(uTime * 0.02));
|
|
46
|
+
vec4 mvPosition = viewMatrix * w; gl_Position = projectionMatrix * mvPosition;
|
|
47
|
+
#include <fog_vertex>
|
|
48
|
+
}`,
|
|
49
|
+
fragmentShader: `/* glsl */ uniform sampler2D uNoise; uniform float uLight; varying vec2 vW; varying vec3 vE;
|
|
50
|
+
#include <fog_pars_fragment>
|
|
51
|
+
void main(){ float n = texture2D(uNoise, vW + vE.x * 3.0).r, m = texture2D(uNoise, vW * 2.7 + vec2(vE.z, vE.x)).a;
|
|
52
|
+
#ifdef EMBER_OVER
|
|
53
|
+
float f = smoothstep(0.7, 0.98, n * 0.65 + m * 0.6); // only the hottest specks show over the ash
|
|
54
|
+
gl_FragColor = vec4((vec3(2.4, 0.5, 0.07) * 1.5 * f * f + vec3(0.9, 0.5, 0.1) * pow(f, 6.0)) * vE.y * (1.22 - uLight), 1.0); // by day the glow is lost in the light
|
|
55
|
+
#else
|
|
56
|
+
float f = smoothstep(0.2, 0.85, n * 0.65 + m * 0.6);
|
|
57
|
+
gl_FragColor = vec4(vec3(2.4, 0.5, 0.07) * vE.y * (0.06 + 1.5 * f * f) + vec3(0.9, 0.5, 0.1) * vE.y * pow(f, 6.0), 1.0);
|
|
58
|
+
#endif
|
|
59
|
+
#include <tonemapping_fragment>
|
|
60
|
+
#include <colorspace_fragment>
|
|
61
|
+
#include <fog_fragment>
|
|
62
|
+
}`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
// Each particle is one camera-facing quad (instanced), moved entirely in the vertex shader from its spawn data.
|
|
66
|
+
// The alpha pool draws smoke, dust and shadow as lit, billowing puffs; the additive pool draws glows (kind 0),
|
|
67
|
+
// flame tongues that stand upright and lick upward (kind 1) and embers (kind 2).
|
|
68
|
+
function makeParticles(cap, additive) {
|
|
69
|
+
if (!Number.isInteger(cap) || cap < 1) throw new RangeError('Particle capacity must be a positive integer');
|
|
70
|
+
if (!fxU.uNoise.value) fxU.uNoise.value = noiseTexture();
|
|
71
|
+
const g = new THREE.InstancedBufferGeometry(),
|
|
72
|
+
q = new THREE.PlaneGeometry(1, 1);
|
|
73
|
+
g.setIndex(q.index);
|
|
74
|
+
g.setAttribute('position', q.attributes.position);
|
|
75
|
+
const mk = n => {
|
|
76
|
+
const a = new THREE.InstancedBufferAttribute(new Float32Array(cap * n), n);
|
|
77
|
+
a.setUsage(THREE.DynamicDrawUsage);
|
|
78
|
+
return a;
|
|
79
|
+
};
|
|
80
|
+
const pos = mk(3),
|
|
81
|
+
vel = mk(3),
|
|
82
|
+
dat = mk(4),
|
|
83
|
+
col = mk(4),
|
|
84
|
+
sed = mk(4);
|
|
85
|
+
for (let i = 0; i < cap; i++) {
|
|
86
|
+
dat.array[i * 4] = -1e5;
|
|
87
|
+
dat.array[i * 4 + 1] = 1;
|
|
88
|
+
}
|
|
89
|
+
g.setAttribute('aPos', pos);
|
|
90
|
+
g.setAttribute('aVel', vel);
|
|
91
|
+
g.setAttribute('aData', dat);
|
|
92
|
+
g.setAttribute('aCol', col);
|
|
93
|
+
g.setAttribute('aSeed', sed);
|
|
94
|
+
g.instanceCount = cap;
|
|
95
|
+
g.boundingSphere = new THREE.Sphere(new THREE.Vector3(), 1e7);
|
|
96
|
+
const mat = new THREE.ShaderMaterial({
|
|
97
|
+
transparent: true,
|
|
98
|
+
depthWrite: false,
|
|
99
|
+
blending: additive ? THREE.AdditiveBlending : THREE.NormalBlending,
|
|
100
|
+
fog: false,
|
|
101
|
+
side: THREE.DoubleSide,
|
|
102
|
+
defines: additive ? { FX_ADD: 1 } : {},
|
|
103
|
+
uniforms: {
|
|
104
|
+
uTime: fxU.uTime,
|
|
105
|
+
uFog: fxU.uFog,
|
|
106
|
+
uFogD: fxU.uFogD,
|
|
107
|
+
uNoise: fxU.uNoise,
|
|
108
|
+
uSunW: fxU.uSunW,
|
|
109
|
+
uSunC: fxU.uSunC,
|
|
110
|
+
uSkyC: fxU.uSkyC,
|
|
111
|
+
uGndC: fxU.uGndC,
|
|
112
|
+
uWind: fxU.uWind,
|
|
113
|
+
uGrow: { value: additive ? 1.0 : 4.0 },
|
|
114
|
+
...sceneDepth,
|
|
115
|
+
},
|
|
116
|
+
vertexShader: `/* glsl */ uniform float uTime, uGrow, uFogD; uniform vec2 uWind; uniform vec3 uSunW;
|
|
117
|
+
attribute vec3 aPos, aVel; attribute vec4 aData, aCol, aSeed;
|
|
118
|
+
varying vec4 vCol, vP, vRot; varying float vFogF; varying vec2 vUv, vSoft; varying vec3 vLd, vUd;
|
|
119
|
+
void main(){
|
|
120
|
+
float age = uTime - aData.x, k = age / aData.y;
|
|
121
|
+
vUv = position.xy * 2.0; vLd = vUd = vec3(0.0, 1.0, 0.0); vSoft = vec2(0.0);
|
|
122
|
+
if (k < 0.0 || k >= 1.0) { gl_Position = vec4(0.0, 0.0, 2.0, 1.0); vCol = vP = vRot = vec4(0.0); vFogF = 0.0; return; }
|
|
123
|
+
float drag = aCol.w, tt = drag > 0.01 ? (1.0 - exp(-drag * age)) / drag : age, seed = aSeed.x;
|
|
124
|
+
float still = aSeed.z < -0.5 ? 0.0 : 1.0; // 0: a drop (blood), which neither swells nor drifts
|
|
125
|
+
vec3 p = aPos + aVel * tt; p.y += aData.w * age * age * 0.5;
|
|
126
|
+
p.xz += uWind * (aSeed.w * age * age / (age + 2.5)); // the wind takes hold as the puff climbs: columns lean and trail
|
|
127
|
+
p.xz += sin(vec2(age * 1.9 + aPos.z, age * 1.6 + aPos.x) + seed * 6.283) * min(age, 5.0) * (uGrow > 1.5 ? 0.24 : 0.1) * still;
|
|
128
|
+
vec4 mv = viewMatrix * vec4(p, 1.0);
|
|
129
|
+
float dist = max(-mv.z, 0.1), gk = k * (1.7 - 0.7 * k) * still;
|
|
130
|
+
float sz = min(aData.z * mix(1.0, uGrow, gk), dist * 1.3);
|
|
131
|
+
vSoft.x = dist;
|
|
132
|
+
vec2 c = position.xy;
|
|
133
|
+
vUd = (viewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz; vLd = (viewMatrix * vec4(uSunW, 0.0)).xyz;
|
|
134
|
+
float fade = (1.0 - smoothstep(0.45, 1.0, k));
|
|
135
|
+
#ifdef FX_ADD
|
|
136
|
+
float kind = aSeed.y; vRot = vec4(1.0, 0.0, age, 0.0);
|
|
137
|
+
if (kind > 0.5 && kind < 1.5) { // a flame: upright whatever the camera does, narrowing and stretching as it rises
|
|
138
|
+
vec2 up2 = vUd.xy; float ul = length(up2); up2 = ul > 0.05 ? up2 / ul : vec2(0.0, 1.0); vec2 rt2 = vec2(up2.y, -up2.x);
|
|
139
|
+
float w = sz * (1.0 - 0.3 * k), h = sz * 1.5 * (0.45 + 0.55 * smoothstep(0.0, 0.4, k)), sway = sin(age * (7.0 + seed * 5.0) + seed * 40.0) * 0.22 * w;
|
|
140
|
+
mv.xy += rt2 * (c.x * w + (c.y + 0.5) * sway) + up2 * ((c.y + 0.4) * h);
|
|
141
|
+
fade *= smoothstep(0.0, 0.06, k); vSoft.y = 0.15 * w; // a tongue's root meets the logs softly
|
|
142
|
+
} else {
|
|
143
|
+
if (kind > 1.5) sz = max(sz, dist * 0.0022); // an ember never shrinks below a pixel or two
|
|
144
|
+
else vSoft.y = 0.5 * sz;
|
|
145
|
+
mv.xy += c * sz; fade *= smoothstep(0.0, 0.1, k);
|
|
146
|
+
}
|
|
147
|
+
fade *= kind > 0.5 ? smoothstep(0.25, 1.2, dist) : smoothstep(0.7, 4.0, dist); // a flame stays lit when you sit beside it; the big soft glows fade before they fill the eye
|
|
148
|
+
vP = vec4(seed, kind, k, 0.0);
|
|
149
|
+
#else
|
|
150
|
+
float ang = seed * 6.283 + (seed - 0.5) * age * 0.22; vRot = vec4(cos(ang), sin(ang), age, step(aSeed.y, 0.0)); // w: dust (a negative opacity)
|
|
151
|
+
if (still < 0.5) { // a drop: never below a pixel or so, streaked along its flight
|
|
152
|
+
sz = max(sz, dist * 0.0016);
|
|
153
|
+
vec2 v = (viewMatrix * vec4(aVel * exp(-drag * age) + vec3(0.0, aData.w * age, 0.0), 0.0)).xy;
|
|
154
|
+
float vl = length(v); vec2 d = vl > 1e-3 ? v / vl : vec2(0.0, 1.0);
|
|
155
|
+
mv.xy += d * (c.y * (sz + vl * 0.02)) + vec2(-d.y, d.x) * (c.x * sz);
|
|
156
|
+
fade *= smoothstep(0.15, 0.5, dist) * aSeed.y;
|
|
157
|
+
} else {
|
|
158
|
+
mv.xy += c * sz; vSoft.y = 0.5 * sz; // a puff fades over its radius into what it meets
|
|
159
|
+
fade *= smoothstep(0.0, 0.1, k) * smoothstep(0.7, max(4.0, sz * 0.7), dist) * abs(aSeed.y);
|
|
160
|
+
}
|
|
161
|
+
vP = vec4(seed, 1.0 - still, k, still * aSeed.z * exp(-max(p.y - aPos.y, 0.0) / (aData.z * 1.6 + 0.5))); // the glow of the fire below dies away as the smoke climbs, sooner over a small fire
|
|
162
|
+
#endif
|
|
163
|
+
gl_Position = projectionMatrix * mv;
|
|
164
|
+
vCol = vec4(aCol.rgb, fade);
|
|
165
|
+
float fd = uFogD * dist; vFogF = 1.0 - exp(-fd * fd * 0.6);
|
|
166
|
+
}`,
|
|
167
|
+
fragmentShader:
|
|
168
|
+
(additive
|
|
169
|
+
? `/* glsl */ uniform sampler2D uNoise; varying vec4 vCol, vP, vRot; varying float vFogF; varying vec2 vUv; varying vec3 vLd, vUd; ${MEET}
|
|
170
|
+
void main(){
|
|
171
|
+
float kind = vP.y, seed = vP.x; vec3 c;
|
|
172
|
+
if (kind < 0.5) { float a = max(1.0 - length(vUv), 0.0); a = a * a * (0.5 + 2.2 * a * a); c = vCol.rgb * a; }
|
|
173
|
+
else if (kind < 1.5) {
|
|
174
|
+
float y = vUv.y * 0.5 + 0.5; // 0 at the root of the tongue, 1 at its tip
|
|
175
|
+
vec4 n = texture2D(uNoise, vec2(vUv.x * 0.13 + seed * 5.3, y * 0.2 - vRot.z * 0.42 + seed * 3.1));
|
|
176
|
+
float x = vUv.x + (n.g - 0.5) * 1.1 * y, wd = (0.3 + 0.48 * smoothstep(0.0, 0.25, y)) * pow(1.0 - y, 1.15) + 0.04;
|
|
177
|
+
float f = clamp(1.0 - abs(x) / wd, 0.0, 1.0) * smoothstep(0.0, 0.1, y);
|
|
178
|
+
f = clamp(f * 1.7 - (0.1 + 0.95 * y) * (1.0 - n.r) * 1.25 - (1.0 - n.a) * 0.35 * y, 0.0, 1.0); // torn open toward the tip, solid at the root
|
|
179
|
+
c = vCol.rgb * f * (0.35 + 1.25 * f) * mix(vec3(1.0), vec3(1.0, 0.46, 0.2), y * y) + vec3(1.0, 0.78, 0.36) * vCol.r * 0.42 * f * f * f * (1.0 - y); // yellow at the heart, reddening to the tip
|
|
180
|
+
} else { float a = smoothstep(1.0, 0.0, length(vUv)); c = vCol.rgb * a * a * (0.6 + 0.4 * sin(vRot.z * (9.0 + seed * 14.0) + seed * 40.0)); }
|
|
181
|
+
gl_FragColor = vec4(c * vCol.a * (1.0 - vFogF) * meet(), 1.0);`
|
|
182
|
+
: `/* glsl */ uniform sampler2D uNoise; uniform vec3 uFog, uSunC, uSkyC, uGndC; varying vec4 vCol, vP, vRot; varying float vFogF; varying vec2 vUv; varying vec3 vLd, vUd; ${MEET}
|
|
183
|
+
void main(){
|
|
184
|
+
float r2 = dot(vUv, vUv); if (r2 >= 1.0) discard;
|
|
185
|
+
if (vP.y > 0.5) { // a drop: a dark bead, lit from outside, with a wet glint
|
|
186
|
+
vec3 N = vec3(vUv, sqrt(1.0 - r2)), L = normalize(vLd);
|
|
187
|
+
float sunw = clamp(dot(N, L) * 0.5 + 0.5, 0.0, 1.0), spec = pow(max(dot(N, normalize(L + vec3(0.0, 0.0, 1.0))), 0.0), 40.0);
|
|
188
|
+
vec3 col = vCol.rgb * 0.5 * (uSunC * sunw + uSkyC * (0.6 + 0.4 * dot(N, vUd))) + uSunC * spec * 0.12;
|
|
189
|
+
gl_FragColor = vec4(mix(col, uFog, vFogF), vCol.a * smoothstep(1.0, 0.6, r2));
|
|
190
|
+
} else {
|
|
191
|
+
float r = sqrt(r2), seed = vP.x, k = vP.z;
|
|
192
|
+
vec2 q = vec2(vUv.x * vRot.x - vUv.y * vRot.y, vUv.x * vRot.y + vUv.y * vRot.x);
|
|
193
|
+
vec2 nuv = q * 0.2 + vec2(seed * 7.13, fract(seed * 13.7) * 5.1);
|
|
194
|
+
vec4 n = texture2D(uNoise, nuv); float w = texture2D(uNoise, nuv * 2.7 + 0.31).a;
|
|
195
|
+
float body = 1.0 - r, e = 0.06 + 0.4 * k; // an old puff frays into wisps
|
|
196
|
+
float den = smoothstep(e, e + 0.5, body + (n.r - 0.5) * 0.8 + (w - 0.5) * 0.35) * smoothstep(0.0, 0.16, body);
|
|
197
|
+
vec2 gr = (n.gb - 0.5) * 2.0; gr = vec2(gr.x * vRot.x + gr.y * vRot.y, gr.y * vRot.x - gr.x * vRot.y);
|
|
198
|
+
vec3 N = normalize(vec3(vUv * 0.9 - gr * 0.8, sqrt(max(1.0 - r2 * 0.81, 0.04)))); // a rounded puff with the noise as relief
|
|
199
|
+
float up = dot(N, vUd), sunw = clamp(dot(N, vLd) * 0.62 + 0.38, 0.0, 1.0), cav = 0.5 + 0.5 * smoothstep(0.1, 0.55, n.r);
|
|
200
|
+
vec3 alb = vCol.rgb, col;
|
|
201
|
+
if (vRot.w > 0.5) // dust: lit as the ground it rose from is (the sun on level ground, the whole sky), more
|
|
202
|
+
col = alb * 0.3183 * (uSunC * max(dot(vLd, vUd), 0.0) * (1.0 + 0.7 * clamp(dot(N, vLd), 0.0, 1.0) * cav) + uSkyC); // where it faces the sun, never less
|
|
203
|
+
else {
|
|
204
|
+
alb *= mix(0.5, 1.0, smoothstep(0.0, 0.4, k)); // smoke is sooty where it leaves the fire
|
|
205
|
+
col = alb * 0.5 * (uSunC * sunw * sunw * cav + uSkyC * (0.5 + 0.5 * up) * (0.6 + 0.4 * cav) + uGndC * (0.5 - 0.5 * up));
|
|
206
|
+
}
|
|
207
|
+
col += alb * uSunC * pow(max(-vLd.z, 0.0), 3.0) * (1.0 - den) * 0.22; // thin edges let the sun through
|
|
208
|
+
col += vec3(1.0, 0.3, 0.055) * vP.w * (0.2 + 0.8 * clamp(0.5 - 0.75 * up, 0.0, 1.0)) * (0.45 + 0.9 * w); // lit from underneath by the fire
|
|
209
|
+
gl_FragColor = vec4(mix(col, uFog, vFogF), den * vCol.a * meet());
|
|
210
|
+
}`) +
|
|
211
|
+
`/* glsl */
|
|
212
|
+
#include <tonemapping_fragment>
|
|
213
|
+
#include <colorspace_fragment>
|
|
214
|
+
}`,
|
|
215
|
+
});
|
|
216
|
+
const pts = new THREE.Mesh(g, mat);
|
|
217
|
+
pts.frustumCulled = false;
|
|
218
|
+
pts.renderOrder = additive ? additiveOrder : alphaOrder;
|
|
219
|
+
pts.layers.set(layer);
|
|
220
|
+
scene.add(pts);
|
|
221
|
+
return {
|
|
222
|
+
cap,
|
|
223
|
+
add: additive,
|
|
224
|
+
head: 0,
|
|
225
|
+
pos: pos.array,
|
|
226
|
+
vel: vel.array,
|
|
227
|
+
dat: dat.array,
|
|
228
|
+
col: col.array,
|
|
229
|
+
sed: sed.array,
|
|
230
|
+
attrs: [pos, vel, dat, col, sed],
|
|
231
|
+
lo: 1e9,
|
|
232
|
+
hi: -1,
|
|
233
|
+
dirty: false,
|
|
234
|
+
wrapped: false,
|
|
235
|
+
until: 0, // when the last particle sent out dies: until then the pool is drawn
|
|
236
|
+
pts,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
// The particle pSpawn sends out next, set field by field by its caller: pSpawn is too long for the engine to take into its callers,
|
|
240
|
+
// and the numbers it was passed as arguments were made anew at every call, sixteen a particle. Place, velocity, life (seconds),
|
|
241
|
+
// size, colour, drag and gravity are set for every particle; e1, e2 and e3 only where they differ from what pSpawn puts back:
|
|
242
|
+
// additive pool: kind (0 glow, 1 flame, 2 ember), unused, how hard the wind pushes it;
|
|
243
|
+
// alpha pool: opacity (negative for dust, which is lit as the ground it rose from), heat (orange light from a fire beneath), how
|
|
244
|
+
// hard the wind pushes it.
|
|
245
|
+
const spawn = { x: 0, y: 0, z: 0, vx: 0, vy: 0, vz: 0, life: 0, size: 0, r: 0, g: 0, b: 0, drag: 0, grav: 0, e1: NaN, e2: 0, e3: 0 };
|
|
246
|
+
// Sends out the particle set in `spawn` from pool s, and puts its e1 (NaN: the pool's own, 0 additive, 0.5 alpha), e2 and e3 back.
|
|
247
|
+
function pSpawn(s) {
|
|
248
|
+
const p = spawn,
|
|
249
|
+
i = s.head,
|
|
250
|
+
i3 = i * 3,
|
|
251
|
+
i4 = i * 4;
|
|
252
|
+
s.head = i + 1 === s.cap ? 0 : i + 1;
|
|
253
|
+
if (s.head === 0) s.wrapped = true;
|
|
254
|
+
s.pos[i3] = p.x;
|
|
255
|
+
s.pos[i3 + 1] = p.y;
|
|
256
|
+
s.pos[i3 + 2] = p.z;
|
|
257
|
+
s.vel[i3] = p.vx;
|
|
258
|
+
s.vel[i3 + 1] = p.vy;
|
|
259
|
+
s.vel[i3 + 2] = p.vz;
|
|
260
|
+
const time = elapsed();
|
|
261
|
+
s.dat[i4] = time;
|
|
262
|
+
s.dat[i4 + 1] = p.life;
|
|
263
|
+
if (time + p.life > s.until) s.until = time + p.life;
|
|
264
|
+
s.dat[i4 + 2] = p.size;
|
|
265
|
+
s.dat[i4 + 3] = p.grav;
|
|
266
|
+
s.col[i4] = p.r;
|
|
267
|
+
s.col[i4 + 1] = p.g;
|
|
268
|
+
s.col[i4 + 2] = p.b;
|
|
269
|
+
s.col[i4 + 3] = p.drag;
|
|
270
|
+
s.sed[i4] = random();
|
|
271
|
+
s.sed[i4 + 1] = Number.isNaN(p.e1) ? (s.add ? 0 : 0.5) : p.e1;
|
|
272
|
+
s.sed[i4 + 2] = p.e2;
|
|
273
|
+
s.sed[i4 + 3] = p.e3;
|
|
274
|
+
p.e1 = NaN;
|
|
275
|
+
p.e2 = p.e3 = 0;
|
|
276
|
+
if (i < s.lo) s.lo = i;
|
|
277
|
+
if (i > s.hi) s.hi = i;
|
|
278
|
+
s.dirty = true;
|
|
279
|
+
}
|
|
280
|
+
// Uploads the parts of a particle system's buffers that changed this frame, and draws the system only while a particle lives.
|
|
281
|
+
function pFlush(s) {
|
|
282
|
+
s.pts.visible = elapsed() < s.until;
|
|
283
|
+
if (!s.dirty) return;
|
|
284
|
+
for (let k = 0; k < s.attrs.length; k++) {
|
|
285
|
+
const a = s.attrs[k];
|
|
286
|
+
a.clearUpdateRanges();
|
|
287
|
+
if (!s.wrapped) a.addUpdateRange(s.lo * a.itemSize, (s.hi - s.lo + 1) * a.itemSize);
|
|
288
|
+
a.needsUpdate = true;
|
|
289
|
+
}
|
|
290
|
+
s.dirty = false;
|
|
291
|
+
s.wrapped = false;
|
|
292
|
+
s.lo = 1e9;
|
|
293
|
+
s.hi = -1;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return { emberMaterial, fxU, makeParticles, pFlush, pSpawn, spawn };
|
|
297
|
+
}
|