@combeenation/3d-viewer 6.5.0 → 7.0.0-beta2
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 +111 -111
- package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
- package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
- package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
- package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
- package/dist/lib-cjs/api/classes/element.d.ts +153 -149
- package/dist/lib-cjs/api/classes/element.js +670 -669
- package/dist/lib-cjs/api/classes/element.js.map +1 -1
- package/dist/lib-cjs/api/classes/event.d.ts +396 -342
- package/dist/lib-cjs/api/classes/event.js +419 -365
- package/dist/lib-cjs/api/classes/event.js.map +1 -1
- package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
- package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
- package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -0
- package/dist/lib-cjs/api/classes/fuzzyMap.js +22 -0
- package/dist/lib-cjs/api/classes/fuzzyMap.js.map +1 -0
- package/dist/lib-cjs/api/classes/parameter.d.ts +351 -339
- package/dist/lib-cjs/api/classes/parameter.js +517 -464
- package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
- package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
- package/dist/lib-cjs/api/classes/parameterObservable.js +72 -97
- package/dist/lib-cjs/api/classes/parameterObservable.js.map +1 -1
- package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
- package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
- package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
- package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
- package/dist/lib-cjs/api/classes/variant.d.ts +253 -234
- package/dist/lib-cjs/api/classes/variant.js +843 -818
- package/dist/lib-cjs/api/classes/variant.js.map +1 -1
- package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -44
- package/dist/lib-cjs/api/classes/variantInstance.js +125 -105
- package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
- package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
- package/dist/lib-cjs/api/classes/variantParameterizable.js +88 -88
- package/dist/lib-cjs/api/classes/viewer.d.ts +199 -185
- package/dist/lib-cjs/api/classes/viewer.js +670 -619
- package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
- package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
- package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
- package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
- package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
- package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
- package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
- package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
- package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
- package/dist/lib-cjs/api/manager/animationManager.js +126 -126
- package/dist/lib-cjs/api/manager/animationManager.js.map +1 -1
- package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +78 -78
- package/dist/lib-cjs/api/manager/gltfExportManager.js +241 -241
- package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
- package/dist/lib-cjs/api/manager/sceneManager.js +130 -130
- package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
- package/dist/lib-cjs/api/manager/tagManager.d.ts +108 -0
- package/dist/lib-cjs/api/manager/tagManager.js +420 -0
- package/dist/lib-cjs/api/manager/tagManager.js.map +1 -0
- package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
- package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
- package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +102 -92
- package/dist/lib-cjs/api/manager/variantInstanceManager.js +284 -260
- package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +1 -1
- package/dist/lib-cjs/api/store/specStorage.d.ts +32 -24
- package/dist/lib-cjs/api/store/specStorage.js +65 -50
- package/dist/lib-cjs/api/store/specStorage.js.map +1 -1
- package/dist/lib-cjs/api/util/babylonHelper.d.ts +235 -206
- package/dist/lib-cjs/api/util/babylonHelper.js +745 -668
- package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
- package/dist/lib-cjs/api/util/globalTypes.d.ts +432 -387
- package/dist/lib-cjs/api/util/globalTypes.js +1 -1
- package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
- package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
- package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -43
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js +173 -155
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js.map +1 -1
- package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
- package/dist/lib-cjs/api/util/stringHelper.js +32 -32
- package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/structureHelper.js +48 -48
- package/dist/lib-cjs/buildinfo.json +3 -3
- package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
- package/dist/lib-cjs/index.d.ts +53 -52
- package/dist/lib-cjs/index.js +114 -112
- package/dist/lib-cjs/index.js.map +1 -1
- package/package.json +81 -81
- package/src/api/classes/animationInterface.ts +10 -10
- package/src/api/classes/dottedPath.ts +181 -181
- package/src/api/classes/element.ts +731 -717
- package/src/api/classes/event.ts +452 -385
- package/src/api/classes/eventBroadcaster.ts +52 -52
- package/src/api/classes/fuzzyMap.ts +21 -0
- package/src/api/classes/parameter.ts +554 -497
- package/src/api/classes/parameterObservable.ts +73 -100
- package/src/api/classes/parameterizable.ts +87 -87
- package/src/api/classes/placementAnimation.ts +162 -162
- package/src/api/classes/variant.ts +933 -884
- package/src/api/classes/variantInstance.ts +123 -97
- package/src/api/classes/variantParameterizable.ts +85 -85
- package/src/api/classes/viewer.ts +743 -691
- package/src/api/classes/viewerLight.ts +339 -339
- package/src/api/internal/debugViewer.ts +90 -90
- package/src/api/internal/lensRendering.ts +9 -9
- package/src/api/internal/sceneSetup.ts +205 -205
- package/src/api/manager/animationManager.ts +143 -143
- package/src/api/manager/gltfExportManager.ts +236 -236
- package/src/api/manager/sceneManager.ts +136 -132
- package/src/api/manager/tagManager.ts +451 -0
- package/src/api/manager/textureLoadManager.ts +95 -95
- package/src/api/manager/variantInstanceManager.ts +297 -265
- package/src/api/store/specStorage.ts +68 -51
- package/src/api/util/babylonHelper.ts +817 -739
- package/src/api/util/globalTypes.ts +499 -437
- package/src/api/util/resourceHelper.ts +191 -191
- package/src/api/util/sceneLoaderHelper.ts +170 -151
- package/src/api/util/stringHelper.ts +30 -30
- package/src/api/util/structureHelper.ts +49 -49
- package/src/buildinfo.json +3 -3
- package/src/dev.ts +62 -60
- package/src/index.ts +100 -98
- package/src/types.d.ts +35 -28
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { Event, emitter } from '../classes/event';
|
|
2
|
+
import { FuzzyMap } from '../classes/fuzzyMap';
|
|
3
|
+
import { Parameter } from '../classes/parameter';
|
|
4
|
+
import {
|
|
5
|
+
assertMeshCapability,
|
|
6
|
+
cloneTransformNodeMaterial,
|
|
7
|
+
injectNodeMetadata,
|
|
8
|
+
mapTags,
|
|
9
|
+
setMaterial,
|
|
10
|
+
setMaterialColor,
|
|
11
|
+
setMaterialMetallness,
|
|
12
|
+
setMaterialRoughness,
|
|
13
|
+
} from '../util/babylonHelper';
|
|
14
|
+
import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
|
|
15
|
+
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
|
|
16
|
+
import { Tags } from '@babylonjs/core/Misc/tags';
|
|
17
|
+
import { clone, get, has, merge, uniq } from 'lodash-es';
|
|
18
|
+
|
|
19
|
+
export class TagManager {
|
|
20
|
+
public readonly parameters: TagManagerParameterBag = new FuzzyMap();
|
|
21
|
+
|
|
22
|
+
public readonly parameterObservers: TagManagerParameterObserverBag = new Map();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Constructor.
|
|
26
|
+
*/
|
|
27
|
+
public constructor(protected viewer: Viewer) {
|
|
28
|
+
this.addParameterObservers();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Calls the {@link ParameterObserver} for given {@link Parameter} and applies the {@link ParameterValue} on all
|
|
33
|
+
* nodes with given tagName.
|
|
34
|
+
*/
|
|
35
|
+
public async setTagParameterValue(
|
|
36
|
+
tagName: string,
|
|
37
|
+
parameterName: string,
|
|
38
|
+
value: ParameterValue
|
|
39
|
+
): Promise<ParameterObserverResult> {
|
|
40
|
+
return await this.handleParameter({ tagName: tagName }, parameterName, value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Calls the {@link ParameterObserver} for given {@link Parameter} and applies the {@link ParameterValue} on all
|
|
45
|
+
* nodes with given nodeName.
|
|
46
|
+
*/
|
|
47
|
+
public async setNodeParameterValue(
|
|
48
|
+
nodeName: string,
|
|
49
|
+
parameterName: string,
|
|
50
|
+
value: ParameterValue
|
|
51
|
+
): Promise<ParameterObserverResult> {
|
|
52
|
+
return await this.handleParameter({ nodeName: nodeName }, parameterName, value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Splits the passed {@link TagManagerParameterValue}s into individual values and calls all {@link ParameterObserver}s
|
|
57
|
+
* of all {@link Parameter}s with the respective {@link TagManagerSubject} and {@link ParameterValue}. The result is a
|
|
58
|
+
* map of passed {@link TagManagerParameterValue}s and the associated {@link ParameterObserverResult} of the
|
|
59
|
+
* {@link ParameterObserver}.
|
|
60
|
+
*/
|
|
61
|
+
public async setParameterValues(
|
|
62
|
+
values: TagManagerParameterValue[]
|
|
63
|
+
): Promise<Map<TagManagerParameterValue, ParameterObserverResult>> {
|
|
64
|
+
const observerResultMap: Map<TagManagerParameterValue, ParameterObserverResult> = new Map();
|
|
65
|
+
const tagPromises: Promise<ParameterObserver>[] = [];
|
|
66
|
+
const nodePromises: Promise<ParameterObserver>[] = [];
|
|
67
|
+
for (const value of values) {
|
|
68
|
+
const subject: TagManagerSubject = {};
|
|
69
|
+
if (value.tagName) {
|
|
70
|
+
subject.tagName = value.tagName;
|
|
71
|
+
}
|
|
72
|
+
if (value.nodeName) {
|
|
73
|
+
subject.nodeName = value.nodeName;
|
|
74
|
+
}
|
|
75
|
+
const promise = this.handleParameter(subject, value.parameterName, value.value);
|
|
76
|
+
promise.then(result => observerResultMap.set(value, result));
|
|
77
|
+
if (value.tagName) {
|
|
78
|
+
tagPromises.push(promise);
|
|
79
|
+
}
|
|
80
|
+
if (value.nodeName) {
|
|
81
|
+
nodePromises.push(promise);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
await Promise.all(tagPromises);
|
|
85
|
+
await Promise.all(nodePromises);
|
|
86
|
+
return observerResultMap;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Gets a list of {@link TagManagerSubject}s that are present in both the state of the {@link TagManager}'s
|
|
91
|
+
* {@link parameters} and the given node.
|
|
92
|
+
*/
|
|
93
|
+
public getSubjectsFor(node: TransformNode): TagManagerSubject[] {
|
|
94
|
+
return [...this.parameters.keys()].filter(
|
|
95
|
+
subject =>
|
|
96
|
+
(subject.tagName && Tags.MatchesQuery(node, subject.tagName)) ||
|
|
97
|
+
(subject.nodeName && node.name === subject.nodeName)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Gets a list of {@link TagManagerSubject}s that are present in both the state of the {@link TagManager}'s
|
|
103
|
+
* {@link parameters} and the given nodes.
|
|
104
|
+
*/
|
|
105
|
+
public getApplicableSubjectsFor(nodes: TransformNode[]): TagManagerSubject[] {
|
|
106
|
+
let applicableSubjects: TagManagerSubject[] = [];
|
|
107
|
+
for (const node of nodes) {
|
|
108
|
+
const subjects = this.getSubjectsFor(node);
|
|
109
|
+
applicableSubjects = [...applicableSubjects, ...subjects];
|
|
110
|
+
}
|
|
111
|
+
return uniq(applicableSubjects);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Applies all existing states of the {@link TagManager}'s {@link parameters} for given {@link TagManagerSubject}s.
|
|
116
|
+
*/
|
|
117
|
+
public async applyExistingParameterValuesFor(
|
|
118
|
+
subjects: TagManagerSubject[]
|
|
119
|
+
): Promise<Map<TagManagerSubject, ParameterObserverResult>> {
|
|
120
|
+
const observerResultMap: Map<TagManagerSubject, ParameterObserverResult> = new Map();
|
|
121
|
+
let tagPromises: Promise<ParameterObserver>[] = [];
|
|
122
|
+
let nodePromises: Promise<ParameterObserver>[] = [];
|
|
123
|
+
for (const subject of subjects) {
|
|
124
|
+
this.clearAppliedNodeMetadataParameters(this.getNodesBySubject(subject));
|
|
125
|
+
const promises = this.handleParameterBag(subject, this.parameters.get(subject)!);
|
|
126
|
+
for (const promise of promises) {
|
|
127
|
+
promise.then(result => observerResultMap.set(subject, result));
|
|
128
|
+
}
|
|
129
|
+
if (subject.tagName) {
|
|
130
|
+
tagPromises = [...tagPromises, ...promises];
|
|
131
|
+
}
|
|
132
|
+
if (subject.nodeName) {
|
|
133
|
+
nodePromises = [...nodePromises, ...promises];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
await Promise.all(tagPromises);
|
|
137
|
+
await Promise.all(nodePromises);
|
|
138
|
+
return observerResultMap;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Does the same as {@link setParameterValues} with the difference that all existing states of the
|
|
143
|
+
* {@link TagManager}'s {@link parameters} are applied to all given nodes.
|
|
144
|
+
*/
|
|
145
|
+
public async applyExistingParameterValuesTo(
|
|
146
|
+
nodes: TransformNode[]
|
|
147
|
+
): Promise<Map<TagManagerSubject, ParameterObserverResult>> {
|
|
148
|
+
return this.applyExistingParameterValuesFor(this.getApplicableSubjectsFor(nodes));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Applies all existing states of the {@link TagManager}'s {@link parameters} for all {@link TagManagerSubject}s.
|
|
153
|
+
*/
|
|
154
|
+
public async applyExistingParameterValues(): Promise<Map<TagManagerSubject, ParameterObserverResult>> {
|
|
155
|
+
return this.applyExistingParameterValuesFor([...this.parameters.keys()]);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Maps all tags on given nodes with the {@link TagMapping} and updates the state of the {@link TagManager}'s
|
|
160
|
+
* {@link parameters} for all new tags.
|
|
161
|
+
* Also updates the state of all nodes in the tag managers parameter state.
|
|
162
|
+
*/
|
|
163
|
+
public mapNodesAndTags(nodes: TransformNode[], tagMapping: TagMapping): TagManager {
|
|
164
|
+
this.updateParametersWithNodeAndTagMapping(nodes, tagMapping);
|
|
165
|
+
mapTags(nodes, tagMapping);
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Prints the state of the {@link TagManager}'s {@link parameters} to the console.
|
|
171
|
+
*/
|
|
172
|
+
public printAllTags(): void {
|
|
173
|
+
const printable = [];
|
|
174
|
+
for (const subject of this.parameters.keys()) {
|
|
175
|
+
const parameterBag = this.parameters.get(subject);
|
|
176
|
+
for (const parameter in parameterBag) {
|
|
177
|
+
printable.push({
|
|
178
|
+
subject: JSON.stringify(subject),
|
|
179
|
+
parameter: parameter,
|
|
180
|
+
value: parameterBag[parameter],
|
|
181
|
+
nodes: this.getNodesBySubject(subject),
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
console.table(printable);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Prints the state of the {@link TagManager}'s {@link parameters} for one tagName to the console.
|
|
190
|
+
*/
|
|
191
|
+
public printTagInfo(tagName: string): void {
|
|
192
|
+
const parameterBag = this.parameters.get({ tagName: tagName });
|
|
193
|
+
const printable = [];
|
|
194
|
+
for (const parameter in parameterBag) {
|
|
195
|
+
printable.push({
|
|
196
|
+
parameter: parameter,
|
|
197
|
+
value: parameterBag[parameter],
|
|
198
|
+
nodes: this.getNodesBySubject({ tagName: tagName }),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
console.table(printable);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Sets a {@link ParameterObserver} for the given {@link Parameter}.
|
|
206
|
+
*/
|
|
207
|
+
public setParameterObserver(parameter: string, observer: TagManagerParameterObserver): TagManager {
|
|
208
|
+
this.parameterObservers.set(parameter, observer);
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Gets all nodes for given {@link TagManagerSubject} on the Babylon.js scene.
|
|
214
|
+
*/
|
|
215
|
+
public getNodesBySubject(subject: TagManagerSubject, predicate?: (node: TransformNode) => boolean): TransformNode[] {
|
|
216
|
+
let nodes: TransformNode[] = [];
|
|
217
|
+
if (subject.tagName) {
|
|
218
|
+
nodes = [
|
|
219
|
+
...nodes,
|
|
220
|
+
...this.viewer.scene.getTransformNodesByTags(subject.tagName, predicate),
|
|
221
|
+
...this.viewer.scene.getMeshesByTags(subject.tagName, predicate),
|
|
222
|
+
];
|
|
223
|
+
}
|
|
224
|
+
if (subject.nodeName) {
|
|
225
|
+
nodes = [
|
|
226
|
+
...nodes,
|
|
227
|
+
this.viewer.scene.getTransformNodeByName(subject.nodeName),
|
|
228
|
+
this.viewer.scene.getMeshByName(subject.nodeName),
|
|
229
|
+
].filter(t => !!t && (!predicate || predicate(t))) as TransformNode[];
|
|
230
|
+
}
|
|
231
|
+
return nodes;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Registers observers that are called on every new node added to the Babylon.js scene. The observers ensure that each
|
|
236
|
+
* new node gets the state of the {@link TagManager}'s {@link parameters} applied.
|
|
237
|
+
*/
|
|
238
|
+
public registerNewTransformNodeObservers(scene: Scene) {
|
|
239
|
+
const onNewTransformNodeAdded = async (node: TransformNode) => {
|
|
240
|
+
await this.viewer.scene.whenReadyAsync();
|
|
241
|
+
await this.applyExistingParameterValuesTo([node]);
|
|
242
|
+
};
|
|
243
|
+
scene.onNewTransformNodeAddedObservable.makeObserverBottomPriority(
|
|
244
|
+
scene.onNewTransformNodeAddedObservable.add(onNewTransformNodeAdded)!
|
|
245
|
+
);
|
|
246
|
+
scene.onNewMeshAddedObservable.makeObserverBottomPriority(
|
|
247
|
+
scene.onNewMeshAddedObservable.add(onNewTransformNodeAdded)!
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Adds the default {@link ParameterObserver}s.
|
|
253
|
+
* @protected
|
|
254
|
+
*/
|
|
255
|
+
protected addParameterObservers(): TagManager {
|
|
256
|
+
this.parameterObservers.set(Parameter.VISIBLE, async payload => {
|
|
257
|
+
let visible;
|
|
258
|
+
try {
|
|
259
|
+
visible = Parameter.parseBoolean(payload.newValue);
|
|
260
|
+
} catch (e) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (visible === true) {
|
|
264
|
+
for (const node of payload.nodes) {
|
|
265
|
+
node.setEnabled(true);
|
|
266
|
+
}
|
|
267
|
+
} else if (visible === false) {
|
|
268
|
+
for (const node of payload.nodes) {
|
|
269
|
+
node.setEnabled(false);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return payload;
|
|
273
|
+
});
|
|
274
|
+
this.parameterObservers.set(Parameter.SCALING, async payload => {
|
|
275
|
+
for (const node of payload.nodes) {
|
|
276
|
+
node.scaling = Parameter.parseVector(payload.newValue);
|
|
277
|
+
}
|
|
278
|
+
return payload;
|
|
279
|
+
});
|
|
280
|
+
this.parameterObservers.set(Parameter.POSITION, async payload => {
|
|
281
|
+
for (const node of payload.nodes) {
|
|
282
|
+
node.position = Parameter.parseVector(payload.newValue);
|
|
283
|
+
}
|
|
284
|
+
return payload;
|
|
285
|
+
});
|
|
286
|
+
this.parameterObservers.set(Parameter.ROTATION, async payload => {
|
|
287
|
+
for (const node of payload.nodes) {
|
|
288
|
+
node.rotation = Parameter.parseRotation(payload.newValue);
|
|
289
|
+
}
|
|
290
|
+
return payload;
|
|
291
|
+
});
|
|
292
|
+
this.parameterObservers.set(Parameter.MATERIAL, async payload => {
|
|
293
|
+
for (const node of payload.nodes) {
|
|
294
|
+
assertMeshCapability(node, Parameter.MATERIAL);
|
|
295
|
+
setMaterial(node, payload.newValue.toString(), false);
|
|
296
|
+
}
|
|
297
|
+
return payload;
|
|
298
|
+
});
|
|
299
|
+
this.parameterObservers.set(Parameter.MATERIAL_COLOR, async payload => {
|
|
300
|
+
const parsedValue = Parameter.parseColor(payload.newValue);
|
|
301
|
+
for (const node of payload.nodes) {
|
|
302
|
+
assertMeshCapability(node, Parameter.MATERIAL_COLOR);
|
|
303
|
+
if (this.viewer.cloneMaterialsOnMutation) {
|
|
304
|
+
if (!has(node.metadata, 'dirty.material')) {
|
|
305
|
+
cloneTransformNodeMaterial(node);
|
|
306
|
+
}
|
|
307
|
+
if (!has(node.metadata, 'dirty.material.color')) {
|
|
308
|
+
// inject initial value and mark as dirty
|
|
309
|
+
injectNodeMetadata(node, { dirty: { material: { color: payload.oldValue } } }, false);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
setMaterialColor(node, parsedValue, false);
|
|
313
|
+
}
|
|
314
|
+
return payload;
|
|
315
|
+
});
|
|
316
|
+
this.parameterObservers.set(Parameter.MATERIAL_ROUGHNESS, async payload => {
|
|
317
|
+
const parsedValue = Parameter.parseNumber(payload.newValue);
|
|
318
|
+
for (const node of payload.nodes as AbstractMesh[]) {
|
|
319
|
+
assertMeshCapability(node, Parameter.MATERIAL_ROUGHNESS);
|
|
320
|
+
if (this.viewer.cloneMaterialsOnMutation) {
|
|
321
|
+
if (!has(node.metadata, 'dirty.material')) {
|
|
322
|
+
cloneTransformNodeMaterial(node);
|
|
323
|
+
}
|
|
324
|
+
if (!has(node.metadata, 'dirty.material.roughness')) {
|
|
325
|
+
// inject initial value and mark as dirty
|
|
326
|
+
injectNodeMetadata(node, { dirty: { material: { color: payload.oldValue } } }, false);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
setMaterialRoughness(node, parsedValue, false);
|
|
330
|
+
}
|
|
331
|
+
return payload;
|
|
332
|
+
});
|
|
333
|
+
this.parameterObservers.set(Parameter.MATERIAL_METALLNESS, async payload => {
|
|
334
|
+
const parsedValue = Parameter.parseNumber(payload.newValue);
|
|
335
|
+
for (const node of payload.nodes as AbstractMesh[]) {
|
|
336
|
+
assertMeshCapability(node, Parameter.MATERIAL_METALLNESS);
|
|
337
|
+
if (this.viewer.cloneMaterialsOnMutation) {
|
|
338
|
+
if (!has(node.metadata, 'dirty.material')) {
|
|
339
|
+
cloneTransformNodeMaterial(node);
|
|
340
|
+
}
|
|
341
|
+
if (!has(node.metadata, 'dirty.material.metallness')) {
|
|
342
|
+
// inject initial value and mark as dirty
|
|
343
|
+
injectNodeMetadata(node, { dirty: { material: { color: payload.oldValue } } }, false);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
setMaterialMetallness(node, parsedValue, false);
|
|
347
|
+
}
|
|
348
|
+
return payload;
|
|
349
|
+
});
|
|
350
|
+
return this;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Passes all {@link ParameterValue}s of the given {@link ParameterBag} for given {@link TagManagerSubject} to
|
|
355
|
+
* {@link handleParameter}.
|
|
356
|
+
* @protected
|
|
357
|
+
*/
|
|
358
|
+
protected handleParameterBag(subject: TagManagerSubject, parameters: ParameterBag): Promise<ParameterObserver>[] {
|
|
359
|
+
const observerPromises: Promise<ParameterObserver>[] = [];
|
|
360
|
+
for (const parameter in parameters) {
|
|
361
|
+
observerPromises.push(this.handleParameter(subject, parameter, parameters[parameter]));
|
|
362
|
+
}
|
|
363
|
+
return observerPromises;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Asserts the given {@link Parameter} and its {@link ParameterValue}, stores the state in the {@link TagManager}'s
|
|
368
|
+
* {@link parameters}, calculates the applicable nodes, injects the {@link ParameterValue} in the node's metadata,
|
|
369
|
+
* calls the responsible {@link ParameterObserver}s and emits the event {@link Event.TAG_MANAGER_PARAMETER_COMMITTED}.
|
|
370
|
+
* @protected
|
|
371
|
+
*/
|
|
372
|
+
protected handleParameter(
|
|
373
|
+
subject: TagManagerSubject,
|
|
374
|
+
parameter: string,
|
|
375
|
+
parameterValue: ParameterValue
|
|
376
|
+
): Promise<ParameterObserver> {
|
|
377
|
+
Parameter.assertParameter(Parameter.declarations, parameter, parameterValue);
|
|
378
|
+
if (!this.parameterObservers.has(parameter)) {
|
|
379
|
+
return Promise.resolve(() => undefined);
|
|
380
|
+
}
|
|
381
|
+
const parameterBag = this.parameters.get(subject) || {};
|
|
382
|
+
const oldValue = parameterBag[parameter];
|
|
383
|
+
this.parameters.set(subject, parameterBag);
|
|
384
|
+
this.parameters.get(subject)![parameter] = parameterValue;
|
|
385
|
+
const predicate = (node: TransformNode) => node.metadata?.tagManager?.parameters[parameter] !== parameterValue;
|
|
386
|
+
const nodes = this.getNodesBySubject(subject, predicate);
|
|
387
|
+
if (nodes.length === 0) {
|
|
388
|
+
return Promise.resolve(() => undefined);
|
|
389
|
+
}
|
|
390
|
+
for (const node of nodes) {
|
|
391
|
+
injectNodeMetadata(node, { tagManager: { parameters: { [parameter]: parameterValue } } }, false);
|
|
392
|
+
}
|
|
393
|
+
const observer = this.parameterObservers.get(parameter)!;
|
|
394
|
+
const observerResult = observer({ subject: subject, nodes: nodes, newValue: parameterValue, oldValue: oldValue });
|
|
395
|
+
return Promise.resolve(observerResult).then(result => {
|
|
396
|
+
emitter.emit(Event.TAG_MANAGER_PARAMETER_COMMITTED, {
|
|
397
|
+
subject: subject,
|
|
398
|
+
parameter: parameter,
|
|
399
|
+
value: parameterValue,
|
|
400
|
+
nodes: nodes,
|
|
401
|
+
result: result,
|
|
402
|
+
});
|
|
403
|
+
}) as Promise<ParameterObserver>;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Updates the state of the {@link TagManager}'s {@link parameters} for all new nodes and tags given in
|
|
408
|
+
* {@link TagMapping}.
|
|
409
|
+
* @protected
|
|
410
|
+
*/
|
|
411
|
+
protected updateParametersWithNodeAndTagMapping(nodes: TransformNode[], tagMapping: TagMapping): void {
|
|
412
|
+
// create node mapping by linking the clone source name to the new node name
|
|
413
|
+
const nodeMapping = nodes.reduce((accNodeMapping, curNode) => {
|
|
414
|
+
const oldNode = get(curNode.metadata, 'cloneSource') as TransformNode;
|
|
415
|
+
if (oldNode) {
|
|
416
|
+
accNodeMapping[oldNode.name] = curNode.name;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return accNodeMapping;
|
|
420
|
+
}, {} as TagMapping);
|
|
421
|
+
|
|
422
|
+
for (const subject of this.parameters.keys()) {
|
|
423
|
+
const isNode = subject.nodeName;
|
|
424
|
+
const newName = isNode ? nodeMapping[subject.nodeName] : tagMapping[subject.tagName];
|
|
425
|
+
if (!newName) {
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const newSubject = clone(subject);
|
|
430
|
+
newSubject[isNode ? 'nodeName' : 'tagName'] = newName;
|
|
431
|
+
const parameterBag = clone(this.parameters.get(subject)!);
|
|
432
|
+
merge(parameterBag, this.parameters.get(newSubject));
|
|
433
|
+
this.parameters.set(newSubject, parameterBag);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Clears the applied {@link ParameterBag} state of the node's metadata to ensure that observers trigger (once again).
|
|
439
|
+
* This is necessary e.g. after mapping tags when a nodeName subject shall override the parameterValue of a mapped
|
|
440
|
+
* tag.
|
|
441
|
+
* @protected
|
|
442
|
+
*/
|
|
443
|
+
protected clearAppliedNodeMetadataParameters(nodes: TransformNode[]): void {
|
|
444
|
+
for (const node of nodes) {
|
|
445
|
+
if (!node.metadata?.tagManager?.parameters) {
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
node.metadata.tagManager.parameters = {};
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import { Event } from '../classes/event';
|
|
2
|
-
import { EventBroadcaster } from '../classes/eventBroadcaster';
|
|
3
|
-
import { isTextureWithOnLoadObservable } from '../util/babylonHelper';
|
|
4
|
-
import { sleep } from '../util/resourceHelper';
|
|
5
|
-
import { BaseTexture } from '@babylonjs/core/Materials/Textures/baseTexture';
|
|
6
|
-
import { Scene as BabylonScene } from '@babylonjs/core/scene';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Keeps track of all texture loading/ready states and fires {@link Event.TEXTURE_LOADING_START} and
|
|
10
|
-
* {@link Event.TEXTURE_LOADING_END} events.
|
|
11
|
-
*/
|
|
12
|
-
export class TextureLoadManager extends EventBroadcaster {
|
|
13
|
-
// We're keeping track of all "loading textures" in this map ourselves instead of relying on information from
|
|
14
|
-
// `scene.textures` as that proved to be rather unreliable for our use cases.
|
|
15
|
-
// E.g. see this forums post for some more information:
|
|
16
|
-
// https://forum.babylonjs.com/t/using-scene-createdefaultenvironment-removes-textures-from-scene/35267
|
|
17
|
-
private readonly _loadingTextures: Map<number, BaseTexture> = new Map();
|
|
18
|
-
|
|
19
|
-
// Whenever we fire the {@link Event.TEXTURE_LOADING_START} event, we're starting a polling service which checks all
|
|
20
|
-
// x ms if all awaited textures are in "ready state" or not.
|
|
21
|
-
// This is basically just an additional safety net in addition to our `onLoadObservable`s and was added to prevent
|
|
22
|
-
// situations where a {@link Event.TEXTURE_LOADING_START} event is fired without an accompanying
|
|
23
|
-
// {@link Event.TEXTURE_LOADING_END} as that is a situation which can lead to load masks being endlessly shown in a
|
|
24
|
-
// cfgr.
|
|
25
|
-
// In theory this shouldn't be necessary, but we've already discovered some quirks and inconsistent behavior around
|
|
26
|
-
// the texture load observables etc. and I felt more save with this safety net in place.
|
|
27
|
-
private _readyStatePollingIntervalMs = 200;
|
|
28
|
-
private _readyStatePollingId = 0;
|
|
29
|
-
|
|
30
|
-
private constructor(public scene: BabylonScene) {
|
|
31
|
-
super();
|
|
32
|
-
this.scene.onNewTextureAddedObservable.add(texture => this.onTextureAdded(texture));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public static create(scene: BabylonScene): TextureLoadManager {
|
|
36
|
-
const loadMgr = new TextureLoadManager(scene);
|
|
37
|
-
return loadMgr;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private startReadyStatePollingIfNeeded() {
|
|
41
|
-
if (!this._readyStatePollingId) {
|
|
42
|
-
this._readyStatePollingId = window.setInterval(
|
|
43
|
-
() => this.checkLoadingTexturesState(),
|
|
44
|
-
this._readyStatePollingIntervalMs
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private endReadyStatePollingIfNeeded() {
|
|
50
|
-
if (this._readyStatePollingId) {
|
|
51
|
-
window.clearInterval(this._readyStatePollingId);
|
|
52
|
-
this._readyStatePollingId = 0;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Checks the "ready state" of all textures inside the `this._loadingTextures` map, removes the ready ones and fires
|
|
58
|
-
* the {@link Event.TEXTURE_LOADING_END} event if no more textures are loading (i.e. "all ready").
|
|
59
|
-
*/
|
|
60
|
-
private checkLoadingTexturesState() {
|
|
61
|
-
const allTexturesReadyBefore = !this._loadingTextures.size;
|
|
62
|
-
if (allTexturesReadyBefore) return;
|
|
63
|
-
|
|
64
|
-
// Remove textures which are in "ready state" from the map
|
|
65
|
-
for (const [id, texture] of this._loadingTextures) {
|
|
66
|
-
if (texture.isReady()) this._loadingTextures.delete(id);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const allTexturesReadyAfter = !this._loadingTextures.size;
|
|
70
|
-
if (allTexturesReadyAfter) {
|
|
71
|
-
this.broadcastEvent(Event.TEXTURE_LOADING_END);
|
|
72
|
-
this.endReadyStatePollingIfNeeded();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
private async onTextureAdded(texture: BaseTexture) {
|
|
77
|
-
// Workaround:
|
|
78
|
-
// The texture object is not fully created when the "added observable" is called. E.g. the function
|
|
79
|
-
// `onLoadObservable` is not there at that point.
|
|
80
|
-
// More details: https://forum.babylonjs.com/t/basetexture-whenallready-returns-too-early/34501/4
|
|
81
|
-
await sleep(0);
|
|
82
|
-
|
|
83
|
-
// Some textures are already in "ready state" after the sleep -> We don't care for them anymore as well as for
|
|
84
|
-
// textures which don't come with an `onLoadObservable` (i.e. not of class {@link Texture}).
|
|
85
|
-
if (texture.isReady() || !isTextureWithOnLoadObservable(texture)) return;
|
|
86
|
-
|
|
87
|
-
const noCurrentLoadingTextures = !this._loadingTextures.size;
|
|
88
|
-
if (noCurrentLoadingTextures) {
|
|
89
|
-
this.broadcastEvent(Event.TEXTURE_LOADING_START);
|
|
90
|
-
this.startReadyStatePollingIfNeeded();
|
|
91
|
-
}
|
|
92
|
-
this._loadingTextures.set(texture.uniqueId, texture);
|
|
93
|
-
texture.onLoadObservable.addOnce(texture => this.checkLoadingTexturesState());
|
|
94
|
-
}
|
|
95
|
-
}
|
|
1
|
+
import { Event } from '../classes/event';
|
|
2
|
+
import { EventBroadcaster } from '../classes/eventBroadcaster';
|
|
3
|
+
import { isTextureWithOnLoadObservable } from '../util/babylonHelper';
|
|
4
|
+
import { sleep } from '../util/resourceHelper';
|
|
5
|
+
import { BaseTexture } from '@babylonjs/core/Materials/Textures/baseTexture';
|
|
6
|
+
import { Scene as BabylonScene } from '@babylonjs/core/scene';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Keeps track of all texture loading/ready states and fires {@link Event.TEXTURE_LOADING_START} and
|
|
10
|
+
* {@link Event.TEXTURE_LOADING_END} events.
|
|
11
|
+
*/
|
|
12
|
+
export class TextureLoadManager extends EventBroadcaster {
|
|
13
|
+
// We're keeping track of all "loading textures" in this map ourselves instead of relying on information from
|
|
14
|
+
// `scene.textures` as that proved to be rather unreliable for our use cases.
|
|
15
|
+
// E.g. see this forums post for some more information:
|
|
16
|
+
// https://forum.babylonjs.com/t/using-scene-createdefaultenvironment-removes-textures-from-scene/35267
|
|
17
|
+
private readonly _loadingTextures: Map<number, BaseTexture> = new Map();
|
|
18
|
+
|
|
19
|
+
// Whenever we fire the {@link Event.TEXTURE_LOADING_START} event, we're starting a polling service which checks all
|
|
20
|
+
// x ms if all awaited textures are in "ready state" or not.
|
|
21
|
+
// This is basically just an additional safety net in addition to our `onLoadObservable`s and was added to prevent
|
|
22
|
+
// situations where a {@link Event.TEXTURE_LOADING_START} event is fired without an accompanying
|
|
23
|
+
// {@link Event.TEXTURE_LOADING_END} as that is a situation which can lead to load masks being endlessly shown in a
|
|
24
|
+
// cfgr.
|
|
25
|
+
// In theory this shouldn't be necessary, but we've already discovered some quirks and inconsistent behavior around
|
|
26
|
+
// the texture load observables etc. and I felt more save with this safety net in place.
|
|
27
|
+
private _readyStatePollingIntervalMs = 200;
|
|
28
|
+
private _readyStatePollingId = 0;
|
|
29
|
+
|
|
30
|
+
private constructor(public scene: BabylonScene) {
|
|
31
|
+
super();
|
|
32
|
+
this.scene.onNewTextureAddedObservable.add(texture => this.onTextureAdded(texture));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public static create(scene: BabylonScene): TextureLoadManager {
|
|
36
|
+
const loadMgr = new TextureLoadManager(scene);
|
|
37
|
+
return loadMgr;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private startReadyStatePollingIfNeeded() {
|
|
41
|
+
if (!this._readyStatePollingId) {
|
|
42
|
+
this._readyStatePollingId = window.setInterval(
|
|
43
|
+
() => this.checkLoadingTexturesState(),
|
|
44
|
+
this._readyStatePollingIntervalMs
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private endReadyStatePollingIfNeeded() {
|
|
50
|
+
if (this._readyStatePollingId) {
|
|
51
|
+
window.clearInterval(this._readyStatePollingId);
|
|
52
|
+
this._readyStatePollingId = 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Checks the "ready state" of all textures inside the `this._loadingTextures` map, removes the ready ones and fires
|
|
58
|
+
* the {@link Event.TEXTURE_LOADING_END} event if no more textures are loading (i.e. "all ready").
|
|
59
|
+
*/
|
|
60
|
+
private checkLoadingTexturesState() {
|
|
61
|
+
const allTexturesReadyBefore = !this._loadingTextures.size;
|
|
62
|
+
if (allTexturesReadyBefore) return;
|
|
63
|
+
|
|
64
|
+
// Remove textures which are in "ready state" from the map
|
|
65
|
+
for (const [id, texture] of this._loadingTextures) {
|
|
66
|
+
if (texture.isReady()) this._loadingTextures.delete(id);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const allTexturesReadyAfter = !this._loadingTextures.size;
|
|
70
|
+
if (allTexturesReadyAfter) {
|
|
71
|
+
this.broadcastEvent(Event.TEXTURE_LOADING_END);
|
|
72
|
+
this.endReadyStatePollingIfNeeded();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async onTextureAdded(texture: BaseTexture) {
|
|
77
|
+
// Workaround:
|
|
78
|
+
// The texture object is not fully created when the "added observable" is called. E.g. the function
|
|
79
|
+
// `onLoadObservable` is not there at that point.
|
|
80
|
+
// More details: https://forum.babylonjs.com/t/basetexture-whenallready-returns-too-early/34501/4
|
|
81
|
+
await sleep(0);
|
|
82
|
+
|
|
83
|
+
// Some textures are already in "ready state" after the sleep -> We don't care for them anymore as well as for
|
|
84
|
+
// textures which don't come with an `onLoadObservable` (i.e. not of class {@link Texture}).
|
|
85
|
+
if (texture.isReady() || !isTextureWithOnLoadObservable(texture)) return;
|
|
86
|
+
|
|
87
|
+
const noCurrentLoadingTextures = !this._loadingTextures.size;
|
|
88
|
+
if (noCurrentLoadingTextures) {
|
|
89
|
+
this.broadcastEvent(Event.TEXTURE_LOADING_START);
|
|
90
|
+
this.startReadyStatePollingIfNeeded();
|
|
91
|
+
}
|
|
92
|
+
this._loadingTextures.set(texture.uniqueId, texture);
|
|
93
|
+
texture.onLoadObservable.addOnce(texture => this.checkLoadingTexturesState());
|
|
94
|
+
}
|
|
95
|
+
}
|