create-threenative 0.2.7 → 0.2.8

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 (154) hide show
  1. package/README.md +3 -1
  2. package/agent-docs/references/capability-reference.md +177 -7
  3. package/capabilities.json +247 -4
  4. package/dist/index.js +6 -1
  5. package/dist/threenative.js +3 -2
  6. package/package.json +4 -4
  7. package/templates/action-rpg/package.json +1 -1
  8. package/templates/minimal/package.json +1 -1
  9. package/templates/platformer/package.json +1 -1
  10. package/templates/puzzle/package.json +1 -1
  11. package/templates/racing/package.json +1 -1
  12. package/templates/rain/.codex/config.toml +19 -0
  13. package/templates/rain/.cursor/mcp.json +32 -0
  14. package/templates/rain/.gemini/settings.json +32 -0
  15. package/templates/rain/.mcp.json +32 -0
  16. package/templates/rain/.vscode/mcp.json +36 -0
  17. package/templates/rain/.zed/settings.json +36 -0
  18. package/templates/rain/AGENTS.md +96 -0
  19. package/templates/rain/CLAUDE.md +98 -0
  20. package/templates/rain/assets/rain-loop.wav +0 -0
  21. package/templates/rain/assets/thunder.wav +0 -0
  22. package/templates/rain/assets/wind-loop.wav +0 -0
  23. package/templates/rain/index.html +18 -0
  24. package/templates/rain/kit.json +7 -0
  25. package/templates/rain/native-playtests/boot.playtest.json +13 -0
  26. package/templates/rain/native-playtests/lightning.playtest.json +173 -0
  27. package/templates/rain/opencode.json +41 -0
  28. package/templates/rain/package.json +56 -0
  29. package/templates/rain/playtests/auto-lightning-clear.playtest.json +66 -0
  30. package/templates/rain/playtests/auto-lightning.playtest.json +26 -0
  31. package/templates/rain/playtests/automation.playtest.json +133 -0
  32. package/templates/rain/playtests/cinematic.playtest.json +59 -0
  33. package/templates/rain/playtests/flash.playtest.json +27 -0
  34. package/templates/rain/playtests/fly.playtest.json +54 -0
  35. package/templates/rain/playtests/lightning.playtest.json +220 -0
  36. package/templates/rain/playtests/loading.playtest.json +57 -0
  37. package/templates/rain/playtests/paused.playtest.json +59 -0
  38. package/templates/rain/playtests/performance.playtest.json +22 -0
  39. package/templates/rain/playtests/reduced-motion.playtest.json +87 -0
  40. package/templates/rain/playtests/simulation.playtest.json +20 -0
  41. package/templates/rain/playtests/storm.playtest.json +150 -0
  42. package/templates/rain/playtests/survives.playtest.json +58 -0
  43. package/templates/rain/playtests/tiers.playtest.json +179 -0
  44. package/templates/rain/playtests/weather.playtest.json +77 -0
  45. package/templates/rain/public/favicon.svg +5 -0
  46. package/templates/rain/src/audio/storm.ts +314 -0
  47. package/templates/rain/src/game.ts +114 -0
  48. package/templates/rain/src/main.ts +15 -0
  49. package/templates/rain/src/render/bloom-shader.ts +65 -0
  50. package/templates/rain/src/render/camera.ts +160 -0
  51. package/templates/rain/src/render/clouds-shader.ts +242 -0
  52. package/templates/rain/src/render/clouds.ts +142 -0
  53. package/templates/rain/src/render/lighting.ts +39 -0
  54. package/templates/rain/src/render/lightning.ts +360 -0
  55. package/templates/rain/src/render/loading.ts +327 -0
  56. package/templates/rain/src/render/materials.ts +19 -0
  57. package/templates/rain/src/render/noise-volume.ts +159 -0
  58. package/templates/rain/src/render/palette.ts +18 -0
  59. package/templates/rain/src/render/post-shader.ts +178 -0
  60. package/templates/rain/src/render/postprocessing.ts +226 -0
  61. package/templates/rain/src/render/quality.ts +135 -0
  62. package/templates/rain/src/render/rain.ts +299 -0
  63. package/templates/rain/src/render/sky.ts +28 -0
  64. package/templates/rain/src/render/world-shader.ts +680 -0
  65. package/templates/rain/src/render/world.ts +146 -0
  66. package/templates/rain/src/render/worldEnvironment.ts +809 -0
  67. package/templates/rain/src/scenes/Boot.ts +334 -0
  68. package/templates/rain/src/state.ts +257 -0
  69. package/templates/rain/src/style.css +1160 -0
  70. package/templates/rain/src/ui/App.tsx +29 -0
  71. package/templates/rain/src/ui/GameUi.tsx +187 -0
  72. package/templates/rain/src/ui/Hud.tsx +327 -0
  73. package/templates/rain/src/ui/LoadingOverlay.tsx +46 -0
  74. package/templates/rain/src/ui/Menu.tsx +370 -0
  75. package/templates/rain/src/ui/automation.ts +60 -0
  76. package/templates/rain/src/ui/main.tsx +7 -0
  77. package/templates/rain/src/ui/tempest.ts +149 -0
  78. package/templates/rain/tailwind.config.ts +2 -0
  79. package/templates/rain/threenative.config.ts +66 -0
  80. package/templates/rain/tools/generate-shaders.mjs +656 -0
  81. package/templates/rain/tools/make-storm-audio.mjs +387 -0
  82. package/templates/rain/tools/storm-dsp.mjs +177 -0
  83. package/templates/rain/tools/tempest-bloom.frag +10 -0
  84. package/templates/rain/tools/tempest-clouds.frag +86 -0
  85. package/templates/rain/tools/tempest-post.frag +47 -0
  86. package/templates/rain/tools/tempest-world.frag +210 -0
  87. package/templates/rain/tools/verify-noise-volume.mjs +61 -0
  88. package/templates/rain/tsconfig.json +17 -0
  89. package/templates/rain/vite.config.ts +37 -0
  90. package/templates/rts/package.json +1 -1
  91. package/templates/runner/package.json +1 -1
  92. package/templates/sailing/package.json +1 -1
  93. package/templates/shooter/package.json +1 -1
  94. package/templates/snow/.codex/config.toml +19 -0
  95. package/templates/snow/.cursor/mcp.json +32 -0
  96. package/templates/snow/.gemini/settings.json +32 -0
  97. package/templates/snow/.mcp.json +32 -0
  98. package/templates/snow/.vscode/mcp.json +36 -0
  99. package/templates/snow/.zed/settings.json +36 -0
  100. package/templates/snow/AGENTS.md +88 -0
  101. package/templates/snow/CLAUDE.md +90 -0
  102. package/templates/snow/README.md +26 -0
  103. package/templates/snow/assets/crunch.wav +0 -0
  104. package/templates/snow/assets/wind.wav +0 -0
  105. package/templates/snow/index.html +12 -0
  106. package/templates/snow/kit.json +7 -0
  107. package/templates/snow/native-playtests/snow-physics.playtest.json +150 -0
  108. package/templates/snow/native-playtests/survives.playtest.json +47 -0
  109. package/templates/snow/opencode.json +41 -0
  110. package/templates/snow/package.json +58 -0
  111. package/templates/snow/playtests/footsteps.playtest.json +95 -0
  112. package/templates/snow/playtests/performance.playtest.json +36 -0
  113. package/templates/snow/playtests/snow-physics.playtest.json +154 -0
  114. package/templates/snow/playtests/survives.playtest.json +56 -0
  115. package/templates/snow/playtests/touch-controls.playtest.json +71 -0
  116. package/templates/snow/playtests/weather.playtest.json +119 -0
  117. package/templates/snow/public/favicon.svg +5 -0
  118. package/templates/snow/src/audio.ts +58 -0
  119. package/templates/snow/src/commands.ts +8 -0
  120. package/templates/snow/src/entities/Explorer.ts +279 -0
  121. package/templates/snow/src/game.ts +69 -0
  122. package/templates/snow/src/main.ts +15 -0
  123. package/templates/snow/src/render/bootPrint.ts +50 -0
  124. package/templates/snow/src/render/camera.ts +84 -0
  125. package/templates/snow/src/render/explorer.ts +321 -0
  126. package/templates/snow/src/render/lighting.ts +50 -0
  127. package/templates/snow/src/render/loading.ts +325 -0
  128. package/templates/snow/src/render/materials.ts +36 -0
  129. package/templates/snow/src/render/palette.ts +17 -0
  130. package/templates/snow/src/render/postprocessing.ts +44 -0
  131. package/templates/snow/src/render/props.ts +42 -0
  132. package/templates/snow/src/render/quality.ts +135 -0
  133. package/templates/snow/src/render/scenery.ts +244 -0
  134. package/templates/snow/src/render/sky.ts +57 -0
  135. package/templates/snow/src/render/snowSurface.ts +259 -0
  136. package/templates/snow/src/render/touch-controls.ts +143 -0
  137. package/templates/snow/src/render/weather.ts +279 -0
  138. package/templates/snow/src/render/worldEnvironment.ts +809 -0
  139. package/templates/snow/src/scenes/Boot.ts +12 -0
  140. package/templates/snow/src/scenes/Snow.ts +635 -0
  141. package/templates/snow/src/state.ts +46 -0
  142. package/templates/snow/src/style.css +143 -0
  143. package/templates/snow/src/terrain.ts +64 -0
  144. package/templates/snow/src/ui/App.tsx +15 -0
  145. package/templates/snow/src/ui/Controls.tsx +322 -0
  146. package/templates/snow/src/ui/GameUi.tsx +354 -0
  147. package/templates/snow/src/ui/icons.tsx +54 -0
  148. package/templates/snow/src/ui/main.tsx +7 -0
  149. package/templates/snow/tailwind.config.ts +2 -0
  150. package/templates/snow/threenative.config.ts +51 -0
  151. package/templates/snow/tsconfig.json +17 -0
  152. package/templates/snow/vite.config.ts +37 -0
  153. package/templates/starter/package.json +1 -1
  154. package/templates/tower-defense/package.json +1 -1
@@ -0,0 +1,680 @@
1
+ // GENERATED FILE — do not edit it. Every line below is the output of Three.js's own GLSL to
2
+ // TSL transpiler, run over tools/tempest-world.frag. That .frag file
3
+ // is the source of truth; change it and the generator, never this file.
4
+ //
5
+ // node tools/generate-shaders.mjs
6
+ //
7
+ // The shader is the coast: terrain, road, sea, the rail and cabin on it, wet reflections and the
8
+ // fog that closes the distance. It is raymarched, so it is one full-screen quad and one function
9
+ // call — see src/render/world.ts for the uniforms and the camera basis it expects.
10
+ import { DataTexture, Vector2, Vector3, Vector4 } from "three";
11
+ import {
12
+ Break,
13
+ Fn,
14
+ If,
15
+ Loop,
16
+ abs,
17
+ add,
18
+ atan,
19
+ clamp,
20
+ cos,
21
+ div,
22
+ dot,
23
+ exp,
24
+ float,
25
+ floor,
26
+ fract,
27
+ frameGroup,
28
+ int,
29
+ length,
30
+ mat2,
31
+ max,
32
+ min,
33
+ mix,
34
+ mul,
35
+ normalize,
36
+ oneMinus,
37
+ pow,
38
+ property,
39
+ reflect,
40
+ select,
41
+ sin,
42
+ smoothstep,
43
+ sub,
44
+ texture,
45
+ uniform,
46
+ vec2,
47
+ vec3,
48
+ vec4,
49
+ } from "three/tsl";
50
+ import type { Node, TextureNode } from "three/webgpu";
51
+
52
+ // Uniforms the shader reads. Values are assigned by src/render/world.ts and src/render/clouds.ts,
53
+ // which own the camera basis and the weather; the generated module only holds the nodes.
54
+ export const uTime = uniform(0, "float").setGroup(frameGroup);
55
+ export const uRain = uniform(0, "float").setGroup(frameGroup);
56
+ export const uFog = uniform(0, "float").setGroup(frameGroup);
57
+ export const uWet = uniform(0, "float").setGroup(frameGroup);
58
+ export const uFlash = uniform(0, "float").setGroup(frameGroup);
59
+ export const uAspect = uniform(0, "float").setGroup(frameGroup);
60
+ export const uTan = uniform(0, "float").setGroup(frameGroup);
61
+ export const uCam = uniform(new Vector3(), "vec3").setGroup(frameGroup);
62
+ export const uForward = uniform(new Vector3(), "vec3").setGroup(frameGroup);
63
+ export const uRight = uniform(new Vector3(), "vec3").setGroup(frameGroup);
64
+ export const uUp = uniform(new Vector3(), "vec3").setGroup(frameGroup);
65
+ export const uStrike = uniform(new Vector3(), "vec3").setGroup(frameGroup);
66
+ export const uHazeLow = uniform(new Vector3(), "vec3").setGroup(frameGroup);
67
+ export const uHazeHigh = uniform(new Vector3(), "vec3").setGroup(frameGroup);
68
+ export const uSunDir = uniform(new Vector3(), "vec3").setGroup(frameGroup);
69
+ export const uSunColor = uniform(new Vector3(), "vec3").setGroup(frameGroup);
70
+ export const uFill = uniform(new Vector3(), "vec3").setGroup(frameGroup);
71
+ export const uFlashLight = uniform(new Vector3(), "vec3").setGroup(frameGroup);
72
+ export const uLampLight = uniform(new Vector3(), "vec3").setGroup(frameGroup);
73
+ export const uLampGlow = uniform(new Vector3(), "vec3").setGroup(frameGroup);
74
+ export const uWater = uniform(new Vector3(), "vec3").setGroup(frameGroup);
75
+ const uSkyPlaceholder = new DataTexture(new Uint8Array([0, 0, 0, 255]), 1, 1);
76
+ uSkyPlaceholder.needsUpdate = true;
77
+ export let uSky: TextureNode = texture(uSkyPlaceholder).setGroup(frameGroup);
78
+ export function setSky(value: TextureNode): void {
79
+ uSky = value;
80
+ }
81
+ export const uReflect = uniform(0, "int").setGroup(frameGroup);
82
+
83
+ // Three.js Transpiler r185
84
+
85
+ // The look, owned by src/render/{sky,lighting,materials}.ts and set from there every run.
86
+
87
+ export const hash12 = /*@__PURE__*/ Fn(([p]: [Node<"vec2">]) => {
88
+ const p3 = fract(vec3(p.xyx).mul(0.1031)).toVar();
89
+ p3.addAssign(dot(p3, p3.yzx.add(33.33)));
90
+
91
+ return fract(p3.x.add(p3.y).mul(p3.z));
92
+ });
93
+
94
+ export const hash13 = /*@__PURE__*/ Fn(([p_immutable]: [Node<"vec3">]) => {
95
+ const p = p_immutable.toVar();
96
+ p.assign(fract(p.mul(0.1031)));
97
+ p.addAssign(dot(p, p.zyx.add(31.32)));
98
+
99
+ return fract(p.x.add(p.y).mul(p.z));
100
+ });
101
+
102
+ export const noise2 = /*@__PURE__*/ Fn(([p]: [Node<"vec2">]) => {
103
+ const i = floor(p).toVar();
104
+ const f = fract(p).toVar();
105
+ f.assign(f.mul(f).mul(sub(3, mul(2, f))));
106
+
107
+ return mix(
108
+ mix(hash12(i), hash12(i.add(vec2(1, 0))), f.x),
109
+ mix(hash12(i.add(vec2(0, 1))), hash12(i.add(1)), f.x),
110
+ f.y,
111
+ );
112
+ });
113
+
114
+ export const fbm2 = /*@__PURE__*/ Fn(([p_immutable]: [Node<"vec2">]) => {
115
+ const p = p_immutable.toVar();
116
+ const v = mul(0.5, noise2(p)).toVar();
117
+ p.assign(mat2(0.8, 0.6, -0.6, 0.8).mul(p).mul(2.03).add(19.1));
118
+ v.addAssign(mul(0.25, noise2(p)));
119
+ p.assign(p.mul(2.02).add(9.3));
120
+ v.addAssign(mul(0.125, noise2(p)));
121
+
122
+ return v.add(mul(0.0625, noise2(p.mul(2.01).add(17))));
123
+ });
124
+
125
+ export const ray = /*@__PURE__*/ Fn(([uv]: [Node<"vec2">]) => {
126
+ const q = uv.mul(2).sub(1).toVar();
127
+
128
+ return normalize(
129
+ uForward.add(uRight.mul(q.x).mul(uAspect).mul(uTan)).add(uUp.mul(q.y).mul(uTan)),
130
+ );
131
+ });
132
+
133
+ export const project = /*@__PURE__*/ Fn(([p]: [Node<"vec3">]) => {
134
+ const v = p.sub(uCam).toVar();
135
+ const d = dot(v, uForward).toVar();
136
+
137
+ return vec4(
138
+ dot(v, uRight).div(uAspect.mul(uTan)),
139
+ dot(v, uUp).div(uTan),
140
+ mul(1.00012, d).sub(0.300018),
141
+ d,
142
+ );
143
+ });
144
+
145
+ export const hazeColor = /*@__PURE__*/ Fn(([rd]: [Node<"vec3">]) => {
146
+ return mix(uHazeLow, uHazeHigh, smoothstep(0, 0.7, abs(rd.y))).add(
147
+ uFlash.mul(vec3(0.11, 0.16, 0.24)),
148
+ );
149
+ });
150
+
151
+ export const roadCenter = /*@__PURE__*/ Fn(([z]: [Node<"float">]) => {
152
+ return float(-1.6)
153
+ .add(mul(2.1, sin(z.mul(0.012))))
154
+ .add(mul(15, sub(1, smoothstep(-420, -150, z))));
155
+ });
156
+
157
+ export const coast = /*@__PURE__*/ Fn(([z]: [Node<"float">]) => {
158
+ return roadCenter(z)
159
+ .add(9.2)
160
+ .add(sin(z.mul(0.023)).mul(1.8));
161
+ });
162
+
163
+ export const terrain = /*@__PURE__*/ Fn(([p]: [Node<"vec2">]) => {
164
+ const n = fbm2(p.mul(0.009).add(vec2(4, 9))).toVar();
165
+ const r = sub(1, abs(noise2(p.mul(0.018)).mul(2).sub(1))).toVar();
166
+ const left = smoothstep(13, 155, p.x.negate())
167
+ .mul(add(28, n.mul(135)).add(r.mul(20)))
168
+ .toVar();
169
+ const back = smoothstep(280, 670, p.y.negate())
170
+ .mul(add(46, n.mul(155)).add(r.mul(22)))
171
+ .toVar();
172
+ const farRight = smoothstep(190, 430, p.x)
173
+ .mul(add(30, n.mul(95)))
174
+ .toVar();
175
+ const h = max(max(left, back), farRight).toVar();
176
+ const island = exp(
177
+ dot(p.sub(vec2(94, -205)).div(vec2(39, 48)), p.sub(vec2(94, -205)).div(vec2(39, 48))).negate(),
178
+ )
179
+ .mul(add(18, n.mul(10)))
180
+ .toVar();
181
+ h.assign(max(h, island));
182
+
183
+ If(h.lessThan(1.5), () => {
184
+ h.assign(select(p.x.greaterThan(coast(p.y)), -1.2, add(0.1, n.mul(0.2))));
185
+ });
186
+
187
+ If(p.y.greaterThan(-175).and(abs(p.x.sub(roadCenter(p.y))).lessThan(7.3)), () => {
188
+ h.assign(0);
189
+ });
190
+
191
+ return h;
192
+ });
193
+
194
+ export const sdBox = /*@__PURE__*/ Fn(([p, b]: [Node<"vec3">, Node<"vec3">]) => {
195
+ const q = abs(p).sub(b).toVar();
196
+
197
+ return length(max(q, 0)).add(min(max(q.x, max(q.y, q.z)), 0));
198
+ });
199
+
200
+ export const capsule = /*@__PURE__*/ Fn(
201
+ ([p, a, b, r]: [Node<"vec3">, Node<"vec3">, Node<"vec3">, Node<"float">]) => {
202
+ const pa = p.sub(a).toVar();
203
+ const ba = b.sub(a).toVar();
204
+
205
+ return length(pa.sub(ba.mul(clamp(dot(pa, ba).div(dot(ba, ba)), 0, 1)))).sub(r);
206
+ },
207
+ );
208
+
209
+ export const choose = /*@__PURE__*/ Fn(([a, b]: [Node<"vec2">, Node<"vec2">]) => {
210
+ return select(a.x.lessThan(b.x), a, b);
211
+ });
212
+
213
+ export const sceneMap = /*@__PURE__*/ Fn(([p]: [Node<"vec3">]) => {
214
+ const h = terrain(p.xz).toVar();
215
+ const res = vec2(p.y.sub(select(h.greaterThan(1), h, -3.5)).mul(0.58), 1).toVar();
216
+
217
+ // One bounded procedural tree per spatial cell; crowns fit inside their cells.
218
+
219
+ const cell = floor(p.xz.add(vec2(2, 1)).div(10.5)).toVar();
220
+ const seed = hash12(cell.add(17)).toVar();
221
+ const tc = cell
222
+ .add(0.5)
223
+ .mul(10.5)
224
+ .sub(vec2(2, 1))
225
+ .add(vec2(hash12(cell.add(3)), hash12(cell.add(7))).mul(0.9))
226
+ .sub(0.45)
227
+ .toVar();
228
+ const th = terrain(tc).toVar();
229
+ const rel = tc.x.sub(roadCenter(tc.y)).toVar();
230
+ const forest = rel
231
+ .lessThan(-9)
232
+ .or(tc.y.lessThan(-175))
233
+ .and(th.greaterThan(-0.2))
234
+ .and(th.lessThan(115))
235
+ .and(seed.greaterThan(0.15))
236
+ .and(tc.y.lessThan(-12).or(tc.x.lessThan(-45)))
237
+ .toVar();
238
+
239
+ If(forest, () => {
240
+ const q = p.sub(vec3(tc.x, th, tc.y)).toVar();
241
+ const ht = add(6, seed.mul(13)).toVar();
242
+ const cy = clamp(q.y.div(ht), 0, 1).toVar();
243
+ const angle = atan(q.z, q.x).toVar();
244
+ const tier = add(0.77, mul(0.23, sin(q.y.mul(3.5).add(seed.mul(13))))).toVar();
245
+ const r = sub(1, cy)
246
+ .mul(add(2, seed.mul(1.45)))
247
+ .mul(tier)
248
+ .toVar();
249
+ r.mulAssign(
250
+ add(0.88, mul(0.12, sin(angle.mul(9).add(q.y.mul(2.7))))).add(
251
+ mul(0.07, sin(angle.mul(19).sub(q.y.mul(6)))),
252
+ ),
253
+ );
254
+ const needle = noise2(q.xz.mul(6).add(q.y.mul(2)))
255
+ .sub(0.5)
256
+ .mul(0.18)
257
+ .mul(sub(1, cy))
258
+ .toVar();
259
+ const d = max(length(q.xz).sub(r).add(needle), max(sub(0.8, q.y), q.y.sub(ht)))
260
+ .mul(0.67)
261
+ .toVar();
262
+ res.assign(choose(res, vec2(d, 2)));
263
+ res.assign(
264
+ choose(
265
+ res,
266
+ vec2(
267
+ max(length(q.xz).sub(mul(0.13, sub(1, cy.mul(0.65)))), max(q.y.negate(), q.y.sub(ht))),
268
+ 3,
269
+ ),
270
+ ),
271
+ );
272
+ });
273
+
274
+ // Repeating steel luminaires, including their angled neck and illuminated undersides.
275
+
276
+ const k = clamp(floor(p.z.negate().sub(10).div(38).add(0.5)), 0, 5).toVar();
277
+ const z = float(-10).sub(k.mul(38)).toVar();
278
+ const x = roadCenter(z).sub(6.35).toVar();
279
+ const q = p.sub(vec3(x, 0, z)).toVar();
280
+ const pole = capsule(q, vec3(0, 0, 0), vec3(0, 7.65, 0), 0.075).toVar();
281
+ pole.assign(min(pole, capsule(q, vec3(0, 7.6, 0), vec3(1.6, 8.05, 0), 0.065)));
282
+ pole.assign(min(pole, capsule(q, vec3(1.6, 8.05, 0), vec3(2.5, 8.05, 0), 0.055)));
283
+ pole.assign(min(pole, sdBox(q.sub(vec3(2.43, 8, 0)), vec3(0.4, 0.1, 0.23))));
284
+ res.assign(choose(res, vec2(pole, 4)));
285
+ res.assign(choose(res, vec2(sdBox(q.sub(vec3(2.45, 7.885, 0)), vec3(0.32, 0.021, 0.175)), 6)));
286
+
287
+ // Guardrail on the water side, stopping before the distant curve.
288
+
289
+ If(p.z.lessThan(28).and(p.z.greaterThan(-176)), () => {
290
+ const rx = roadCenter(p.z).add(6.3).toVar();
291
+ const rail = sdBox(vec3(p.x.sub(rx), p.y.sub(0.84), 0), vec3(0.065, 0.13, 1000)).toVar();
292
+ const postZ = floor(p.z.div(5).add(0.5)).mul(5).toVar();
293
+ rail.assign(
294
+ min(
295
+ rail,
296
+ sdBox(p.sub(vec3(roadCenter(postZ).add(6.3), 0.45, postZ)), vec3(0.055, 0.45, 0.055)),
297
+ ),
298
+ );
299
+ res.assign(choose(res, vec2(rail, 4)));
300
+ });
301
+
302
+ // Small roadside field station, with a lit window and antenna.
303
+
304
+ const b = p.sub(vec3(-17, 1.85, -73)).toVar();
305
+ const cabin = sdBox(b, vec3(3.3, 1.85, 2.65)).toVar();
306
+ res.assign(choose(res, vec2(cabin, 9)));
307
+ res.assign(choose(res, vec2(sdBox(p.sub(vec3(-17, 3.85, -73)), vec3(3.6, 0.14, 2.9)), 4)));
308
+ res.assign(
309
+ choose(res, vec2(sdBox(p.sub(vec3(-17.8, 2.1, -70.325)), vec3(0.84, 0.48, 0.028)), 6)),
310
+ );
311
+ res.assign(choose(res, vec2(capsule(p, vec3(-19, 3.9, -74), vec3(-19, 13, -74), 0.045), 4)));
312
+
313
+ return res;
314
+ });
315
+
316
+ export const trace = /*@__PURE__*/ Fn(
317
+ ([ro, rd, limit]: [Node<"vec3">, Node<"vec3">, Node<"float">]) => {
318
+ const t = float(0.25).toVar();
319
+ const mat = float(-1).toVar();
320
+
321
+ Loop({ start: int(0), end: 148 }, ({ i }) => {
322
+ If(t.greaterThan(limit).or(t.greaterThan(1850)), () => {
323
+ Break();
324
+ });
325
+
326
+ const p = ro.add(rd.mul(t)).toVar();
327
+ const d = sceneMap(p).toVar();
328
+ const eps = max(0.007, t.mul(0.00048)).toVar();
329
+
330
+ If(d.x.lessThan(eps), () => {
331
+ mat.assign(d.y);
332
+ Break();
333
+ });
334
+
335
+ const stepD = max(0.05, d.x.mul(0.87)).toVar();
336
+
337
+ If(p.y.lessThan(34).and(t.lessThan(270)), () => {
338
+ stepD.assign(min(stepD, 4.8));
339
+ });
340
+
341
+ t.addAssign(stepD);
342
+ });
343
+
344
+ return vec2(t, mat);
345
+ },
346
+ );
347
+
348
+ export const getNormal = /*@__PURE__*/ Fn(([p, t]: [Node<"vec3">, Node<"float">]) => {
349
+ const e = max(0.008, t.mul(0.00032)).toVar();
350
+ const k = vec2(1, -1).mul(0.5773).toVar();
351
+
352
+ return normalize(
353
+ k.xyy
354
+ .mul(sceneMap(p.add(k.xyy.mul(e))).x)
355
+ .add(k.yyx.mul(sceneMap(p.add(k.yyx.mul(e))).x))
356
+ .add(k.yxy.mul(sceneMap(p.add(k.yxy.mul(e))).x))
357
+ .add(k.xxx.mul(sceneMap(p.add(k.xxx.mul(e))).x)),
358
+ );
359
+ });
360
+
361
+ export const baseColor = /*@__PURE__*/ Fn(
362
+ ([p, mat, n]: [Node<"vec3">, Node<"float">, Node<"vec3">]) => {
363
+ const grit = noise2(p.xz.mul(4.3).add(p.y))
364
+ .mul(0.2)
365
+ .add(noise2(p.xz.mul(31)).mul(0.09))
366
+ .toVar();
367
+
368
+ // One value, one return. The transpiler turns a GLSL early return into a JavaScript return that
369
+ // only leaves the `If` callback the node graph is being built inside, so the branch is never
370
+ // assigned and every material would take the colour of the last line. `else if` keeps the
371
+ // first-match order the five separate `if`s had, and `res` is seeded with the fall-through value.
372
+
373
+ const res = vec3(0.1).toVar();
374
+
375
+ If(mat.lessThan(1.5), () => {
376
+ const moss = smoothstep(0.46, 0.66, fbm2(p.xz.mul(0.3).add(p.y.mul(0.4))))
377
+ .mul(smoothstep(0.4, 0.9, n.y))
378
+ .toVar();
379
+ res.assign(
380
+ mix(vec3(0.075, 0.091, 0.09), vec3(0.038, 0.068, 0.042), moss).mul(add(0.7, grit)),
381
+ );
382
+ })
383
+ .ElseIf(mat.lessThan(2.5), () => {
384
+ res.assign(
385
+ mix(
386
+ vec3(0.017, 0.034, 0.027),
387
+ vec3(0.045, 0.071, 0.047),
388
+ noise2(p.xz.mul(5).add(p.y)),
389
+ ).mul(add(0.8, max(n.y, 0).mul(0.35))),
390
+ );
391
+ })
392
+ .ElseIf(mat.lessThan(3.5), () => {
393
+ res.assign(vec3(0.06, 0.047, 0.036));
394
+ })
395
+ .ElseIf(mat.lessThan(4.5), () => {
396
+ res.assign(vec3(0.12, 0.155, 0.17).mul(add(0.8, grit)));
397
+ })
398
+ .ElseIf(mat.greaterThan(8.5), () => {
399
+ const seam = add(
400
+ 0.75,
401
+ mul(0.25, smoothstep(0.01, 0.1, abs(fract(p.x.mul(3)).sub(0.5)))),
402
+ ).toVar();
403
+ res.assign(vec3(0.09, 0.13, 0.14).mul(seam));
404
+ });
405
+
406
+ return res;
407
+ },
408
+ );
409
+
410
+ export const surfaceLight = /*@__PURE__*/ Fn(
411
+ ([p, n, v, albedo, rough]: [
412
+ Node<"vec3">,
413
+ Node<"vec3">,
414
+ Node<"vec3">,
415
+ Node<"vec3">,
416
+ Node<"float">,
417
+ ]) => {
418
+ const hemi = add(0.45, mul(0.55, max(n.y, 0))).toVar();
419
+ const c = albedo.mul(uFill.mul(hemi).add(uSunColor.mul(max(dot(n, uSunDir), 0)))).toVar();
420
+ c.addAssign(
421
+ albedo
422
+ .mul(uFlash)
423
+ .mul(add(0.6, max(dot(n, normalize(uStrike.sub(p))), 0).mul(2.5)))
424
+ .mul(uFlashLight),
425
+ );
426
+
427
+ Loop({ start: int(0), end: 6 }, ({ i }) => {
428
+ const z = float(-10).sub(float(i).mul(38)).toVar();
429
+ const lp = vec3(roadCenter(z).sub(3.9), 7.87, z).toVar();
430
+ const l = lp.sub(p).toVar();
431
+ const dist2 = dot(l, l).toVar();
432
+ l.assign(normalize(l));
433
+ const ndl = max(dot(n, l), 0).toVar();
434
+ const cone = smoothstep(0.1, 0.62, l.y.negate())
435
+ .mul(smoothstep(-0.1, 0.1, l.y))
436
+ .toVar();
437
+ const atten = div(48, add(1, dist2)).toVar();
438
+ const warm = uLampLight.toVar();
439
+ c.addAssign(albedo.mul(warm).mul(atten).mul(ndl).mul(2));
440
+ const hv = normalize(l.add(v)).toVar();
441
+ const shin = mix(20, 900, sub(1, rough)).toVar();
442
+ const spec = pow(max(dot(n, hv), 0), shin)
443
+ .mul(shin.add(2))
444
+ .mul(0.018)
445
+ .toVar();
446
+ c.addAssign(
447
+ warm
448
+ .mul(spec)
449
+ .mul(atten)
450
+ .mul(ndl)
451
+ .mul(select(rough.greaterThan(0.55), 0.022, 1)),
452
+ );
453
+ });
454
+
455
+ return c;
456
+ },
457
+ );
458
+
459
+ export const rippleSlope = /*@__PURE__*/ Fn(([p]: [Node<"vec2">]) => {
460
+ const cell = floor(p.div(2.2)).toVar();
461
+ const sum = vec2(0).toVar();
462
+
463
+ Loop(
464
+ { start: int(-1), end: int(1), condition: "<=" },
465
+ { start: int(-1), end: int(1), condition: "<=" },
466
+ ({ i, j }) => {
467
+ const c = cell.add(vec2(float(i), float(j))).toVar();
468
+ const h = hash12(c.add(3.9)).toVar();
469
+ const ctr = c
470
+ .add(vec2(h, hash12(c.add(19))))
471
+ .mul(2.2)
472
+ .toVar();
473
+ const v = p.sub(ctr).toVar();
474
+ const d = length(v).add(0.001).toVar();
475
+ const age = fract(uTime.mul(add(0.6, uRain.mul(0.2))).add(h.mul(7.3))).toVar();
476
+ const ring = d.sub(age.mul(1.7)).toVar();
477
+ const env = exp(abs(ring).negate().mul(20))
478
+ .mul(sub(1, age))
479
+ .mul(smoothstep(0.025, 0.1, age))
480
+ .toVar();
481
+ sum.addAssign(
482
+ v
483
+ .div(d)
484
+ .mul(cos(ring.mul(95)))
485
+ .mul(env)
486
+ .mul(0.055),
487
+ );
488
+ },
489
+ );
490
+
491
+ return sum.mul(uRain);
492
+ });
493
+
494
+ export const reflected = /*@__PURE__*/ Fn(
495
+ ([p, rd, sky]: [Node<"vec3">, Node<"vec3">, Node<"vec3">]) => {
496
+ // Same reason as baseColor: no early return. The `uReflect` guard becomes the condition around the
497
+ // march, which is still a skip, and a hit becomes a `res` plus a `Break`, which is what the trace
498
+ // already does. A ray that reaches the end of the march leaves `res` as the sky, as it did.
499
+
500
+ const res = sky.toVar();
501
+
502
+ If(uReflect.notEqual(0), () => {
503
+ const t = float(0.5).toVar();
504
+
505
+ Loop({ start: int(0), end: 36 }, ({ i }) => {
506
+ If(t.greaterThan(280), () => {
507
+ Break();
508
+ });
509
+
510
+ const q = p.add(rd.mul(t)).toVar();
511
+ const d = sceneMap(q).toVar();
512
+
513
+ If(d.x.lessThan(max(0.03, t.mul(0.0016))), () => {
514
+ If(d.y.greaterThan(5.5).and(d.y.lessThan(6.5)), () => {
515
+ res.assign(uLampGlow.mul(0.75));
516
+ }).Else(() => {
517
+ res.assign(
518
+ mix(
519
+ baseColor(q, d.y, vec3(0, 1, 0)).mul(0.7),
520
+ hazeColor(rd),
521
+ sub(1, exp(t.negate().mul(add(0.0015, uFog.mul(0.004))))),
522
+ ),
523
+ );
524
+ });
525
+
526
+ Break();
527
+ });
528
+
529
+ t.addAssign(max(0.15, d.x.mul(0.9)));
530
+ });
531
+ });
532
+
533
+ return res;
534
+ },
535
+ );
536
+
537
+ export const tempestWorld = /*@__PURE__*/ Fn(([vUv]: [Node<"vec2">]) => {
538
+ const worldDepth = float(1).toVar();
539
+ const rd = ray(vUv).toVar();
540
+ const sky = uSky.sample(vec2(vUv.x, vUv.y.oneMinus())).rgb.toVar();
541
+ const planeT = select(rd.y.lessThan(-0.0001), uCam.y.negate().div(rd.y), 2200).toVar();
542
+ const pp = uCam.add(rd.mul(planeT)).toVar();
543
+ const water = pp.x.greaterThan(coast(pp.z)).and(terrain(pp.xz).lessThan(0)).toVar();
544
+
545
+ If(water.and(rd.y.lessThan(-0.0001)), () => {
546
+ planeT.assign(float(-0.6).sub(uCam.y).div(rd.y));
547
+ });
548
+
549
+ const hit = trace(uCam, rd, min(planeT, 2200)).toVar();
550
+ const t = hit.x.toVar();
551
+ const mat = hit.y.toVar();
552
+ const p = uCam.add(rd.mul(t)).toVar();
553
+ const n = property("vec3").toVar();
554
+ const color = sky.toVar();
555
+ const ground = mat.lessThan(0).and(planeT.greaterThan(0)).and(planeT.lessThan(1800)).toVar();
556
+
557
+ If(ground, () => {
558
+ t.assign(planeT);
559
+ p.assign(uCam.add(rd.mul(t)));
560
+ const rx = abs(p.x.sub(roadCenter(p.z))).toVar();
561
+ water.assign(p.x.greaterThan(coast(p.z)).and(terrain(p.xz).lessThan(0)));
562
+ const road = rx.lessThan(5.35).and(p.z.greaterThan(-190)).toVar();
563
+ const ripple = rippleSlope(p.xz).toVar();
564
+ const coarse = noise2(p.xz.mul(2.6)).toVar();
565
+ const micro = noise2(p.xz.mul(72)).toVar();
566
+ const puddle = smoothstep(0.37, 0.64, fbm2(p.xz.mul(0.25).add(7)))
567
+ .mul(uWet)
568
+ .toVar();
569
+
570
+ If(water, () => {
571
+ puddle.assign(1);
572
+ });
573
+
574
+ const wave = vec2(
575
+ cos(p.x.mul(1.3).add(p.z.mul(0.8)).add(uTime.mul(1.7))),
576
+ sin(p.z.mul(1.7).sub(p.x.mul(0.4)).sub(uTime.mul(1.2))),
577
+ )
578
+ .mul(select(water, 0.018, 0.0015))
579
+ .toVar();
580
+ const bump = vec2(noise2(p.xz.mul(35)), noise2(p.xz.mul(35).add(9)))
581
+ .sub(0.5)
582
+ .mul(mix(0.22, 0.013, puddle))
583
+ .toVar();
584
+ n.assign(
585
+ normalize(vec3(bump.x.add(wave.x).add(ripple.x), 1, bump.y.add(wave.y).add(ripple.y))),
586
+ );
587
+ const alb = select(
588
+ road,
589
+ vec3(0.031, 0.037, 0.042).mul(add(0.58, coarse.mul(0.4)).add(micro.mul(0.3))),
590
+ vec3(0.075, 0.086, 0.07).mul(add(0.7, coarse.mul(0.45))),
591
+ ).toVar();
592
+ const rough = mix(0.7, 0.08, puddle).toVar();
593
+
594
+ If(road, () => {
595
+ const center = p.x.sub(roadCenter(p.z)).toVar();
596
+ const stripe = sub(1, smoothstep(0.042, 0.062, abs(abs(center).sub(0.14)))).toVar();
597
+ const edge = sub(1, smoothstep(0.06, 0.085, abs(rx.sub(5.04)))).toVar();
598
+ const wear = smoothstep(0.1, 0.35, noise2(p.xz.mul(26))).toVar();
599
+ alb.assign(mix(alb, vec3(0.5, 0.36, 0.11), stripe.mul(wear).mul(0.85)));
600
+ alb.assign(mix(alb, vec3(0.49, 0.54, 0.5), edge.mul(wear).mul(0.6)));
601
+ });
602
+
603
+ If(water, () => {
604
+ alb.assign(uWater);
605
+ rough.assign(0.045);
606
+ });
607
+
608
+ color.assign(surfaceLight(p, n, rd.negate(), alb, rough));
609
+ const fres = add(0.025, mul(0.975, pow(sub(1, max(dot(rd.negate(), n), 0)), 5))).toVar();
610
+ const suv = clamp(vUv.add(vec2(n.x, n.z).mul(vec2(0.07, 0.05))), 0.002, 0.998).toVar();
611
+ const refl = reflected(
612
+ p.add(n.mul(0.1)),
613
+ reflect(rd, n),
614
+ uSky.sample(vec2(suv.x, suv.y.oneMinus())).rgb,
615
+ ).toVar();
616
+ const reflection = select(
617
+ water,
618
+ add(0.25, fres.mul(0.7)),
619
+ uWet.mul(0.05).add(puddle.mul(0.23)).add(fres.mul(0.5)),
620
+ ).toVar();
621
+ color.assign(mix(color, refl, clamp(reflection, 0, 0.94)));
622
+
623
+ // Tiny high-frequency impact coronas, strongest against dark wet tarmac.
624
+
625
+ color.addAssign(
626
+ length(ripple)
627
+ .mul(vec3(0.055, 0.077, 0.09))
628
+ .mul(uRain),
629
+ );
630
+ mat.assign(select(water, 8, select(road, 7, 1)));
631
+ }).ElseIf(mat.greaterThan(0), () => {
632
+ n.assign(getNormal(p, t));
633
+
634
+ If(mat.greaterThan(5.5).and(mat.lessThan(6.5)), () => {
635
+ color.assign(uLampGlow);
636
+ }).Else(() => {
637
+ color.assign(
638
+ surfaceLight(
639
+ p,
640
+ n,
641
+ rd.negate(),
642
+ baseColor(p, mat, n),
643
+ select(mat.greaterThan(3.5), 0.28, 0.88),
644
+ ),
645
+ );
646
+ });
647
+
648
+ const ao = clamp(p.y.sub(terrain(p.xz)).mul(0.09).add(0.5), 0.4, 1).toVar();
649
+
650
+ If(mat.notEqual(6), () => {
651
+ color.mulAssign(ao);
652
+ });
653
+ });
654
+
655
+ If(mat.greaterThan(0).or(ground), () => {
656
+ // Height-dependent aerial perspective with advected rain curtains.
657
+
658
+ const curtain = add(
659
+ 0.8,
660
+ mul(0.4, fbm2(p.xz.mul(0.012).add(vec2(uTime.mul(0.014), 0)))),
661
+ ).toVar();
662
+ const fog = sub(
663
+ 1,
664
+ exp(
665
+ t
666
+ .negate()
667
+ .mul(add(0.0011, uFog.mul(0.0048)))
668
+ .mul(curtain)
669
+ .mul(exp(max(p.y, 0).negate().mul(0.008))),
670
+ ),
671
+ ).toVar();
672
+ color.assign(mix(color, hazeColor(rd), clamp(fog, 0, 0.98)));
673
+ const d = max(0.15, t.mul(dot(rd, uForward))).toVar();
674
+ worldDepth.assign(clamp(sub(1.00006, div(0.150009, d)), 0, 0.999999));
675
+ }).Else(() => {
676
+ worldDepth.assign(1);
677
+ });
678
+
679
+ return vec4(max(color, vec3(0)), worldDepth);
680
+ });