@bendyline/molen-client 0.0.12 → 0.0.15

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.
@@ -1,7 +1,8 @@
1
1
  import { n as installWebGpuTimestampGuard } from "./webgpu-frame-timer-Xnuq1Hld.mjs";
2
- import { instanceIndex, objectGroup } from "three/tsl";
2
+ import { a as isInstanceBatch, n as INSTANCE_BATCH_MATRIX_COLUMNS, o as isInstanceBatchGeometry, t as INSTANCE_BATCH_COLOR } from "./instance-batch-Ds-oKQW4.mjs";
3
+ import { attribute, instanceIndex, mat4, normalLocal, objectGroup, positionLocal, transformNormal, varyingProperty } from "three/tsl";
3
4
  import * as WebGPU from "three/webgpu";
4
- import { CanvasTarget, StorageBufferNode, WebGPUBackend, WebGPURenderer } from "three/webgpu";
5
+ import { CanvasTarget, NodeMaterial, StorageBufferNode, WebGPUBackend, WebGPURenderer } from "three/webgpu";
5
6
  //#region src/three/webgpu-depth-bias.ts
6
7
  const guardedBackends = /* @__PURE__ */ new WeakSet();
7
8
  /**
@@ -35,6 +36,83 @@ function installWebGpuDepthBiasGuard(renderer) {
35
36
  };
36
37
  }
37
38
  //#endregion
39
+ //#region src/three/webgpu-instance-batches.ts
40
+ const patchedMaterials = /* @__PURE__ */ new WeakSet();
41
+ const patchedRenderObjects = /* @__PURE__ */ new WeakSet();
42
+ const patchedFactories = /* @__PURE__ */ new WeakSet();
43
+ const keyViews = /* @__PURE__ */ new WeakMap();
44
+ /**
45
+ * Draw `InstanceBatch`es with one node build per material and model layout (see `InstanceBatch`).
46
+ *
47
+ * - Position setup reads a batch's instance matrix (and color) from its geometry by attribute name,
48
+ * as three's instanced mesh node does from the mesh's own buffer. Every node material and the
49
+ * shadow pass's material run it, so a batch casts its instances' shadows.
50
+ * - A render object's cache key names its mesh when that mesh is instanced (r184–r186 append its
51
+ * `uuid`), because three's instance node is bound to the mesh. A batch's instances are vertex
52
+ * buffers each render object binds for itself, so its key is computed as a plain mesh's.
53
+ *
54
+ * Three's render objects are reached through the first one the renderer creates; a batch drawn
55
+ * before that keeps a key of its own, which is correct, only not shared.
56
+ */
57
+ function installWebGpuInstanceBatches(renderer, nodes) {
58
+ installPositionSetup(nodes);
59
+ const objects = renderer._objects;
60
+ if (objects == null || patchedFactories.has(objects)) return;
61
+ patchedFactories.add(objects);
62
+ const create = objects.createRenderObject;
63
+ objects.createRenderObject = (...args) => {
64
+ const renderObject = create.apply(objects, args);
65
+ installCacheKey(Object.getPrototypeOf(renderObject));
66
+ return renderObject;
67
+ };
68
+ }
69
+ function installPositionSetup(nodes) {
70
+ const prototype = nodes.NodeMaterial.prototype;
71
+ const setupPosition = prototype.setupPosition;
72
+ if (typeof setupPosition !== "function" || patchedMaterials.has(prototype)) return;
73
+ patchedMaterials.add(prototype);
74
+ const { attribute, mat4, transformNormal, varyingProperty } = nodes;
75
+ const positionLocal = nodes.positionLocal;
76
+ const normalLocal = nodes.normalLocal;
77
+ prototype.setupPosition = function(builder) {
78
+ if (isInstanceBatch(builder.object)) {
79
+ const matrix = mat4(...INSTANCE_BATCH_MATRIX_COLUMNS.map((name) => attribute(name, "vec4")));
80
+ positionLocal.assign(matrix.mul(positionLocal).xyz);
81
+ if (builder.hasGeometryAttribute("normal")) normalLocal.assign(transformNormal(normalLocal, matrix));
82
+ if (builder.hasGeometryAttribute("instanceColor")) varyingProperty("vec3", "vInstanceColor").assign(attribute(INSTANCE_BATCH_COLOR, "vec3"));
83
+ }
84
+ return setupPosition.call(this, builder);
85
+ };
86
+ }
87
+ function installCacheKey(prototype) {
88
+ if (patchedRenderObjects.has(prototype)) return;
89
+ patchedRenderObjects.add(prototype);
90
+ const getMaterialCacheKey = prototype.getMaterialCacheKey;
91
+ if (typeof getMaterialCacheKey !== "function") return;
92
+ prototype.getMaterialCacheKey = function() {
93
+ const object = this.object;
94
+ if (!isInstanceBatch(object)) return getMaterialCacheKey.call(this);
95
+ this.object = keyView(object);
96
+ try {
97
+ return getMaterialCacheKey.call(this);
98
+ } finally {
99
+ this.object = object;
100
+ }
101
+ };
102
+ }
103
+ /** The batch as its cache key should see it: a plain mesh with one instance. */
104
+ function keyView(object) {
105
+ let view = keyViews.get(object);
106
+ if (view === void 0) {
107
+ view = Object.create(object, {
108
+ isInstancedMesh: { value: false },
109
+ count: { value: 1 }
110
+ });
111
+ keyViews.set(object, view);
112
+ }
113
+ return view;
114
+ }
115
+ //#endregion
38
116
  //#region src/three/webgpu-render-object-release.ts
39
117
  const guarded = /* @__PURE__ */ new WeakSet();
40
118
  /**
@@ -99,13 +177,25 @@ function installWebGpuRenderObjectRelease(renderer) {
99
177
  return renderObject;
100
178
  };
101
179
  }
102
- /** A stand-in that reads the live render object while it exists and its last attributes after. */
180
+ /**
181
+ * A stand-in that reads the live render object while it exists and its last attributes after.
182
+ * An instance batch's geometry shares its model's buffers with every other batch of that model;
183
+ * it hides them while it is disposed (see `InstanceBatch.dispose`), and only what it still holds
184
+ * is freed.
185
+ */
103
186
  function geometryOwner(renderObject) {
104
187
  const ref = new WeakRef(renderObject);
105
188
  const attributes = renderObject.getAttributes();
189
+ const geometry = renderObject.geometry;
190
+ const batch = isInstanceBatchGeometry(geometry);
106
191
  return {
107
192
  geometry: renderObject.geometry,
108
- getAttributes: () => ref.deref()?.getAttributes() ?? attributes
193
+ getAttributes: () => {
194
+ const listed = ref.deref()?.getAttributes() ?? attributes;
195
+ if (!batch) return listed;
196
+ const held = new Set(Object.values(geometry.attributes));
197
+ return listed.filter((attribute) => held.has(attribute));
198
+ }
109
199
  };
110
200
  }
111
201
  /**
@@ -178,6 +268,15 @@ async function createWebGpuDriver(options) {
178
268
  installWebGpuTimestampGuard(renderer);
179
269
  installWebGpuDepthBiasGuard(renderer);
180
270
  installWebGpuRenderObjectRelease(renderer);
271
+ installWebGpuInstanceBatches(renderer, {
272
+ NodeMaterial,
273
+ attribute,
274
+ mat4,
275
+ positionLocal,
276
+ normalLocal,
277
+ transformNormal,
278
+ varyingProperty
279
+ });
181
280
  installWebGpuSharedInstancing({
182
281
  InstanceNode: Reflect.get(WebGPU, "InstanceNode"),
183
282
  StorageBufferNode,
@@ -0,0 +1,55 @@
1
+ import { c as snowCoverAmount, d as snowSurfaceNode, l as snowCoverFacing } from "./snow-cover-Bh0woabf.mjs";
2
+ import * as tsl from "three/tsl";
3
+ import { MeshStandardNodeMaterial, NodeMaterial } from "three/webgpu";
4
+ //#region src/three/webgpu-snow-cover.ts
5
+ const patched = /* @__PURE__ */ new WeakSet();
6
+ const registered = /* @__PURE__ */ new WeakMap();
7
+ /**
8
+ * Settle the weather's lying snow on every lit node material marked with `enableSnowCover`, as
9
+ * the WebGL hook in weather/snow-cover.ts does. Call before the renderer builds its first lit
10
+ * material.
11
+ */
12
+ function installWebGpuSnowCover(renderer, uniforms) {
13
+ registered.set(renderer, {
14
+ snow: tsl.uniform(uniforms.molenSnowCover.value).setGroup(tsl.renderGroup),
15
+ origin: tsl.uniform(uniforms.molenSnowOrigin.value).setGroup(tsl.renderGroup)
16
+ });
17
+ const base = NodeMaterial.prototype;
18
+ const standard = MeshStandardNodeMaterial.prototype;
19
+ const { setupDiffuseColor } = base;
20
+ const { setupVariants } = standard;
21
+ if (typeof setupDiffuseColor !== "function" || patched.has(base)) return;
22
+ patched.add(base);
23
+ const dusting = tsl.property("float", "molenSnow");
24
+ const dusted = /* @__PURE__ */ new WeakSet();
25
+ base.setupDiffuseColor = function(builder) {
26
+ setupDiffuseColor.call(this, builder);
27
+ const nodes = registered.get(builder.renderer);
28
+ const amount = snowCoverAmount(this);
29
+ if (nodes === void 0 || amount === 0 || this.lights === false) return;
30
+ const { cover, color } = snowSurfaceNode(tsl, tsl.positionWorld.xz.add(nodes.origin), tsl.positionWorld.y, tsl.normalWorld.y, nodes.snow, amount, snowCoverFacing(this));
31
+ dusting.assign(cover);
32
+ dusted.add(builder);
33
+ tsl.diffuseColor.rgb.assign(tsl.mix(tsl.diffuseColor.rgb, color, dusting));
34
+ };
35
+ if (typeof setupVariants !== "function") return;
36
+ standard.setupVariants = function(builder) {
37
+ if (!dusted.has(builder)) {
38
+ setupVariants.call(this, builder);
39
+ return;
40
+ }
41
+ const { metalnessNode, roughnessNode } = this;
42
+ const metalness = metalnessNode ? tsl.float(metalnessNode) : tsl.materialMetalness;
43
+ const roughness = roughnessNode ? tsl.float(roughnessNode) : tsl.materialRoughness;
44
+ this.metalnessNode = metalness.mul(dusting.oneMinus());
45
+ this.roughnessNode = tsl.mix(roughness, .9, dusting);
46
+ try {
47
+ setupVariants.call(this, builder);
48
+ } finally {
49
+ this.metalnessNode = metalnessNode;
50
+ this.roughnessNode = roughnessNode;
51
+ }
52
+ };
53
+ }
54
+ //#endregion
55
+ export { installWebGpuSnowCover };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bendyline/molen-client",
3
- "version": "0.0.12",
3
+ "version": "0.0.15",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -49,6 +49,10 @@
49
49
  "types": "./dist/vite.d.mts",
50
50
  "default": "./dist/vite.mjs"
51
51
  },
52
+ "./instancing": {
53
+ "types": "./dist/instancing.d.mts",
54
+ "default": "./dist/instancing.mjs"
55
+ },
52
56
  "./vehicles": {
53
57
  "types": "./dist/vehicles.d.mts",
54
58
  "default": "./dist/vehicles.mjs"
@@ -72,6 +76,10 @@
72
76
  "./audio": {
73
77
  "types": "./dist/audio.d.mts",
74
78
  "default": "./dist/audio.mjs"
79
+ },
80
+ "./snow": {
81
+ "types": "./dist/snow.d.mts",
82
+ "default": "./dist/snow.mjs"
75
83
  }
76
84
  },
77
85
  "files": [
@@ -79,8 +87,8 @@
79
87
  "NOTICE.md"
80
88
  ],
81
89
  "dependencies": {
82
- "@bendyline/molen-materials": "0.0.11",
83
- "@bendyline/molen-schema": "0.0.10",
90
+ "@bendyline/molen-materials": "0.0.12",
91
+ "@bendyline/molen-schema": "0.0.11",
84
92
  "zod": "^4.6.5"
85
93
  },
86
94
  "peerDependencies": {
@@ -1,56 +0,0 @@
1
- import { n as ModelSignals, t as ModelSignalVisual } from "./model-signals-BpeXhsim.mjs";
2
- import { AircraftCollisionProbe, AircraftContact, VehicleInteriorSpec, VehicleSpec } from "@bendyline/molen-schema";
3
- import * as THREE from "three";
4
- import { Object3D } from "three";
5
-
6
- //#region src/aircraft-collision.d.ts
7
- interface AircraftObstacleScene {
8
- /** Collision tree origin and query, e.g. WalkCollision. */
9
- origin: THREE.Vector3;
10
- octree: {
11
- sphereIntersect(sphere: THREE.Sphere): false | {
12
- normal: THREE.Vector3;
13
- depth: number;
14
- };
15
- getSphereTriangles?(sphere: THREE.Sphere, triangles: THREE.Triangle[]): unknown;
16
- };
17
- cars: ReadonlyArray<{
18
- transform: {
19
- pos: [number, number, number];
20
- rot: [number, number, number, number];
21
- };
22
- spec: Pick<VehicleSpec, "width" | "height" | "length">;
23
- }>;
24
- /** Other aircraft body centers. The host excludes the queried aircraft. */
25
- aircraft: ReadonlyArray<[number, number, number]>;
26
- }
27
- /** Contact normals from streamed geometry and nearby parked vehicles, in absolute coordinates. */
28
- declare function aircraftObstacleContacts(probes: readonly AircraftCollisionProbe[], scene: AircraftObstacleScene): AircraftContact[];
29
- //#endregion
30
- //#region src/vehicle-camera.d.ts
31
- interface VehicleCameraOptions {
32
- view: "cockpit" | "chase";
33
- lookYaw?: number;
34
- lookPitch?: number;
35
- /** Distance to the first static obstruction between two absolute world points. */
36
- obstructionDistance?: (from: [number, number, number], to: [number, number, number]) => number | undefined;
37
- }
38
- interface VehicleCameraPose {
39
- position: [number, number, number];
40
- lookAt: [number, number, number];
41
- yaw: number;
42
- pitch: number;
43
- }
44
- /** Seat-space cockpit and collision-shortened chase views for any vehicle host. */
45
- declare function vehicleCameraPose(transform: {
46
- pos: [number, number, number];
47
- rot: [number, number, number, number];
48
- }, spec: Pick<VehicleSpec, "driverEye" | "length">, options: VehicleCameraOptions): VehicleCameraPose;
49
- //#endregion
50
- //#region src/vehicle-interior.d.ts
51
- type VehicleInteriorSignals = ModelSignals;
52
- type VehicleInteriorVisual = ModelSignalVisual;
53
- /** Vehicle convenience wrapper around the platform's model-signal binding system. */
54
- declare function createVehicleInteriorVisual(model: Object3D, spec: VehicleInteriorSpec): VehicleInteriorVisual;
55
- //#endregion
56
- export { VehicleCameraPose as a, aircraftObstacleContacts as c, VehicleCameraOptions as i, VehicleInteriorVisual as n, vehicleCameraPose as o, createVehicleInteriorVisual as r, AircraftObstacleScene as s, VehicleInteriorSignals as t };