@threenative/terrain 0.1.11 → 0.1.12

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 (218) hide show
  1. package/README.md +1 -0
  2. package/dist/bake-DJ5cxg0C.d.ts +82 -0
  3. package/dist/editor/index.d.ts +4 -4
  4. package/dist/editor/index.js +1 -1
  5. package/dist/editor/server.d.ts +2 -2
  6. package/dist/editor/worker.d.ts +1 -1
  7. package/dist/editor/worker.js +1 -1
  8. package/dist/export.d.ts +1 -1
  9. package/dist/index.d.ts +51 -222
  10. package/dist/index.js +111 -2
  11. package/dist/kit-world.d.ts +107 -0
  12. package/dist/kit-world.js +5134 -0
  13. package/dist/kit.d.ts +21 -4
  14. package/dist/kit.js +19 -4
  15. package/dist/mcp/cook-child.js +9 -6
  16. package/dist/mcp/server.js +14 -4
  17. package/dist/prop-package-Dwz0EWoB.d.ts +141 -0
  18. package/dist/{server-B5RdvEAC.d.ts → server-LnFwY8sr.d.ts} +1 -1
  19. package/dist/starter.d.ts +1141 -6
  20. package/dist/starter.js +1999 -17
  21. package/dist/{terrain-CAEaW0mQ.d.ts → terrain-DYCyAOCJ.d.ts} +1 -1
  22. package/dist/three.d.ts +1 -1
  23. package/dist/{types-BpH00qsx.d.ts → types-kpI0C4hW.d.ts} +1 -1
  24. package/dist/{world-package-B8QgHjAQ.d.ts → world-package-DqJC3-Nn.d.ts} +2 -2
  25. package/docs/authoring/new-environment.md +97 -0
  26. package/package.json +8 -8
  27. package/starter/alpine/README.md +22 -14
  28. package/starter/alpine/bake.mjs +14 -1
  29. package/starter/alpine/biome.data.json +187 -25
  30. package/starter/alpine/heightmap.r16 +0 -0
  31. package/starter/alpine/playerRig.ts +89 -0
  32. package/starter/alpine/recipe.json +169 -15
  33. package/starter/alpine/render/bakedFile.ts +17 -0
  34. package/starter/alpine/render/benchmark.ts +77 -0
  35. package/starter/alpine/render/biomeData.ts +14 -0
  36. package/starter/alpine/render/biomes.ts +687 -0
  37. package/starter/alpine/render/ground/cover.ts +565 -0
  38. package/starter/alpine/render/ground/horizon.ts +219 -0
  39. package/starter/alpine/render/ground/palette.d.ts +1 -0
  40. package/starter/alpine/render/ground/palette.js +11 -0
  41. package/starter/alpine/render/ground/scatter.ts +104 -0
  42. package/starter/alpine/render/ground/terrain.ts +1860 -0
  43. package/starter/alpine/render/kitAssets.ts +28 -0
  44. package/starter/alpine/render/loading.ts +354 -0
  45. package/starter/alpine/render/lookKnobs.ts +37 -0
  46. package/starter/alpine/render/orbitCamera.ts +155 -0
  47. package/starter/alpine/render/props/farForest.ts +94 -0
  48. package/starter/alpine/render/props/pack.ts +1114 -0
  49. package/starter/alpine/render/props/prepared.ts +225 -0
  50. package/starter/alpine/render/props/propMaterials.ts +1239 -0
  51. package/starter/alpine/render/props/propProgress.ts +2 -0
  52. package/starter/alpine/render/props/propWorld.ts +105 -0
  53. package/starter/alpine/render/props/props.ts +633 -0
  54. package/starter/alpine/render/props/spruce.ts +440 -0
  55. package/starter/alpine/render/sky/atmosphere.ts +329 -0
  56. package/starter/alpine/render/sky/sky.ts +382 -0
  57. package/starter/alpine/render/texturePreparation.ts +135 -0
  58. package/starter/alpine/render/water/river.ts +1132 -0
  59. package/starter/alpine/render/worldNames.ts +6 -0
  60. package/starter/alpine/scene.ts +715 -0
  61. package/starter/alpine/sceneProps.ts +561 -0
  62. package/starter/alpine/sceneState.ts +33 -0
  63. package/starter/alpine/world/terrainAssets.ts +145 -0
  64. package/starter/alpine/world/walkBand.ts +47 -0
  65. package/starter/alpine/world/water.ts +74 -0
  66. package/starter/alpine/worlds.ts +13 -0
  67. package/starter/coastal/README.md +26 -27
  68. package/starter/coastal/assets.json +10 -0
  69. package/starter/coastal/bake.mjs +14 -1
  70. package/starter/coastal/biome.data.json +18 -3
  71. package/starter/coastal/playerRig.ts +89 -0
  72. package/starter/coastal/recipe.json +220 -18
  73. package/starter/coastal/render/bakedFile.ts +17 -0
  74. package/starter/coastal/render/benchmark.ts +81 -0
  75. package/starter/coastal/render/biomeData.ts +14 -0
  76. package/starter/coastal/render/biomes.ts +678 -0
  77. package/starter/coastal/render/ground/cover.ts +565 -0
  78. package/starter/coastal/render/ground/horizon.ts +219 -0
  79. package/starter/coastal/render/ground/palette.d.ts +1 -0
  80. package/starter/coastal/render/ground/palette.js +11 -0
  81. package/starter/coastal/render/ground/scatter.ts +104 -0
  82. package/starter/coastal/render/ground/terrain.ts +1829 -0
  83. package/starter/coastal/render/kitAssets.ts +28 -0
  84. package/starter/coastal/render/loading.ts +354 -0
  85. package/starter/coastal/render/lookKnobs.ts +37 -0
  86. package/starter/coastal/render/orbitCamera.ts +155 -0
  87. package/starter/coastal/render/props/farForest.ts +94 -0
  88. package/starter/coastal/render/props/pack.ts +1109 -0
  89. package/starter/coastal/render/props/prepared.ts +225 -0
  90. package/starter/coastal/render/props/propMaterials.ts +1239 -0
  91. package/starter/coastal/render/props/propProgress.ts +2 -0
  92. package/starter/coastal/render/props/propWorld.ts +105 -0
  93. package/starter/coastal/render/props/props.ts +633 -0
  94. package/starter/coastal/render/props/spruce.ts +440 -0
  95. package/starter/coastal/render/sky/atmosphere.ts +329 -0
  96. package/starter/coastal/render/sky/sky.ts +382 -0
  97. package/starter/coastal/render/texturePreparation.ts +135 -0
  98. package/starter/coastal/render/water/ocean.ts +284 -0
  99. package/starter/coastal/render/water/river.ts +1132 -0
  100. package/starter/coastal/render/worldNames.ts +6 -0
  101. package/starter/coastal/scene.ts +740 -0
  102. package/starter/coastal/sceneProps.ts +559 -0
  103. package/starter/coastal/sceneState.ts +33 -0
  104. package/starter/coastal/world/terrainAssets.ts +151 -0
  105. package/starter/coastal/world/walkBand.ts +47 -0
  106. package/starter/coastal/world/water.ts +74 -0
  107. package/starter/coastal/worlds.ts +13 -0
  108. package/starter/desert/README.md +51 -25
  109. package/starter/desert/bake-flora.mjs +142 -0
  110. package/starter/desert/bake-rocks.mjs +291 -0
  111. package/starter/desert/bake.mjs +14 -1
  112. package/starter/desert/biome.data.json +24 -12
  113. package/starter/desert/playerRig.ts +89 -0
  114. package/starter/desert/recipe.json +63 -16
  115. package/starter/desert/render/bakedFile.ts +17 -0
  116. package/starter/desert/render/benchmark.ts +79 -0
  117. package/starter/desert/render/biomeData.ts +14 -0
  118. package/starter/desert/render/biomes.ts +660 -0
  119. package/starter/desert/render/ground/cover.ts +565 -0
  120. package/starter/desert/render/ground/debris.ts +136 -0
  121. package/starter/desert/render/ground/horizon.ts +219 -0
  122. package/starter/desert/render/ground/palette.d.ts +1 -0
  123. package/starter/desert/render/ground/palette.js +11 -0
  124. package/starter/desert/render/ground/scatter.ts +104 -0
  125. package/starter/desert/render/ground/terrain.ts +1854 -0
  126. package/starter/desert/render/kitAssets.ts +28 -0
  127. package/starter/desert/render/loading.ts +354 -0
  128. package/starter/desert/render/lookKnobs.ts +37 -0
  129. package/starter/desert/render/orbitCamera.ts +155 -0
  130. package/starter/desert/render/props/farForest.ts +96 -0
  131. package/starter/desert/render/props/formations.ts +630 -0
  132. package/starter/desert/render/props/pack.ts +1195 -0
  133. package/starter/desert/render/props/prepared.ts +225 -0
  134. package/starter/desert/render/props/propMaterials.ts +1255 -0
  135. package/starter/desert/render/props/propProgress.ts +2 -0
  136. package/starter/desert/render/props/propWorld.ts +105 -0
  137. package/starter/desert/render/props/props.ts +624 -0
  138. package/starter/desert/render/props/spruce.ts +440 -0
  139. package/starter/desert/render/sand.ts +581 -0
  140. package/starter/desert/render/sky/atmosphere.ts +329 -0
  141. package/starter/desert/render/sky/sky.ts +382 -0
  142. package/starter/desert/render/texturePreparation.ts +135 -0
  143. package/starter/desert/render/water/river.ts +1132 -0
  144. package/starter/desert/render/worldNames.ts +6 -0
  145. package/starter/desert/scene.ts +748 -0
  146. package/starter/desert/sceneProps.ts +562 -0
  147. package/starter/desert/sceneState.ts +33 -0
  148. package/starter/desert/world/terrainAssets.ts +162 -0
  149. package/starter/desert/world/walkBand.ts +47 -0
  150. package/starter/desert/world/water.ts +74 -0
  151. package/starter/desert/worlds.ts +13 -0
  152. package/starter/forest/README.md +8 -5
  153. package/starter/forest/bake.mjs +21 -413
  154. package/starter/forest/playerRig.ts +37 -171
  155. package/starter/forest/recipe.json +237 -47
  156. package/starter/forest/render/biomes.ts +2 -1
  157. package/starter/forest/render/ground/horizon.ts +4 -302
  158. package/starter/forest/render/ground/terrain.ts +14 -32
  159. package/starter/forest/render/kitAssets.ts +3 -2
  160. package/starter/forest/render/props/farForest.ts +2 -1
  161. package/starter/forest/render/props/pack.ts +4 -2
  162. package/starter/forest/render/props/prepared.ts +2 -1
  163. package/starter/forest/render/props/propProgress.ts +1 -11
  164. package/starter/forest/render/props/propWorld.ts +48 -662
  165. package/starter/forest/render/props/props.ts +64 -248
  166. package/starter/forest/render/sky/sky.ts +2 -1
  167. package/starter/forest/render/worldNames.ts +6 -2
  168. package/starter/forest/scene.ts +34 -25
  169. package/starter/forest/sceneProps.ts +2 -2
  170. package/starter/forest/sceneState.ts +5 -4
  171. package/starter/forest/surface.json +2 -2
  172. package/starter/forest/worlds.ts +3 -2
  173. package/starter/tundra/README.md +35 -16
  174. package/starter/tundra/bake.mjs +14 -1
  175. package/starter/tundra/biome.data.json +6 -6
  176. package/starter/tundra/playerRig.ts +89 -0
  177. package/starter/tundra/recipe.json +111 -110
  178. package/starter/tundra/render/bakedFile.ts +17 -0
  179. package/starter/tundra/render/benchmark.ts +79 -0
  180. package/starter/tundra/render/biomeData.ts +14 -0
  181. package/starter/tundra/render/biomes.ts +714 -0
  182. package/starter/tundra/render/ground/cover.ts +565 -0
  183. package/starter/tundra/render/ground/horizon.ts +219 -0
  184. package/starter/tundra/render/ground/palette.d.ts +1 -0
  185. package/starter/tundra/render/ground/palette.js +11 -0
  186. package/starter/tundra/render/ground/scatter.ts +104 -0
  187. package/starter/tundra/render/ground/terrain.ts +1874 -0
  188. package/starter/tundra/render/kitAssets.ts +28 -0
  189. package/starter/tundra/render/loading.ts +354 -0
  190. package/starter/tundra/render/lookKnobs.ts +37 -0
  191. package/starter/tundra/render/orbitCamera.ts +155 -0
  192. package/starter/tundra/render/props/farForest.ts +96 -0
  193. package/starter/tundra/render/props/pack.ts +1088 -0
  194. package/starter/tundra/render/props/prepared.ts +225 -0
  195. package/starter/tundra/render/props/propMaterials.ts +1247 -0
  196. package/starter/tundra/render/props/propProgress.ts +2 -0
  197. package/starter/tundra/render/props/propWorld.ts +105 -0
  198. package/starter/tundra/render/props/props.ts +624 -0
  199. package/starter/tundra/render/props/spruce.ts +440 -0
  200. package/starter/tundra/render/sky/atmosphere.ts +329 -0
  201. package/starter/tundra/render/sky/sky.ts +382 -0
  202. package/starter/tundra/render/texturePreparation.ts +135 -0
  203. package/starter/tundra/render/water/river.ts +1132 -0
  204. package/starter/tundra/render/worldNames.ts +6 -0
  205. package/starter/tundra/scene.ts +715 -0
  206. package/starter/tundra/sceneProps.ts +559 -0
  207. package/starter/tundra/sceneState.ts +33 -0
  208. package/starter/tundra/world/terrainAssets.ts +159 -0
  209. package/starter/tundra/world/walkBand.ts +47 -0
  210. package/starter/tundra/world/water.ts +74 -0
  211. package/starter/tundra/worlds.ts +13 -0
  212. package/starter-assets.json +4 -4
  213. package/starter-credits.json +613 -10
  214. package/starter/forest/diagnostics.ts +0 -1020
  215. package/starter/forest/render/sky/skyVisibility.ts +0 -121
  216. package/starter/forest/spawnGate.ts +0 -183
  217. package/starter/forest/viewStatistics.ts +0 -47
  218. package/starter/forest/world/streamedGround.ts +0 -139
@@ -0,0 +1,329 @@
1
+ /** Game-owned air; the installed Atmosphere owns LUT compute and lifetime. */
2
+ import { Atmosphere } from "@threenative/core";
3
+ import { BackSide } from "three";
4
+ import {
5
+ cameraPosition,
6
+ color,
7
+ dot,
8
+ float,
9
+ max,
10
+ mix,
11
+ mx_fractal_noise_float,
12
+ normalize,
13
+ output,
14
+ positionLocal,
15
+ positionWorld,
16
+ pow,
17
+ select,
18
+ smoothstep,
19
+ vec2,
20
+ vec3,
21
+ vec4,
22
+ } from "three/tsl";
23
+ import { MeshBasicNodeMaterial, type Node } from "three/webgpu";
24
+ import type { IBiome } from "../biomes.js";
25
+ import { LOOK } from "../lookKnobs.js";
26
+
27
+ export function createAtmosphere(look: IBiome): Atmosphere {
28
+ const air = new Atmosphere({
29
+ rayleigh: look.atmosphere.rayleigh,
30
+ mie: [look.atmosphere.mie, look.atmosphere.mie, look.atmosphere.mie],
31
+ ozone: [0.00065, 0.001881, 0.000085],
32
+ planetRadius: 6360,
33
+ atmosphereRadius: 6460,
34
+ });
35
+ air.name = "world-atmosphere";
36
+ return air;
37
+ }
38
+
39
+ /** Same spectral extinction and solar phase on the dome and the distant landscape. */
40
+ export function skyRadiance(
41
+ air: Atmosphere,
42
+ direction: Node<"vec3">,
43
+ sun: Node<"vec3">,
44
+ look: IBiome,
45
+ ): Node<"vec3"> {
46
+ const mu = dot(direction, sun).clamp(-1, 1);
47
+ const elevation = direction.y.max(0.035);
48
+ const transmission = air.sunTransmittance(
49
+ vec3(0, elevation, float(1).sub(elevation.mul(elevation)).max(0).sqrt()),
50
+ ) as Node<"vec3">;
51
+ // Absorption removes light; only the scattering share can become sky radiance.
52
+ const parameters = air.parameters;
53
+ const rayleighColumn = vec3(parameters.rayleigh).mul(8);
54
+ const scatteringShare = rayleighColumn.div(
55
+ rayleighColumn.add(vec3(parameters.mie).mul(1.2)).add(vec3(parameters.ozone).mul(15)),
56
+ );
57
+ const rayleighPhase = mu.mul(mu).add(1).mul(0.75);
58
+ const g = look.sky.mieDirectionalG;
59
+ const miePhase = float(1 - g * g).div(
60
+ float(1 + g * g)
61
+ .sub(mu.mul(2 * g))
62
+ .max(0.001)
63
+ .pow(1.5),
64
+ );
65
+ // The LUT's angular coordinate follows the sun, rather than a fixed world X axis.
66
+ const multiple = air.luts.sampleSkyView(
67
+ vec2(mu.mul(0.5).add(0.5), direction.y.mul(0.5).add(0.5)),
68
+ ).rgb;
69
+ const sunlight = (air.sunTransmittance(sun) as Node<"vec3">).mul(color(look.sun.color));
70
+ return transmission
71
+ .oneMinus()
72
+ .mul(scatteringShare)
73
+ .mul(rayleighPhase)
74
+ .mul(look.atmosphere.radiance)
75
+ .add(multiple.mul(look.atmosphere.radiance * 0.25))
76
+ .add(sunlight.mul(miePhase).mul(look.atmosphere.mie * 4));
77
+ }
78
+
79
+ export function skyColour(air: Atmosphere, sun: Node<"vec3">, look: IBiome): Node<"vec3"> {
80
+ const direction = normalize(positionLocal);
81
+ const mu = dot(direction, sun);
82
+ const disc = smoothstep(Math.cos(0.006), Math.cos(0.00465), mu);
83
+ return skyRadiance(air, direction, sun, look).add(
84
+ (air.sunTransmittance(sun) as Node<"vec3">).mul(color(look.sun.color)).mul(disc).mul(48),
85
+ );
86
+ }
87
+
88
+ /** Depth-reconstructed world position applies one air composite to the complete lit scene. */
89
+ export function aerialPerspective(
90
+ air: Atmosphere,
91
+ sun: Node<"vec3">,
92
+ look: IBiome,
93
+ enabled = true,
94
+ surface: Node<"vec3"> = positionWorld,
95
+ eye: Node<"vec3"> = cameraPosition,
96
+ input: Node<"vec4"> = output,
97
+ ): Node<"vec4"> {
98
+ const ray = surface.sub(eye);
99
+ const direction = normalize(ray);
100
+ const height = look.haze.height;
101
+ const low = eye.y.min(surface.y).max(0);
102
+ const span = ray.y.abs().div(height);
103
+ // Analytic mean density along the ray; the limit at equal heights is one.
104
+ const column = span.negate().exp().oneMinus().div(span.max(0.001));
105
+ // Mean density along the ray relative to its low end, as UE's exponential height fog integrates
106
+ // it: a ridge 800 m above the valley sits above most of the haze and stays readable.
107
+ const climb = mix(1, column, smoothstep(0.001, 0.01, span));
108
+ const density = low.div(-height).exp().mul(climb);
109
+ // Clear foreground; aerosol weather builds on landscape distances, not prop distances.
110
+ const distanceKm = ray.length().sub(150).max(0).div(1000);
111
+ // The playable valley keeps its clear air; the alpine fog and the deeper blue build past `reach`.
112
+ const fog = look.atmosphere.heightFog;
113
+ const reach = fog ? smoothstep(fog.reach[0], fog.reach[1], ray.length()) : float(0);
114
+ const opticalDistance = distanceKm.mul(
115
+ density
116
+ .mul(0.65)
117
+ .add(0.35)
118
+ .mul(
119
+ fog
120
+ ? mix(look.atmosphere.distanceScale, fog.distanceScale, reach)
121
+ : look.atmosphere.distanceScale,
122
+ ),
123
+ );
124
+ const zenith = air.sunTransmittance(vec3(0, 1, 0)) as Node<"vec3">;
125
+ // ponytail: the decorative continuation ends at 2.3 km; extend it before increasing this weather cutoff.
126
+ const boundary = smoothstep(
127
+ look.atmosphere.horizonFade[0],
128
+ look.atmosphere.horizonFade[1],
129
+ ray.length(),
130
+ );
131
+ const transmission = enabled
132
+ ? zenith.max(0.0001).pow(opticalDistance.div(8)).mul(boundary.oneMinus())
133
+ : vec3(1);
134
+ const scattering = skyRadiance(air, direction, sun, look);
135
+ // Local aerosol tint preserves soil-coloured shadows; the remote column is the LUT sky.
136
+ const valleyHaze = ray
137
+ .length()
138
+ .mul(look.haze.density)
139
+ .mul(climb)
140
+ .pow(2)
141
+ .negate()
142
+ .exp()
143
+ .oneMinus()
144
+ .mul(smoothstep(150, 250, ray.length()));
145
+ const localAmount = !enabled
146
+ ? float(0)
147
+ : fog
148
+ ? mix(valleyHaze, heightFogAmount(fog, eye, direction, ray.length(), surface.y), reach)
149
+ : valleyHaze;
150
+ const localTint = mix(
151
+ color(look.haze.color),
152
+ color(look.sun.color).mul(1.4),
153
+ dot(direction, sun).max(0).pow(6).mul(look.haze.sunScatter),
154
+ );
155
+ let localColour: Node<"vec3"> = mix(input.rgb, localTint, localAmount);
156
+ const mist = look.atmosphere.groundMist;
157
+ if (mist && enabled) {
158
+ // The density, thickness, ceiling and colour are `LOOK` uniforms, so a tweak box moves them live.
159
+ const term = {
160
+ density: LOOK.mistDensity as unknown as number,
161
+ height: mist.term.height,
162
+ scale: LOOK.mistScale as unknown as number,
163
+ };
164
+ const amount = heightFogAmount(
165
+ { reach: [0, 0], distanceScale: 1, start: mist.start, terms: [term] },
166
+ eye,
167
+ direction,
168
+ ray.length(),
169
+ surface.y,
170
+ ).min(LOOK.mistOpacity);
171
+ // Toward the low sun the mist carries its light; away from it, the shade's cool air.
172
+ const lit = dot(direction, sun)
173
+ .max(0)
174
+ .pow(4)
175
+ .mul(look.haze.sunScatter * 2)
176
+ .min(1);
177
+ localColour = mix(
178
+ localColour,
179
+ mix(LOOK.mistColor, color(look.sun.color).mul(1.2), lit),
180
+ amount,
181
+ );
182
+ }
183
+ return vec4(localColour.mul(transmission).add(scattering.mul(transmission.oneMinus())), input.a);
184
+ }
185
+
186
+ /**
187
+ * Opacity of the height fog between the eye and a surface, with UE's analytic line integral of an
188
+ * exponential density along the ray (UE 5.8.3: Engine/Shaders/Private/HeightFogCommon.ush:207-214,
189
+ * start distance :288-305). Linear in distance, unlike the distance-squared haze: a 10% farther
190
+ * gully is about 10% more hazed, not twice, so spur and gully keep their own shading.
191
+ */
192
+ function heightFogAmount(
193
+ fog: NonNullable<IBiome["atmosphere"]["heightFog"]>,
194
+ eye: Node<"vec3">,
195
+ direction: Node<"vec3">,
196
+ length: Node<"float">,
197
+ surfaceY: Node<"float">,
198
+ ): Node<"float"> {
199
+ const travelled = length.sub(fog.start).max(0);
200
+ const startY = eye.y.add(direction.y.mul(length.min(fog.start)));
201
+ const rise = surfaceY.sub(startY);
202
+ let depth: Node<"float"> = float(0);
203
+ for (const term of fog.terms) {
204
+ const x = rise.div(term.scale).clamp(-60, 60);
205
+ // (1 - e^-x) / x, with its Taylor limit where the ray runs level.
206
+ const shape = select(
207
+ x.abs().lessThan(0.001),
208
+ float(1).sub(x.mul(0.5)),
209
+ x.negate().exp().oneMinus().div(x),
210
+ );
211
+ // `div` then `negate`, not `div(-scale)`: the mist's scale is a uniform node, and `-node` is NaN.
212
+ const atStart = startY
213
+ .sub(term.height)
214
+ .div(term.scale)
215
+ .negate()
216
+ .min(60)
217
+ .exp()
218
+ .mul(term.density);
219
+ depth = depth.add(atStart.mul(shape).mul(travelled));
220
+ }
221
+ return depth.negate().exp().oneMinus();
222
+ }
223
+
224
+ /** The cumulus deck. Every number is this game's weather. */
225
+ const CLOUDS = {
226
+ /** Noise cells across the whole sky. Fewer is a bigger, calmer deck. */
227
+ scale: 1.5,
228
+ /**
229
+ * Vertical stretch of the field, and it is a stretch and not a squash for the reason it looks like
230
+ * one: the field is sampled in view *direction*, so multiplying y up by three makes each cell
231
+ * three times wider than it is tall, which is what a cumulus deck seen from underneath is —
232
+ * puffs with flat bases. Squash it and every cell becomes a column, and the sky fills with smoke.
233
+ */
234
+ stretch: 2.2,
235
+ /**
236
+ * Where the bank field runs from bare sky to solid deck, and where the puff field runs from
237
+ * nothing to solid on top of it. Both ramps are narrow on purpose: a wide one is a smear, and a
238
+ * smear across the whole sky is a fog bank, not a cumulus.
239
+ */
240
+ bank: [-0.2, 0.22],
241
+ coverage: [0.02, 0.42],
242
+ /** The band of sky the deck occupies: its base just above the horizon, thinning towards the zenith. */
243
+ band: [0.02, 0.1, 0.99, 0.5],
244
+ /** Sunlit crown, shaded underside, and the silver a thin fringe takes when it faces the sun. */
245
+ tint: { lit: 0xf7f8f9, shade: 0xc8d3de, silver: 0xfff2d4 },
246
+ /** How much a dense core is darkened relative to a thin fringe. */
247
+ core: 0.55,
248
+ /** Up share of a puff's side normal: lower is a lumpier, more sculpted body. */
249
+ relief: 1.2,
250
+ /**
251
+ * The deck's brightness against the physical sky behind it. The sky is in radiance units several
252
+ * times above one, so a cloud written as plain white came out darker than the blue around it —
253
+ * grey blobs — once the rig stopped fogging its own dome.
254
+ */
255
+ radiance: 1.65,
256
+ } as const;
257
+
258
+ /**
259
+ * The cloud deck: a dome inside the rig's sky box, alpha-blended over the physical sky.
260
+ *
261
+ * Lit by one dot product against the sun rather than by a volume integral, because a volume is a
262
+ * compute pass per frame and this is a fragment shader over the sky's own pixels. What sells it is
263
+ * the shading, not the integration: sun side white, away side blue-grey, a silver lining where the
264
+ * coverage is thin and the sun is behind, and every core darkened — which is the same read a
265
+ * volumetric integral gives at this size, for one noise field and one `dot`.
266
+ */
267
+ export function cloudDome(air: Atmosphere, sun: Node<"vec3">, look: IBiome): MeshBasicNodeMaterial {
268
+ const material = new MeshBasicNodeMaterial({
269
+ // Seen from the inside, and never written to depth: the deck is behind everything else in the
270
+ // world and must not occlude a single blade of grass in front of it.
271
+ depthWrite: false,
272
+ fog: false,
273
+ side: BackSide,
274
+ transparent: true,
275
+ });
276
+ // The only coordinate a sky has is the direction to the fragment.
277
+ const direction = normalize(positionLocal);
278
+ // Ray intersection with a cloud layer: distant cells shrink towards the horizon.
279
+ const field = vec3(direction.x, direction.y.mul(CLOUDS.stretch), direction.z)
280
+ .div(direction.y.max(0.08))
281
+ .mul(CLOUDS.scale);
282
+ const puff = mx_fractal_noise_float(field, 4, 2, 0.5);
283
+ const bank = mx_fractal_noise_float(field.mul(0.3), 2, 2, 0.5);
284
+ // Density sampled towards the live sun approximates self-shadow inside each soft puff.
285
+ const lightDepth = mx_fractal_noise_float(field.add(sun.mul(0.55)), 3, 2, 0.5);
286
+ const crown = smoothstep(-0.12, 0.24, puff.sub(lightDepth.mul(0.65)));
287
+ const coverage = smoothstep(float(CLOUDS.coverage[0]), float(CLOUDS.coverage[1]), puff);
288
+ const deck = smoothstep(float(CLOUDS.band[0]), float(CLOUDS.band[1]), direction.y).mul(
289
+ smoothstep(float(CLOUDS.band[2]), float(CLOUDS.band[3]), direction.y),
290
+ );
291
+ const toSun = max(dot(direction, sun), 0);
292
+ const sunlight = (air.sunTransmittance(sun) as Node<"vec3">).mul(color(look.sun.color));
293
+ const shade = skyRadiance(air, normalize(vec3(direction.x, 0.7, direction.z)), sun, look)
294
+ .mul(0.4)
295
+ .add(color(CLOUDS.tint.shade).mul(0.35));
296
+ // Each puff as a lit body: the field's gradient is the outward normal of its side (the field's
297
+ // x and z are the world's), so the side that faces the sun is bright and the far side and the
298
+ // thick base fall into shade, the read a volume integral gives a cumulus at this size.
299
+ const step = 0.03;
300
+ const outward = normalize(
301
+ vec3(
302
+ puff.sub(mx_fractal_noise_float(field.add(vec3(step, 0, 0)), 4, 2, 0.5)).div(step),
303
+ float(CLOUDS.relief),
304
+ puff.sub(mx_fractal_noise_float(field.add(vec3(0, 0, step)), 4, 2, 0.5)).div(step),
305
+ ),
306
+ );
307
+ const sunSide = smoothstep(-0.25, 0.55, dot(outward, sun));
308
+ const lit = mix(
309
+ shade,
310
+ sunlight.mul(color(CLOUDS.tint.lit)),
311
+ sunSide.mul(crown.mul(0.5).add(0.5)).max(smoothstep(float(0.2), float(0.7), toSun)),
312
+ );
313
+ // The lining: thin coverage facing the sun, and only there.
314
+ const fringe = color(CLOUDS.tint.silver)
315
+ .mul(pow(toSun, float(8)))
316
+ .mul(float(1).sub(coverage))
317
+ .mul(float(0.55))
318
+ .mul(sunlight);
319
+ material.colorNode = lit
320
+ .add(fringe)
321
+ .mul(mix(float(1), float(CLOUDS.core), coverage))
322
+ .mul(CLOUDS.radiance);
323
+ // Optical thickness leaves translucent wisps instead of a hard clipped noise contour.
324
+ const density = coverage
325
+ .mul(smoothstep(float(CLOUDS.bank[0]), float(CLOUDS.bank[1]), bank))
326
+ .mul(deck);
327
+ material.opacityNode = float(1).sub(density.mul(-2.4).exp()).mul(look.clouds);
328
+ return material;
329
+ }