@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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { inputProfileIssues, inputProfileSchema } from "@bendyline/molen-schema";
|
|
2
2
|
//#region src/input.ts
|
|
3
|
+
/** With no gamepad announced, the browser's pads are still polled every this many updates. */
|
|
4
|
+
const IDLE_POLL = 120;
|
|
3
5
|
/** Resolve a key or Mouse<button> code to its bound action. */
|
|
4
6
|
function resolveAction(bindings, code) {
|
|
5
7
|
return Object.hasOwn(bindings, code) ? bindings[code] : void 0;
|
|
@@ -59,6 +61,13 @@ var InputMap = class {
|
|
|
59
61
|
provider;
|
|
60
62
|
listeners = [];
|
|
61
63
|
status = "unavailable";
|
|
64
|
+
/**
|
|
65
|
+
* With the browser's own gamepads and a target that reports `gamepadconnected`, the pads
|
|
66
|
+
* connected since (and the updates since the last poll): `navigator.getGamepads()` blocked
|
|
67
|
+
* Chrome's main thread several milliseconds a frame on Windows with no gamepad attached.
|
|
68
|
+
*/
|
|
69
|
+
padsAnnounced;
|
|
70
|
+
idleUpdates = 0;
|
|
62
71
|
constructor(opts) {
|
|
63
72
|
this.base = copyProfile({
|
|
64
73
|
bindings: opts.bindings,
|
|
@@ -72,6 +81,19 @@ var InputMap = class {
|
|
|
72
81
|
this.provider = opts.getGamepads ?? (() => typeof navigator !== "undefined" && typeof navigator.getGamepads === "function" ? navigator.getGamepads() : []);
|
|
73
82
|
if (opts.getGamepads || typeof navigator !== "undefined" && typeof navigator.getGamepads === "function") this.status = "available";
|
|
74
83
|
if (this.target) this.attach(this.target);
|
|
84
|
+
if (opts.getGamepads === void 0 && this.target && this.status === "available") {
|
|
85
|
+
this.padsAnnounced = 0;
|
|
86
|
+
const add = (type, fn) => {
|
|
87
|
+
this.target?.addEventListener(type, fn);
|
|
88
|
+
this.listeners.push([type, fn]);
|
|
89
|
+
};
|
|
90
|
+
add("gamepadconnected", () => {
|
|
91
|
+
this.padsAnnounced = (this.padsAnnounced ?? 0) + 1;
|
|
92
|
+
});
|
|
93
|
+
add("gamepaddisconnected", () => {
|
|
94
|
+
this.padsAnnounced = Math.max(0, (this.padsAnnounced ?? 0) - 1);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
75
97
|
}
|
|
76
98
|
get accepting() {
|
|
77
99
|
return this.enabled && this.focused && this.suspensions === 0;
|
|
@@ -123,19 +145,23 @@ var InputMap = class {
|
|
|
123
145
|
/** Poll fresh browser objects; unplugging or losing permission releases affected controls. */
|
|
124
146
|
update() {
|
|
125
147
|
if (this.disposed) return;
|
|
148
|
+
const quiet = this.padsAnnounced === 0 && this.pads.length === 0 && this.idleUpdates++ % IDLE_POLL !== 0;
|
|
126
149
|
try {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
150
|
+
if (quiet) this.pads = [];
|
|
151
|
+
else {
|
|
152
|
+
this.pads = this.provider().filter((pad) => pad?.connected === true).map((pad) => ({
|
|
153
|
+
id: pad.id,
|
|
154
|
+
index: pad.index,
|
|
155
|
+
connected: pad.connected,
|
|
156
|
+
mapping: pad.mapping,
|
|
157
|
+
axes: Array.from(pad.axes),
|
|
158
|
+
buttons: Array.from(pad.buttons, (button) => ({
|
|
159
|
+
pressed: button.pressed,
|
|
160
|
+
value: button.value
|
|
161
|
+
}))
|
|
162
|
+
}));
|
|
163
|
+
if (this.status === "blocked") this.status = "available";
|
|
164
|
+
}
|
|
139
165
|
} catch {
|
|
140
166
|
this.pads = [];
|
|
141
167
|
this.status = "blocked";
|
|
@@ -262,6 +288,10 @@ var InputMap = class {
|
|
|
262
288
|
value(action) {
|
|
263
289
|
return this.values.get(action) ?? 0;
|
|
264
290
|
}
|
|
291
|
+
/** Whether an enabled, connected axis supplies this action, including a zero reading. */
|
|
292
|
+
hasAxis(action) {
|
|
293
|
+
return !this.disposed && this.accepting && (this.profile.axes ?? []).some((binding) => binding.action === action && this.pads.some((pad) => matches(binding.device, pad) && Number.isFinite(pad.axes[binding.axis])));
|
|
294
|
+
}
|
|
265
295
|
isActive(action) {
|
|
266
296
|
return this.value(action) !== 0;
|
|
267
297
|
}
|
|
@@ -64,6 +64,13 @@ declare class InputMap {
|
|
|
64
64
|
private readonly provider;
|
|
65
65
|
private readonly listeners;
|
|
66
66
|
private status;
|
|
67
|
+
/**
|
|
68
|
+
* With the browser's own gamepads and a target that reports `gamepadconnected`, the pads
|
|
69
|
+
* connected since (and the updates since the last poll): `navigator.getGamepads()` blocked
|
|
70
|
+
* Chrome's main thread several milliseconds a frame on Windows with no gamepad attached.
|
|
71
|
+
*/
|
|
72
|
+
private padsAnnounced;
|
|
73
|
+
private idleUpdates;
|
|
67
74
|
constructor(opts: InputMapOptions);
|
|
68
75
|
private get accepting();
|
|
69
76
|
private get profile();
|
|
@@ -98,6 +105,8 @@ declare class InputMap {
|
|
|
98
105
|
clearVirtual(source?: string): void;
|
|
99
106
|
/** Numeric action value; strongest absolute contribution wins, keyboard wins ties. */
|
|
100
107
|
value(action: string): number;
|
|
108
|
+
/** Whether an enabled, connected axis supplies this action, including a zero reading. */
|
|
109
|
+
hasAxis(action: string): boolean;
|
|
101
110
|
isActive(action: string): boolean;
|
|
102
111
|
activeActions(): Set<string>;
|
|
103
112
|
private contribute;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { a as readModelSignals, i as modelSignalDisplacement, n as ModelSignals, r as createModelSignalVisual, t as ModelSignalVisual } from "./model-signals-BpeXhsim.mjs";
|
|
2
|
+
import { ModelSignalBinding, ModelSignalExpression, ModelSignalSpec } from "@bendyline/molen-schema";
|
|
3
|
+
|
|
4
|
+
//#region src/model-signal-expressions.d.ts
|
|
5
|
+
/** Compile a bounded dependency graph once. Evaluation is read-only and never executes code. */
|
|
6
|
+
declare function compileModelSignalExpressions(definitions: Record<string, ModelSignalExpression> | undefined): (sources: ModelSignals) => ModelSignals;
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/instruments.d.ts
|
|
9
|
+
interface DialInstrumentOptions {
|
|
10
|
+
node: string;
|
|
11
|
+
source: string;
|
|
12
|
+
/** Measured [reading, needle radians from authored rest] pairs, including nonlinear scales. */
|
|
13
|
+
calibration: [number, number][];
|
|
14
|
+
axis?: "x" | "y" | "z";
|
|
15
|
+
responseSeconds?: number;
|
|
16
|
+
/** Usually 2π for a continuously rotating heading or altimeter needle. */
|
|
17
|
+
period?: number;
|
|
18
|
+
missing?: "hold" | "rest";
|
|
19
|
+
}
|
|
20
|
+
/** The same binding can calibrate tick placement with modelSignalDisplacement(binding, reading). */
|
|
21
|
+
declare function dialInstrument(options: DialInstrumentOptions): ModelSignalBinding;
|
|
22
|
+
interface AltimeterInstrumentOptions {
|
|
23
|
+
/** Caller supplies barometrically indicated altitude in feet, or explicitly labels an approximation. */
|
|
24
|
+
source: string;
|
|
25
|
+
hundreds: string;
|
|
26
|
+
thousands: string;
|
|
27
|
+
tenThousands?: string;
|
|
28
|
+
axis?: "x" | "y" | "z";
|
|
29
|
+
/** +Z facing the viewer uses -1 for clockwise motion. Reverse for a frame facing -Z. */
|
|
30
|
+
direction?: 1 | -1;
|
|
31
|
+
responseSeconds?: number;
|
|
32
|
+
}
|
|
33
|
+
/** Three independent, continuous needles: one turn per 1,000 / 10,000 / 100,000 feet. */
|
|
34
|
+
declare function altimeterInstrument(options: AltimeterInstrumentOptions): ModelSignalBinding[];
|
|
35
|
+
interface AttitudeInstrumentOptions {
|
|
36
|
+
/** A bank frame containing the pitch card, inside the authored face mask. */
|
|
37
|
+
bankNode: string;
|
|
38
|
+
pitchNode: string;
|
|
39
|
+
rollSource?: string;
|
|
40
|
+
pitchSource?: string;
|
|
41
|
+
bankAxis?: "x" | "y" | "z";
|
|
42
|
+
pitchAxis?: "x" | "y" | "z";
|
|
43
|
+
/** Local meters per radian; sign follows the face's orientation. */
|
|
44
|
+
pitchScale: number;
|
|
45
|
+
pitchLimit: number;
|
|
46
|
+
bankDirection?: 1 | -1;
|
|
47
|
+
responseSeconds?: number;
|
|
48
|
+
}
|
|
49
|
+
/** Moving horizon behind a fixed aircraft symbol. Geometry supplies the circular mask. */
|
|
50
|
+
declare function attitudeInstrument(options: AttitudeInstrumentOptions): ModelSignalBinding[];
|
|
51
|
+
/** Own an authored lamp/flag node; positive means on, missing readings turn it off. */
|
|
52
|
+
declare function annunciatorInstrument(node: string, source: string): ModelSignalBinding;
|
|
53
|
+
interface NumericInstrumentOptions {
|
|
54
|
+
source: string;
|
|
55
|
+
/** Most-significant to least-significant digit; each has seven node names in a,b,c,d,e,f,g order. */
|
|
56
|
+
digits: [string, string, string, string, string, string, string][];
|
|
57
|
+
/** Prefix for private derived variables, unique when combining multiple readouts. */
|
|
58
|
+
prefix: string;
|
|
59
|
+
}
|
|
60
|
+
/** Fixed-width unsigned integer display; no DOM, textures or per-frame glyph allocation. */
|
|
61
|
+
declare function numericInstrument(options: NumericInstrumentOptions): ModelSignalSpec;
|
|
62
|
+
//#endregion
|
|
63
|
+
export { AltimeterInstrumentOptions, AttitudeInstrumentOptions, DialInstrumentOptions, type ModelSignalVisual, type ModelSignals, NumericInstrumentOptions, altimeterInstrument, annunciatorInstrument, attitudeInstrument, compileModelSignalExpressions, createModelSignalVisual, dialInstrument, modelSignalDisplacement, numericInstrument, readModelSignals };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { i as compileModelSignalExpressions, n as modelSignalDisplacement, r as readModelSignals, t as createModelSignalVisual } from "./model-signals-DLgvjBeZ.mjs";
|
|
2
|
+
//#region src/instruments.ts
|
|
3
|
+
/** The same binding can calibrate tick placement with modelSignalDisplacement(binding, reading). */
|
|
4
|
+
function dialInstrument(options) {
|
|
5
|
+
return {
|
|
6
|
+
node: options.node,
|
|
7
|
+
source: options.source,
|
|
8
|
+
property: "rotation",
|
|
9
|
+
axis: options.axis ?? "z",
|
|
10
|
+
keys: options.calibration.map(([reading, angle]) => [reading, angle]),
|
|
11
|
+
responseSeconds: options.responseSeconds ?? 0,
|
|
12
|
+
...options.period === void 0 ? {} : { period: options.period },
|
|
13
|
+
missing: options.missing ?? "hold"
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Three independent, continuous needles: one turn per 1,000 / 10,000 / 100,000 feet. */
|
|
17
|
+
function altimeterInstrument(options) {
|
|
18
|
+
return [
|
|
19
|
+
[options.hundreds, 1e3],
|
|
20
|
+
[options.thousands, 1e4],
|
|
21
|
+
[options.tenThousands, 1e5]
|
|
22
|
+
].flatMap(([node, feet]) => node === void 0 ? [] : [{
|
|
23
|
+
node,
|
|
24
|
+
source: options.source,
|
|
25
|
+
property: "rotation",
|
|
26
|
+
axis: options.axis ?? "z",
|
|
27
|
+
scale: (options.direction ?? -1) * 2 * Math.PI / feet,
|
|
28
|
+
period: 2 * Math.PI,
|
|
29
|
+
responseSeconds: options.responseSeconds ?? 0
|
|
30
|
+
}]);
|
|
31
|
+
}
|
|
32
|
+
/** Moving horizon behind a fixed aircraft symbol. Geometry supplies the circular mask. */
|
|
33
|
+
function attitudeInstrument(options) {
|
|
34
|
+
return [{
|
|
35
|
+
node: options.bankNode,
|
|
36
|
+
source: options.rollSource ?? "roll",
|
|
37
|
+
property: "rotation",
|
|
38
|
+
axis: options.bankAxis ?? "z",
|
|
39
|
+
scale: options.bankDirection ?? -1,
|
|
40
|
+
period: 2 * Math.PI,
|
|
41
|
+
responseSeconds: options.responseSeconds ?? 0
|
|
42
|
+
}, {
|
|
43
|
+
node: options.pitchNode,
|
|
44
|
+
source: options.pitchSource ?? "pitch",
|
|
45
|
+
property: "position",
|
|
46
|
+
axis: options.pitchAxis ?? "y",
|
|
47
|
+
scale: options.pitchScale,
|
|
48
|
+
min: -Math.abs(options.pitchLimit),
|
|
49
|
+
max: Math.abs(options.pitchLimit),
|
|
50
|
+
responseSeconds: options.responseSeconds ?? 0
|
|
51
|
+
}];
|
|
52
|
+
}
|
|
53
|
+
/** Own an authored lamp/flag node; positive means on, missing readings turn it off. */
|
|
54
|
+
function annunciatorInstrument(node, source) {
|
|
55
|
+
return {
|
|
56
|
+
node,
|
|
57
|
+
source,
|
|
58
|
+
property: "visible",
|
|
59
|
+
scale: 1,
|
|
60
|
+
missing: "hide"
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Fixed-width unsigned integer display; no DOM, textures or per-frame glyph allocation. */
|
|
64
|
+
function numericInstrument(options) {
|
|
65
|
+
if (!options.prefix || options.digits.length < 1 || options.digits.length > 9) throw new Error("Numeric instruments require a prefix and 1–9 digits");
|
|
66
|
+
const derived = Object.create(null);
|
|
67
|
+
const bindings = [];
|
|
68
|
+
const whole = `${options.prefix}.whole`;
|
|
69
|
+
derived[whole] = {
|
|
70
|
+
op: "floor",
|
|
71
|
+
args: [options.source]
|
|
72
|
+
};
|
|
73
|
+
const segmentMasks = [
|
|
74
|
+
63,
|
|
75
|
+
6,
|
|
76
|
+
91,
|
|
77
|
+
79,
|
|
78
|
+
102,
|
|
79
|
+
109,
|
|
80
|
+
125,
|
|
81
|
+
7,
|
|
82
|
+
127,
|
|
83
|
+
111
|
|
84
|
+
];
|
|
85
|
+
const valid = `${options.prefix}.valid`, positive = `${options.prefix}.positive`, inRange = `${options.prefix}.inRange`;
|
|
86
|
+
derived[positive] = {
|
|
87
|
+
op: "greater",
|
|
88
|
+
args: [whole, -1]
|
|
89
|
+
};
|
|
90
|
+
derived[inRange] = {
|
|
91
|
+
op: "less",
|
|
92
|
+
args: [whole, 10 ** options.digits.length]
|
|
93
|
+
};
|
|
94
|
+
derived[valid] = {
|
|
95
|
+
op: "and",
|
|
96
|
+
args: [positive, inRange]
|
|
97
|
+
};
|
|
98
|
+
options.digits.forEach((nodes, index) => {
|
|
99
|
+
const quotient = `${options.prefix}.${index}.quotient`, integer = `${options.prefix}.${index}.integer`;
|
|
100
|
+
const digit = `${options.prefix}.${index}.digit`, display = `${options.prefix}.${index}.display`;
|
|
101
|
+
derived[quotient] = {
|
|
102
|
+
op: "divide",
|
|
103
|
+
args: [whole, 10 ** (options.digits.length - index - 1)]
|
|
104
|
+
};
|
|
105
|
+
derived[integer] = {
|
|
106
|
+
op: "floor",
|
|
107
|
+
args: [quotient]
|
|
108
|
+
};
|
|
109
|
+
derived[digit] = {
|
|
110
|
+
op: "modulo",
|
|
111
|
+
args: [integer, 10]
|
|
112
|
+
};
|
|
113
|
+
derived[display] = {
|
|
114
|
+
op: "if",
|
|
115
|
+
args: [
|
|
116
|
+
valid,
|
|
117
|
+
digit,
|
|
118
|
+
-1
|
|
119
|
+
]
|
|
120
|
+
};
|
|
121
|
+
nodes.forEach((node, segment) => {
|
|
122
|
+
bindings.push({
|
|
123
|
+
node,
|
|
124
|
+
source: display,
|
|
125
|
+
property: "visible",
|
|
126
|
+
missing: "hide",
|
|
127
|
+
keys: [[-1, 0], ...segmentMasks.map((mask, number) => [number, Number((mask & 1 << segment) !== 0)])]
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
derived,
|
|
133
|
+
bindings
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { altimeterInstrument, annunciatorInstrument, attitudeInstrument, compileModelSignalExpressions, createModelSignalVisual, dialInstrument, modelSignalDisplacement, numericInstrument, readModelSignals };
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import { DecalGeometry } from "three/addons/geometries/DecalGeometry.js";
|
|
3
|
+
//#region src/livery.ts
|
|
4
|
+
function inside(x, y, points) {
|
|
5
|
+
let hit = false;
|
|
6
|
+
for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
|
|
7
|
+
const a = points[i], b = points[j];
|
|
8
|
+
if (a[1] > y !== b[1] > y && x < (b[0] - a[0]) * (y - a[1]) / (b[1] - a[1]) + a[0]) hit = !hit;
|
|
9
|
+
}
|
|
10
|
+
return hit;
|
|
11
|
+
}
|
|
12
|
+
function textureFrom(bytes, size) {
|
|
13
|
+
const texture = new THREE.DataTexture(bytes, size, size);
|
|
14
|
+
texture.colorSpace = THREE.SRGBColorSpace;
|
|
15
|
+
texture.magFilter = THREE.LinearFilter;
|
|
16
|
+
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
17
|
+
texture.generateMipmaps = true;
|
|
18
|
+
texture.needsUpdate = true;
|
|
19
|
+
return texture;
|
|
20
|
+
}
|
|
21
|
+
/** Flat vector artwork, including outlined text; no browser fonts or image downloads. */
|
|
22
|
+
function liveryGraphicTexture(graphic, size = 256) {
|
|
23
|
+
const bytes = new Uint8Array(size * size * 4);
|
|
24
|
+
for (const shape of graphic.shapes) {
|
|
25
|
+
const hex = Number.parseInt(shape.color.slice(1), 16);
|
|
26
|
+
const xs = shape.points.map((p) => p[0]), ys = shape.points.map((p) => p[1]);
|
|
27
|
+
const x0 = Math.max(0, Math.floor(Math.min(...xs) * size)), x1 = Math.min(size, Math.ceil(Math.max(...xs) * size));
|
|
28
|
+
const y0 = Math.max(0, Math.floor(Math.min(...ys) * size)), y1 = Math.min(size, Math.ceil(Math.max(...ys) * size));
|
|
29
|
+
for (let y = y0; y < y1; y++) for (let x = x0; x < x1; x++) {
|
|
30
|
+
if (!inside((x + .5) / size, (y + .5) / size, shape.points)) continue;
|
|
31
|
+
const i = (y * size + x) * 4;
|
|
32
|
+
bytes[i] = hex >> 16 & 255;
|
|
33
|
+
bytes[i + 1] = hex >> 8 & 255;
|
|
34
|
+
bytes[i + 2] = hex & 255;
|
|
35
|
+
bytes[i + 3] = 255;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return textureFrom(bytes, size);
|
|
39
|
+
}
|
|
40
|
+
function camouflageTexture(pattern, channel) {
|
|
41
|
+
const size = 128, bytes = new Uint8Array(size * size * 4);
|
|
42
|
+
const hash = (x, y) => {
|
|
43
|
+
let h = Math.imul((x & 7) + 31 * (y & 7), 374761393) ^ pattern.seed;
|
|
44
|
+
h = Math.imul(h ^ h >>> 13, 1274126177);
|
|
45
|
+
return ((h ^ h >>> 16) >>> 0) / 4294967296;
|
|
46
|
+
};
|
|
47
|
+
for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
|
|
48
|
+
const fx = x * 8 / size, fy = y * 8 / size, ix = Math.floor(fx), iy = Math.floor(fy);
|
|
49
|
+
let u = fx - ix, v = fy - iy;
|
|
50
|
+
u = u * u * (3 - 2 * u);
|
|
51
|
+
v = v * v * (3 - 2 * v);
|
|
52
|
+
const n = THREE.MathUtils.lerp(THREE.MathUtils.lerp(hash(ix, iy), hash(ix + 1, iy), u), THREE.MathUtils.lerp(hash(ix, iy + 1), hash(ix + 1, iy + 1), u), v);
|
|
53
|
+
const color = pattern.colors[Math.min(pattern.colors.length - 1, Math.floor(n * pattern.colors.length))];
|
|
54
|
+
const hex = Number.parseInt(color.slice(1), 16), i = (y * size + x) * 4;
|
|
55
|
+
bytes[i] = hex >> 16 & 255;
|
|
56
|
+
bytes[i + 1] = hex >> 8 & 255;
|
|
57
|
+
bytes[i + 2] = hex & 255;
|
|
58
|
+
bytes[i + 3] = 255;
|
|
59
|
+
}
|
|
60
|
+
const texture = textureFrom(bytes, size);
|
|
61
|
+
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
|
|
62
|
+
texture.channel = channel;
|
|
63
|
+
texture.repeat.setScalar(pattern.scale);
|
|
64
|
+
return texture;
|
|
65
|
+
}
|
|
66
|
+
const materialsOf = (mesh) => Array.isArray(mesh.material) ? mesh.material : [mesh.material];
|
|
67
|
+
const restMatrices = /* @__PURE__ */ new WeakMap();
|
|
68
|
+
/** Extract just a slot's triangles, including material arrays with geometry groups. */
|
|
69
|
+
function surfaceGeometry(mesh, names) {
|
|
70
|
+
const source = mesh.geometry.index ? mesh.geometry.toNonIndexed() : mesh.geometry.clone();
|
|
71
|
+
const materials = materialsOf(mesh), groups = Array.isArray(mesh.material) ? source.groups : [{
|
|
72
|
+
start: 0,
|
|
73
|
+
count: source.getAttribute("position").count,
|
|
74
|
+
materialIndex: 0
|
|
75
|
+
}];
|
|
76
|
+
const positions = [], normals = [];
|
|
77
|
+
const p = source.getAttribute("position"), n = source.getAttribute("normal");
|
|
78
|
+
for (const group of groups) {
|
|
79
|
+
if (!names.has(materials[group.materialIndex ?? 0]?.name ?? "")) continue;
|
|
80
|
+
for (let i = group.start; i < Math.min(p.count, group.start + group.count); i++) {
|
|
81
|
+
positions.push(p.getX(i), p.getY(i), p.getZ(i));
|
|
82
|
+
if (n) normals.push(n.getX(i), n.getY(i), n.getZ(i));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
source.dispose();
|
|
86
|
+
const geometry = new THREE.BufferGeometry();
|
|
87
|
+
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
|
|
88
|
+
if (normals.length) geometry.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3));
|
|
89
|
+
else geometry.computeVertexNormals();
|
|
90
|
+
return geometry;
|
|
91
|
+
}
|
|
92
|
+
function projectPaint(mesh, bounds, transform, names, channel) {
|
|
93
|
+
const geometry = mesh.geometry.index ? mesh.geometry.toNonIndexed() : mesh.geometry.clone();
|
|
94
|
+
const attribute = channel === 0 ? "uv" : `uv${channel}`;
|
|
95
|
+
const p = geometry.getAttribute("position"), uv = geometry.getAttribute(attribute) ? Float32Array.from(geometry.getAttribute(attribute).array) : new Float32Array(p.count * 2);
|
|
96
|
+
const selected = new Uint8Array(p.count);
|
|
97
|
+
if (!Array.isArray(mesh.material)) selected.fill(1);
|
|
98
|
+
else for (const group of geometry.groups) {
|
|
99
|
+
if (!names.includes(mesh.material[group.materialIndex ?? 0]?.name ?? "")) continue;
|
|
100
|
+
selected.fill(1, group.start, Math.min(p.count, group.start + group.count));
|
|
101
|
+
}
|
|
102
|
+
const extent = bounds.getSize(new THREE.Vector3()), scale = Math.max(extent.x, extent.y, extent.z, .001);
|
|
103
|
+
const a = new THREE.Vector3(), b = new THREE.Vector3(), c = new THREE.Vector3();
|
|
104
|
+
for (let i = 0; i < p.count; i += 3) {
|
|
105
|
+
if (!selected[i]) continue;
|
|
106
|
+
a.fromBufferAttribute(p, i).applyMatrix4(transform);
|
|
107
|
+
b.fromBufferAttribute(p, i + 1).applyMatrix4(transform);
|
|
108
|
+
c.fromBufferAttribute(p, i + 2).applyMatrix4(transform);
|
|
109
|
+
const normal = b.clone().sub(a).cross(c.clone().sub(a));
|
|
110
|
+
const axis = Math.abs(normal.y) >= Math.max(Math.abs(normal.x), Math.abs(normal.z)) ? "y" : Math.abs(normal.x) > Math.abs(normal.z) ? "x" : "z";
|
|
111
|
+
for (const [j, point] of [
|
|
112
|
+
a,
|
|
113
|
+
b,
|
|
114
|
+
c
|
|
115
|
+
].entries()) {
|
|
116
|
+
uv[(i + j) * 2] = ((axis === "x" ? point.z : point.x) - (axis === "x" ? bounds.min.z : bounds.min.x)) / scale;
|
|
117
|
+
uv[(i + j) * 2 + 1] = ((axis === "y" ? point.z : point.y) - (axis === "y" ? bounds.min.z : bounds.min.y)) / scale;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
geometry.setAttribute(attribute, new THREE.BufferAttribute(uv, 2));
|
|
121
|
+
return geometry;
|
|
122
|
+
}
|
|
123
|
+
/** Apply to an instance, never a cache template. Does not mutate shared materials or geometry. */
|
|
124
|
+
function applyLivery(model, data) {
|
|
125
|
+
const variant = data.variants.find((v) => v.id === data.selected);
|
|
126
|
+
if (!variant) throw new Error(`Unknown livery: ${data.selected}`);
|
|
127
|
+
const warnings = [];
|
|
128
|
+
const originals = /* @__PURE__ */ new Map();
|
|
129
|
+
const materials = /* @__PURE__ */ new Set(), geometries = /* @__PURE__ */ new Set(), textures = /* @__PURE__ */ new Set();
|
|
130
|
+
const decals = [];
|
|
131
|
+
const remember = (mesh) => {
|
|
132
|
+
if (!originals.has(mesh)) originals.set(mesh, {
|
|
133
|
+
material: mesh.material,
|
|
134
|
+
geometry: mesh.geometry
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
model.updateWorldMatrix(true, true);
|
|
138
|
+
const rootInverse = model.matrixWorld.clone().invert();
|
|
139
|
+
let rest = restMatrices.get(model);
|
|
140
|
+
if (!rest) {
|
|
141
|
+
rest = /* @__PURE__ */ new Map();
|
|
142
|
+
model.traverse((object) => {
|
|
143
|
+
if (object instanceof THREE.Mesh) rest?.set(object, rootInverse.clone().multiply(object.matrixWorld));
|
|
144
|
+
});
|
|
145
|
+
restMatrices.set(model, rest);
|
|
146
|
+
}
|
|
147
|
+
const matrix = (mesh) => (rest?.get(mesh) ?? rootInverse.clone().multiply(mesh.matrixWorld)).clone();
|
|
148
|
+
const slots = /* @__PURE__ */ new Map();
|
|
149
|
+
for (const [name, binding] of Object.entries(data.slots)) {
|
|
150
|
+
const subtree = binding.node === void 0 ? model : model.getObjectByName(binding.node);
|
|
151
|
+
const meshes = [], bounds = new THREE.Box3();
|
|
152
|
+
subtree?.traverse((object) => {
|
|
153
|
+
if (!(object instanceof THREE.Mesh) || !materialsOf(object).some((m) => binding.materials.includes(m.name))) return;
|
|
154
|
+
if (object.isSkinnedMesh || object.morphTargetInfluences?.length) {
|
|
155
|
+
warnings.push(`Slot ${name}: deforming mesh ${object.name} is unsupported`);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
meshes.push(object);
|
|
159
|
+
const surface = surfaceGeometry(object, new Set(binding.materials));
|
|
160
|
+
surface.applyMatrix4(matrix(object));
|
|
161
|
+
surface.computeBoundingBox();
|
|
162
|
+
if (surface.boundingBox) bounds.union(surface.boundingBox);
|
|
163
|
+
surface.dispose();
|
|
164
|
+
});
|
|
165
|
+
if (!meshes.length || bounds.isEmpty()) warnings.push(`Slot ${name}: no matching surface`);
|
|
166
|
+
slots.set(name, {
|
|
167
|
+
meshes,
|
|
168
|
+
bounds,
|
|
169
|
+
binding
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
const painted = /* @__PURE__ */ new Map();
|
|
173
|
+
const occupiedChannels = /* @__PURE__ */ new Set();
|
|
174
|
+
model.traverse((object) => {
|
|
175
|
+
if (!(object instanceof THREE.Mesh)) return;
|
|
176
|
+
for (const material of materialsOf(object)) for (const value of Object.values(material)) if (value instanceof THREE.Texture) occupiedChannels.add(value.channel);
|
|
177
|
+
});
|
|
178
|
+
const channel = [
|
|
179
|
+
3,
|
|
180
|
+
2,
|
|
181
|
+
1,
|
|
182
|
+
0
|
|
183
|
+
].find((candidate) => !occupiedChannels.has(candidate));
|
|
184
|
+
for (const [name, paint] of Object.entries(variant.paints)) {
|
|
185
|
+
const slot = slots.get(name);
|
|
186
|
+
if (!slot || slot.bounds.isEmpty()) {
|
|
187
|
+
warnings.push(`Paint ${name}: missing slot`);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const clones = /* @__PURE__ */ new Map();
|
|
191
|
+
if (paint.camouflage && channel === void 0) warnings.push(`Paint ${name}: no unused UV channel for camouflage; using the base color`);
|
|
192
|
+
const texture = paint.camouflage && channel !== void 0 ? camouflageTexture(paint.camouflage, channel) : void 0;
|
|
193
|
+
if (texture) textures.add(texture);
|
|
194
|
+
for (const mesh of slot.meshes) {
|
|
195
|
+
remember(mesh);
|
|
196
|
+
const used = painted.get(mesh) ?? /* @__PURE__ */ new Set();
|
|
197
|
+
painted.set(mesh, used);
|
|
198
|
+
const next = materialsOf(mesh).map((source, index) => {
|
|
199
|
+
if (!slot.binding.materials.includes(source.name)) return source;
|
|
200
|
+
if (used.has(index)) {
|
|
201
|
+
warnings.push(`Paint ${name}: overlapping slot on ${mesh.name}`);
|
|
202
|
+
return source;
|
|
203
|
+
}
|
|
204
|
+
used.add(index);
|
|
205
|
+
let clone = clones.get(source);
|
|
206
|
+
if (!clone) {
|
|
207
|
+
clone = source.clone();
|
|
208
|
+
clones.set(source, clone);
|
|
209
|
+
materials.add(clone);
|
|
210
|
+
if ("color" in clone) clone.color.set(paint.color);
|
|
211
|
+
if ("roughness" in clone && paint.roughness !== void 0) clone.roughness = paint.roughness;
|
|
212
|
+
if ("metalness" in clone && paint.metalness !== void 0) clone.metalness = paint.metalness;
|
|
213
|
+
if ("map" in clone) clone.map = texture ?? null;
|
|
214
|
+
if (texture && "color" in clone) clone.color.set("#ffffff");
|
|
215
|
+
clone.needsUpdate = true;
|
|
216
|
+
}
|
|
217
|
+
return clone;
|
|
218
|
+
});
|
|
219
|
+
mesh.material = Array.isArray(mesh.material) ? next : next[0];
|
|
220
|
+
if (texture) {
|
|
221
|
+
mesh.geometry = projectPaint(mesh, slot.bounds, matrix(mesh), slot.binding.materials, texture.channel);
|
|
222
|
+
geometries.add(mesh.geometry);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const marking of variant.markings ?? []) {
|
|
227
|
+
const anchor = data.anchors?.[marking.anchor], slot = anchor && slots.get(anchor.slot);
|
|
228
|
+
if (!anchor || !slot || slot.bounds.isEmpty()) {
|
|
229
|
+
warnings.push(`Marking ${marking.anchor}: missing surface`);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const extent = slot.bounds.getSize(new THREE.Vector3());
|
|
233
|
+
const position = new THREE.Vector3(...anchor.position).multiply(extent).add(slot.bounds.min);
|
|
234
|
+
const size = new THREE.Vector3(...anchor.size).multiplyScalar(Math.max(extent.x, extent.y, extent.z));
|
|
235
|
+
const rotation = new THREE.Euler(...anchor.rotation);
|
|
236
|
+
const direction = new THREE.Vector3(0, 0, 1).applyEuler(rotation);
|
|
237
|
+
const texture = liveryGraphicTexture(marking.graphic);
|
|
238
|
+
textures.add(texture);
|
|
239
|
+
const material = new THREE.MeshStandardMaterial({
|
|
240
|
+
map: texture,
|
|
241
|
+
transparent: true,
|
|
242
|
+
depthWrite: false,
|
|
243
|
+
roughness: .65,
|
|
244
|
+
polygonOffset: true,
|
|
245
|
+
polygonOffsetFactor: -2,
|
|
246
|
+
polygonOffsetUnits: -2
|
|
247
|
+
});
|
|
248
|
+
materials.add(material);
|
|
249
|
+
let count = 0;
|
|
250
|
+
for (const mesh of slot.meshes) {
|
|
251
|
+
const surface = surfaceGeometry(mesh, new Set(slot.binding.materials));
|
|
252
|
+
const toRoot = matrix(mesh);
|
|
253
|
+
surface.applyMatrix4(toRoot);
|
|
254
|
+
const p = surface.getAttribute("position"), n = surface.getAttribute("normal"), indices = [];
|
|
255
|
+
const doubleSided = materialsOf(mesh).some((m) => slot.binding.materials.includes(m.name) && m.side === THREE.DoubleSide);
|
|
256
|
+
for (let i = 0; i < p.count; i += 3) {
|
|
257
|
+
const facing = new THREE.Vector3().fromBufferAttribute(n, i).dot(direction);
|
|
258
|
+
if (facing > .05) indices.push(i, i + 1, i + 2);
|
|
259
|
+
else if (doubleSided && facing < -.05) {
|
|
260
|
+
indices.push(i, i + 2, i + 1);
|
|
261
|
+
for (let j = i; j < i + 3; j++) n.setXYZ(j, -n.getX(j), -n.getY(j), -n.getZ(j));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
surface.setIndex(indices);
|
|
265
|
+
const geometry = new DecalGeometry(new THREE.Mesh(surface), position, rotation, size);
|
|
266
|
+
surface.dispose();
|
|
267
|
+
if (!geometry.getAttribute("position").count) {
|
|
268
|
+
geometry.dispose();
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
geometry.applyMatrix4(toRoot.invert());
|
|
272
|
+
geometries.add(geometry);
|
|
273
|
+
const decal = new THREE.Mesh(geometry, material);
|
|
274
|
+
decal.name = `livery:${marking.anchor}`;
|
|
275
|
+
decal.userData.liveryMarking = true;
|
|
276
|
+
decal.receiveShadow = true;
|
|
277
|
+
decal.renderOrder = 1;
|
|
278
|
+
mesh.add(decal);
|
|
279
|
+
decals.push(decal);
|
|
280
|
+
count++;
|
|
281
|
+
}
|
|
282
|
+
if (!count) warnings.push(`Marking ${marking.anchor}: projector misses its surface`);
|
|
283
|
+
}
|
|
284
|
+
let disposed = false;
|
|
285
|
+
return {
|
|
286
|
+
warnings,
|
|
287
|
+
dispose() {
|
|
288
|
+
if (disposed) return;
|
|
289
|
+
disposed = true;
|
|
290
|
+
for (const decal of decals) decal.removeFromParent();
|
|
291
|
+
for (const [mesh, original] of originals) {
|
|
292
|
+
mesh.material = original.material;
|
|
293
|
+
mesh.geometry = original.geometry;
|
|
294
|
+
}
|
|
295
|
+
for (const geometry of geometries) geometry.dispose();
|
|
296
|
+
for (const material of materials) material.dispose();
|
|
297
|
+
for (const texture of textures) texture.dispose();
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
export { liveryGraphicTexture as n, applyLivery as t };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ModelSignalBinding, 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
|
+
/** Unavailable binding sources on the most recent update (including invalid formulas). */
|
|
9
|
+
readonly missingSignals: readonly string[];
|
|
10
|
+
/** Absolute simulation time enables responseSeconds; omission applies readings immediately. */
|
|
11
|
+
update(signals: ModelSignals, timeSeconds?: number): void;
|
|
12
|
+
/** Restore authored transforms before removing or replacing bindings. */
|
|
13
|
+
reset(): void;
|
|
14
|
+
}
|
|
15
|
+
/** Read data only: no expressions, evaluation, prototype traversal, or simulation writes. */
|
|
16
|
+
declare function readModelSignals(sources: Readonly<Record<string, ModelSignalSource>> | undefined, readComponent: (name: string) => unknown): ModelSignals;
|
|
17
|
+
/**
|
|
18
|
+
* A binding's displacement for a signal, before its limits: linear in the signal, or along its
|
|
19
|
+
* keyframed curve, which holds its end values outside the keys. Exported for tests.
|
|
20
|
+
*/
|
|
21
|
+
declare function modelSignalDisplacement(binding: ModelSignalBinding, signal: number): number;
|
|
22
|
+
/** Model-agnostic; moves existing geometry without creating a cockpit or changing physics. */
|
|
23
|
+
declare function createModelSignalVisual(model: Object3D, spec: ModelSignalSpec): ModelSignalVisual;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { readModelSignals as a, modelSignalDisplacement as i, ModelSignals as n, createModelSignalVisual as r, ModelSignalVisual as t };
|