@driftengine/core 4.13.3 → 4.13.5

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.
Files changed (153) hide show
  1. package/CHANGELOG.json +52 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +3 -2
  4. package/dist/render/anisotropicLobe.d.ts +5 -0
  5. package/dist/render/anisotropicLobe.js +13 -0
  6. package/dist/render/atmosphere.d.ts +79 -1
  7. package/dist/render/atmosphere.js +66 -19
  8. package/dist/render/backend/webgl2/renderer.d.ts +34 -2
  9. package/dist/render/backend/webgl2/renderer.js +44 -12
  10. package/dist/render/backend/webgpu/boltPass.d.ts +1 -1
  11. package/dist/render/backend/webgpu/causticsPass.d.ts +1 -1
  12. package/dist/render/backend/webgpu/clothSetPack.js +1 -0
  13. package/dist/render/backend/webgpu/clothSolvePack.d.ts +5 -2
  14. package/dist/render/backend/webgpu/clothSolvePack.js +2 -1
  15. package/dist/render/backend/webgpu/filmPass.d.ts +1 -1
  16. package/dist/render/backend/webgpu/gpuClothSet.js +1 -1
  17. package/dist/render/backend/webgpu/gpuDrivenPass.js +20 -3
  18. package/dist/render/backend/webgpu/linePass.d.ts +1 -1
  19. package/dist/render/backend/webgpu/mediumFields.d.ts +17 -0
  20. package/dist/render/backend/webgpu/mediumFields.js +74 -0
  21. package/dist/render/backend/webgpu/plumePass.d.ts +3 -3
  22. package/dist/render/backend/webgpu/postPass.d.ts +7 -2
  23. package/dist/render/backend/webgpu/renderer.d.ts +13 -3
  24. package/dist/render/backend/webgpu/renderer.js +86 -150
  25. package/dist/render/backend/webgpu/scatterPass.d.ts +1 -1
  26. package/dist/render/backend/webgpu/shaders/clothSolve.wgsl.d.ts +1 -1
  27. package/dist/render/backend/webgpu/shaders/clothSolve.wgsl.js +7 -5
  28. package/dist/render/backend/webgpu/waterPass.d.ts +1 -1
  29. package/dist/render/environmentMixing.d.ts +18 -0
  30. package/dist/render/environmentMixing.js +40 -0
  31. package/dist/render/fog.d.ts +43 -1
  32. package/dist/render/fog.js +87 -22
  33. package/dist/render/globalMedium.d.ts +112 -10
  34. package/dist/render/globalMedium.js +96 -1
  35. package/dist/render/globalMediumPass.d.ts +7 -3
  36. package/dist/render/globalMediumPass.js +22 -1
  37. package/dist/render/hairLobes.d.ts +27 -2
  38. package/dist/render/hairLobes.js +57 -4
  39. package/dist/render/lightBudget.d.ts +12 -0
  40. package/dist/render/lightBudget.js +24 -0
  41. package/dist/render/sceneTarget.d.ts +5 -3
  42. package/dist/render/sceneTarget.js +14 -8
  43. package/dist/render/shaders/arcane.d.ts +1 -1
  44. package/dist/render/shaders/bolt.d.ts +1 -1
  45. package/dist/render/shaders/caustics.d.ts +1 -1
  46. package/dist/render/shaders/film.d.ts +1 -1
  47. package/dist/render/shaders/film.js +4 -1
  48. package/dist/render/shaders/fire.d.ts +1 -1
  49. package/dist/render/shaders/flat/anisotropic.d.ts +8 -1
  50. package/dist/render/shaders/flat/anisotropic.js +25 -3
  51. package/dist/render/shaders/flat/hair.js +77 -27
  52. package/dist/render/shaders/flat/main.d.ts +1 -1
  53. package/dist/render/shaders/flat/main.js +23 -3
  54. package/dist/render/shaders/flat/models.js +12 -5
  55. package/dist/render/shaders/flat/preamble.js +7 -0
  56. package/dist/render/shaders/fog.d.ts +4 -4
  57. package/dist/render/shaders/fog.js +81 -32
  58. package/dist/render/shaders/generated/arcane.wgsl.d.ts +29 -39
  59. package/dist/render/shaders/generated/arcane.wgsl.js +29 -39
  60. package/dist/render/shaders/generated/bolt.wgsl.d.ts +26 -36
  61. package/dist/render/shaders/generated/bolt.wgsl.js +26 -36
  62. package/dist/render/shaders/generated/caustics.wgsl.d.ts +25 -35
  63. package/dist/render/shaders/generated/caustics.wgsl.js +25 -35
  64. package/dist/render/shaders/generated/film.wgsl.d.ts +28 -38
  65. package/dist/render/shaders/generated/film.wgsl.js +28 -38
  66. package/dist/render/shaders/generated/fire.wgsl.d.ts +28 -38
  67. package/dist/render/shaders/generated/fire.wgsl.js +28 -38
  68. package/dist/render/shaders/generated/flat.wgsl.d.ts +1466 -1546
  69. package/dist/render/shaders/generated/flat.wgsl.js +1489 -1569
  70. package/dist/render/shaders/generated/globalMedium.wgsl.d.ts +98 -12
  71. package/dist/render/shaders/generated/globalMedium.wgsl.js +98 -12
  72. package/dist/render/shaders/generated/line.wgsl.d.ts +25 -35
  73. package/dist/render/shaders/generated/line.wgsl.js +25 -35
  74. package/dist/render/shaders/generated/particle.wgsl.d.ts +100 -140
  75. package/dist/render/shaders/generated/particle.wgsl.js +100 -140
  76. package/dist/render/shaders/generated/rush.wgsl.d.ts +20 -10
  77. package/dist/render/shaders/generated/rush.wgsl.js +20 -10
  78. package/dist/render/shaders/generated/scatter.wgsl.d.ts +25 -35
  79. package/dist/render/shaders/generated/scatter.wgsl.js +25 -35
  80. package/dist/render/shaders/generated/smoke.wgsl.d.ts +28 -38
  81. package/dist/render/shaders/generated/smoke.wgsl.js +28 -38
  82. package/dist/render/shaders/generated/water.wgsl.d.ts +29 -39
  83. package/dist/render/shaders/generated/water.wgsl.js +29 -39
  84. package/dist/render/shaders/globalMedium.js +114 -10
  85. package/dist/render/shaders/gpudriven/blendRaster.wgsl.js +2 -1
  86. package/dist/render/shaders/gpudriven/fog.wgsl.d.ts +10 -4
  87. package/dist/render/shaders/gpudriven/fog.wgsl.js +77 -19
  88. package/dist/render/shaders/gpudriven/frameBlock.wgsl.d.ts +1 -1
  89. package/dist/render/shaders/gpudriven/frameBlock.wgsl.js +12 -1
  90. package/dist/render/shaders/gpudriven/shade.wgsl.js +3 -1
  91. package/dist/render/shaders/line.d.ts +1 -1
  92. package/dist/render/shaders/line.js +4 -2
  93. package/dist/render/shaders/localExposure.d.ts +42 -1
  94. package/dist/render/shaders/localExposure.js +73 -6
  95. package/dist/render/shaders/particle.d.ts +3 -3
  96. package/dist/render/shaders/particle.js +10 -6
  97. package/dist/render/shaders/scatter.d.ts +1 -1
  98. package/dist/render/shaders/scatter.js +4 -2
  99. package/dist/render/shaders/smoke.d.ts +1 -1
  100. package/dist/render/shaders/water.d.ts +1 -1
  101. package/dist/render/shaders/water.js +2 -1
  102. package/dist/render/surfaceModel.d.ts +23 -4
  103. package/dist/render/surfaceModel.js +12 -2
  104. package/package.json +6 -6
  105. package/src/index.ts +12 -2
  106. package/src/render/anisotropicLobe.ts +20 -0
  107. package/src/render/atmosphere.ts +153 -20
  108. package/src/render/backend/webgl2/renderer.ts +63 -13
  109. package/src/render/backend/webgpu/clothSetPack.ts +1 -0
  110. package/src/render/backend/webgpu/clothSolvePack.ts +6 -2
  111. package/src/render/backend/webgpu/gpuClothSet.ts +1 -1
  112. package/src/render/backend/webgpu/gpuDrivenPass.ts +20 -3
  113. package/src/render/backend/webgpu/mediumFields.ts +87 -0
  114. package/src/render/backend/webgpu/renderer.ts +109 -157
  115. package/src/render/backend/webgpu/shaders/clothSolve.wgsl.ts +7 -5
  116. package/src/render/environmentMixing.ts +48 -0
  117. package/src/render/fog.ts +125 -25
  118. package/src/render/globalMedium.ts +214 -11
  119. package/src/render/globalMediumPass.ts +29 -4
  120. package/src/render/hairLobes.ts +109 -3
  121. package/src/render/lightBudget.ts +32 -0
  122. package/src/render/sceneTarget.ts +15 -11
  123. package/src/render/shaders/film.ts +4 -1
  124. package/src/render/shaders/flat/anisotropic.ts +25 -3
  125. package/src/render/shaders/flat/hair.ts +77 -27
  126. package/src/render/shaders/flat/main.ts +23 -3
  127. package/src/render/shaders/flat/models.ts +12 -5
  128. package/src/render/shaders/flat/preamble.ts +7 -0
  129. package/src/render/shaders/fog.ts +81 -32
  130. package/src/render/shaders/generated/arcane.wgsl.ts +29 -39
  131. package/src/render/shaders/generated/bolt.wgsl.ts +26 -36
  132. package/src/render/shaders/generated/caustics.wgsl.ts +25 -35
  133. package/src/render/shaders/generated/film.wgsl.ts +28 -38
  134. package/src/render/shaders/generated/fire.wgsl.ts +28 -38
  135. package/src/render/shaders/generated/flat.wgsl.ts +1489 -1569
  136. package/src/render/shaders/generated/globalMedium.wgsl.ts +98 -12
  137. package/src/render/shaders/generated/line.wgsl.ts +25 -35
  138. package/src/render/shaders/generated/particle.wgsl.ts +100 -140
  139. package/src/render/shaders/generated/rush.wgsl.ts +20 -10
  140. package/src/render/shaders/generated/scatter.wgsl.ts +25 -35
  141. package/src/render/shaders/generated/smoke.wgsl.ts +28 -38
  142. package/src/render/shaders/generated/water.wgsl.ts +29 -39
  143. package/src/render/shaders/globalMedium.ts +114 -10
  144. package/src/render/shaders/gpudriven/blendRaster.wgsl.ts +2 -1
  145. package/src/render/shaders/gpudriven/fog.wgsl.ts +78 -19
  146. package/src/render/shaders/gpudriven/frameBlock.wgsl.ts +12 -1
  147. package/src/render/shaders/gpudriven/shade.wgsl.ts +3 -1
  148. package/src/render/shaders/line.ts +4 -2
  149. package/src/render/shaders/localExposure.ts +115 -6
  150. package/src/render/shaders/particle.ts +10 -6
  151. package/src/render/shaders/scatter.ts +4 -2
  152. package/src/render/shaders/water.ts +2 -1
  153. package/src/render/surfaceModel.ts +41 -6
@@ -21,12 +21,43 @@ fn mediumColour() -> vec3<f32> {
21
21
  return mix(frame.fogColour.xyz, frame.fogUnderwater.xyz, frame.fogUnderwater.w);
22
22
  }
23
23
 
24
+ /**
25
+ * The haze's optical depth from where it begins along the ray to a point at "pointY", "dist" metres
26
+ * away: each layer's density at the eye carried over what is left of the ray, the second only
27
+ * where there is one. mediumFog and mediumGlow both integrate it.
28
+ */
29
+ fn hazeDepth(dist: f32, pointY: f32) -> f32 {
30
+ /* Where the medium begins along the ray: fogRange.x, the linear mode's near, which this mode
31
+ never read before Atmosphere.fogStart. Integrated from there on, at that point's density; at
32
+ a start of 0 every term is the one it always was. See render/fog.ts. */
33
+ let start = min(frame.fogRange.x, dist);
34
+ var before = 0.0;
35
+ if (dist > 0.0) { before = start / dist; }
36
+ let t = (pointY - frame.fogColour.w) * frame.fogHeight.x;
37
+ let rest = t * (1.0 - before);
38
+ /* (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than branched: at t = 0
39
+ the quotient is 0/0, and 1e-4 of a scale height is 8 mm. */
40
+ var denom = rest;
41
+ if (abs(rest) < 1e-4) { denom = 1e-4; }
42
+ var depth = frame.fogHeight.y * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom;
43
+ if (frame.fogHeight.z > 0.0) {
44
+ let t2 = (pointY - frame.fogColour.w) * frame.fogHeight.w;
45
+ let rest2 = t2 * (1.0 - before);
46
+ var denom2 = rest2;
47
+ if (abs(rest2) < 1e-4) { denom2 = 1e-4; }
48
+ depth += frame.fogHeight.z * exp(-t2 * before) * (dist - start) * (1.0 - exp(-denom2)) / denom2;
49
+ }
50
+ return depth;
51
+ }
52
+
24
53
  /**
25
54
  * How much of a surface at "pointY", "dist" metres away, is lost to the medium between.
26
55
  * 0 is perfectly clear, 1 is the medium's own colour.
27
56
  */
28
57
  fn mediumFog(dist: f32, pointY: f32) -> f32 {
29
58
  let underwaterFactor = frame.fogUnderwater.w;
59
+ /* Past the cutoff the air hides nothing, in either mode; the water column keeps its own law. */
60
+ let cut = frame.fogLimits.y > 0.0 && dist > frame.fogLimits.y;
30
61
  if (frame.fogRange.z == ${LINEAR_FOG}.0) {
31
62
  /*
32
63
  * A consumer asking for a hard cut writes far == near, and the guard is why that is a cut and
@@ -40,35 +71,48 @@ fn mediumFog(dist: f32, pointY: f32) -> f32 {
40
71
  * shape from the other direction on 2026-09-18: a device may assume NaN never arrives.
41
72
  */
42
73
  let span = max(frame.fogRange.y - frame.fogRange.x, 1e-4);
43
- let ramp = clamp((dist - frame.fogRange.x) / span, 0.0, 1.0);
74
+ var ramp = min(clamp((dist - frame.fogRange.x) / span, 0.0, 1.0), 1.0 - frame.fogLimits.x);
75
+ if (cut) { ramp = 0.0; }
44
76
  if (underwaterFactor <= 0.0) { return ramp; }
45
77
  let wetLinear = frame.fogRange.w * dist;
46
78
  return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), underwaterFactor);
47
79
  }
48
80
 
49
- /* Where the medium begins along the ray: fogRange.x, the linear mode's near, which this mode
50
- never read before Atmosphere.fogStart. Integrated from there on, at that point's density; at
51
- a start of 0 every term is the one it always was. See render/fog.ts. */
52
- let start = min(frame.fogRange.x, dist);
53
- var before = 0.0;
54
- if (dist > 0.0) { before = start / dist; }
55
- let t = (pointY - frame.fogColour.w) * frame.fogHeight.x;
56
- let rest = t * (1.0 - before);
57
- /* (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than branched: at t = 0
58
- the quotient is 0/0, and 1e-4 of a scale height is 8 mm. */
59
- var denom = rest;
60
- if (abs(rest) < 1e-4) { denom = 1e-4; }
61
- let air =
62
- 1.0 - exp(-frame.fogHeight.y * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom);
81
+ /* Beer-Lambert through both layers, leaving at least fogLimits.x of the surface. */
82
+ var air = 1.0 - max(exp(-hazeDepth(dist, pointY)), frame.fogLimits.x);
83
+ if (cut) { air = 0.0; }
63
84
  if (underwaterFactor <= 0.0) { return air; }
64
85
  /* Water is its own medium and keeps its own curve, so crossing the surface changes nothing
65
86
  about how the water column reads. */
66
87
  let wet = frame.fogRange.w * dist;
67
88
  return mix(air, 1.0 - exp2(-wet * wet * 1.442695), underwaterFactor);
68
89
  }
90
+
91
+ /**
92
+ * The sun's light the haze scatters toward the eye along a ray to a point at "pointY", "dist"
93
+ * metres away in the unit direction "toPoint": added after the fog, never mixed. None in a linear
94
+ * ramp, past the cutoff or under water; render/fog.ts says the rest.
95
+ */
96
+ fn mediumGlow(dist: f32, pointY: f32, toPoint: vec3<f32>) -> vec3<f32> {
97
+ if (frame.fogRange.z == ${LINEAR_FOG}.0) { return vec3<f32>(0.0); }
98
+ if (max(max(frame.fogGlow.x, frame.fogGlow.y), frame.fogGlow.z) <= 0.0) { return vec3<f32>(0.0); }
99
+ if (frame.fogLimits.y > 0.0 && dist > frame.fogLimits.y) { return vec3<f32>(0.0); }
100
+ let span = dist - min(frame.fogRange.x, dist);
101
+ if (span <= 0.0) { return vec3<f32>(0.0); }
102
+ let perMetre = hazeDepth(dist, pointY) / span;
103
+ let hidden = 1.0 - exp(-perMetre * max(span - frame.fogGlowDir.w, 0.0));
104
+ let lobe = pow(clamp(dot(toPoint, frame.fogGlowDir.xyz), 0.0, 1.0), frame.fogGlow.w);
105
+ return frame.fogGlow.xyz * (lobe * hidden * (1.0 - frame.fogUnderwater.w));
106
+ }
69
107
  `;
70
- /** Floats a case of the fog check: distance, height, and the ten options. */
71
- export const FOG_CASE_FLOATS = 16;
108
+ /**
109
+ * Floats a case of the fog check: distance, height, the ten options, the second layer, the floor
110
+ * and the cutoff, the glow's colour, exponent, direction and start, the ray's direction, and one
111
+ * spare.
112
+ */
113
+ export const FOG_CASE_FLOATS = 32;
114
+ /** Floats a case's answer: the fog, the colour, and the glow, and one spare. */
115
+ export const FOG_RESULT_FLOATS = 8;
72
116
  /**
73
117
  * An entry point that does nothing but fog, so `gpu-parity.mjs` can compare the expression above
74
118
  * with `render/fog.ts` directly rather than through a shading pass that writes to a texture.
@@ -87,6 +131,12 @@ struct Frame {
87
131
  fogHeight: vec4<f32>,
88
132
  /** Linear near and far, the mode, and the water's own density. */
89
133
  fogRange: vec4<f32>,
134
+ /** The least of a surface the haze must leave, the cutoff, and two spare. */
135
+ fogLimits: vec4<f32>,
136
+ /** The sun glow's colour and exponent. */
137
+ fogGlow: vec4<f32>,
138
+ /** Surface → its light, and its start. */
139
+ fogGlowDir: vec4<f32>,
90
140
  }
91
141
 
92
142
  @group(0) @binding(0) var<storage, read> cases: array<f32>;
@@ -102,14 +152,22 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) {
102
152
  let pointY = cases[at + 1u];
103
153
  frame.fogColour = vec4<f32>(cases[at + 2u], cases[at + 3u], cases[at + 4u], cases[at + 5u]);
104
154
  frame.fogUnderwater = vec4<f32>(cases[at + 6u], cases[at + 7u], cases[at + 8u], cases[at + 9u]);
105
- frame.fogHeight = vec4<f32>(cases[at + 10u], cases[at + 11u], 0.0, 0.0);
155
+ frame.fogHeight = vec4<f32>(cases[at + 10u], cases[at + 11u], cases[at + 16u], cases[at + 17u]);
106
156
  frame.fogRange = vec4<f32>(cases[at + 12u], cases[at + 13u], cases[at + 14u], cases[at + 15u]);
157
+ frame.fogLimits = vec4<f32>(cases[at + 18u], cases[at + 19u], 0.0, 0.0);
158
+ frame.fogGlow = vec4<f32>(cases[at + 20u], cases[at + 21u], cases[at + 22u], cases[at + 23u]);
159
+ frame.fogGlowDir = vec4<f32>(cases[at + 24u], cases[at + 25u], cases[at + 26u], cases[at + 27u]);
160
+ let toPoint = vec3<f32>(cases[at + 28u], cases[at + 29u], cases[at + 30u]);
107
161
 
108
- let out = id.x * 4u;
162
+ let out = id.x * ${FOG_RESULT_FLOATS}u;
109
163
  results[out] = mediumFog(dist, pointY);
110
164
  let colour = mediumColour();
111
165
  results[out + 1u] = colour.x;
112
166
  results[out + 2u] = colour.y;
113
167
  results[out + 3u] = colour.z;
168
+ let glow = mediumGlow(dist, pointY, toPoint);
169
+ results[out + 4u] = glow.x;
170
+ results[out + 5u] = glow.y;
171
+ results[out + 6u] = glow.z;
114
172
  }
115
173
  `;
@@ -10,4 +10,4 @@
10
10
  * `gpuDrivenPass.ts` writes it through the `FRAME_*` offsets beside `FRAME_FLOATS`, which are the
11
11
  * other half of this contract.
12
12
  */
13
- export declare const FRAME_BLOCK_WGSL = "\nstruct Frame {\n viewProj: mat4x4<f32>,\n /*\n * The light's, corrected for the lookup rather than for the raster: x and y still in -1..1 and\n * z already in 0..1. gpudriven/shadowCamera.ts carries why those are two different matrices.\n */\n lightViewProj: mat4x4<f32>,\n eye: vec4<f32>,\n lightDir: vec4<f32>,\n lightColour: vec4<f32>,\n sky: vec4<f32>,\n ground: vec4<f32>,\n /** width, height, the simulation time, and the latent layer's edge in texels. */\n size: vec4<f32>,\n /** The shadow: strength, map size, depth span, the distance it fades over. */\n shadow: vec4<f32>,\n /** Its remaining limits: the steepest sun that still casts, the tap count, and two spare. */\n shadowLimits: vec4<f32>,\n /** The probe: texels across level 0, the coarsest GGX level, the cosine level, and 1 if bound. */\n environment: vec4<f32>,\n /**\n * How much of the hemispheric ambient the probe's irradiance replaces, the frame's emissive gain\n * times its night factor, and two spare.\n */\n environmentMix: vec4<f32>,\n /**\n * The medium, in four blocks, and render/fog.ts is what reads them.\n *\n * **A frame that asks for nothing draws nothing**: a density of zero and the medium mode make\n * mediumFog exactly zero at every distance, which is arithmetically the frame the pipeline drew\n * before it had a haze at all.\n */\n /** rgb is the haze's colour, w is the eye's height. */\n fogColour: vec4<f32>,\n /** rgb is the water's colour, w is how far under it the camera is. */\n fogUnderwater: vec4<f32>,\n /** Reciprocal scale height, extinction per metre at the eye, and two spare. */\n fogHeight: vec4<f32>,\n /** Linear near and far, the mode, and the water's own density. */\n fogRange: vec4<f32>,\n /**\n * The visibility buffer's row in texels \u2014 the width rounded up for the copy's row alignment \u2014\n * and three spare. A pixel is found by this and placed by size.x, which is the width the raster\n * drew at; the two differ on every frame whose width is not a multiple of sixty-four. Not\n * \"target\", which WGSL reserves.\n */\n pitch: vec4<f32>,\n}\n";
13
+ export declare const FRAME_BLOCK_WGSL = "\nstruct Frame {\n viewProj: mat4x4<f32>,\n /*\n * The light's, corrected for the lookup rather than for the raster: x and y still in -1..1 and\n * z already in 0..1. gpudriven/shadowCamera.ts carries why those are two different matrices.\n */\n lightViewProj: mat4x4<f32>,\n eye: vec4<f32>,\n lightDir: vec4<f32>,\n lightColour: vec4<f32>,\n sky: vec4<f32>,\n ground: vec4<f32>,\n /** width, height, the simulation time, and the latent layer's edge in texels. */\n size: vec4<f32>,\n /** The shadow: strength, map size, depth span, the distance it fades over. */\n shadow: vec4<f32>,\n /** Its remaining limits: the steepest sun that still casts, the tap count, and two spare. */\n shadowLimits: vec4<f32>,\n /** The probe: texels across level 0, the coarsest GGX level, the cosine level, and 1 if bound. */\n environment: vec4<f32>,\n /**\n * How much of the hemispheric ambient the probe's irradiance replaces, the frame's emissive gain\n * times its night factor, and two spare.\n */\n environmentMix: vec4<f32>,\n /**\n * The medium, in four blocks, and render/fog.ts is what reads them.\n *\n * **A frame that asks for nothing draws nothing**: a density of zero and the medium mode make\n * mediumFog exactly zero at every distance, which is arithmetically the frame the pipeline drew\n * before it had a haze at all.\n */\n /** rgb is the haze's colour, w is the eye's height. */\n fogColour: vec4<f32>,\n /** rgb is the water's colour, w is how far under it the camera is. */\n fogUnderwater: vec4<f32>,\n /**\n * Reciprocal scale height and extinction per metre at the eye; then the second layer's\n * extinction at the eye and its reciprocal scale height, zero for none.\n */\n fogHeight: vec4<f32>,\n /** Linear near and far, the mode, and the water's own density. */\n fogRange: vec4<f32>,\n /**\n * The visibility buffer's row in texels \u2014 the width rounded up for the copy's row alignment \u2014\n * and three spare. A pixel is found by this and placed by size.x, which is the width the raster\n * drew at; the two differ on every frame whose width is not a multiple of sixty-four. Not\n * \"target\", which WGSL reserves.\n */\n pitch: vec4<f32>,\n /**\n * The rest of the medium, after the pitch so nothing above it moves: the least of a surface the\n * haze must leave and the cutoff, both none at zero, and two spare; the sun glow's colour, black\n * for none, and its exponent; and surface \u2192 its light, and its start. render/fog.ts reads them.\n */\n fogLimits: vec4<f32>,\n fogGlow: vec4<f32>,\n fogGlowDir: vec4<f32>,\n}\n";
@@ -47,7 +47,10 @@ struct Frame {
47
47
  fogColour: vec4<f32>,
48
48
  /** rgb is the water's colour, w is how far under it the camera is. */
49
49
  fogUnderwater: vec4<f32>,
50
- /** Reciprocal scale height, extinction per metre at the eye, and two spare. */
50
+ /**
51
+ * Reciprocal scale height and extinction per metre at the eye; then the second layer's
52
+ * extinction at the eye and its reciprocal scale height, zero for none.
53
+ */
51
54
  fogHeight: vec4<f32>,
52
55
  /** Linear near and far, the mode, and the water's own density. */
53
56
  fogRange: vec4<f32>,
@@ -58,5 +61,13 @@ struct Frame {
58
61
  * "target", which WGSL reserves.
59
62
  */
60
63
  pitch: vec4<f32>,
64
+ /**
65
+ * The rest of the medium, after the pitch so nothing above it moves: the least of a surface the
66
+ * haze must leave and the cutoff, both none at zero, and two spare; the sun glow's colour, black
67
+ * for none, and its exponent; and surface → its light, and its start. render/fog.ts reads them.
68
+ */
69
+ fogLimits: vec4<f32>,
70
+ fogGlow: vec4<f32>,
71
+ fogGlowDir: vec4<f32>,
61
72
  }
62
73
  `;
@@ -816,7 +816,9 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) {
816
816
  * point and for the same reason: fog is the air in front of the picture rather than a property
817
817
  * of the surface, so it is the last thing between a shaded colour and the frame.
818
818
  */
819
- let hazed = mix(lit, mediumColour(), mediumFog(distance(world, frame.eye.xyz), world.y));
819
+ let dist = distance(world, frame.eye.xyz);
820
+ let hazed = mix(lit, mediumColour(), mediumFog(dist, world.y))
821
+ + mediumGlow(dist, world.y, normalize(world - frame.eye.xyz));
820
822
 
821
823
  textureStore(frameColour, vec2<u32>(x, y), vec4<f32>(hazed, 1.0));
822
824
  }
@@ -15,4 +15,4 @@
15
15
  * draws.
16
16
  */
17
17
  export declare const LINE_VERT = "#version 300 es\nlayout(location = 0) in vec3 aFrom;\nlayout(location = 1) in vec3 aTo;\n/** Along the segment (0 or 1), and which side of it (-1 or +1). */\nlayout(location = 2) in vec2 aCorner;\n\nuniform mat4 uViewProj;\nuniform mat4 uModel;\nuniform vec3 uCameraPos;\n/** Half-width in metres, and how much of a pixel a far line must still cover. */\nuniform float uWidth;\nuniform float uMinWidthPerMetre;\n\nout float vSide;\nout vec3 vWorldPos;\n\nvoid main() {\n /*\n * Through the model matrix before anything else, unlike an arc, whose endpoints are\n * world-space by nature. A line is usually attached to something that moves, and the\n * alternative is making every caller transform its own points on the CPU each frame.\n *\n * The width is deliberately *not* scaled by the model. A stroke's weight is a property\n * of how it is drawn rather than of the geometry it follows, so a card that shrinks\n * keeps the same line weight, which is what a reader expects and what a designer means.\n */\n vec3 from = (uModel * vec4(aFrom, 1.0)).xyz;\n vec3 to = (uModel * vec4(aTo, 1.0)).xyz;\n\n vec3 mid = mix(from, to, aCorner.x);\n vec3 dir = to - from;\n float len = length(dir);\n dir = len > 1e-6 ? dir / len : vec3(0.0, 1.0, 0.0);\n\n vec3 view = mid - uCameraPos;\n float dist = length(view);\n view = dist > 1e-6 ? view / dist : vec3(0.0, 0.0, 1.0);\n\n /*\n * Across the segment and across the line of sight. Degenerate exactly when the segment\n * points at the viewer, where any perpendicular is as good as another, and where the\n * segment covers almost no screen anyway.\n */\n vec3 across = cross(dir, view);\n float span = length(across);\n across = span > 1e-4 ? across / span : normalize(cross(dir, vec3(0.0, 1.0, 0.0)) + vec3(1e-3));\n\n /*\n * A floor on the world-space width that grows with distance, so a stroke never thins\n * below a pixel. Under a pixel a thin line does not get fainter, it *flickers*, because\n * the rasteriser catches it on some frames and not others.\n */\n float halfWidth = max(uWidth, dist * uMinWidthPerMetre);\n vec3 world = mid + across * aCorner.y * halfWidth;\n\n vSide = aCorner.y;\n vWorldPos = world;\n gl_Position = uViewProj * vec4(world, 1.0);\n}\n";
18
- export declare const LINE_FRAG = "#version 300 es\nprecision highp float;\n\nin float vSide;\nin vec3 vWorldPos;\n\nuniform vec3 uColor;\nuniform float uOpacity;\nuniform vec3 uCameraPos;\n/**\n * How far in from the edge the stroke fades, as a fraction of its half-width. Zero is a\n * clean edge, one is a stroke that is soft all the way to its middle.\n */\nuniform float uSoftness;\n\n\nuniform vec3 uFogColor;\n/** Extinction per metre **at the camera's own height**, not at the base. */\nuniform float uFogDensity;\n/** Reciprocal scale height of the haze, per metre. 0 is a uniform medium. */\nuniform float uFogHeightFalloff;\n/** The camera's height, so a fragment's own height is one subtraction away. */\nuniform float uFogEyeY;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFogDensity;\nuniform float uUnderwaterFactor;\n/** 0 integrates a medium (Beer-Lambert); 1 ramps linearly between near and far. */\nuniform int uFogMode;\n/** Linear mode only: where haze begins, and where it is total. */\nuniform float uFogNear;\nuniform float uFogFar;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogMode == 1) {\n float span = max(uFogFar - uFogNear, 1e-4);\n float ramp = clamp((dist - uFogNear) / span, 0.0, 1.0);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uUnderwaterFogDensity * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Where the medium begins along the ray: uFogNear, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogNear, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogEyeY) * uFogHeightFalloff;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float air =\n 1.0 - exp(-uFogDensity * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uUnderwaterFogDensity * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n /*\n * Coverage across the ribbon: one down the middle, falling to zero at both edges.\n *\n * The band is at least one pixel wide *in screen space*, which is what lets one shader\n * draw a stroke of any width at any distance without either aliasing into a staircase or\n * blurring into a smear. A constant band is tuned for one width and is wrong at the rest,\n * which is the same argument `SDF_TEXT_FRAG` makes for the same reason.\n */\n float edge = 1.0 - abs(vSide);\n float band = max(fwidth(vSide), uSoftness);\n float coverage = smoothstep(0.0, band, edge);\n\n /*\n * Fogged like geometry, which is where this parts company with `bolt.ts`. That shader\n * includes the same helpers and never calls them, because an arc is light arriving\n * rather than a surface losing some of itself to the air on the way over. A drawn line\n * is a thing in the world and recedes like one.\n */\n float fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);\n vec3 color = mix(uColor, mediumColor(), fog);\n outColor = vec4(applyOutputTransform(color), coverage * uOpacity);\n}\n";
18
+ export declare const LINE_FRAG = "#version 300 es\nprecision highp float;\n\nin float vSide;\nin vec3 vWorldPos;\n\nuniform vec3 uColor;\nuniform float uOpacity;\nuniform vec3 uCameraPos;\n/**\n * How far in from the edge the stroke fades, as a fraction of its half-width. Zero is a\n * clean edge, one is a stroke that is soft all the way to its middle.\n */\nuniform float uSoftness;\n\n\nuniform vec3 uFogColor;\n/**\n * The haze: its extinction per metre **at the camera's own height**, not at the base; its\n * reciprocal scale height per metre, 0 for a uniform medium; the camera's height, so a fragment's\n * own height is one subtraction away; and the water column's own density.\n *\n * **Four numbers in one vector rather than four uniforms**, and the reason is a phone: a scalar\n * costs a whole row of the uniform grid, and the lit stage at its eight-light rung must fit the 256\n * rows an Adreno 740 offers (uniformVectorBudget.ts). Packed, the haze's eight scalars cost two\n * rows where they cost eight, which is what paid for the second layer and the sun's glow below.\n */\nuniform vec4 uFogMedium;\n/** Where haze begins, where a linear ramp is total, and the mode: 0 a medium, 1 a ramp. */\nuniform vec4 uFogRange;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFactor;\n/**\n * The second layer's extinction per metre at the camera's height and its falloff; then the least of\n * a surface the haze must leave, Atmosphere.fogMaxOpacity's complement, and the distance past which\n * it hides nothing (0 is none). **Every lane is none at zero**, and so are uFogGlow's: a program a\n * binder never wrote these into draws the haze it drew before they existed, rather than none.\n */\nuniform vec4 uFogLayer2;\n/** The sun's glow in the haze: its colour, black for none, and the exponent of its lobe. */\nuniform vec4 uFogGlow;\n/** Surface \u2192 the glow's light, and how far past the haze's start the glow begins. */\nuniform vec4 uFogGlowDir;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * The haze's optical depth from where it begins along the ray to a point at height pointY, dist\n * metres away: each layer's density at the eye carried over what is left of the ray, with the\n * second layer only where there is one. The fog and the sun's glow both integrate it.\n */\nfloat hazeDepth(float dist, float pointY) {\n // Where the medium begins along the ray: uFogRange.x, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogRange.x, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogMedium.z) * uFogMedium.y;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float depth = uFogMedium.x * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom;\n if (uFogLayer2.x > 0.0) {\n float t2 = (pointY - uFogMedium.z) * uFogLayer2.y;\n float rest2 = t2 * (1.0 - before);\n float denom2 = abs(rest2) < 1e-4 ? 1e-4 : rest2;\n depth += uFogLayer2.x * exp(-t2 * before) * (dist - start) * (1.0 - exp(-denom2)) / denom2;\n }\n return depth;\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /* Past the cutoff the air hides nothing, in either mode; the water column keeps its own law. */\n bool cut = uFogLayer2.w > 0.0 && dist > uFogLayer2.w;\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogRange.z == 1.0) {\n float span = max(uFogRange.y - uFogRange.x, 1e-4);\n float ramp = cut ? 0.0 : min(clamp((dist - uFogRange.x) / span, 0.0, 1.0), 1.0 - uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uFogMedium.w * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Beer-Lambert through both layers, leaving at least uFogLayer2.z of the surface: a floor on\n // what survives rather than a ceiling on the depth, so the curve keeps its shape above it.\n float air = cut ? 0.0 : 1.0 - max(exp(-hazeDepth(dist, pointY)), uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uFogMedium.w * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n/**\n * The sun's light the haze scatters toward the eye along a ray to a point at height pointY, dist\n * metres away in the direction toPoint: added to a surface after its fog, never mixed. A lobe of\n * cos^exponent about the light, by as much of the ray past the glow's own start as the haze hides \u2014\n * the haze's depth per metre carried over that stretch. None in a linear ramp, past the cutoff, or\n * under water, which keeps its own medium.\n */\nvec3 mediumGlow(float dist, float pointY, vec3 toPoint) {\n if (uFogRange.z == 1.0 || max(max(uFogGlow.r, uFogGlow.g), uFogGlow.b) <= 0.0) return vec3(0.0);\n if (uFogLayer2.w > 0.0 && dist > uFogLayer2.w) return vec3(0.0);\n float span = dist - min(uFogRange.x, dist);\n if (span <= 0.0) return vec3(0.0);\n float perMetre = hazeDepth(dist, pointY) / span;\n float hidden = 1.0 - exp(-perMetre * max(span - uFogGlowDir.w, 0.0));\n float lobe = pow(clamp(dot(toPoint, uFogGlowDir.xyz), 0.0, 1.0), uFogGlow.a);\n return uFogGlow.rgb * (lobe * hidden * (1.0 - uUnderwaterFactor));\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n /*\n * Coverage across the ribbon: one down the middle, falling to zero at both edges.\n *\n * The band is at least one pixel wide *in screen space*, which is what lets one shader\n * draw a stroke of any width at any distance without either aliasing into a staircase or\n * blurring into a smear. A constant band is tuned for one width and is wrong at the rest,\n * which is the same argument `SDF_TEXT_FRAG` makes for the same reason.\n */\n float edge = 1.0 - abs(vSide);\n float band = max(fwidth(vSide), uSoftness);\n float coverage = smoothstep(0.0, band, edge);\n\n /*\n * Fogged like geometry, which is where this parts company with `bolt.ts`. That shader\n * includes the same helpers and never calls them, because an arc is light arriving\n * rather than a surface losing some of itself to the air on the way over. A drawn line\n * is a thing in the world and recedes like one.\n */\n float dist = distance(vWorldPos, uCameraPos);\n float fog = mediumFog(dist, vWorldPos.y);\n vec3 color =\n mix(uColor, mediumColor(), fog) + mediumGlow(dist, vWorldPos.y, normalize(vWorldPos - uCameraPos));\n outColor = vec4(applyOutputTransform(color), coverage * uOpacity);\n}\n";
@@ -115,8 +115,10 @@ void main() {
115
115
  * rather than a surface losing some of itself to the air on the way over. A drawn line
116
116
  * is a thing in the world and recedes like one.
117
117
  */
118
- float fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);
119
- vec3 color = mix(uColor, mediumColor(), fog);
118
+ float dist = distance(vWorldPos, uCameraPos);
119
+ float fog = mediumFog(dist, vWorldPos.y);
120
+ vec3 color =
121
+ mix(uColor, mediumColor(), fog) + mediumGlow(dist, vWorldPos.y, normalize(vWorldPos - uCameraPos));
120
122
  outColor = vec4(applyOutputTransform(color), coverage * uOpacity);
121
123
  }
122
124
  `;
@@ -14,7 +14,8 @@ export declare const LOCAL_RANGE_STOPS = 3;
14
14
  */
15
15
  export declare const EXPOSURE_LOCAL_FRAG: string;
16
16
  /**
17
- * The composite's half. Needs `AUTO_EXPOSURE_GLSL` before it, for the held brightness.
17
+ * The composite's half. Needs `AUTO_EXPOSURE_GLSL` and `uOutputExposure` before it, for the held
18
+ * brightness and the frame's exposure.
18
19
  *
19
20
  * A block is read clamped half a texel inside its own tiles, so the filter never reaches into the
20
21
  * neighbouring band's block. Every fetch is at level zero: the grid has one level, and the branch
@@ -31,5 +32,45 @@ export declare function localBin(pixelLog2: number): {
31
32
  export declare function sliceLocal(sumLower: number, countLower: number, sumUpper: number, countUpper: number, weight: number, tileMean: number): number;
32
33
  /** A region's gain for a local level and the frame's held one at `strength`, as the composite. */
33
34
  export declare function localExposureGain(localLog2: number, heldLog2: number, strength: number): number;
35
+ /**
36
+ * Local exposure as a shape rather than one strength: each region turned about middle grey at the
37
+ * frame's exposure, the real-time form most engines expose.
38
+ *
39
+ * A region's level is its stops from that grey: it keeps `highlightContrast` of them where it is
40
+ * brighter and `shadowContrast` where it is darker, so 1 leaves it where it is and 0 brings it all
41
+ * the way to grey. A pixel keeps `detail` of its own stops from its region, so above 1 sharpens the
42
+ * texture inside a region a curve would have flattened. **What it gives up** is the held level the
43
+ * one-number form turns about: a frame metered far from grey, a night, is pulled toward grey rather
44
+ * than toward itself, which is what the form is for and what a caller holding a night dark leaves
45
+ * off.
46
+ */
47
+ export interface LocalExposureOptions {
48
+ /** The share of a region's stops above middle grey it keeps. 1 is none moved. */
49
+ readonly highlightContrast: number;
50
+ /** The share it keeps below. */
51
+ readonly shadowContrast: number;
52
+ /** The share of a pixel's own stops from its region it keeps. Default 1. */
53
+ readonly detail?: number;
54
+ /** How far a region's level leans from its bilateral band toward its tile's mean. Default 0.4. */
55
+ readonly blurredBlend?: number;
56
+ /** Stops added to middle grey, the level regions turn about. Default 0. */
57
+ readonly middleGreyBias?: number;
58
+ /** The furthest a region moves either way, in stops. Default 8. */
59
+ readonly range?: number;
60
+ }
61
+ /** Floats `resolveLocalExposure` fills: `uLocalShape`, then `uLocalPivot`. */
62
+ export declare const LOCAL_EXPOSURE_FLOATS = 8;
63
+ /**
64
+ * Either form of `setLocalExposure` as the composite reads it, into `out`, and whether regions move
65
+ * at all. The one number is the shape it always was, both shares `1 − strength` about the held
66
+ * level, at most three stops: so a strength draws what it drew, to the bit.
67
+ */
68
+ export declare function resolveLocalExposure(setting: number | LocalExposureOptions, out: Float32Array): boolean;
69
+ /**
70
+ * A pixel's gain from either form, as the composite computes it: `pixelLog2` its own level,
71
+ * `localLog2` its region's before the blend, `tileMean` its tile's, `heldLog2` the frame's held
72
+ * brightness and `exposure` the frame's exposure, gain and all; `shape` is `resolveLocalExposure`'s.
73
+ */
74
+ export declare function localExposureShapedGain(pixelLog2: number, localLog2: number, tileMean: number, heldLog2: number, exposure: number, shape: ArrayLike<number>): number;
34
75
  /** A local exposure strength as the shader takes it: 0 to 1, anything else off. */
35
76
  export declare function clampLocalExposure(strength: number): number;
@@ -26,7 +26,7 @@
26
26
  * whose drama is the contrast itself: a single shaft into a dark room wants the room dark, and the
27
27
  * strength is the caller's for that reason.
28
28
  */
29
- import { METER_GRID, METER_MAX_LOG2, METER_MIN_LOG2, METER_TAPS } from './exposure.js';
29
+ import { ADAPTED_KEY, METER_GRID, METER_MAX_LOG2, METER_MIN_LOG2, METER_TAPS } from './exposure.js';
30
30
  /** Bands of log luminance, and how many stops each is wide. */
31
31
  export const LOCAL_BINS = 10;
32
32
  export const LOCAL_BIN_STOPS = (METER_MAX_LOG2 - METER_MIN_LOG2) / LOCAL_BINS;
@@ -78,15 +78,27 @@ void main() {
78
78
  }
79
79
  `;
80
80
  /**
81
- * The composite's half. Needs `AUTO_EXPOSURE_GLSL` before it, for the held brightness.
81
+ * The composite's half. Needs `AUTO_EXPOSURE_GLSL` and `uOutputExposure` before it, for the held
82
+ * brightness and the frame's exposure.
82
83
  *
83
84
  * A block is read clamped half a texel inside its own tiles, so the filter never reaches into the
84
85
  * neighbouring band's block. Every fetch is at level zero: the grid has one level, and the branch
85
86
  * on the strength above them is uniform but the pixel's band is not.
86
87
  */
87
88
  export const LOCAL_EXPOSURE_GLSL = `
88
- /** How far each region is brought toward the frame, 0 to 1. 0 is off and the whole of the off path. */
89
+ /** Above 0 when regions move at all: 0 is off and the whole of the off path. */
89
90
  uniform float uLocalExposure;
91
+ /**
92
+ * How regions move, as \`resolveLocalExposure\` packs it: the share of a region's stops from the
93
+ * pivot it keeps above the pivot and below it, each less one; the share of a pixel's own stops
94
+ * from its region it keeps, less one; and how far a region's level leans toward its tile's mean.
95
+ */
96
+ uniform vec4 uLocalShape;
97
+ /**
98
+ * What they move about: 1 for the frame's held brightness, 0 for middle grey at the frame's
99
+ * exposure; stops added to it; and the furthest a region moves either way.
100
+ */
101
+ uniform vec4 uLocalPivot;
90
102
  /** The bilateral grid: a block of tiles per band of brightness, and one of the tiles' means. */
91
103
  uniform highp sampler2D uExposureLocal;
92
104
 
@@ -105,9 +117,15 @@ float localExposureGain(vec2 uv, vec3 light) {
105
117
  vec2 banded = mix(localBlock(uv, lower), localBlock(uv, upper), clamp(b - lower, 0.0, 1.0));
106
118
  float tileMean = localBlock(uv, ${LOCAL_BINS.toFixed(1)}).r;
107
119
  float bilateral = banded.y > 1e-3 ? banded.x / banded.y : tileMean;
108
- float local = mix(bilateral, tileMean, ${LOCAL_BLURRED_BLEND.toFixed(1)});
109
- float stops = uLocalExposure * (held - local);
110
- return exp2(clamp(stops, -${LOCAL_RANGE_STOPS.toFixed(1)}, ${LOCAL_RANGE_STOPS.toFixed(1)}));
120
+ float local = mix(bilateral, tileMean, uLocalShape.w);
121
+ /* Middle grey at the frame's exposure, in the scene light the grid measured. */
122
+ float pivot = uLocalPivot.x > 0.5
123
+ ? held
124
+ : ${Math.log2(ADAPTED_KEY).toFixed(6)} - log2(max(autoExposureGain() * uOutputExposure, 1e-8));
125
+ pivot += uLocalPivot.y;
126
+ float keep = local > pivot ? uLocalShape.x : uLocalShape.y;
127
+ float stops = (local - pivot) * keep + (pixel - local) * uLocalShape.z;
128
+ return exp2(clamp(stops, -uLocalPivot.z, uLocalPivot.z));
111
129
  }
112
130
  `;
113
131
  const clamp = (value, low, high) => Math.min(Math.max(value, low), high);
@@ -132,6 +150,55 @@ export function localExposureGain(localLog2, heldLog2, strength) {
132
150
  const stops = strength * (heldLog2 - localLog2);
133
151
  return 2 ** clamp(stops, -LOCAL_RANGE_STOPS, LOCAL_RANGE_STOPS);
134
152
  }
153
+ /** Floats `resolveLocalExposure` fills: `uLocalShape`, then `uLocalPivot`. */
154
+ export const LOCAL_EXPOSURE_FLOATS = 8;
155
+ /**
156
+ * Either form of `setLocalExposure` as the composite reads it, into `out`, and whether regions move
157
+ * at all. The one number is the shape it always was, both shares `1 − strength` about the held
158
+ * level, at most three stops: so a strength draws what it drew, to the bit.
159
+ */
160
+ export function resolveLocalExposure(setting, out) {
161
+ if (typeof setting === 'number') {
162
+ const strength = clampLocalExposure(setting);
163
+ out[0] = -strength;
164
+ out[1] = -strength;
165
+ out[2] = 0;
166
+ out[3] = LOCAL_BLURRED_BLEND;
167
+ out[4] = 1;
168
+ out[5] = 0;
169
+ out[6] = LOCAL_RANGE_STOPS;
170
+ out[7] = 0;
171
+ return strength > 0;
172
+ }
173
+ const share = (value, fallback) => value !== undefined && Number.isFinite(value) ? Math.max(0, value) : fallback;
174
+ const highlight = share(setting.highlightContrast, 1);
175
+ const shadow = share(setting.shadowContrast, 1);
176
+ const detail = share(setting.detail, 1);
177
+ out[0] = highlight - 1;
178
+ out[1] = shadow - 1;
179
+ out[2] = detail - 1;
180
+ out[3] = clamp(share(setting.blurredBlend, LOCAL_BLURRED_BLEND), 0, 1);
181
+ out[4] = 0;
182
+ const bias = setting.middleGreyBias;
183
+ out[5] = bias !== undefined && Number.isFinite(bias) ? bias : 0;
184
+ out[6] = share(setting.range, 8);
185
+ out[7] = 0;
186
+ return highlight !== 1 || shadow !== 1 || detail !== 1;
187
+ }
188
+ /**
189
+ * A pixel's gain from either form, as the composite computes it: `pixelLog2` its own level,
190
+ * `localLog2` its region's before the blend, `tileMean` its tile's, `heldLog2` the frame's held
191
+ * brightness and `exposure` the frame's exposure, gain and all; `shape` is `resolveLocalExposure`'s.
192
+ */
193
+ export function localExposureShapedGain(pixelLog2, localLog2, tileMean, heldLog2, exposure, shape) {
194
+ const local = localLog2 + (tileMean - localLog2) * shape[3];
195
+ const pivot = (shape[4] > 0.5 ? heldLog2 : Math.log2(ADAPTED_KEY) - Math.log2(exposure)) +
196
+ shape[5];
197
+ const keep = local > pivot ? shape[0] : shape[1];
198
+ const stops = (local - pivot) * keep + (pixelLog2 - local) * shape[2];
199
+ const range = shape[6];
200
+ return 2 ** clamp(stops, -range, range);
201
+ }
135
202
  /** A local exposure strength as the shader takes it: 0 to 1, anything else off. */
136
203
  export function clampLocalExposure(strength) {
137
204
  return Number.isFinite(strength) ? clamp(strength, 0, 1) : 0;
@@ -47,7 +47,7 @@ export declare const PARTICLE_VERT = "#version 300 es\n/** Per-vertex: the quad
47
47
  * and unlit smoke in a world lit by fires is the tell that gives an effect away
48
48
  * fastest. A puff catching a flame's colour is most of what makes it belong.
49
49
  */
50
- export declare const PARTICLE_SMOKE_FRAG = "#version 300 es\nprecision highp float;\n\nin vec2 vUv;\nin vec3 vColor;\nin float vAlpha;\nin float vAge;\nin float vSeed;\nin vec3 vWorldPos;\nin vec3 vNormal;\n\nuniform float uTime;\nuniform vec3 uDirectionalDir;\nuniform vec3 uDirectionalColor;\nuniform vec3 uAmbient;\nuniform vec3 uCameraPos;\nuniform int uNoiseOctaves;\n/** How strongly the noise eats into the puff. 0 is a plain soft ball. */\nuniform float uErosion;\n\n#define MAX_LIGHTS 16\nuniform int uLightCount;\nuniform vec3 uLightPos[MAX_LIGHTS];\nuniform vec3 uLightColor[MAX_LIGHTS];\nuniform float uLightRadius[MAX_LIGHTS];\n// How present each light is, 0 to 1. See pointLightSelection.ts.\nuniform float uLightWeight[MAX_LIGHTS];\n\n\nuniform vec3 uFogColor;\n/** Extinction per metre **at the camera's own height**, not at the base. */\nuniform float uFogDensity;\n/** Reciprocal scale height of the haze, per metre. 0 is a uniform medium. */\nuniform float uFogHeightFalloff;\n/** The camera's height, so a fragment's own height is one subtraction away. */\nuniform float uFogEyeY;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFogDensity;\nuniform float uUnderwaterFactor;\n/** 0 integrates a medium (Beer-Lambert); 1 ramps linearly between near and far. */\nuniform int uFogMode;\n/** Linear mode only: where haze begins, and where it is total. */\nuniform float uFogNear;\nuniform float uFogFar;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogMode == 1) {\n float span = max(uFogFar - uFogNear, 1e-4);\n float ramp = clamp((dist - uFogNear) / span, 0.0, 1.0);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uUnderwaterFogDensity * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Where the medium begins along the ray: uFogNear, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogNear, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogEyeY) * uFogHeightFalloff;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float air =\n 1.0 - exp(-uFogDensity * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uUnderwaterFogDensity * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n\nfloat hash21(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);\n}\n\nfloat valueNoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = fract(p);\n vec2 u = f * f * (3.0 - 2.0 * f);\n return mix(\n mix(hash21(i), hash21(i + vec2(1.0, 0.0)), u.x),\n mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0, 1.0)), u.x),\n u.y\n );\n}\n\nfloat billow(vec2 p, float t, int octaves) {\n float v = valueNoise(p * 2.4 + vec2(0.0, -t * 0.5)) * 0.62;\n if (octaves == 1) return v / 0.62;\n v += valueNoise(p * 5.1 + vec2(t * 0.15, -t * 0.8)) * 0.38;\n return v / 1.0;\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n /*\n * A round, soft body first. 'smoothstep' rather than a hard disc: the edge is\n * the entire difference between a puff and a sprite, and a cut-off circle reads\n * as a decal however good the noise inside it is.\n */\n float r = length(vUv);\n float body = 1.0 - smoothstep(0.35, 1.0, r);\n if (body <= 0.0) discard;\n\n /*\n * Then eaten into by noise that drifts and coarsens as the puff ages, so it\n * dissipates by *breaking up* rather than by uniformly thinning. Uniform\n * thinning is what a fading sprite does, and it is what the old cube did.\n */\n float t = uTime * 0.55 + vSeed * 7.31;\n float n = billow(vUv * (0.9 + vAge * 1.4) + vec2(vSeed * 3.7, 0.0), t, uNoiseOctaves);\n float erode = mix(1.0, n, uErosion * (0.35 + vAge * 0.65));\n float mask = clamp(body * erode, 0.0, 1.0);\n // Feather the last of it out rather than letting a noise threshold flicker.\n mask *= smoothstep(0.0, 0.25, mask);\n if (mask <= 0.002) discard;\n\n vec3 n3 = normalize(vNormal);\n /*\n * Wrapped diffuse. Smoke transmits as well as reflects, so the terminator on a\n * puff sits well past 90\u00B0 \u2014 a hard 'max(dot, 0)' gives a lump with a black side,\n * which is exactly the \"coloured box\" failure the fidelity rules name.\n */\n float ndl = dot(n3, uDirectionalDir) * 0.5 + 0.5;\n vec3 lit = uAmbient + uDirectionalColor * ndl;\n\n for (int i = 0; i < MAX_LIGHTS; i++) {\n if (i >= uLightCount) break;\n vec3 toLight = uLightPos[i] - vWorldPos;\n float dist = length(toLight);\n float radius = uLightRadius[i];\n if (radius <= 0.0 || dist >= radius) continue;\n float falloff = 1.0 - dist / radius;\n float wrapped = dot(n3, toLight / max(dist, 1e-4)) * 0.5 + 0.5;\n lit += uLightColor[i] * falloff * falloff * wrapped * uLightWeight[i];\n }\n\n vec3 shaded = vColor * lit;\n float fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);\n outColor = vec4(applyOutputTransform(mix(shaded, mediumColor(), fog)), mask * vAlpha);\n}\n";
50
+ export declare const PARTICLE_SMOKE_FRAG = "#version 300 es\nprecision highp float;\n\nin vec2 vUv;\nin vec3 vColor;\nin float vAlpha;\nin float vAge;\nin float vSeed;\nin vec3 vWorldPos;\nin vec3 vNormal;\n\nuniform float uTime;\nuniform vec3 uDirectionalDir;\nuniform vec3 uDirectionalColor;\nuniform vec3 uAmbient;\nuniform vec3 uCameraPos;\nuniform int uNoiseOctaves;\n/** How strongly the noise eats into the puff. 0 is a plain soft ball. */\nuniform float uErosion;\n\n#define MAX_LIGHTS 16\nuniform int uLightCount;\nuniform vec3 uLightPos[MAX_LIGHTS];\nuniform vec3 uLightColor[MAX_LIGHTS];\nuniform float uLightRadius[MAX_LIGHTS];\n// How present each light is, 0 to 1. See pointLightSelection.ts.\nuniform float uLightWeight[MAX_LIGHTS];\n\n\nuniform vec3 uFogColor;\n/**\n * The haze: its extinction per metre **at the camera's own height**, not at the base; its\n * reciprocal scale height per metre, 0 for a uniform medium; the camera's height, so a fragment's\n * own height is one subtraction away; and the water column's own density.\n *\n * **Four numbers in one vector rather than four uniforms**, and the reason is a phone: a scalar\n * costs a whole row of the uniform grid, and the lit stage at its eight-light rung must fit the 256\n * rows an Adreno 740 offers (uniformVectorBudget.ts). Packed, the haze's eight scalars cost two\n * rows where they cost eight, which is what paid for the second layer and the sun's glow below.\n */\nuniform vec4 uFogMedium;\n/** Where haze begins, where a linear ramp is total, and the mode: 0 a medium, 1 a ramp. */\nuniform vec4 uFogRange;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFactor;\n/**\n * The second layer's extinction per metre at the camera's height and its falloff; then the least of\n * a surface the haze must leave, Atmosphere.fogMaxOpacity's complement, and the distance past which\n * it hides nothing (0 is none). **Every lane is none at zero**, and so are uFogGlow's: a program a\n * binder never wrote these into draws the haze it drew before they existed, rather than none.\n */\nuniform vec4 uFogLayer2;\n/** The sun's glow in the haze: its colour, black for none, and the exponent of its lobe. */\nuniform vec4 uFogGlow;\n/** Surface \u2192 the glow's light, and how far past the haze's start the glow begins. */\nuniform vec4 uFogGlowDir;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * The haze's optical depth from where it begins along the ray to a point at height pointY, dist\n * metres away: each layer's density at the eye carried over what is left of the ray, with the\n * second layer only where there is one. The fog and the sun's glow both integrate it.\n */\nfloat hazeDepth(float dist, float pointY) {\n // Where the medium begins along the ray: uFogRange.x, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogRange.x, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogMedium.z) * uFogMedium.y;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float depth = uFogMedium.x * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom;\n if (uFogLayer2.x > 0.0) {\n float t2 = (pointY - uFogMedium.z) * uFogLayer2.y;\n float rest2 = t2 * (1.0 - before);\n float denom2 = abs(rest2) < 1e-4 ? 1e-4 : rest2;\n depth += uFogLayer2.x * exp(-t2 * before) * (dist - start) * (1.0 - exp(-denom2)) / denom2;\n }\n return depth;\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /* Past the cutoff the air hides nothing, in either mode; the water column keeps its own law. */\n bool cut = uFogLayer2.w > 0.0 && dist > uFogLayer2.w;\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogRange.z == 1.0) {\n float span = max(uFogRange.y - uFogRange.x, 1e-4);\n float ramp = cut ? 0.0 : min(clamp((dist - uFogRange.x) / span, 0.0, 1.0), 1.0 - uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uFogMedium.w * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Beer-Lambert through both layers, leaving at least uFogLayer2.z of the surface: a floor on\n // what survives rather than a ceiling on the depth, so the curve keeps its shape above it.\n float air = cut ? 0.0 : 1.0 - max(exp(-hazeDepth(dist, pointY)), uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uFogMedium.w * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n/**\n * The sun's light the haze scatters toward the eye along a ray to a point at height pointY, dist\n * metres away in the direction toPoint: added to a surface after its fog, never mixed. A lobe of\n * cos^exponent about the light, by as much of the ray past the glow's own start as the haze hides \u2014\n * the haze's depth per metre carried over that stretch. None in a linear ramp, past the cutoff, or\n * under water, which keeps its own medium.\n */\nvec3 mediumGlow(float dist, float pointY, vec3 toPoint) {\n if (uFogRange.z == 1.0 || max(max(uFogGlow.r, uFogGlow.g), uFogGlow.b) <= 0.0) return vec3(0.0);\n if (uFogLayer2.w > 0.0 && dist > uFogLayer2.w) return vec3(0.0);\n float span = dist - min(uFogRange.x, dist);\n if (span <= 0.0) return vec3(0.0);\n float perMetre = hazeDepth(dist, pointY) / span;\n float hidden = 1.0 - exp(-perMetre * max(span - uFogGlowDir.w, 0.0));\n float lobe = pow(clamp(dot(toPoint, uFogGlowDir.xyz), 0.0, 1.0), uFogGlow.a);\n return uFogGlow.rgb * (lobe * hidden * (1.0 - uUnderwaterFactor));\n}\n\n\nfloat hash21(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);\n}\n\nfloat valueNoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = fract(p);\n vec2 u = f * f * (3.0 - 2.0 * f);\n return mix(\n mix(hash21(i), hash21(i + vec2(1.0, 0.0)), u.x),\n mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0, 1.0)), u.x),\n u.y\n );\n}\n\nfloat billow(vec2 p, float t, int octaves) {\n float v = valueNoise(p * 2.4 + vec2(0.0, -t * 0.5)) * 0.62;\n if (octaves == 1) return v / 0.62;\n v += valueNoise(p * 5.1 + vec2(t * 0.15, -t * 0.8)) * 0.38;\n return v / 1.0;\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n /*\n * A round, soft body first. 'smoothstep' rather than a hard disc: the edge is\n * the entire difference between a puff and a sprite, and a cut-off circle reads\n * as a decal however good the noise inside it is.\n */\n float r = length(vUv);\n float body = 1.0 - smoothstep(0.35, 1.0, r);\n if (body <= 0.0) discard;\n\n /*\n * Then eaten into by noise that drifts and coarsens as the puff ages, so it\n * dissipates by *breaking up* rather than by uniformly thinning. Uniform\n * thinning is what a fading sprite does, and it is what the old cube did.\n */\n float t = uTime * 0.55 + vSeed * 7.31;\n float n = billow(vUv * (0.9 + vAge * 1.4) + vec2(vSeed * 3.7, 0.0), t, uNoiseOctaves);\n float erode = mix(1.0, n, uErosion * (0.35 + vAge * 0.65));\n float mask = clamp(body * erode, 0.0, 1.0);\n // Feather the last of it out rather than letting a noise threshold flicker.\n mask *= smoothstep(0.0, 0.25, mask);\n if (mask <= 0.002) discard;\n\n vec3 n3 = normalize(vNormal);\n /*\n * Wrapped diffuse. Smoke transmits as well as reflects, so the terminator on a\n * puff sits well past 90\u00B0 \u2014 a hard 'max(dot, 0)' gives a lump with a black side,\n * which is exactly the \"coloured box\" failure the fidelity rules name.\n */\n float ndl = dot(n3, uDirectionalDir) * 0.5 + 0.5;\n vec3 lit = uAmbient + uDirectionalColor * ndl;\n\n for (int i = 0; i < MAX_LIGHTS; i++) {\n if (i >= uLightCount) break;\n vec3 toLight = uLightPos[i] - vWorldPos;\n float dist = length(toLight);\n float radius = uLightRadius[i];\n if (radius <= 0.0 || dist >= radius) continue;\n float falloff = 1.0 - dist / radius;\n float wrapped = dot(n3, toLight / max(dist, 1e-4)) * 0.5 + 0.5;\n lit += uLightColor[i] * falloff * falloff * wrapped * uLightWeight[i];\n }\n\n vec3 shaded = vColor * lit;\n float dist = distance(vWorldPos, uCameraPos);\n float fog = mediumFog(dist, vWorldPos.y);\n vec3 glow = mediumGlow(dist, vWorldPos.y, normalize(vWorldPos - uCameraPos));\n outColor = vec4(applyOutputTransform(mix(shaded, mediumColor(), fog) + glow), mask * vAlpha);\n}\n";
51
51
  /**
52
52
  * Sparks and embers: additive, hot in the middle, and stretched along travel.
53
53
  *
@@ -55,7 +55,7 @@ export declare const PARTICLE_SMOKE_FRAG = "#version 300 es\nprecision highp flo
55
55
  * is what turned the first version into a grey cube at night. The core is allowed
56
56
  * past white so it reads as incandescent rather than as a pale dot.
57
57
  */
58
- export declare const PARTICLE_SPARK_FRAG = "#version 300 es\nprecision highp float;\n\nin vec2 vUv;\nin vec3 vColor;\nin float vAlpha;\nin float vAge;\nin float vSeed;\nin vec3 vWorldPos;\nin vec3 vNormal;\n\nuniform float uTime;\nuniform vec3 uCameraPos;\n/** How far past white the centre may go. */\nuniform float uCoreGain;\n\nuniform vec3 uFogColor;\n/** Extinction per metre **at the camera's own height**, not at the base. */\nuniform float uFogDensity;\n/** Reciprocal scale height of the haze, per metre. 0 is a uniform medium. */\nuniform float uFogHeightFalloff;\n/** The camera's height, so a fragment's own height is one subtraction away. */\nuniform float uFogEyeY;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFogDensity;\nuniform float uUnderwaterFactor;\n/** 0 integrates a medium (Beer-Lambert); 1 ramps linearly between near and far. */\nuniform int uFogMode;\n/** Linear mode only: where haze begins, and where it is total. */\nuniform float uFogNear;\nuniform float uFogFar;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogMode == 1) {\n float span = max(uFogFar - uFogNear, 1e-4);\n float ramp = clamp((dist - uFogNear) / span, 0.0, 1.0);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uUnderwaterFogDensity * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Where the medium begins along the ray: uFogNear, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogNear, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogEyeY) * uFogHeightFalloff;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float air =\n 1.0 - exp(-uFogDensity * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uUnderwaterFogDensity * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n\nfloat hash21(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);\n}\n\nfloat valueNoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = fract(p);\n vec2 u = f * f * (3.0 - 2.0 * f);\n return mix(\n mix(hash21(i), hash21(i + vec2(1.0, 0.0)), u.x),\n mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0, 1.0)), u.x),\n u.y\n );\n}\n\nfloat billow(vec2 p, float t, int octaves) {\n float v = valueNoise(p * 2.4 + vec2(0.0, -t * 0.5)) * 0.62;\n if (octaves == 1) return v / 0.62;\n v += valueNoise(p * 5.1 + vec2(t * 0.15, -t * 0.8)) * 0.38;\n return v / 1.0;\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n float r = length(vUv);\n if (r > 1.0) discard;\n\n /*\n * Two falloffs again \u2014 a tight core and a broad halo \u2014 because one gives either\n * a hard dot or a soft smudge. The halo is what makes a shower of embers read\n * as glowing air rather than as a scatter of points.\n */\n float core = pow(1.0 - clamp(r, 0.0, 1.0), 6.0);\n float halo = pow(1.0 - clamp(r, 0.0, 1.0), 1.6);\n\n /*\n * Embers flicker as they tumble and cool. Per-particle phase from the seed, so\n * a shower crackles instead of pulsing as one body.\n */\n float flick = 0.7 + 0.3 * valueNoise(vec2(uTime * 9.0 + vSeed * 13.0, vSeed));\n\n // Cooling: the last of a spark's life is dimmer as well as smaller.\n float cool = 1.0 - vAge * vAge;\n float energy = (halo * 0.55 + core * uCoreGain) * flick * cool * vAlpha;\n\n float fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);\n energy *= 1.0 - fog;\n /*\n * Alpha 1, energy in the colour \u2014 see the note in 'bolt.ts'. Additive blending is\n * 'rgb * a', so carrying the energy in both channels squares it and quietly halves\n * every emissive effect in the game.\n */\n outColor = vec4(applyOutputTransform(vColor * energy), 1.0);\n}\n";
58
+ export declare const PARTICLE_SPARK_FRAG = "#version 300 es\nprecision highp float;\n\nin vec2 vUv;\nin vec3 vColor;\nin float vAlpha;\nin float vAge;\nin float vSeed;\nin vec3 vWorldPos;\nin vec3 vNormal;\n\nuniform float uTime;\nuniform vec3 uCameraPos;\n/** How far past white the centre may go. */\nuniform float uCoreGain;\n\nuniform vec3 uFogColor;\n/**\n * The haze: its extinction per metre **at the camera's own height**, not at the base; its\n * reciprocal scale height per metre, 0 for a uniform medium; the camera's height, so a fragment's\n * own height is one subtraction away; and the water column's own density.\n *\n * **Four numbers in one vector rather than four uniforms**, and the reason is a phone: a scalar\n * costs a whole row of the uniform grid, and the lit stage at its eight-light rung must fit the 256\n * rows an Adreno 740 offers (uniformVectorBudget.ts). Packed, the haze's eight scalars cost two\n * rows where they cost eight, which is what paid for the second layer and the sun's glow below.\n */\nuniform vec4 uFogMedium;\n/** Where haze begins, where a linear ramp is total, and the mode: 0 a medium, 1 a ramp. */\nuniform vec4 uFogRange;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFactor;\n/**\n * The second layer's extinction per metre at the camera's height and its falloff; then the least of\n * a surface the haze must leave, Atmosphere.fogMaxOpacity's complement, and the distance past which\n * it hides nothing (0 is none). **Every lane is none at zero**, and so are uFogGlow's: a program a\n * binder never wrote these into draws the haze it drew before they existed, rather than none.\n */\nuniform vec4 uFogLayer2;\n/** The sun's glow in the haze: its colour, black for none, and the exponent of its lobe. */\nuniform vec4 uFogGlow;\n/** Surface \u2192 the glow's light, and how far past the haze's start the glow begins. */\nuniform vec4 uFogGlowDir;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * The haze's optical depth from where it begins along the ray to a point at height pointY, dist\n * metres away: each layer's density at the eye carried over what is left of the ray, with the\n * second layer only where there is one. The fog and the sun's glow both integrate it.\n */\nfloat hazeDepth(float dist, float pointY) {\n // Where the medium begins along the ray: uFogRange.x, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogRange.x, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogMedium.z) * uFogMedium.y;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float depth = uFogMedium.x * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom;\n if (uFogLayer2.x > 0.0) {\n float t2 = (pointY - uFogMedium.z) * uFogLayer2.y;\n float rest2 = t2 * (1.0 - before);\n float denom2 = abs(rest2) < 1e-4 ? 1e-4 : rest2;\n depth += uFogLayer2.x * exp(-t2 * before) * (dist - start) * (1.0 - exp(-denom2)) / denom2;\n }\n return depth;\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /* Past the cutoff the air hides nothing, in either mode; the water column keeps its own law. */\n bool cut = uFogLayer2.w > 0.0 && dist > uFogLayer2.w;\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogRange.z == 1.0) {\n float span = max(uFogRange.y - uFogRange.x, 1e-4);\n float ramp = cut ? 0.0 : min(clamp((dist - uFogRange.x) / span, 0.0, 1.0), 1.0 - uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uFogMedium.w * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Beer-Lambert through both layers, leaving at least uFogLayer2.z of the surface: a floor on\n // what survives rather than a ceiling on the depth, so the curve keeps its shape above it.\n float air = cut ? 0.0 : 1.0 - max(exp(-hazeDepth(dist, pointY)), uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uFogMedium.w * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n/**\n * The sun's light the haze scatters toward the eye along a ray to a point at height pointY, dist\n * metres away in the direction toPoint: added to a surface after its fog, never mixed. A lobe of\n * cos^exponent about the light, by as much of the ray past the glow's own start as the haze hides \u2014\n * the haze's depth per metre carried over that stretch. None in a linear ramp, past the cutoff, or\n * under water, which keeps its own medium.\n */\nvec3 mediumGlow(float dist, float pointY, vec3 toPoint) {\n if (uFogRange.z == 1.0 || max(max(uFogGlow.r, uFogGlow.g), uFogGlow.b) <= 0.0) return vec3(0.0);\n if (uFogLayer2.w > 0.0 && dist > uFogLayer2.w) return vec3(0.0);\n float span = dist - min(uFogRange.x, dist);\n if (span <= 0.0) return vec3(0.0);\n float perMetre = hazeDepth(dist, pointY) / span;\n float hidden = 1.0 - exp(-perMetre * max(span - uFogGlowDir.w, 0.0));\n float lobe = pow(clamp(dot(toPoint, uFogGlowDir.xyz), 0.0, 1.0), uFogGlow.a);\n return uFogGlow.rgb * (lobe * hidden * (1.0 - uUnderwaterFactor));\n}\n\n\nfloat hash21(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);\n}\n\nfloat valueNoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = fract(p);\n vec2 u = f * f * (3.0 - 2.0 * f);\n return mix(\n mix(hash21(i), hash21(i + vec2(1.0, 0.0)), u.x),\n mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0, 1.0)), u.x),\n u.y\n );\n}\n\nfloat billow(vec2 p, float t, int octaves) {\n float v = valueNoise(p * 2.4 + vec2(0.0, -t * 0.5)) * 0.62;\n if (octaves == 1) return v / 0.62;\n v += valueNoise(p * 5.1 + vec2(t * 0.15, -t * 0.8)) * 0.38;\n return v / 1.0;\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n float r = length(vUv);\n if (r > 1.0) discard;\n\n /*\n * Two falloffs again \u2014 a tight core and a broad halo \u2014 because one gives either\n * a hard dot or a soft smudge. The halo is what makes a shower of embers read\n * as glowing air rather than as a scatter of points.\n */\n float core = pow(1.0 - clamp(r, 0.0, 1.0), 6.0);\n float halo = pow(1.0 - clamp(r, 0.0, 1.0), 1.6);\n\n /*\n * Embers flicker as they tumble and cool. Per-particle phase from the seed, so\n * a shower crackles instead of pulsing as one body.\n */\n float flick = 0.7 + 0.3 * valueNoise(vec2(uTime * 9.0 + vSeed * 13.0, vSeed));\n\n // Cooling: the last of a spark's life is dimmer as well as smaller.\n float cool = 1.0 - vAge * vAge;\n float energy = (halo * 0.55 + core * uCoreGain) * flick * cool * vAlpha;\n\n float fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);\n energy *= 1.0 - fog;\n /*\n * Alpha 1, energy in the colour \u2014 see the note in 'bolt.ts'. Additive blending is\n * 'rgb * a', so carrying the energy in both channels squares it and quietly halves\n * every emissive effect in the game.\n */\n outColor = vec4(applyOutputTransform(vColor * energy), 1.0);\n}\n";
59
59
  /**
60
60
  * Dust motes and drifting glints: unlit, alpha blended, and fogged only when the caller asks.
61
61
  *
@@ -79,7 +79,7 @@ export declare const PARTICLE_SPARK_FRAG = "#version 300 es\nprecision highp flo
79
79
  * option and stay unconditionally fogged, matching every particle drawn before this material
80
80
  * existed; see `ParticleBatchOptions.fog`.
81
81
  */
82
- export declare const PARTICLE_MOTE_FRAG = "#version 300 es\nprecision highp float;\n\nin vec2 vUv;\nin vec3 vColor;\nin float vAlpha;\nin float vAge;\nin float vSeed;\nin vec3 vWorldPos;\nin vec3 vNormal;\n\nuniform vec3 uCameraPos;\n/**\n * Whether the fragment stage mixes toward the medium's own colour at all. 0 \u2014 what a batch\n * gets by leaving ParticleBatchOptions.fog unset \u2014 leaves vColor untouched by distance, which\n * is what a caller reaching for this material over 'spark'/'smoke' is usually asking for: an\n * unlit point drawn exactly as its own colour says. 1 mixes toward mediumColor() by\n * mediumFog(), the same medium every other pass in the frame recedes into.\n */\nuniform int uFogEnabled;\n\nuniform vec3 uFogColor;\n/** Extinction per metre **at the camera's own height**, not at the base. */\nuniform float uFogDensity;\n/** Reciprocal scale height of the haze, per metre. 0 is a uniform medium. */\nuniform float uFogHeightFalloff;\n/** The camera's height, so a fragment's own height is one subtraction away. */\nuniform float uFogEyeY;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFogDensity;\nuniform float uUnderwaterFactor;\n/** 0 integrates a medium (Beer-Lambert); 1 ramps linearly between near and far. */\nuniform int uFogMode;\n/** Linear mode only: where haze begins, and where it is total. */\nuniform float uFogNear;\nuniform float uFogFar;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogMode == 1) {\n float span = max(uFogFar - uFogNear, 1e-4);\n float ramp = clamp((dist - uFogNear) / span, 0.0, 1.0);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uUnderwaterFogDensity * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Where the medium begins along the ray: uFogNear, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogNear, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogEyeY) * uFogHeightFalloff;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float air =\n 1.0 - exp(-uFogDensity * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uUnderwaterFogDensity * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n /*\n * One soft, round falloff \u2014 no erosion, no core past white, because this material draws\n * neither a body being eaten by noise nor a light source. Squared rather than linear so the\n * edge feathers instead of ramping, which is what keeps a field of these reading as dust\n * rather than as a scatter of hard-edged discs.\n */\n float r = length(vUv);\n float body = 1.0 - smoothstep(0.4, 1.0, r);\n if (body <= 0.0) discard;\n float alpha = body * body * vAlpha;\n if (alpha <= 0.002) discard;\n\n vec3 color = vColor;\n if (uFogEnabled != 0) {\n float fog = mediumFog(distance(vWorldPos, uCameraPos), vWorldPos.y);\n color = mix(color, mediumColor(), fog);\n }\n /*\n * A real alpha blend (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) rather than spark's energy-in-colour\n * trick: this point genuinely occludes what little is behind it instead of adding light to it.\n */\n outColor = vec4(applyOutputTransform(color), alpha);\n}\n";
82
+ export declare const PARTICLE_MOTE_FRAG = "#version 300 es\nprecision highp float;\n\nin vec2 vUv;\nin vec3 vColor;\nin float vAlpha;\nin float vAge;\nin float vSeed;\nin vec3 vWorldPos;\nin vec3 vNormal;\n\nuniform vec3 uCameraPos;\n/**\n * Whether the fragment stage mixes toward the medium's own colour at all. 0 \u2014 what a batch\n * gets by leaving ParticleBatchOptions.fog unset \u2014 leaves vColor untouched by distance, which\n * is what a caller reaching for this material over 'spark'/'smoke' is usually asking for: an\n * unlit point drawn exactly as its own colour says. 1 mixes toward mediumColor() by\n * mediumFog(), the same medium every other pass in the frame recedes into.\n */\nuniform int uFogEnabled;\n\nuniform vec3 uFogColor;\n/**\n * The haze: its extinction per metre **at the camera's own height**, not at the base; its\n * reciprocal scale height per metre, 0 for a uniform medium; the camera's height, so a fragment's\n * own height is one subtraction away; and the water column's own density.\n *\n * **Four numbers in one vector rather than four uniforms**, and the reason is a phone: a scalar\n * costs a whole row of the uniform grid, and the lit stage at its eight-light rung must fit the 256\n * rows an Adreno 740 offers (uniformVectorBudget.ts). Packed, the haze's eight scalars cost two\n * rows where they cost eight, which is what paid for the second layer and the sun's glow below.\n */\nuniform vec4 uFogMedium;\n/** Where haze begins, where a linear ramp is total, and the mode: 0 a medium, 1 a ramp. */\nuniform vec4 uFogRange;\nuniform vec3 uUnderwaterColor;\nuniform float uUnderwaterFactor;\n/**\n * The second layer's extinction per metre at the camera's height and its falloff; then the least of\n * a surface the haze must leave, Atmosphere.fogMaxOpacity's complement, and the distance past which\n * it hides nothing (0 is none). **Every lane is none at zero**, and so are uFogGlow's: a program a\n * binder never wrote these into draws the haze it drew before they existed, rather than none.\n */\nuniform vec4 uFogLayer2;\n/** The sun's glow in the haze: its colour, black for none, and the exponent of its lobe. */\nuniform vec4 uFogGlow;\n/** Surface \u2192 the glow's light, and how far past the haze's start the glow begins. */\nuniform vec4 uFogGlowDir;\n\n/** What distance converges toward: the day's haze, or the water column. */\nvec3 mediumColor() {\n return mix(uFogColor, uUnderwaterColor, uUnderwaterFactor);\n}\n\n/**\n * The haze's optical depth from where it begins along the ray to a point at height pointY, dist\n * metres away: each layer's density at the eye carried over what is left of the ray, with the\n * second layer only where there is one. The fog and the sun's glow both integrate it.\n */\nfloat hazeDepth(float dist, float pointY) {\n // Where the medium begins along the ray: uFogRange.x, the linear mode's slot, which this mode\n // never read before Atmosphere.fogStart. The ray is integrated from there on, at the density\n // of that point; at a start of 0 every term is the one it always was. See render/fog.ts.\n float start = min(uFogRange.x, dist);\n float before = dist > 0.0 ? start / dist : 0.0;\n float t = (pointY - uFogMedium.z) * uFogMedium.y;\n float rest = t * (1.0 - before);\n // (1 - e^-t)/t, which tends to 1 as the ray levels out. Guarded rather than\n // branched: at t = 0 the quotient is 0/0, and 1e-4 of a scale height is 8 mm.\n float denom = abs(rest) < 1e-4 ? 1e-4 : rest;\n float depth = uFogMedium.x * exp(-t * before) * (dist - start) * (1.0 - exp(-denom)) / denom;\n if (uFogLayer2.x > 0.0) {\n float t2 = (pointY - uFogMedium.z) * uFogLayer2.y;\n float rest2 = t2 * (1.0 - before);\n float denom2 = abs(rest2) < 1e-4 ? 1e-4 : rest2;\n depth += uFogLayer2.x * exp(-t2 * before) * (dist - start) * (1.0 - exp(-denom2)) / denom2;\n }\n return depth;\n}\n\n/**\n * How much of a surface standing at height pointY, dist metres away, is lost to\n * the medium in between. 0 is perfectly clear, 1 is the medium's own colour.\n */\nfloat mediumFog(float dist, float pointY) {\n /* Past the cutoff the air hides nothing, in either mode; the water column keeps its own law. */\n bool cut = uFogLayer2.w > 0.0 && dist > uFogLayer2.w;\n /*\n * A depth ramp rather than a medium, for worlds authored against one.\n *\n * Beer-Lambert is the honest model and stays the default: it integrates something\n * physically there, so extinction begins at the eye and never stops. A linear near/far\n * fog is not that \u2014 it is a *look*, with a hard start distance before which the air is\n * perfectly clear, and it is what the fixed-function pipeline offered and what several\n * renderers still expose. Approximating one with the other is not a matter of tuning:\n * matched at half occlusion, the exponential curve still lays haze over everything\n * inside near, where the ramp lays none, and near-field colour desaturates toward the\n * fog colour with nothing to show for it.\n *\n * Height falloff has no meaning here and is ignored: a ramp is defined on distance\n * alone.\n */\n if (uFogRange.z == 1.0) {\n float span = max(uFogRange.y - uFogRange.x, 1e-4);\n float ramp = cut ? 0.0 : min(clamp((dist - uFogRange.x) / span, 0.0, 1.0), 1.0 - uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return ramp;\n float wetLinear = uFogMedium.w * dist;\n return mix(ramp, 1.0 - exp2(-wetLinear * wetLinear * 1.442695), uUnderwaterFactor);\n }\n\n // Beer-Lambert through both layers, leaving at least uFogLayer2.z of the surface: a floor on\n // what survives rather than a ceiling on the depth, so the curve keeps its shape above it.\n float air = cut ? 0.0 : 1.0 - max(exp(-hazeDepth(dist, pointY)), uFogLayer2.z);\n if (uUnderwaterFactor <= 0.0) return air;\n // Water is its own medium and keeps its own curve, so crossing the surface\n // changes nothing about how the water column reads.\n float wet = uFogMedium.w * dist;\n return mix(air, 1.0 - exp2(-wet * wet * 1.442695), uUnderwaterFactor);\n}\n\n/**\n * The sun's light the haze scatters toward the eye along a ray to a point at height pointY, dist\n * metres away in the direction toPoint: added to a surface after its fog, never mixed. A lobe of\n * cos^exponent about the light, by as much of the ray past the glow's own start as the haze hides \u2014\n * the haze's depth per metre carried over that stretch. None in a linear ramp, past the cutoff, or\n * under water, which keeps its own medium.\n */\nvec3 mediumGlow(float dist, float pointY, vec3 toPoint) {\n if (uFogRange.z == 1.0 || max(max(uFogGlow.r, uFogGlow.g), uFogGlow.b) <= 0.0) return vec3(0.0);\n if (uFogLayer2.w > 0.0 && dist > uFogLayer2.w) return vec3(0.0);\n float span = dist - min(uFogRange.x, dist);\n if (span <= 0.0) return vec3(0.0);\n float perMetre = hazeDepth(dist, pointY) / span;\n float hidden = 1.0 - exp(-perMetre * max(span - uFogGlowDir.w, 0.0));\n float lobe = pow(clamp(dot(toPoint, uFogGlowDir.xyz), 0.0, 1.0), uFogGlow.a);\n return uFogGlow.rgb * (lobe * hidden * (1.0 - uUnderwaterFactor));\n}\n\n\nout vec4 outColor;\n\n/** 0 none, 1 sRGB, 2 ACES then sRGB, 3 the highlight shoulder then sRGB. See renderQuality.ts. */\nuniform int uOutputTransform;\n/** Scales the scene into the tone curve. 1 is the reference grade. See renderQuality.ts. */\nuniform float uOutputExposure;\n\n/*\n * ACES: the reference fit, not the cheap one.\n *\n * This was Narkowicz's single rational function, on the argument that the difference from\n * the reference curve is invisible next to the gain. That argument holds for a look and\n * fails for a *match*, which is what this option is for \u2014 aces exists so a world ported\n * from three.js reads the same, and three.js applies the full fit: into AP1 through\n * ACESInputMat, the RRT and ODT together, then back out through ACESOutputMat.\n *\n * The two curves are not a shade apart. Narkowicz is markedly brighter through the\n * midtones, which is exactly where an interior scene lives, and it is a large part of a\n * measured ~1.7x that survived correcting the colour space and the missing Lambert pi.\n * An approximation of a standard is the wrong thing to hold when the standard is the\n * specification.\n */\nvec3 rrtAndOdtFit(vec3 v) {\n vec3 a = v * (v + 0.0245786) - 0.000090537;\n vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;\n return a / b;\n}\n\nvec3 acesFilmic(vec3 x) {\n /*\n * Exposure first, because a tone curve is only defined relative to one.\n *\n * The reference RRT and ODT are graded for a dark cinema. Every renderer that ships ACES\n * scales its input to suit whatever it is actually shown on, and the factor each one\n * picks is a *look*, not a standard - so this engine takes no position and reads it from\n * the caller. Neutral by default, which is the reference grade unmodified. three.js's own\n * pick is 1 / 0.6, for a consumer matching one.\n */\n x *= uOutputExposure;\n // Columns, as GLSL reads them: sRGB -> XYZ -> D65_2_D60 -> AP1 -> RRT_SAT.\n const mat3 ACES_INPUT = mat3(\n 0.59719, 0.07600, 0.02840,\n 0.35458, 0.90834, 0.13383,\n 0.04823, 0.01566, 0.83777\n );\n // ODT_SAT -> XYZ -> D60_2_D65 -> sRGB.\n const mat3 ACES_OUTPUT = mat3(\n 1.60475, -0.10208, -0.00327,\n -0.53108, 1.10813, -0.07276,\n -0.07367, -0.00605, 1.07602\n );\n return clamp(ACES_OUTPUT * rrtAndOdtFit(ACES_INPUT * x), 0.0, 1.0);\n}\n\n\n/*\n * Linear to sRGB. The piecewise form rather than a plain pow: the linear toe near black\n * is where banding lives in an 8-bit buffer, and the cheap approximation puts it in the\n * wrong place.\n */\nvec3 linearToSrgb(vec3 c) {\n vec3 low = c * 12.92;\n vec3 high = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055;\n return mix(high, low, step(c, vec3(0.0031308)));\n}\n\n\nvec3 highlightShoulder(vec3 c) {\n float m = max(c.r, max(c.g, c.b));\n if (m <= 0.8) return c;\n float e = m - 0.8;\n return c * ((0.8 + 0.2 * e / (e + 0.2)) / m);\n}\n\nvec3 applyOutputTransform(vec3 c) {\n if (uOutputTransform == 0) return c;\n if (uOutputTransform == 2) c = acesFilmic(c);\n if (uOutputTransform == 3) c = highlightShoulder(c * uOutputExposure);\n return linearToSrgb(c);\n}\n\n\nvoid main() {\n /*\n * One soft, round falloff \u2014 no erosion, no core past white, because this material draws\n * neither a body being eaten by noise nor a light source. Squared rather than linear so the\n * edge feathers instead of ramping, which is what keeps a field of these reading as dust\n * rather than as a scatter of hard-edged discs.\n */\n float r = length(vUv);\n float body = 1.0 - smoothstep(0.4, 1.0, r);\n if (body <= 0.0) discard;\n float alpha = body * body * vAlpha;\n if (alpha <= 0.002) discard;\n\n vec3 color = vColor;\n if (uFogEnabled != 0) {\n float dist = distance(vWorldPos, uCameraPos);\n float fog = mediumFog(dist, vWorldPos.y);\n color = mix(color, mediumColor(), fog) + mediumGlow(dist, vWorldPos.y, normalize(vWorldPos - uCameraPos));\n }\n /*\n * A real alpha blend (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) rather than spark's energy-in-colour\n * trick: this point genuinely occludes what little is behind it instead of adding light to it.\n */\n outColor = vec4(applyOutputTransform(color), alpha);\n}\n";
83
83
  /**
84
84
  * A textured sprite: an image a consumer supplies, a cell of it at a time, at the colour and opacity
85
85
  * each particle carries — what another engine's sprite renderer draws, and the material the three