@bendyline/molen-client 0.0.9 → 0.0.11
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/README.md +1 -0
- package/dist/aircraft.d.mts +115 -6
- package/dist/aircraft.mjs +755 -10
- package/dist/audio.d.mts +9 -0
- package/dist/audio.mjs +56 -8
- package/dist/camera-track.d.mts +1 -1
- package/dist/index.d.mts +24 -7
- package/dist/index.mjs +516 -46
- package/dist/{input-BVZjf4T-.mjs → input-BliZYUY1.mjs} +42 -12
- package/dist/{input-CD-dHuv0.d.mts → input-Ckev_Kvj.d.mts} +9 -0
- package/dist/instruments.d.mts +63 -0
- package/dist/instruments.mjs +137 -0
- package/dist/livery-C0abgthY.mjs +302 -0
- package/dist/model-signals-BpeXhsim.d.mts +25 -0
- package/dist/model-signals-DLgvjBeZ.mjs +172 -0
- package/dist/navigation.d.mts +52 -6
- package/dist/navigation.mjs +702 -169
- package/dist/{renderer-BRtQK4jX.d.mts → renderer-Dzus24ZF.d.mts} +147 -10
- package/dist/text.d.mts +24 -0
- package/dist/text.mjs +62 -0
- package/dist/{vehicle-interior-GWcXnvXS.d.mts → vehicle-interior-BgcC750p.d.mts} +1 -1
- package/dist/{vehicle-interior-vJQsRLQy.mjs → vehicle-interior-DfpZwWEh.mjs} +4 -1
- package/dist/vehicles.d.mts +6 -5
- package/dist/vehicles.mjs +67 -25
- package/dist/webgpu-ambient-occlusion-9gnp3qOY.mjs +126 -0
- package/dist/{webgpu-driver-Bt2mvZk9.mjs → webgpu-driver-BQnmPRqs.mjs} +41 -1
- package/dist/{webgpu-scene-optimizer-DKRyHBRC.mjs → webgpu-scene-optimizer-Cup4Yvzs.mjs} +9 -2
- package/package.json +12 -4
- package/dist/model-signals-DVlLyP-w.d.mts +0 -17
- package/dist/model-signals-Dp5OV-Y0.mjs +0 -73
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
//#region src/three/webgpu-ambient-occlusion.ts
|
|
3
|
+
async function createWebGpuAmbientOcclusion(renderer, scene, camera, options = {}) {
|
|
4
|
+
const [webgpu, tsl, gtao] = await Promise.all([
|
|
5
|
+
import("three/webgpu"),
|
|
6
|
+
import("three/tsl"),
|
|
7
|
+
import("three/addons/tsl/display/GTAONode.js")
|
|
8
|
+
]);
|
|
9
|
+
const { abs, clamp, colorToDirection, directionToColor, float, Fn, max, mix, mrt, normalView, output, perspectiveDepthToViewZ, rtt, sample, screenUV, select, texture, uniform, uv, vec2, vec4 } = tsl;
|
|
10
|
+
const depthTexture = new THREE.DepthTexture(1, 1);
|
|
11
|
+
if (renderer.reversedDepthBuffer) depthTexture.type = THREE.FloatType;
|
|
12
|
+
const target = new webgpu.RenderTarget(1, 1, {
|
|
13
|
+
count: 2,
|
|
14
|
+
samples: renderer.samples,
|
|
15
|
+
type: THREE.HalfFloatType,
|
|
16
|
+
depthTexture
|
|
17
|
+
});
|
|
18
|
+
target.textures[0].name = "output";
|
|
19
|
+
const normalTexture = target.textures[1];
|
|
20
|
+
normalTexture.name = "normal";
|
|
21
|
+
normalTexture.type = THREE.UnsignedByteType;
|
|
22
|
+
const normals = mrt({
|
|
23
|
+
output,
|
|
24
|
+
normal: directionToColor(normalView)
|
|
25
|
+
});
|
|
26
|
+
const image = texture(target.textures[0]);
|
|
27
|
+
const depth = texture(depthTexture);
|
|
28
|
+
const packed = texture(normalTexture);
|
|
29
|
+
const normal = sample((uv) => colorToDirection(packed.sample(uv)));
|
|
30
|
+
const lens = camera;
|
|
31
|
+
const nearPlane = uniform(lens.near);
|
|
32
|
+
const farPlane = uniform(lens.far);
|
|
33
|
+
const reach = uniform(options.reach ?? 250);
|
|
34
|
+
const meters = (stored) => perspectiveDepthToViewZ(stored, nearPlane, farPlane).negate();
|
|
35
|
+
const viewDepth = (at) => meters(depth.sample(at).r);
|
|
36
|
+
const sceneDepth = sample((uv) => {
|
|
37
|
+
const d = depth.sample(uv).r;
|
|
38
|
+
return vec4(select(meters(d).greaterThan(reach), float(1), d));
|
|
39
|
+
});
|
|
40
|
+
const occlusion = gtao.ao(sceneDepth, normal, camera);
|
|
41
|
+
occlusion.resolutionScale = .5;
|
|
42
|
+
occlusion.radius.value = options.radius ?? 3;
|
|
43
|
+
occlusion.scale.value = 2.5;
|
|
44
|
+
occlusion.distanceFallOff.value = .6;
|
|
45
|
+
occlusion.samples.value = 8;
|
|
46
|
+
const raw = occlusion.getTextureNode();
|
|
47
|
+
const texel = uniform(new THREE.Vector2(1, 1));
|
|
48
|
+
const cleaned = rtt(Fn(() => {
|
|
49
|
+
const at = uv();
|
|
50
|
+
const middle = viewDepth(at).toVar();
|
|
51
|
+
const sum = float(0).toVar();
|
|
52
|
+
const total = float(0).toVar();
|
|
53
|
+
for (const y of [
|
|
54
|
+
-1.5,
|
|
55
|
+
0,
|
|
56
|
+
1.5
|
|
57
|
+
]) for (const x of [
|
|
58
|
+
-1.5,
|
|
59
|
+
0,
|
|
60
|
+
1.5
|
|
61
|
+
]) {
|
|
62
|
+
const tap = at.add(texel.mul(vec2(x, y)));
|
|
63
|
+
const there = viewDepth(tap).toVar();
|
|
64
|
+
const weight = float((x === 0 ? 1 : 2) * (y === 0 ? 1 : 2)).mul(clamp(float(1).sub(abs(there.sub(middle)).div(middle.mul(.08))), 0, 1)).mul(there.lessThanEqual(reach).select(float(1), float(0)));
|
|
65
|
+
sum.addAssign(raw.sample(tap).r.mul(weight));
|
|
66
|
+
total.addAssign(weight);
|
|
67
|
+
}
|
|
68
|
+
return vec4(select(total.greaterThan(0), sum.div(max(total, 1e-4)), float(1)), 0, 0, 1);
|
|
69
|
+
})(), 1, 1);
|
|
70
|
+
const fade = float(1).sub(meters(depth.r).smoothstep(reach.mul(.67), reach));
|
|
71
|
+
const shade = mix(float(1), cleaned.sample(screenUV).r, fade.mul(options.strength ?? .85));
|
|
72
|
+
const pipeline = new webgpu.RenderPipeline(renderer);
|
|
73
|
+
pipeline.outputNode = vec4(image.rgb.mul(shade), image.a);
|
|
74
|
+
const copy = new webgpu.RenderPipeline(renderer);
|
|
75
|
+
copy.outputNode = image;
|
|
76
|
+
const size = new THREE.Vector2();
|
|
77
|
+
const intoTarget = (use) => {
|
|
78
|
+
const previousTarget = renderer.getRenderTarget();
|
|
79
|
+
const previousMrt = renderer.getMRT();
|
|
80
|
+
try {
|
|
81
|
+
renderer.setRenderTarget(target);
|
|
82
|
+
renderer.setMRT(normals);
|
|
83
|
+
return use();
|
|
84
|
+
} finally {
|
|
85
|
+
renderer.setRenderTarget(previousTarget);
|
|
86
|
+
renderer.setMRT(previousMrt);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return {
|
|
90
|
+
compile: (object) => intoTarget(() => renderer.compileAsync(object, camera, scene)),
|
|
91
|
+
render(sky, occlude = true) {
|
|
92
|
+
renderer.getDrawingBufferSize(size);
|
|
93
|
+
nearPlane.value = lens.near;
|
|
94
|
+
farPlane.value = lens.far;
|
|
95
|
+
if (target.width !== size.x || target.height !== size.y) {
|
|
96
|
+
target.setSize(size.x, size.y);
|
|
97
|
+
cleaned.setSize(Math.ceil(size.x / 2), Math.ceil(size.y / 2));
|
|
98
|
+
texel.value.set(1 / Math.max(1, Math.round(size.x / 2)), 1 / Math.max(1, Math.round(size.y / 2)));
|
|
99
|
+
}
|
|
100
|
+
const autoClear = renderer.autoClear;
|
|
101
|
+
try {
|
|
102
|
+
intoTarget(() => {
|
|
103
|
+
renderer.autoClear = true;
|
|
104
|
+
if (sky) {
|
|
105
|
+
renderer.render(sky.scene, sky.camera);
|
|
106
|
+
renderer.autoClear = false;
|
|
107
|
+
renderer.clearDepth();
|
|
108
|
+
}
|
|
109
|
+
renderer.render(scene, camera);
|
|
110
|
+
});
|
|
111
|
+
} finally {
|
|
112
|
+
renderer.autoClear = autoClear;
|
|
113
|
+
}
|
|
114
|
+
(occlude ? pipeline : copy).render();
|
|
115
|
+
},
|
|
116
|
+
dispose() {
|
|
117
|
+
pipeline.dispose();
|
|
118
|
+
copy.dispose();
|
|
119
|
+
occlusion.dispose();
|
|
120
|
+
cleaned.dispose();
|
|
121
|
+
target.dispose();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
export { createWebGpuAmbientOcclusion };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { n as installWebGpuTimestampGuard } from "./webgpu-frame-timer-Xnuq1Hld.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { instanceIndex, objectGroup } from "three/tsl";
|
|
3
|
+
import * as WebGPU from "three/webgpu";
|
|
4
|
+
import { CanvasTarget, StorageBufferNode, WebGPUBackend, WebGPURenderer } from "three/webgpu";
|
|
3
5
|
//#region src/three/webgpu-depth-bias.ts
|
|
4
6
|
const guardedBackends = /* @__PURE__ */ new WeakSet();
|
|
5
7
|
/**
|
|
@@ -122,6 +124,38 @@ function listenWeakly(ref, material, geometry) {
|
|
|
122
124
|
};
|
|
123
125
|
}
|
|
124
126
|
//#endregion
|
|
127
|
+
//#region src/three/webgpu-shared-instancing.ts
|
|
128
|
+
const patched = /* @__PURE__ */ new WeakSet();
|
|
129
|
+
/**
|
|
130
|
+
* Give every InstancedMesh drawn with one material the same shader. Three reads an InstancedMesh's
|
|
131
|
+
* matrices from a uniform array declared with the batch's instance count and named after its node,
|
|
132
|
+
* so each batch compiled a WGSL module and a GPU pipeline of its own. Streaming worldgen's plant
|
|
133
|
+
* batches in flight made hundreds a minute, and those created mid-frame, synchronously, stalled the
|
|
134
|
+
* browser's GPU process for seconds. A runtime-sized storage buffer under one name makes the shader
|
|
135
|
+
* text identical, and three's pipeline cache serves every batch after the first; matrices upload
|
|
136
|
+
* when the attribute's version changes, as vertex buffers do. Each batch still builds its node
|
|
137
|
+
* state on the CPU, since three keys that by mesh. (Sharing that state across meshes, each render
|
|
138
|
+
* object binding its own mesh's buffers, broke the WebGPU scene optimizer's render bundles: whole
|
|
139
|
+
* buildings stopped drawing.) Applies to WebGPU backends only; the matrices of the previous frame
|
|
140
|
+
* (velocity) keep three's own path.
|
|
141
|
+
*/
|
|
142
|
+
function installWebGpuSharedInstancing(three) {
|
|
143
|
+
const prototype = three.InstanceNode?.prototype;
|
|
144
|
+
const Storage = three.StorageBufferNode;
|
|
145
|
+
const { instanceIndex, objectGroup } = three;
|
|
146
|
+
if (!prototype || patched.has(prototype) || typeof prototype._createInstanceMatrixNode !== "function") return;
|
|
147
|
+
patched.add(prototype);
|
|
148
|
+
const create = prototype._createInstanceMatrixNode;
|
|
149
|
+
prototype._createInstanceMatrixNode = function(assignBuffer, builder) {
|
|
150
|
+
if (!assignBuffer || builder.renderer?.backend?.isWebGPUBackend !== true) return create.call(this, assignBuffer, builder);
|
|
151
|
+
const matrices = this.instanceMatrix;
|
|
152
|
+
const node = new Storage(matrices, "mat4", Math.max(matrices.count, 1));
|
|
153
|
+
node.name = "instanceMatrices";
|
|
154
|
+
node.groupNode = objectGroup;
|
|
155
|
+
return node.element(instanceIndex);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
125
159
|
//#region src/three/webgpu-driver.ts
|
|
126
160
|
/** Loaded only by the asynchronous backend factory, never by a legacy WebGL-only viewer. */
|
|
127
161
|
async function createWebGpuDriver(options) {
|
|
@@ -144,6 +178,12 @@ async function createWebGpuDriver(options) {
|
|
|
144
178
|
installWebGpuTimestampGuard(renderer);
|
|
145
179
|
installWebGpuDepthBiasGuard(renderer);
|
|
146
180
|
installWebGpuRenderObjectRelease(renderer);
|
|
181
|
+
installWebGpuSharedInstancing({
|
|
182
|
+
InstanceNode: Reflect.get(WebGPU, "InstanceNode"),
|
|
183
|
+
StorageBufferNode,
|
|
184
|
+
instanceIndex,
|
|
185
|
+
objectGroup
|
|
186
|
+
});
|
|
147
187
|
renderer.getContext();
|
|
148
188
|
if (options.canvas !== void 0) {
|
|
149
189
|
const previousTarget = renderer.getCanvasTarget();
|
|
@@ -85,6 +85,8 @@ var WebGpuSceneOptimizer = class {
|
|
|
85
85
|
matrixSamples = /* @__PURE__ */ new WeakMap();
|
|
86
86
|
restoreRefresh;
|
|
87
87
|
materialSamples = /* @__PURE__ */ new Map();
|
|
88
|
+
/** The render each material last refreshed a replayed object in; see the constructor. */
|
|
89
|
+
refreshed = /* @__PURE__ */ new WeakMap();
|
|
88
90
|
videoMaterials = /* @__PURE__ */ new WeakSet();
|
|
89
91
|
materialStates = /* @__PURE__ */ new WeakMap();
|
|
90
92
|
constructor(renderer) {
|
|
@@ -94,9 +96,14 @@ var WebGpuSceneOptimizer = class {
|
|
|
94
96
|
const refresh = nodes.needsRefresh;
|
|
95
97
|
nodes.needsRefresh = (object) => {
|
|
96
98
|
const group = object.bundle;
|
|
97
|
-
if (group !== null && this.groups.has(group) && group.eligible && group.version === group.recordedVersion && !renderer.
|
|
99
|
+
if (group !== null && this.groups.has(group) && group.eligible && group.version === group.recordedVersion && !renderer.getMRT()?.has("velocity")) {
|
|
98
100
|
const monitor = object.getMonitor();
|
|
99
|
-
|
|
101
|
+
const material = object.material;
|
|
102
|
+
if (!monitor.hasAnimation && (monitor.hasNode ? material.userData.bundleStatic === true : monitor.renderObjects.has(object))) {
|
|
103
|
+
const renderId = nodes.nodeFrame.renderId;
|
|
104
|
+
if (this.refreshed.get(material) === renderId) return false;
|
|
105
|
+
this.refreshed.set(material, renderId);
|
|
106
|
+
}
|
|
100
107
|
}
|
|
101
108
|
return refresh.call(nodes, object);
|
|
102
109
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bendyline/molen-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
"audio"
|
|
30
30
|
],
|
|
31
31
|
"exports": {
|
|
32
|
+
"./text": {
|
|
33
|
+
"types": "./dist/text.d.mts",
|
|
34
|
+
"default": "./dist/text.mjs"
|
|
35
|
+
},
|
|
32
36
|
".": {
|
|
33
37
|
"types": "./dist/index.d.mts",
|
|
34
38
|
"default": "./dist/index.mjs"
|
|
@@ -57,6 +61,10 @@
|
|
|
57
61
|
"types": "./dist/markers.d.mts",
|
|
58
62
|
"default": "./dist/markers.mjs"
|
|
59
63
|
},
|
|
64
|
+
"./instruments": {
|
|
65
|
+
"types": "./dist/instruments.d.mts",
|
|
66
|
+
"default": "./dist/instruments.mjs"
|
|
67
|
+
},
|
|
60
68
|
"./audio": {
|
|
61
69
|
"types": "./dist/audio.d.mts",
|
|
62
70
|
"default": "./dist/audio.mjs"
|
|
@@ -67,9 +75,9 @@
|
|
|
67
75
|
"NOTICE.md"
|
|
68
76
|
],
|
|
69
77
|
"dependencies": {
|
|
70
|
-
"
|
|
71
|
-
"@bendyline/molen-
|
|
72
|
-
"
|
|
78
|
+
"@bendyline/molen-materials": "0.0.10",
|
|
79
|
+
"@bendyline/molen-schema": "0.0.9",
|
|
80
|
+
"zod": "^4.6.5"
|
|
73
81
|
},
|
|
74
82
|
"peerDependencies": {
|
|
75
83
|
"@types/three": ">=0.184.0 <0.187.0",
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ModelSignalSource, ModelSignalSpec } from "@bendyline/molen-schema";
|
|
2
|
-
import { Object3D } from "three";
|
|
3
|
-
|
|
4
|
-
//#region src/model-signals.d.ts
|
|
5
|
-
type ModelSignals = Readonly<Record<string, number | undefined>>;
|
|
6
|
-
interface ModelSignalVisual {
|
|
7
|
-
readonly missingNodes: readonly string[];
|
|
8
|
-
update(signals: ModelSignals): void;
|
|
9
|
-
/** Restore authored transforms before removing or replacing bindings. */
|
|
10
|
-
reset(): void;
|
|
11
|
-
}
|
|
12
|
-
/** Read data only: no expressions, evaluation, prototype traversal, or simulation writes. */
|
|
13
|
-
declare function readModelSignals(sources: Readonly<Record<string, ModelSignalSource>> | undefined, readComponent: (name: string) => unknown): ModelSignals;
|
|
14
|
-
/** Model-agnostic; moves existing geometry without creating a cockpit or changing physics. */
|
|
15
|
-
declare function createModelSignalVisual(model: Object3D, spec: ModelSignalSpec): ModelSignalVisual;
|
|
16
|
-
//#endregion
|
|
17
|
-
export { readModelSignals as i, ModelSignals as n, createModelSignalVisual as r, ModelSignalVisual as t };
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
//#region src/model-signals.ts
|
|
2
|
-
/** Read data only: no expressions, evaluation, prototype traversal, or simulation writes. */
|
|
3
|
-
function readModelSignals(sources, readComponent) {
|
|
4
|
-
const signals = Object.create(null);
|
|
5
|
-
for (const [name, source] of Object.entries(sources ?? {})) {
|
|
6
|
-
let value = readComponent(source.component);
|
|
7
|
-
for (const part of source.path) {
|
|
8
|
-
if (value === null || typeof value !== "object" || !Object.hasOwn(value, part)) {
|
|
9
|
-
value = void 0;
|
|
10
|
-
break;
|
|
11
|
-
}
|
|
12
|
-
value = value[part];
|
|
13
|
-
}
|
|
14
|
-
if (typeof value === "boolean") signals[name] = Number(value);
|
|
15
|
-
else if (typeof value === "number" && Number.isFinite(value)) signals[name] = value;
|
|
16
|
-
}
|
|
17
|
-
return signals;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* A binding's displacement for a signal, before its limits: linear in the signal, or along its
|
|
21
|
-
* keyframed curve, which holds its end values outside the keys. Exported for tests.
|
|
22
|
-
*/
|
|
23
|
-
function modelSignalDisplacement(binding, signal) {
|
|
24
|
-
const keys = binding.keys;
|
|
25
|
-
if (keys === void 0) return signal * (binding.scale ?? 1) + (binding.offset ?? 0);
|
|
26
|
-
const first = keys[0];
|
|
27
|
-
const last = keys[keys.length - 1];
|
|
28
|
-
if (first === void 0 || last === void 0) return 0;
|
|
29
|
-
if (signal <= first[0]) return first[1];
|
|
30
|
-
if (signal >= last[0]) return last[1];
|
|
31
|
-
let index = 1;
|
|
32
|
-
while (index < keys.length - 1 && signal > (keys[index]?.[0] ?? Infinity)) index++;
|
|
33
|
-
const [x0, y0] = keys[index - 1] ?? first;
|
|
34
|
-
const [x1, y1] = keys[index] ?? last;
|
|
35
|
-
let f = (signal - x0) / (x1 - x0);
|
|
36
|
-
if (binding.ease === "smooth") f = f * f * (3 - 2 * f);
|
|
37
|
-
return y0 + (y1 - y0) * f;
|
|
38
|
-
}
|
|
39
|
-
/** Model-agnostic; moves existing geometry without creating a cockpit or changing physics. */
|
|
40
|
-
function createModelSignalVisual(model, spec) {
|
|
41
|
-
const nodes = /* @__PURE__ */ new Map();
|
|
42
|
-
model.traverse((node) => {
|
|
43
|
-
const matches = nodes.get(node.name) ?? [];
|
|
44
|
-
matches.push(node);
|
|
45
|
-
nodes.set(node.name, matches);
|
|
46
|
-
});
|
|
47
|
-
const missingNodes = [...new Set(spec.bindings.map((b) => b.node))].filter((name) => !nodes.has(name));
|
|
48
|
-
const bindings = spec.bindings.flatMap((binding) => (nodes.get(binding.node) ?? []).map((node) => {
|
|
49
|
-
node.matrixAutoUpdate = true;
|
|
50
|
-
return {
|
|
51
|
-
binding,
|
|
52
|
-
node,
|
|
53
|
-
rest: node[binding.property][binding.axis]
|
|
54
|
-
};
|
|
55
|
-
}));
|
|
56
|
-
return {
|
|
57
|
-
missingNodes,
|
|
58
|
-
update(signals) {
|
|
59
|
-
for (const { binding, node, rest } of bindings) {
|
|
60
|
-
const signal = signals[binding.source];
|
|
61
|
-
if (signal === void 0 || !Number.isFinite(signal)) continue;
|
|
62
|
-
const value = modelSignalDisplacement(binding, signal);
|
|
63
|
-
if (!Number.isFinite(value)) continue;
|
|
64
|
-
node[binding.property][binding.axis] = rest + Math.max(binding.min ?? -Infinity, Math.min(binding.max ?? Infinity, value));
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
reset() {
|
|
68
|
-
for (const { binding, node, rest } of bindings) node[binding.property][binding.axis] = rest;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
//#endregion
|
|
73
|
-
export { readModelSignals as n, createModelSignalVisual as t };
|