@volter/editor-model-play 0.5.189 → 0.5.191

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.
@@ -0,0 +1,199 @@
1
+ /**
2
+ * RECOLOURING THE DETACHED COPY — `play.tint` and `play.setOpacity` (`play-script.ts`).
3
+ *
4
+ * Changing a presented mesh's colour the three.js way does not work on the copy, for reasons
5
+ * the presenter has and a script cannot see:
6
+ *
7
+ * - A Blender mesh's `material` is an ARRAY, one entry per material slot (a lone material only
8
+ * for an object with no slots, which wears the presenter's grey fallback), so
9
+ * `mesh.material.color` is `undefined`.
10
+ * - The entries are SHARED: one material per Blender material, worn by every object with it
11
+ * in a slot. Recolouring one in place recolours them all.
12
+ * - The presenter OWNS `mesh.material`: whenever it re-applies shading it assigns every mesh's
13
+ * slots again from its own table, so materials a script assigned are put back.
14
+ * - `clone()` drops the presenter's draw hooks (its shader, private uniforms and node graph).
15
+ * - A material whose Base Color or Alpha a node graph drives never reads `color` or `opacity`:
16
+ * the graph's output replaces them in the shader.
17
+ *
18
+ * So an override is per OBJECT: the object's slots are swapped for copies of their own (the
19
+ * document's `ownMaterial`, which keeps its hooks and draws the material's constants, its
20
+ * image texture included), the copies carry the tint and opacity, and the slots the presenter
21
+ * gave are remembered. A slot the document cannot copy (no `ownMaterial`, or a material that
22
+ * is not the document's) leaves the object as it is and says so through `unsupported`; a
23
+ * `clone()` would draw without the presenter's hooks.
24
+ *
25
+ * The presenter's slots are re-read whenever they may have moved: an object whose slots the
26
+ * presenter has put back is given copies of the slots it now has, both each frame and before
27
+ * any change, so clearing returns the presenter's current slots, never a stale set. An object
28
+ * the script removed from the copy is let go on the next frame. That is heard, not searched
29
+ * for: each overridden mesh and its parents up to the copy's root report their own removal
30
+ * (three's `removed` event, which `remove`, `add` elsewhere and `attach` all dispatch), and only
31
+ * a mesh one of them reported is walked up to see whether it is still in the copy.
32
+ */
33
+ import type * as THREE from 'three';
34
+
35
+ type Slots = THREE.Material | THREE.Material[];
36
+ type Colored = THREE.Material & { color?: THREE.Color; emissive?: THREE.Color };
37
+
38
+ interface Override {
39
+ readonly mesh: THREE.Mesh;
40
+ authored: Slots;
41
+ shown: Slots;
42
+ copies: Colored[];
43
+ color: THREE.ColorRepresentation | null;
44
+ opacity: number | null;
45
+ /** The mesh and its parents below the copy's root, each told to report its removal. */
46
+ watched: THREE.Object3D[];
47
+ /** One of them was removed from its parent since the last frame. */
48
+ moved: boolean;
49
+ readonly onRemoved: () => void;
50
+ }
51
+
52
+ const slotsOf = (slots: Slots): THREE.Material[] => Array.isArray(slots) ? slots : [slots];
53
+ const isColor = (value: unknown): value is THREE.Color =>
54
+ typeof value === 'object' && value !== null && (value as THREE.Color).isColor === true;
55
+
56
+ export interface MaterialOverrides {
57
+ tint(target: THREE.Object3D, color: THREE.ColorRepresentation | null): void;
58
+ setOpacity(target: THREE.Object3D, opacity: number | null): void;
59
+ /** After the script's update: let go of removed objects, and re-wear the copies on any
60
+ * object the presenter re-dressed. */
61
+ frame(): void;
62
+ /** Return every object the presenter's slots and release the copies; later calls do nothing. */
63
+ dispose(): void;
64
+ }
65
+
66
+ /**
67
+ * `ownMaterial` answers the document's copy of one of its materials, or null for one that is
68
+ * not the document's. `unsupported` hears of an object that could not be overridden.
69
+ */
70
+ export function materialOverrides(options: {
71
+ readonly root: THREE.Object3D;
72
+ readonly ownMaterial: ((material: THREE.Material) => THREE.Material | null) | undefined;
73
+ readonly unsupported: (mesh: THREE.Mesh, material: THREE.Material) => void;
74
+ }): MaterialOverrides {
75
+ const { root, ownMaterial } = options;
76
+ const overrides = new Map<THREE.Mesh, Override>();
77
+ let disposed = false;
78
+ const release = (override: Override): void => {
79
+ for (const copy of override.copies) copy.dispose();
80
+ override.copies = [];
81
+ };
82
+ const unwatch = (override: Override): void => {
83
+ for (const object of override.watched) object.removeEventListener('removed', override.onRemoved);
84
+ override.watched = [];
85
+ };
86
+ /** Hear the mesh or a parent leave; a mesh not under the copy's root is let go next frame. */
87
+ const watch = (override: Override): void => {
88
+ unwatch(override);
89
+ let at: THREE.Object3D | null = override.mesh;
90
+ for (; at && at !== root; at = at.parent) {
91
+ at.addEventListener('removed', override.onRemoved);
92
+ override.watched.push(at);
93
+ }
94
+ override.moved = at !== root;
95
+ };
96
+ /** Wear copies of the object's slots; false (and nothing worn) when one cannot be copied. */
97
+ const dress = (override: Override): boolean => {
98
+ release(override);
99
+ const copies: Colored[] = [];
100
+ for (const material of slotsOf(override.authored)) {
101
+ const copy = ownMaterial?.(material) ?? null;
102
+ if (!copy) {
103
+ for (const made of copies) made.dispose();
104
+ override.mesh.material = override.authored;
105
+ options.unsupported(override.mesh, material);
106
+ return false;
107
+ }
108
+ copies.push(copy as Colored);
109
+ }
110
+ override.copies = copies;
111
+ override.shown = Array.isArray(override.authored) ? copies : copies[0]!;
112
+ override.mesh.material = override.shown;
113
+ return true;
114
+ };
115
+ // Values only: the copies were made from the authored slots, so each starts from its own.
116
+ const paint = (override: Override): void => {
117
+ const authored = slotsOf(override.authored) as Colored[];
118
+ override.copies.forEach((copy, index) => {
119
+ const source = authored[index]!;
120
+ if (isColor(copy.color) && isColor(source.color)) {
121
+ if (override.color === null) copy.color.copy(source.color);
122
+ else copy.color.set(override.color);
123
+ }
124
+ // An emitting surface glows in its tint; one that does not stays unlit by it.
125
+ if (isColor(copy.emissive) && isColor(source.emissive)) {
126
+ const emits = source.emissive.r + source.emissive.g + source.emissive.b > 0;
127
+ if (override.color === null || !emits) copy.emissive.copy(source.emissive);
128
+ else copy.emissive.set(override.color);
129
+ }
130
+ copy.opacity = override.opacity ?? source.opacity;
131
+ const transparent = source.transparent || copy.opacity < 1;
132
+ if (copy.transparent !== transparent) {
133
+ copy.transparent = transparent;
134
+ copy.needsUpdate = true;
135
+ }
136
+ });
137
+ };
138
+ const forget = (override: Override): void => {
139
+ if (override.mesh.material === override.shown) override.mesh.material = override.authored;
140
+ release(override);
141
+ unwatch(override);
142
+ overrides.delete(override.mesh);
143
+ };
144
+ /** The presenter re-assigned the object's slots since it last wore copies: adopt its slots
145
+ * as the authored ones and dress again. False when they cannot be copied. */
146
+ const resync = (override: Override): boolean => {
147
+ if (override.mesh.material === override.shown) return true;
148
+ override.authored = override.mesh.material;
149
+ override.shown = override.mesh.material;
150
+ if (dress(override)) return true;
151
+ release(override);
152
+ unwatch(override);
153
+ overrides.delete(override.mesh);
154
+ return false;
155
+ };
156
+ const change = (target: THREE.Object3D, edit: (override: Override) => void): void => {
157
+ if (disposed) return;
158
+ target.traverse((object) => {
159
+ const mesh = object as THREE.Mesh;
160
+ if (mesh.isMesh !== true || !mesh.material) return;
161
+ let override = overrides.get(mesh);
162
+ if (override && !resync(override)) override = undefined;
163
+ if (!override) {
164
+ const fresh: Override = { mesh, authored: mesh.material, shown: mesh.material, copies: [], color: null, opacity: null,
165
+ watched: [], moved: false, onRemoved: () => { fresh.moved = true; } };
166
+ edit(fresh);
167
+ if (fresh.color === null && fresh.opacity === null) return;
168
+ if (!dress(fresh)) return;
169
+ overrides.set(mesh, fresh);
170
+ watch(fresh);
171
+ override = fresh;
172
+ } else edit(override);
173
+ if (override.color === null && override.opacity === null) forget(override);
174
+ else paint(override);
175
+ });
176
+ };
177
+ return {
178
+ tint(target, color) { change(target, (override) => { override.color = color; }); },
179
+ setOpacity(target, opacity) {
180
+ if (opacity !== null && !Number.isFinite(opacity)) throw new Error(`setOpacity takes a number from 0 to 1, or null; it was given ${String(opacity)}.`);
181
+ const value = opacity === null ? null : Math.min(1, Math.max(0, opacity));
182
+ change(target, (override) => { override.opacity = value; });
183
+ },
184
+ frame() {
185
+ for (const override of [...overrides.values()]) {
186
+ if (override.moved) {
187
+ // Re-walked only after a reported removal; one put back under the copy is watched anew.
188
+ watch(override);
189
+ if (override.moved) { forget(override); continue; }
190
+ }
191
+ if (override.mesh.material !== override.shown && resync(override)) paint(override);
192
+ }
193
+ },
194
+ dispose() {
195
+ disposed = true;
196
+ for (const override of [...overrides.values()]) forget(override);
197
+ },
198
+ };
199
+ }