@bendyline/molen-client 0.0.10 → 0.0.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.
package/dist/vehicles.mjs CHANGED
@@ -1,7 +1,263 @@
1
1
  import { t as createModelSignalVisual } from "./model-signals-DLgvjBeZ.mjs";
2
- import { t as createVehicleInteriorVisual } from "./vehicle-interior-DfpZwWEh.mjs";
2
+ import { createGlassVisual } from "./glass.mjs";
3
+ import { t as applyLivery } from "./livery-qcDLzbU9.mjs";
4
+ import { n as aircraftObstacleContacts, r as createAircraftDamageVisual, t as createVehicleInteriorVisual } from "./vehicle-interior-CeyzF5El.mjs";
3
5
  import * as THREE from "three";
4
- import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
6
+ //#region src/vehicle-proxy.ts
7
+ const templates = /* @__PURE__ */ new Map();
8
+ /**
9
+ * Shared street-distance silhouette for parked cars and instanced traffic. Painted faces come
10
+ * first so a static batch can tint the body and roof without tinting the glass, tyres or lamps.
11
+ * 148 triangles, one material, no textures or model downloads. Never dispose the cached result.
12
+ */
13
+ function vehicleProxyGeometry(spec) {
14
+ const { width: w, length: l, height: h, wheelbase, wheelTrack, wheelRadius: r } = spec;
15
+ const key = `${w}:${l}:${h}:${wheelbase}:${wheelTrack}:${r}`;
16
+ const cached = templates.get(key);
17
+ if (cached) return cached;
18
+ const positions = [];
19
+ const colors = [];
20
+ const paint = new THREE.Color("#ffffff");
21
+ const glass = new THREE.Color("#4d6178");
22
+ const tyre = new THREE.Color("#20242a");
23
+ const wheelFace = new THREE.Color("#424950");
24
+ const headlamp = new THREE.Color("#eee9d4");
25
+ const tailLamp = new THREE.Color("#b82323");
26
+ const triangle = (a, b, c, color) => {
27
+ positions.push(...a, ...b, ...c);
28
+ for (let i = 0; i < 3; i++) colors.push(color.r, color.g, color.b);
29
+ };
30
+ const quad = (a, b, c, d, color = paint) => {
31
+ triangle(a, b, d, color);
32
+ triangle(b, c, d, color);
33
+ };
34
+ const bottom = r * 1.2;
35
+ const belt = h * .57;
36
+ const tall = h / w > .9;
37
+ const hatch = tall || l / w < 2.4;
38
+ const rearBase = l * (hatch ? -.44 : -.32);
39
+ const rearRoof = l * (hatch ? -.34 : -.18);
40
+ const frontBase = l * .27;
41
+ const frontRoof = l * (tall ? .15 : .1);
42
+ const rings = [
43
+ [
44
+ -.5,
45
+ .44,
46
+ h * .48
47
+ ],
48
+ [
49
+ rearBase / l,
50
+ .48,
51
+ belt
52
+ ],
53
+ [
54
+ frontBase / l,
55
+ .48,
56
+ belt
57
+ ],
58
+ [
59
+ .5,
60
+ .44,
61
+ h * .46
62
+ ]
63
+ ].map(([z = 0, x = 0, y = 0]) => [
64
+ [
65
+ -w * x,
66
+ bottom,
67
+ l * z
68
+ ],
69
+ [
70
+ w * x,
71
+ bottom,
72
+ l * z
73
+ ],
74
+ [
75
+ w * x,
76
+ y,
77
+ l * z
78
+ ],
79
+ [
80
+ -w * x,
81
+ y,
82
+ l * z
83
+ ]
84
+ ]);
85
+ for (let i = 1; i < rings.length; i++) {
86
+ const a = rings[i - 1];
87
+ const b = rings[i];
88
+ for (let j = 0; j < 4; j++) quad(a[j], a[(j + 1) % 4], b[(j + 1) % 4], b[j]);
89
+ }
90
+ const rear = rings[0];
91
+ const front = rings[3];
92
+ quad(rear[3], rear[2], rear[1], rear[0]);
93
+ quad(front[0], front[1], front[2], front[3]);
94
+ const lower = w * .44;
95
+ const upper = w * .36;
96
+ const back = [
97
+ [
98
+ -lower,
99
+ belt,
100
+ rearBase
101
+ ],
102
+ [
103
+ lower,
104
+ belt,
105
+ rearBase
106
+ ],
107
+ [
108
+ upper,
109
+ h,
110
+ rearRoof
111
+ ],
112
+ [
113
+ -upper,
114
+ h,
115
+ rearRoof
116
+ ]
117
+ ];
118
+ const nose = [
119
+ [
120
+ -lower,
121
+ belt,
122
+ frontBase
123
+ ],
124
+ [
125
+ lower,
126
+ belt,
127
+ frontBase
128
+ ],
129
+ [
130
+ upper,
131
+ h,
132
+ frontRoof
133
+ ],
134
+ [
135
+ -upper,
136
+ h,
137
+ frontRoof
138
+ ]
139
+ ];
140
+ const cabinFaces = [
141
+ [
142
+ back[0],
143
+ nose[0],
144
+ nose[3],
145
+ back[3]
146
+ ],
147
+ [
148
+ nose[1],
149
+ back[1],
150
+ back[2],
151
+ nose[2]
152
+ ],
153
+ [
154
+ back[1],
155
+ back[0],
156
+ back[3],
157
+ back[2]
158
+ ],
159
+ [
160
+ nose[0],
161
+ nose[1],
162
+ nose[2],
163
+ nose[3]
164
+ ]
165
+ ];
166
+ for (const face of cabinFaces) quad(...face);
167
+ quad(back[3], nose[3], nose[2], back[2]);
168
+ const bodyVertices = positions.length / 3;
169
+ for (let side = 0; side < cabinFaces.length; side++) {
170
+ const [a, b, c, d] = cabinFaces[side];
171
+ const normal = new THREE.Vector3().subVectors(new THREE.Vector3(...b), new THREE.Vector3(...a)).cross(new THREE.Vector3().subVectors(new THREE.Vector3(...d), new THREE.Vector3(...a))).normalize().multiplyScalar(.003);
172
+ const point = (u, v) => new THREE.Vector3(...a).lerp(new THREE.Vector3(...b), u).lerp(new THREE.Vector3(...d).lerp(new THREE.Vector3(...c), u), v).add(normal).toArray();
173
+ for (const [start = 0, end = 1] of side < 2 ? [[.06, .47], [.53, .94]] : [[.07, .93]]) quad(point(start, .12), point(end, .12), point(end, .9), point(start, .9), glass);
174
+ }
175
+ const halfTyre = Math.max(w * .05, (w - wheelTrack) / 2);
176
+ for (const sign of [-1, 1]) {
177
+ const inner = sign * (wheelTrack / 2 - halfTyre);
178
+ const outer = sign * (wheelTrack / 2 + halfTyre);
179
+ for (const z of [-wheelbase / 2, wheelbase / 2]) {
180
+ const rim = [];
181
+ const inside = [];
182
+ for (let i = 0; i < 8; i++) {
183
+ const angle = sign * i * Math.PI / 4;
184
+ const y = r + Math.cos(angle) * r;
185
+ const depth = z + Math.sin(angle) * r;
186
+ rim.push([
187
+ outer,
188
+ y,
189
+ depth
190
+ ]);
191
+ inside.push([
192
+ inner,
193
+ y,
194
+ depth
195
+ ]);
196
+ }
197
+ for (let i = 0; i < 8; i++) {
198
+ const j = (i + 1) % 8;
199
+ quad(inside[i], inside[j], rim[j], rim[i], tyre);
200
+ }
201
+ for (let i = 1; i < 7; i++) triangle(rim[0], rim[i], rim[i + 1], wheelFace);
202
+ }
203
+ }
204
+ for (const end of [-1, 1]) {
205
+ const z = end * (l / 2 + .003);
206
+ const y = h * (end > 0 ? .46 : .48);
207
+ for (const side of [-1, 1]) {
208
+ const x = side * w * .31;
209
+ const a = [
210
+ x - w * .095,
211
+ y - h * .1,
212
+ z
213
+ ];
214
+ const b = [
215
+ x + w * .095,
216
+ y - h * .1,
217
+ z
218
+ ];
219
+ const c = [
220
+ b[0],
221
+ y - h * .025,
222
+ z
223
+ ];
224
+ const d = [
225
+ a[0],
226
+ c[1],
227
+ z
228
+ ];
229
+ if (end > 0) quad(a, b, c, d, headlamp);
230
+ else quad(d, c, b, a, tailLamp);
231
+ }
232
+ }
233
+ quad([
234
+ -w * .18,
235
+ bottom,
236
+ l / 2 + .004
237
+ ], [
238
+ w * .18,
239
+ bottom,
240
+ l / 2 + .004
241
+ ], [
242
+ w * .18,
243
+ h * .36,
244
+ l / 2 + .004
245
+ ], [
246
+ -w * .18,
247
+ h * .36,
248
+ l / 2 + .004
249
+ ], tyre);
250
+ const geometry = new THREE.BufferGeometry();
251
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
252
+ geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
253
+ geometry.computeVertexNormals();
254
+ geometry.computeBoundingBox();
255
+ geometry.computeBoundingSphere();
256
+ geometry.userData.bodyVertices = bodyVertices;
257
+ templates.set(key, geometry);
258
+ return geometry;
259
+ }
260
+ //#endregion
5
261
  //#region src/vehicle-camera.ts
6
262
  /** Seat-space cockpit and collision-shortened chase views for any vehicle host. */
7
263
  function vehicleCameraPose(transform, spec, options) {
@@ -42,14 +298,14 @@ const travel = new THREE.Vector3();
42
298
  * each moves along the body's up axis to keep its centre at its resting height, so the outside
43
299
  * wheels stay on the road in a turn rather than sinking into it.
44
300
  */
45
- function createVehicleVisual(model, vehicle) {
301
+ function createVehicleVisual(model, vehicle, livery, glassData) {
46
302
  const interior = vehicle.visual.interior === void 0 ? void 0 : createVehicleInteriorVisual(model, vehicle.visual.interior);
47
303
  const animation = vehicle.visual.animation === void 0 ? void 0 : createModelSignalVisual(model, vehicle.visual.animation);
48
304
  const nodes = /* @__PURE__ */ new Map();
49
305
  const frontWheels = new Set(vehicle.visual.frontWheelNodes);
50
306
  const wheels = [];
51
307
  let steeringWheel;
52
- const color = vehicle.color !== void 0 ? new THREE.Color(vehicle.color) : void 0;
308
+ const color = livery === void 0 && vehicle.color !== void 0 ? new THREE.Color(vehicle.color) : void 0;
53
309
  model.traverse((object) => {
54
310
  object.userData.vehicleGeometry = true;
55
311
  const named = nodes.get(object.name) ?? [];
@@ -80,11 +336,20 @@ function createVehicleVisual(model, vehicle) {
80
336
  });
81
337
  }
82
338
  if (vehicle.visual.steeringWheelNode !== void 0) steeringWheel = nodes.get(vehicle.visual.steeringWheelNode)?.[0];
339
+ let appearance = livery === void 0 ? void 0 : applyLivery(model, livery);
340
+ const glass = glassData === void 0 ? void 0 : createGlassVisual(model, glassData);
341
+ const damage = createAircraftDamageVisual(model, { collisionParts: vehicle.spec?.collisionParts });
83
342
  return {
343
+ setLivery(data) {
344
+ appearance?.dispose();
345
+ damage.reset();
346
+ appearance = data === void 0 ? void 0 : applyLivery(model, data);
347
+ },
84
348
  object: model,
85
349
  interior,
86
350
  animation,
87
- update(steer, angle, signals, timeSeconds) {
351
+ glass,
352
+ update(steer, angle, signals, timeSeconds, glassState, damageState) {
88
353
  const roll = signals?.bodyRoll ?? 0;
89
354
  const pitch = signals?.bodyPitch ?? 0;
90
355
  const lean = Number.isFinite(roll) && Number.isFinite(pitch) && (roll !== 0 || pitch !== 0);
@@ -107,8 +372,13 @@ function createVehicleVisual(model, vehicle) {
107
372
  };
108
373
  interior?.update(telemetry, timeSeconds);
109
374
  animation?.update(telemetry, timeSeconds);
375
+ glass?.update(glassState, timeSeconds ?? 0);
376
+ damage.update(damageState ?? {});
110
377
  },
111
378
  dispose() {
379
+ damage.reset();
380
+ glass?.dispose();
381
+ appearance?.dispose();
112
382
  const geometries = /* @__PURE__ */ new Set();
113
383
  const materials = /* @__PURE__ */ new Set();
114
384
  model.traverse((object) => {
@@ -127,60 +397,9 @@ const parkedMaterial = new THREE.MeshStandardMaterial({
127
397
  roughness: .55,
128
398
  metalness: .12
129
399
  });
130
- const parkedTemplates = /* @__PURE__ */ new Map();
131
- function coloredBox(x, y, z, width, height, length, color) {
132
- const geometry = new THREE.BoxGeometry(width, height, length).translate(x, y, z).toNonIndexed();
133
- geometry.deleteAttribute("uv");
134
- const tint = new THREE.Color(color);
135
- const colors = new Float32Array(geometry.getAttribute("position").count * 3);
136
- for (let i = 0; i < colors.length; i += 3) {
137
- colors[i] = tint.r;
138
- colors[i + 1] = tint.g;
139
- colors[i + 2] = tint.b;
140
- }
141
- geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
142
- return geometry;
143
- }
144
- /**
145
- * The parked-car proxy shape with a white body, for instanced rendering: per-instance paint comes
146
- * from `instanceColor` (three multiplies vertex colours by it, so glass and tyres stay dark).
147
- * Cached by dimensions; never dispose the returned geometry.
148
- */
149
- function vehicleProxyGeometry(dimensions) {
150
- return parkedTemplate(dimensions, "#ffffff");
151
- }
400
+ const paints = /* @__PURE__ */ new Map();
152
401
  /** The shared vertex-coloured material of parked and proxy vehicles. */
153
402
  const vehicleProxyMaterial = parkedMaterial;
154
- /** Generic dimension-driven proxy used only for large dormant parking batches. */
155
- function parkedTemplate(spec, color) {
156
- const key = `${spec.width}:${spec.length}:${spec.height}:${spec.wheelbase}:${color}`;
157
- const cached = parkedTemplates.get(key);
158
- if (cached !== void 0) return cached;
159
- const parts = [coloredBox(0, spec.height * .34, 0, spec.width, spec.height * .48, spec.length, color), coloredBox(0, spec.height * .72, -spec.length * .06, spec.width * .82, spec.height * .42, spec.length * .52, "#526b78")];
160
- for (const x of [-spec.wheelTrack / 2, spec.wheelTrack / 2]) for (const z of [-spec.wheelbase / 2, spec.wheelbase / 2]) {
161
- const wheel = new THREE.CylinderGeometry(spec.wheelRadius, spec.wheelRadius, spec.width * .1, 8);
162
- wheel.rotateZ(Math.PI / 2);
163
- wheel.translate(x, spec.wheelRadius, z);
164
- const wheelGeometry = wheel.toNonIndexed();
165
- wheelGeometry.deleteAttribute("uv");
166
- const charcoal = new THREE.Color("#20242a");
167
- const colors = new Float32Array(wheelGeometry.getAttribute("position").count * 3);
168
- for (let i = 0; i < colors.length; i += 3) {
169
- colors[i] = charcoal.r;
170
- colors[i + 1] = charcoal.g;
171
- colors[i + 2] = charcoal.b;
172
- }
173
- wheelGeometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
174
- parts.push(wheelGeometry);
175
- wheel.dispose();
176
- }
177
- const geometry = mergeGeometries(parts);
178
- for (const part of parts) part.dispose();
179
- geometry.computeBoundingBox();
180
- geometry.computeBoundingSphere();
181
- parkedTemplates.set(key, geometry);
182
- return geometry;
183
- }
184
403
  /**
185
404
  * Dormant terrain-owned cars merged into one static mesh per tile. A plain mesh shares its
186
405
  * compiled shader with every other tile; an instanced batch per model and paint would build one
@@ -191,33 +410,60 @@ function createParkedVehicleBatch(placements, origin = [0, 0]) {
191
410
  const group = new THREE.Group();
192
411
  group.name = "surface:parked-cars";
193
412
  if (placements.length === 0) return group;
194
- const templates = placements.map((placement) => parkedTemplate(placement.spec, placement.color));
413
+ const templates = placements.map((placement) => vehicleProxyGeometry(placement.spec));
195
414
  const vertices = templates.reduce((sum, template) => sum + template.getAttribute("position").count, 0);
196
415
  const positions = new Float32Array(vertices * 3);
197
416
  const normals = new Float32Array(vertices * 3);
198
417
  const colors = new Float32Array(vertices * 3);
199
418
  const ranges = new Uint32Array(placements.length * 2);
200
419
  const matrix = new THREE.Matrix4();
201
- const normalMatrix = new THREE.Matrix3();
202
420
  const rotation = new THREE.Quaternion();
203
421
  const position = new THREE.Vector3();
204
422
  const scale = new THREE.Vector3(1, 1, 1);
205
- const vertex = new THREE.Vector3();
423
+ const min = new THREE.Vector3(Infinity, Infinity, Infinity);
424
+ const max = new THREE.Vector3(-Infinity, -Infinity, -Infinity);
206
425
  let offset = 0;
207
426
  placements.forEach((placement, index) => {
208
427
  const template = templates[index];
209
428
  position.set(placement.position[0] - origin[0], placement.position[1], placement.position[2] - origin[1]);
210
429
  rotation.setFromEuler(new THREE.Euler(placement.pitch ?? 0, placement.yaw, placement.roll ?? 0, "YXZ"));
211
430
  matrix.compose(position, rotation, scale);
212
- normalMatrix.getNormalMatrix(matrix);
213
- const source = template.getAttribute("position");
214
- const sourceNormals = template.getAttribute("normal");
215
- const sourceColors = template.getAttribute("color");
216
- for (let i = 0; i < source.count; i++) {
217
- vertex.fromBufferAttribute(source, i).applyMatrix4(matrix).toArray(positions, (offset + i) * 3);
218
- vertex.fromBufferAttribute(sourceNormals, i).applyMatrix3(normalMatrix).normalize().toArray(normals, (offset + i) * 3);
219
- vertex.fromBufferAttribute(sourceColors, i).toArray(colors, (offset + i) * 3);
431
+ const e = matrix.elements;
432
+ const points = template.getAttribute("position").array;
433
+ const sourceNormals = template.getAttribute("normal").array;
434
+ const base = offset * 3;
435
+ for (let i = 0; i < points.length; i += 3) {
436
+ const x = points[i], y = points[i + 1], z = points[i + 2];
437
+ const px = Math.fround(e[0] * x + e[4] * y + e[8] * z + e[12]);
438
+ const py = Math.fround(e[1] * x + e[5] * y + e[9] * z + e[13]);
439
+ const pz = Math.fround(e[2] * x + e[6] * y + e[10] * z + e[14]);
440
+ positions[base + i] = px;
441
+ positions[base + i + 1] = py;
442
+ positions[base + i + 2] = pz;
443
+ if (px < min.x) min.x = px;
444
+ if (px > max.x) max.x = px;
445
+ if (py < min.y) min.y = py;
446
+ if (py > max.y) max.y = py;
447
+ if (pz < min.z) min.z = pz;
448
+ if (pz > max.z) max.z = pz;
449
+ const nx = sourceNormals[i], ny = sourceNormals[i + 1], nz = sourceNormals[i + 2];
450
+ normals[base + i] = e[0] * nx + e[4] * ny + e[8] * nz;
451
+ normals[base + i + 1] = e[1] * nx + e[5] * ny + e[9] * nz;
452
+ normals[base + i + 2] = e[2] * nx + e[6] * ny + e[10] * nz;
453
+ }
454
+ colors.set(template.getAttribute("color").array, base);
455
+ let paint = paints.get(placement.color);
456
+ if (paint === void 0) {
457
+ paint = new THREE.Color(placement.color);
458
+ paints.set(placement.color, paint);
220
459
  }
460
+ const body = base + template.userData.bodyVertices * 3;
461
+ for (let i = base; i < body; i += 3) {
462
+ colors[i] = paint.r;
463
+ colors[i + 1] = paint.g;
464
+ colors[i + 2] = paint.b;
465
+ }
466
+ const source = template.getAttribute("position");
221
467
  ranges[index * 2] = offset;
222
468
  ranges[index * 2 + 1] = source.count;
223
469
  offset += source.count;
@@ -226,8 +472,8 @@ function createParkedVehicleBatch(placements, origin = [0, 0]) {
226
472
  geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
227
473
  geometry.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
228
474
  geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
229
- geometry.computeBoundingBox();
230
- geometry.computeBoundingSphere();
475
+ geometry.boundingBox = new THREE.Box3(min, max);
476
+ geometry.boundingSphere = geometry.boundingBox.getBoundingSphere(new THREE.Sphere());
231
477
  const mesh = new THREE.Mesh(geometry, parkedMaterial);
232
478
  mesh.name = "vehicle:parked";
233
479
  mesh.userData.vehicleIds = placements.map((placement) => placement.id);
@@ -272,4 +518,4 @@ function setParkedVehicleHidden(batch, index, hidden) {
272
518
  return true;
273
519
  }
274
520
  //#endregion
275
- export { createParkedVehicleBatch, createVehicleInteriorVisual, createVehicleVisual, isParkedVehicleHidden, setParkedVehicleHidden, vehicleCameraPose, vehicleProxyGeometry, vehicleProxyMaterial };
521
+ export { createParkedVehicleBatch, createVehicleInteriorVisual, createVehicleVisual, isParkedVehicleHidden, setParkedVehicleHidden, vehicleCameraPose, aircraftObstacleContacts as vehicleObstacleContacts, vehicleProxyGeometry, vehicleProxyMaterial };
@@ -0,0 +1,186 @@
1
+ import "./clouds-DmASLVoe.mjs";
2
+ import * as THREE from "three";
3
+ import { Break, Discard, Fn, If, Loop, abs, attribute, cameraPosition, clamp, dot, exp, exp2, float, floor, fract, lightPosition, lightTargetPosition, log2, max, min, mix, normalize, positionWorld, pow, renderGroup, screenCoordinate, select, smoothstep, texture, texture3D, uniform, vec2, vec3, vec4 } from "three/tsl";
4
+ import { DirectionalLightNode, MeshBasicNodeMaterial } from "three/webgpu";
5
+ //#region src/three/webgpu-clouds.ts
6
+ /** WebGPU deck material reading `u` and `t` by reference. */
7
+ function createWebGpuCloudMaterial(u, t) {
8
+ const slab = uniform(u.slab);
9
+ const field = uniform(u.field);
10
+ const weights = uniform(u.mix);
11
+ const form = uniform(u.form);
12
+ const erode = uniform(u.erode);
13
+ const relief = uniform(u.relief);
14
+ const shapeUv = uniform(u.shape);
15
+ const detailUv = uniform(u.detail);
16
+ const march = uniform(u.march);
17
+ const sun = uniform(u.sun);
18
+ const sunColor = uniform(u.sunColor);
19
+ const ambientTop = uniform(u.ambientTop);
20
+ const ambientBottom = uniform(u.ambientBottom);
21
+ const phase = uniform(u.phase);
22
+ const fog = uniform(u.fog);
23
+ const fogColor = uniform(u.fogColor);
24
+ const density = (detailed) => Fn(([p, lod]) => {
25
+ const d = float(0).toVar();
26
+ const h = p.y.sub(slab.x).mul(slab.z).toVar();
27
+ If(h.greaterThan(0).and(h.lessThan(1)), () => {
28
+ const uv = p.xz.mul(field.x).add(field.yz).toVar();
29
+ const w = texture(t.weather, uv, lod).toVar();
30
+ const strength = dot(w, weights).toVar();
31
+ const soft = erode.z.mul(lod.mul(.6).add(1));
32
+ const threshold = field.w.mul(soft.add(1)).oneMinus();
33
+ const hn = h.div(mix(form.x, 1, w.g).mul(relief.x.mul(strength.oneMinus()).oneMinus())).toVar();
34
+ If(hn.lessThan(1), () => {
35
+ const spread = form.w.mul(smoothstep(.55, .9, hn)).toVar();
36
+ If(spread.greaterThan(0), () => {
37
+ const wide = dot(texture(t.weather, uv, lod.add(relief.z)), weights).add(relief.w);
38
+ strength.assign(mix(strength, max(strength, wide), spread));
39
+ });
40
+ const cover = clamp(strength.sub(threshold).sub(relief.y.mul(hn).mul(spread.oneMinus())).div(soft), 0, 1);
41
+ const profile = smoothstep(0, form.y, hn).mul(smoothstep(form.z, 1, hn).oneMinus());
42
+ const body = cover.mul(mix(profile, 1, spread.mul(.5))).toVar();
43
+ If(body.greaterThan(0), () => {
44
+ const carve = erode.x.mul(mix(.3, 1, smoothstep(0, .3, hn))).mul(smoothstep(2, 4.5, lod).oneMinus()).toVar();
45
+ const s = float(1).toVar();
46
+ If(carve.greaterThan(0), () => {
47
+ s.assign(mix(1, texture3D(t.shape, p.mul(shapeUv.x).add(shapeUv.yzw), 0).r, carve));
48
+ });
49
+ d.assign(clamp(s.sub(1).add(body).div(max(body, .001)), 0, 1).mul(body));
50
+ if (detailed) If(erode.y.greaterThan(0).and(d.greaterThan(0)).and(lod.lessThan(2)), () => {
51
+ const n = texture3D(t.detail, p.mul(detailUv.x).add(detailUv.yzw), 0).r.toVar();
52
+ const wear = mix(n, n.oneMinus(), clamp(hn.mul(3), 0, 1)).mul(erode.y).toVar();
53
+ d.assign(clamp(d.sub(wear).div(wear.oneMinus()), 0, 1));
54
+ });
55
+ });
56
+ });
57
+ });
58
+ return d;
59
+ });
60
+ const full = density(true);
61
+ const cheap = density(false);
62
+ const lightDepth = Fn(([p, lod]) => {
63
+ const edge = select(sun.y.greaterThan(0), slab.y, slab.x);
64
+ const len = select(abs(sun.y).greaterThan(1e-4), min(sun.w, edge.sub(p.y).div(sun.y)), sun.w).div(exp2(march.y).sub(1)).toVar();
65
+ const depth = float(0).toVar();
66
+ const along = float(0).toVar();
67
+ Loop(6, ({ i }) => {
68
+ If(float(i).greaterThanEqual(march.y), () => {
69
+ Break();
70
+ });
71
+ const q = p.add(sun.xyz.mul(along.add(len.mul(.5))));
72
+ depth.addAssign(cheap(q, lod).mul(len));
73
+ along.addAssign(len);
74
+ len.mulAssign(2);
75
+ });
76
+ return depth.mul(march.w);
77
+ });
78
+ const hg = (c, g) => {
79
+ const g2 = g.mul(g);
80
+ return g2.oneMinus().div(pow(max(g2.add(1).sub(g.mul(c).mul(2)), 1e-4), 1.5));
81
+ };
82
+ const cloud = Fn(() => {
83
+ const ro = cameraPosition;
84
+ const toProxy = positionWorld.sub(ro).toVar();
85
+ const t0 = toProxy.length().toVar();
86
+ const rd = toProxy.div(t0).toVar();
87
+ const range = attribute("cloudRange", "vec2");
88
+ const enter = float(0).toVar();
89
+ const leave = float(1e9).toVar();
90
+ If(abs(rd.y).greaterThan(1e-5), () => {
91
+ const tb = slab.x.sub(ro.y).div(rd.y);
92
+ const tt = slab.y.sub(ro.y).div(rd.y);
93
+ enter.assign(max(min(tb, tt), 0));
94
+ leave.assign(max(tb, tt));
95
+ }).ElseIf(ro.y.lessThanEqual(slab.x).or(ro.y.greaterThanEqual(slab.y)), () => {
96
+ leave.assign(-1);
97
+ });
98
+ const a = max(t0, enter).toVar();
99
+ const b = min(min(leave, range.x), min(slab.w, a.add(march.z))).toVar();
100
+ const steps = max(2, floor(march.x.mul(range.y).add(.5))).toVar();
101
+ const c = dot(rd, sun.xyz);
102
+ const lobes = mix(hg(c, phase.x), hg(c, phase.y), phase.z).toVar();
103
+ const jitter = fract(fract(dot(screenCoordinate.xy, vec2(.06711056, .00583715))).mul(52.9829189)).toVar();
104
+ const span = mix(6, 1.5, smoothstep(2, 8, b.sub(a).mul(slab.z))).toVar();
105
+ const growth = pow(span, float(1).div(steps)).toVar();
106
+ const offset = pow(growth, jitter);
107
+ const first = b.sub(a).mul(growth.sub(1)).div(span.sub(1));
108
+ const ds = first.mul(offset).toVar();
109
+ const t = a.add(first.mul(offset.sub(1)).div(growth.sub(1))).toVar();
110
+ const transmittance = float(1).toVar();
111
+ const color = vec3(0).toVar();
112
+ If(b.greaterThan(a), () => {
113
+ Loop(64, ({ i }) => {
114
+ If(float(i).greaterThanEqual(steps).or(transmittance.lessThan(.01)), () => {
115
+ Break();
116
+ });
117
+ const p = ro.add(rd.mul(t)).toVar();
118
+ const length = float(ds).toVar();
119
+ t.addAssign(ds);
120
+ ds.mulAssign(growth);
121
+ const lod = clamp(log2(length.mul(field.x).mul(256)), 0, 6).toVar();
122
+ const d = full(p, lod).toVar();
123
+ If(d.greaterThan(0), () => {
124
+ const depth = lightDepth(p, lod).toVar();
125
+ const sunlight = exp(depth.negate()).mul(lobes).add(float(.3).div(depth.mul(.08).add(1)));
126
+ const h = p.y.sub(slab.x).mul(slab.z);
127
+ const light = sunColor.mul(sunlight).add(mix(ambientBottom, ambientTop, h));
128
+ const step = exp(d.mul(march.w).mul(length).negate()).toVar();
129
+ color.addAssign(light.mul(transmittance.mul(step.oneMinus())));
130
+ transmittance.mulAssign(step);
131
+ });
132
+ });
133
+ });
134
+ const opacity = transmittance.oneMinus().toVar();
135
+ const alpha = opacity.mul(smoothstep(slab.w.mul(phase.w), slab.w, a).oneMinus()).toVar();
136
+ Discard(alpha.lessThan(.002));
137
+ const hazeDepth = a.mul(mix(1, .35, smoothstep(.05, .5, abs(rd.y))));
138
+ const hazed = select(fog.w.lessThan(.5), float(0), select(fog.w.lessThan(1.5), smoothstep(fog.x, fog.y, hazeDepth), exp(fog.z.mul(fog.z).mul(hazeDepth).mul(hazeDepth).negate()).oneMinus()));
139
+ return vec4(mix(color.div(max(opacity, 1e-4)), fogColor.xyz, hazed), alpha.mul(hazed.mul(fogColor.w).oneMinus()));
140
+ });
141
+ const material = new MeshBasicNodeMaterial({
142
+ name: "molen:clouds",
143
+ transparent: true,
144
+ depthWrite: false,
145
+ side: THREE.DoubleSide,
146
+ fog: false
147
+ });
148
+ material.colorNode = cloud();
149
+ material.forceSinglePass = true;
150
+ return material;
151
+ }
152
+ /**
153
+ * Multiply every directional light by the cloud shadow, as the WebGL hook in cloud-shadows.ts
154
+ * does. Call before the renderer builds its first lit material.
155
+ */
156
+ function installWebGpuCloudShadows(renderer, uniforms) {
157
+ const map = uniforms.molenCloudMap.value;
158
+ const shadow = uniform(uniforms.molenCloudShadow.value).setGroup(renderGroup);
159
+ const field = uniform(uniforms.molenCloudField.value).setGroup(renderGroup);
160
+ const weights = uniform(uniforms.molenCloudMix.value).setGroup(renderGroup);
161
+ const fit = uniform(uniforms.molenCloudFit.value).setGroup(renderGroup);
162
+ const factor = (light) => {
163
+ const toLight = normalize(lightPosition(light).sub(lightTargetPosition(light))).toVar();
164
+ const world = positionWorld;
165
+ const cast = mix(shadow.y, shadow.z, fit.z);
166
+ const w = texture(map, world.xz.add(toLight.xz.mul(max(cast.sub(world.y), 0).div(max(toLight.y, .05)))).mul(field.x).add(field.yz), 0);
167
+ const soft = fit.x;
168
+ const cell = dot(w, weights).toVar();
169
+ const cover = clamp(cell.add(field.w.mul(soft.add(1))).sub(1).div(soft), 0, 1).mul(fit.w.mul(cell.oneMinus()).oneMinus());
170
+ const strength = select(toLight.y.greaterThan(0), shadow.x.mul(smoothstep(shadow.y, shadow.z, world.y).oneMinus()), float(0));
171
+ return mix(float(1), exp(cover.mul(fit.y).negate()), strength);
172
+ };
173
+ class CloudShadowedLightNode extends DirectionalLightNode {
174
+ setupDirect(builder) {
175
+ const direct = super.setupDirect(builder);
176
+ const light = this.light;
177
+ return {
178
+ ...direct,
179
+ lightColor: direct.lightColor.mul(factor(light))
180
+ };
181
+ }
182
+ }
183
+ renderer.library?.lightNodes?.set(THREE.DirectionalLight, CloudShadowedLightNode);
184
+ }
185
+ //#endregion
186
+ export { createWebGpuCloudMaterial, installWebGpuCloudShadows };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bendyline/molen-client",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -29,6 +29,10 @@
29
29
  "audio"
30
30
  ],
31
31
  "exports": {
32
+ "./glass": {
33
+ "types": "./dist/glass.d.mts",
34
+ "default": "./dist/glass.mjs"
35
+ },
32
36
  "./text": {
33
37
  "types": "./dist/text.d.mts",
34
38
  "default": "./dist/text.mjs"
@@ -75,8 +79,8 @@
75
79
  "NOTICE.md"
76
80
  ],
77
81
  "dependencies": {
78
- "@bendyline/molen-materials": "0.0.9",
79
- "@bendyline/molen-schema": "0.0.8",
82
+ "@bendyline/molen-materials": "0.0.11",
83
+ "@bendyline/molen-schema": "0.0.10",
80
84
  "zod": "^4.6.5"
81
85
  },
82
86
  "peerDependencies": {