@combeenation/3d-viewer 6.1.0 → 6.2.0
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 +149 -149
- package/dist/lib-cjs/api/classes/element.js +669 -669
- package/dist/lib-cjs/api/classes/event.d.ts +342 -342
- package/dist/lib-cjs/api/classes/event.js +365 -365
- 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/parameter.d.ts +339 -339
- package/dist/lib-cjs/api/classes/parameter.js +464 -464
- package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
- package/dist/lib-cjs/api/classes/parameterObservable.js +97 -97
- 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 +238 -234
- package/dist/lib-cjs/api/classes/variant.js +841 -828
- package/dist/lib-cjs/api/classes/variant.js.map +1 -1
- package/dist/lib-cjs/api/classes/variantInstance.d.ts +44 -44
- package/dist/lib-cjs/api/classes/variantInstance.js +105 -105
- 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 +192 -187
- package/dist/lib-cjs/api/classes/viewer.js +639 -594
- 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/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/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/textureLoadManager.d.ts +22 -22
- package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
- package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
- package/dist/lib-cjs/api/manager/variantInstanceManager.js +260 -260
- package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
- package/dist/lib-cjs/api/store/specStorage.js +50 -50
- package/dist/lib-cjs/api/util/babylonHelper.d.ts +187 -187
- package/dist/lib-cjs/api/util/babylonHelper.js +596 -596
- package/dist/lib-cjs/api/util/globalTypes.d.ts +387 -383
- 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 +42 -42
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js +139 -139
- package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/stringHelper.js +25 -25
- 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 +51 -51
- package/dist/lib-cjs/index.js +110 -110
- 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 +717 -717
- package/src/api/classes/event.ts +385 -385
- package/src/api/classes/eventBroadcaster.ts +52 -52
- package/src/api/classes/parameter.ts +497 -497
- package/src/api/classes/parameterObservable.ts +100 -100
- package/src/api/classes/parameterizable.ts +87 -87
- package/src/api/classes/placementAnimation.ts +162 -162
- package/src/api/classes/variant.ts +910 -896
- package/src/api/classes/variantInstance.ts +97 -97
- package/src/api/classes/variantParameterizable.ts +85 -85
- package/src/api/classes/viewer.ts +720 -672
- 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 +132 -132
- package/src/api/manager/textureLoadManager.ts +95 -95
- package/src/api/manager/variantInstanceManager.ts +265 -265
- package/src/api/store/specStorage.ts +51 -51
- package/src/api/util/babylonHelper.ts +663 -663
- package/src/api/util/globalTypes.ts +437 -432
- package/src/api/util/resourceHelper.ts +191 -191
- package/src/api/util/sceneLoaderHelper.ts +137 -137
- package/src/api/util/stringHelper.ts +23 -23
- package/src/api/util/structureHelper.ts +49 -49
- package/src/buildinfo.json +3 -3
- package/src/dev.ts +61 -61
- package/src/index.ts +96 -96
- package/src/types.d.ts +28 -28
|
@@ -1,717 +1,717 @@
|
|
|
1
|
-
import {
|
|
2
|
-
activateTransformNode as activate,
|
|
3
|
-
addToHighlightLayer,
|
|
4
|
-
addToShadowGenerator,
|
|
5
|
-
assertTransformNode,
|
|
6
|
-
cloneTransformNode,
|
|
7
|
-
cloneTransformNodeMaterial,
|
|
8
|
-
deactivateTransformNode as deactivate,
|
|
9
|
-
getClientRectFromMesh,
|
|
10
|
-
injectNodeMetadata,
|
|
11
|
-
mapToDottedNodes,
|
|
12
|
-
removeFromHighlightLayer,
|
|
13
|
-
removeFromShadowGenerator,
|
|
14
|
-
setMaterial,
|
|
15
|
-
setMaterialColor,
|
|
16
|
-
setMaterialMetallness,
|
|
17
|
-
setMaterialRoughness,
|
|
18
|
-
setMaterialTexture,
|
|
19
|
-
setReceiveShadows,
|
|
20
|
-
transformTransformNode,
|
|
21
|
-
} from '../util/babylonHelper';
|
|
22
|
-
import { createImageFromImgSrc, createImageFromSvg } from '../util/resourceHelper';
|
|
23
|
-
import { addMissingMaterialObserver } from '../util/sceneLoaderHelper';
|
|
24
|
-
import { VariantParameterizable } from './../classes/variantParameterizable';
|
|
25
|
-
import { DottedPath } from './dottedPath';
|
|
26
|
-
import { Parameter } from './parameter';
|
|
27
|
-
import { Variant } from './variant';
|
|
28
|
-
import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
|
|
29
|
-
import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
|
|
30
|
-
import { Light } from '@babylonjs/core/Lights/light';
|
|
31
|
-
import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
|
|
32
|
-
import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
|
|
33
|
-
import { Material } from '@babylonjs/core/Materials/material';
|
|
34
|
-
import { Color3 } from '@babylonjs/core/Maths/math.color';
|
|
35
|
-
import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
|
|
36
|
-
import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
|
|
37
|
-
import { Mesh } from '@babylonjs/core/Meshes/mesh';
|
|
38
|
-
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
|
|
39
|
-
import { has, isArray, isNumber, isPlainObject, merge, union } from 'lodash-es';
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
|
|
43
|
-
* {@link Variant}.
|
|
44
|
-
*
|
|
45
|
-
* When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
|
|
46
|
-
* [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
|
|
47
|
-
*/
|
|
48
|
-
export class Element extends VariantParameterizable {
|
|
49
|
-
public readonly nodes: TransformNode[] = [];
|
|
50
|
-
|
|
51
|
-
protected readonly _dottedNodes: Map<DottedPath, TransformNode> = new Map();
|
|
52
|
-
|
|
53
|
-
protected _highlightLayer?: HighlightLayer;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Constructor.
|
|
57
|
-
*/
|
|
58
|
-
protected constructor(public readonly variant: Variant, public readonly name: string) {
|
|
59
|
-
super(variant, name);
|
|
60
|
-
if (process.env.NODE_ENV?.toLowerCase().includes('dev')) {
|
|
61
|
-
this.assertPathDefinitions();
|
|
62
|
-
}
|
|
63
|
-
const nodes = this.variant.inheritedNodes.map(node => cloneTransformNode(node, this.filterNode.bind(this)));
|
|
64
|
-
this.nodes = nodes.filter(node => !!node) as TransformNode[];
|
|
65
|
-
this._dottedNodes = mapToDottedNodes<TransformNode>(this.nodes, node => node instanceof TransformNode);
|
|
66
|
-
this.addMissingMaterialObservers();
|
|
67
|
-
this.addParameterObservers();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Creates an {@link Element} with given name.
|
|
72
|
-
*/
|
|
73
|
-
public static async create(variant: Variant, name: string): Promise<Element> {
|
|
74
|
-
return new Element(variant, name);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The {@link DottedPath} in the built tree of {@link Element}s.
|
|
79
|
-
* E.g. "_.top-1.sub-2.sub-sub-3.el-1"
|
|
80
|
-
*/
|
|
81
|
-
get dottedPath(): DottedPath {
|
|
82
|
-
return DottedPath.create(this.variant.dottedPath).addPart(this.name);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The id representing a {@link DottedPath}.
|
|
87
|
-
*/
|
|
88
|
-
get id(): string {
|
|
89
|
-
const dottedPath = DottedPath.create(this.dottedPath);
|
|
90
|
-
dottedPath.shiftPart(); // remove root
|
|
91
|
-
return dottedPath.path;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* The {@link ElementDefinition} of the {@link Element}.
|
|
96
|
-
*/
|
|
97
|
-
get definition(): ElementDefinition {
|
|
98
|
-
const definition = this.variant.structureJson.elements![this.name];
|
|
99
|
-
if (isArray(definition)) {
|
|
100
|
-
return {
|
|
101
|
-
paths: {
|
|
102
|
-
include: definition,
|
|
103
|
-
},
|
|
104
|
-
} as ElementDefinition;
|
|
105
|
-
}
|
|
106
|
-
return definition as ElementDefinition;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* The {@link PathDefinitions} of the {@link Element}.
|
|
111
|
-
*/
|
|
112
|
-
get pathDefinitions(): PathDefinitions {
|
|
113
|
-
return this.definition.paths;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* The {@link TraceableDefinitions} of the {@link Element}.
|
|
118
|
-
*/
|
|
119
|
-
get traceableDefinitions(): TraceableDefinitions {
|
|
120
|
-
return this.definition.traceables || {};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* The {@link PaintableDefinition} of the {@link Element}.
|
|
125
|
-
*/
|
|
126
|
-
get paintableDefinitions(): PaintableDefinitions {
|
|
127
|
-
return this.definition.paintables || {};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* The inherited {@link ParameterBag}.
|
|
132
|
-
* Merges the {@link Variant}'s parameters and those from the {@link Element}.
|
|
133
|
-
*/
|
|
134
|
-
get inheritedParameters(): ParameterBag {
|
|
135
|
-
return merge({}, this.variant.inheritedParameters, this.parameters);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* The actual {@link Mesh}es that make up this {@link Element}.
|
|
140
|
-
* Handy for e.g. creating a bounding box around an entire element.
|
|
141
|
-
*/
|
|
142
|
-
get meshesFlat(): Mesh[] {
|
|
143
|
-
const flatMeshes = this.nodes.reduce((accFlatMeshes, curNode) => {
|
|
144
|
-
const currMeshes = curNode.getChildMeshes(false, n => n instanceof Mesh) as Mesh[];
|
|
145
|
-
return [...accFlatMeshes, ...currMeshes];
|
|
146
|
-
}, [] as Mesh[]);
|
|
147
|
-
return flatMeshes;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* The actual {@link AbstractMesh}es that make up this {@link Element}.
|
|
152
|
-
* I.e. (real) Meshes as well as InstancedMeshes.
|
|
153
|
-
*/
|
|
154
|
-
get abstractMeshesFlat(): AbstractMesh[] {
|
|
155
|
-
const allAbstractMeshes = this.nodes.reduce((acc, currNode) => {
|
|
156
|
-
const currAbstractMeshes = currNode.getChildMeshes(false, n => n instanceof AbstractMesh);
|
|
157
|
-
if (currNode instanceof AbstractMesh) {
|
|
158
|
-
currAbstractMeshes.unshift(currNode);
|
|
159
|
-
}
|
|
160
|
-
return [...acc, ...currAbstractMeshes];
|
|
161
|
-
}, [] as AbstractMesh[]); // initial accumulator value
|
|
162
|
-
return allAbstractMeshes;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* The meshes of this {@link Element}.
|
|
167
|
-
*/
|
|
168
|
-
get meshes(): Mesh[] {
|
|
169
|
-
return this.nodes.filter(node => node instanceof Mesh) as Mesh[];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* @protected
|
|
174
|
-
*/
|
|
175
|
-
set highlightLayer(layer: HighlightLayer | undefined) {
|
|
176
|
-
this._highlightLayer = layer;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
get highlightLayer(): HighlightLayer | undefined {
|
|
180
|
-
return this._highlightLayer;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Destroys this {@link Element} and dispose all nodes.
|
|
185
|
-
*/
|
|
186
|
-
public destroy(): Element {
|
|
187
|
-
this.nodes.forEach(node => {
|
|
188
|
-
node.dispose();
|
|
189
|
-
});
|
|
190
|
-
return this;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Gets a node by its {@link DottedPath}.
|
|
195
|
-
*/
|
|
196
|
-
public getNode(dottedPath: DottedPathArgument): TransformNode {
|
|
197
|
-
const _dottedPath = DottedPath.create(dottedPath);
|
|
198
|
-
const keys = Array.from(this._dottedNodes.keys()).map(dp => dp.path);
|
|
199
|
-
const values = Array.from(this._dottedNodes.values());
|
|
200
|
-
const node = values[keys.indexOf(_dottedPath.path)];
|
|
201
|
-
if (!node) {
|
|
202
|
-
throw new Error(`Node with path "${_dottedPath.path}" does not exist for element "${this.id}".`);
|
|
203
|
-
}
|
|
204
|
-
return node;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Gets a mesh by its {@link DottedPath}.
|
|
209
|
-
*/
|
|
210
|
-
public async getMesh(dottedPath: DottedPathArgument): Promise<Mesh | null> {
|
|
211
|
-
const node = await this.getNode(dottedPath);
|
|
212
|
-
if (node instanceof Mesh) {
|
|
213
|
-
return node;
|
|
214
|
-
}
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Gets a {@link PaintableDefinition} by its name.
|
|
220
|
-
*/
|
|
221
|
-
public getPaintableDefinition(name: string): PaintableDefinition {
|
|
222
|
-
if (!this.paintableDefinitions || !this.paintableDefinitions[name]) {
|
|
223
|
-
throw new Error(`No paintable "${name}" defined for element "${this.id}".`);
|
|
224
|
-
}
|
|
225
|
-
return this.paintableDefinitions[name];
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Gets a {@link TraceableDefinition} by its name.
|
|
230
|
-
*/
|
|
231
|
-
public getTraceableDefinition(name: string): TraceableDefinition {
|
|
232
|
-
if (!this.traceableDefinitions || !this.traceableDefinitions[name]) {
|
|
233
|
-
throw new Error(`No traceable "${name}" defined to add observer in element "${this.id}".`);
|
|
234
|
-
}
|
|
235
|
-
return this.traceableDefinitions[name];
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Gets the paintable Node by its name.
|
|
240
|
-
*/
|
|
241
|
-
public getPaintableNode(name: string): TransformNode {
|
|
242
|
-
return this.getNode(this.getPaintableDefinition(name).path);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Gets the traceable Node by its name.
|
|
247
|
-
*/
|
|
248
|
-
public getTraceableNode(name: string): TransformNode {
|
|
249
|
-
return this.getNode(this.getTraceableDefinition(name).path);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* @see {@link VariantParameterizable.commitParameters}
|
|
254
|
-
* @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
|
|
255
|
-
*/
|
|
256
|
-
public async commitParameters(parameters?: ParameterBag): Promise<VariantParameterizable> {
|
|
257
|
-
return super.commitParameters(parameters);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Adds an observer function for camera matrix changes.
|
|
262
|
-
* The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
|
|
263
|
-
*/
|
|
264
|
-
public addTraceableObserver(traceable: string, observer: CallableFunction, payload?: any): Element {
|
|
265
|
-
if (payload && !(payload instanceof HTMLElement)) {
|
|
266
|
-
throw new Error(`Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".`);
|
|
267
|
-
}
|
|
268
|
-
if (!this.traceableDefinitions || !this.traceableDefinitions[traceable]) {
|
|
269
|
-
throw new Error(`No traceable "${traceable}" defined to add observer in element "${this.id}".`);
|
|
270
|
-
}
|
|
271
|
-
const node = this.getTraceableNode(traceable);
|
|
272
|
-
if (!(node instanceof AbstractMesh)) {
|
|
273
|
-
throw new Error(`The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".`);
|
|
274
|
-
}
|
|
275
|
-
this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add((eventData, eventState) => {
|
|
276
|
-
const position = getClientRectFromMesh(node, this.variant.viewer.scene, this.variant.viewer.canvas);
|
|
277
|
-
if (payload) {
|
|
278
|
-
payload.style.top = `${position.top}px`;
|
|
279
|
-
payload.style.left = `${position.left}px`;
|
|
280
|
-
}
|
|
281
|
-
observer(node, position);
|
|
282
|
-
});
|
|
283
|
-
return this;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
|
|
288
|
-
*/
|
|
289
|
-
public drawPaintable(paintable: string, imageSource: CanvasImageSource): Element {
|
|
290
|
-
// node and material checks and preperation
|
|
291
|
-
const node = this.getPaintableNode(paintable);
|
|
292
|
-
if (!(node instanceof AbstractMesh)) {
|
|
293
|
-
throw new Error(
|
|
294
|
-
`The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` + `in element "${this.id}".`
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (node.material && this.variant.viewer.cloneMaterialsOnMutation) {
|
|
299
|
-
if (node.material && !has(node.metadata, 'dirty.material')) {
|
|
300
|
-
cloneTransformNodeMaterial(node);
|
|
301
|
-
}
|
|
302
|
-
if (!has(node.metadata, 'dirty.material.texture')) {
|
|
303
|
-
// inject initial value and mark as dirty
|
|
304
|
-
injectNodeMetadata(node, { dirty: { material: { texture: true } } });
|
|
305
|
-
}
|
|
306
|
-
} else if (!node.material) {
|
|
307
|
-
// paintables should also work if no material is assigned to the node per default
|
|
308
|
-
// in this case a certain fallback material with fitting default values is defined
|
|
309
|
-
const pbrMat = new PBRMaterial(`${this.id}.${paintable}.material`, this.variant.viewer.scene);
|
|
310
|
-
pbrMat.roughness = 1.0;
|
|
311
|
-
pbrMat.metallic = 1.0;
|
|
312
|
-
node.material = pbrMat;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// TODO: Not sure if this really should be overwritten every time the paintable is redrawn.
|
|
316
|
-
// Maybe the 3D artist had something in mind when setting a certain transparency mode, which would get overwritten
|
|
317
|
-
// in this case.
|
|
318
|
-
// Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
|
|
319
|
-
// Removing this line will most likely break existing configurators, so be cautios!
|
|
320
|
-
node.material.transparencyMode = Material.MATERIAL_ALPHATESTANDBLEND;
|
|
321
|
-
|
|
322
|
-
// consider width and height of the paintable
|
|
323
|
-
const widthAndHeight = {
|
|
324
|
-
width: imageSource.width,
|
|
325
|
-
height: imageSource.height,
|
|
326
|
-
};
|
|
327
|
-
const paintableDefinition = this.getPaintableDefinition(paintable);
|
|
328
|
-
if (isNumber(paintableDefinition.textureOptions)) {
|
|
329
|
-
widthAndHeight.width = paintableDefinition.textureOptions;
|
|
330
|
-
widthAndHeight.height = paintableDefinition.textureOptions;
|
|
331
|
-
} else if (isPlainObject(paintableDefinition.textureOptions)) {
|
|
332
|
-
widthAndHeight.width = paintableDefinition.textureOptions.width;
|
|
333
|
-
widthAndHeight.height = paintableDefinition.textureOptions.height;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// create dynamic texture on which the image can be drawn
|
|
337
|
-
const texture = new DynamicTexture(
|
|
338
|
-
`${this.id}.${paintable}.texture`,
|
|
339
|
-
widthAndHeight,
|
|
340
|
-
this.variant.viewer.scene,
|
|
341
|
-
false
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
// draw image on texture
|
|
345
|
-
const ctx = texture.getContext();
|
|
346
|
-
ctx.drawImage(imageSource, 0, 0);
|
|
347
|
-
texture.update(false);
|
|
348
|
-
|
|
349
|
-
// finally apply the texture on the desired node material
|
|
350
|
-
setMaterialTexture(node, texture, false);
|
|
351
|
-
|
|
352
|
-
return this;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
|
|
357
|
-
*/
|
|
358
|
-
public async drawPaintableFromSvg(paintable: string, svgSource: string): Promise<Element> {
|
|
359
|
-
const image = await createImageFromSvg(svgSource);
|
|
360
|
-
return this.drawPaintable(paintable, image);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
|
|
365
|
-
*/
|
|
366
|
-
public async drawPaintableFromImgSrc(paintable: string, imgSource: string): Promise<Element> {
|
|
367
|
-
const image = await createImageFromImgSrc(imgSource);
|
|
368
|
-
return this.drawPaintable(paintable, image);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Adds observers for loading missing materials where needed
|
|
373
|
-
*/
|
|
374
|
-
protected addMissingMaterialObservers() {
|
|
375
|
-
for (const abstractMesh of this.abstractMeshesFlat) {
|
|
376
|
-
addMissingMaterialObserver(abstractMesh);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
|
|
382
|
-
*/
|
|
383
|
-
protected addParameterObservers(): Element {
|
|
384
|
-
this._parameterObservers.set(Parameter.VISIBLE, [
|
|
385
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
386
|
-
let visible;
|
|
387
|
-
try {
|
|
388
|
-
visible = Parameter.parseBoolean(newValue);
|
|
389
|
-
} catch (e) {
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
if (visible === true) {
|
|
393
|
-
element.nodes.forEach(node => {
|
|
394
|
-
injectNodeMetadata(node, { visibility: node.isEnabled() });
|
|
395
|
-
activate(node);
|
|
396
|
-
});
|
|
397
|
-
} else if (visible === false) {
|
|
398
|
-
element.nodes.forEach(node => {
|
|
399
|
-
injectNodeMetadata(node, { visibility: node.isEnabled() });
|
|
400
|
-
deactivate(node);
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
},
|
|
404
|
-
]);
|
|
405
|
-
this._parameterObservers.set(Parameter.SCALING, [
|
|
406
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
407
|
-
// we have to deal just with root nodes here due to relative impacts in a node tree
|
|
408
|
-
element.nodes.forEach(node =>
|
|
409
|
-
transformTransformNode(node, {
|
|
410
|
-
scaling: Parameter.parseVector(newValue),
|
|
411
|
-
position: Parameter.parseVector(element.inheritedParameters[Parameter.POSITION] || '(0, 0, 0)'),
|
|
412
|
-
rotation: Parameter.parseRotation(element.inheritedParameters[Parameter.ROTATION] || '(0, 0, 0)'),
|
|
413
|
-
})
|
|
414
|
-
);
|
|
415
|
-
},
|
|
416
|
-
]);
|
|
417
|
-
this._parameterObservers.set(Parameter.POSITION, [
|
|
418
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
419
|
-
// we have to deal just with root nodes here due to relative impacts in a node tree
|
|
420
|
-
element.nodes.forEach(node =>
|
|
421
|
-
transformTransformNode(node, {
|
|
422
|
-
scaling: Parameter.parseVector(element.inheritedParameters[Parameter.SCALING] || '(1, 1, 1)'),
|
|
423
|
-
position: Parameter.parseVector(newValue),
|
|
424
|
-
rotation: Parameter.parseRotation(element.inheritedParameters[Parameter.ROTATION] || '(0, 0, 0)'),
|
|
425
|
-
})
|
|
426
|
-
);
|
|
427
|
-
},
|
|
428
|
-
]);
|
|
429
|
-
this._parameterObservers.set(Parameter.ROTATION, [
|
|
430
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
431
|
-
// we have to deal just with root nodes here due to relative impacts in a node tree
|
|
432
|
-
element.nodes.forEach(node =>
|
|
433
|
-
transformTransformNode(node, {
|
|
434
|
-
scaling: Parameter.parseVector(element.inheritedParameters[Parameter.SCALING] || '(1, 1, 1)'),
|
|
435
|
-
position: Parameter.parseVector(element.inheritedParameters[Parameter.POSITION] || '(0, 0, 0)'),
|
|
436
|
-
rotation: Parameter.parseRotation(newValue),
|
|
437
|
-
})
|
|
438
|
-
);
|
|
439
|
-
},
|
|
440
|
-
]);
|
|
441
|
-
this._parameterObservers.set(Parameter.MATERIAL, [
|
|
442
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
443
|
-
const materialName = newValue.toString();
|
|
444
|
-
element.nodes.forEach(node => {
|
|
445
|
-
assertTransformNode(node, (node: AbstractMesh) => {
|
|
446
|
-
if (node instanceof InstancedMesh) {
|
|
447
|
-
throw new Error(
|
|
448
|
-
`Changing parameter "${Parameter.MATERIAL}" ` +
|
|
449
|
-
`of an InstancedMesh is not supported. ` +
|
|
450
|
-
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
451
|
-
);
|
|
452
|
-
}
|
|
453
|
-
});
|
|
454
|
-
setMaterial(element.variant, node, materialName);
|
|
455
|
-
});
|
|
456
|
-
},
|
|
457
|
-
]);
|
|
458
|
-
this._parameterObservers.set(Parameter.MATERIAL_COLOR, [
|
|
459
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
460
|
-
const color = Parameter.parseColor(newValue);
|
|
461
|
-
element.nodes.forEach(node => {
|
|
462
|
-
assertTransformNode(node, (node: AbstractMesh) => {
|
|
463
|
-
if (node instanceof InstancedMesh) {
|
|
464
|
-
throw new Error(
|
|
465
|
-
`Changing parameter "${Parameter.MATERIAL_COLOR}" ` +
|
|
466
|
-
`of an InstancedMesh is not supported. ` +
|
|
467
|
-
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
if (this.variant.viewer.cloneMaterialsOnMutation) {
|
|
472
|
-
if (!has(node.metadata, 'dirty.material')) {
|
|
473
|
-
cloneTransformNodeMaterial(node);
|
|
474
|
-
}
|
|
475
|
-
if (!has(node.metadata, 'dirty.material.color')) {
|
|
476
|
-
// inject initial value and mark as dirty
|
|
477
|
-
injectNodeMetadata(node, { dirty: { material: { color: oldValue } } });
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
setMaterialColor(node, color);
|
|
481
|
-
});
|
|
482
|
-
},
|
|
483
|
-
]);
|
|
484
|
-
this._parameterObservers.set(Parameter.MATERIAL_METALLNESS, [
|
|
485
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
486
|
-
const metallness = Parameter.parseNumber(newValue);
|
|
487
|
-
element.nodes.forEach(node => {
|
|
488
|
-
assertTransformNode(node, (node: AbstractMesh) => {
|
|
489
|
-
if (node instanceof InstancedMesh) {
|
|
490
|
-
throw new Error(
|
|
491
|
-
`Changing parameter "${Parameter.MATERIAL_METALLNESS}" ` +
|
|
492
|
-
`of an InstancedMesh is not supported. ` +
|
|
493
|
-
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
494
|
-
);
|
|
495
|
-
}
|
|
496
|
-
});
|
|
497
|
-
if (this.variant.viewer.cloneMaterialsOnMutation) {
|
|
498
|
-
if (!has(node.metadata, 'dirty.material')) {
|
|
499
|
-
cloneTransformNodeMaterial(node);
|
|
500
|
-
}
|
|
501
|
-
if (!has(node.metadata, 'dirty.material.metallness')) {
|
|
502
|
-
// inject initial value and mark as dirty
|
|
503
|
-
injectNodeMetadata(node, { dirty: { material: { metallness: oldValue } } });
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
setMaterialMetallness(node, metallness);
|
|
507
|
-
});
|
|
508
|
-
},
|
|
509
|
-
]);
|
|
510
|
-
this._parameterObservers.set(Parameter.MATERIAL_ROUGHNESS, [
|
|
511
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
512
|
-
const roughness = Parameter.parseNumber(newValue);
|
|
513
|
-
element.nodes.forEach(node => {
|
|
514
|
-
assertTransformNode(node, (node: AbstractMesh) => {
|
|
515
|
-
if (node instanceof InstancedMesh) {
|
|
516
|
-
throw new Error(
|
|
517
|
-
`Changing parameter "${Parameter.MATERIAL_ROUGHNESS}" ` +
|
|
518
|
-
`of an InstancedMesh is not supported. ` +
|
|
519
|
-
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
520
|
-
);
|
|
521
|
-
}
|
|
522
|
-
});
|
|
523
|
-
if (this.variant.viewer.cloneMaterialsOnMutation) {
|
|
524
|
-
if (!has(node.metadata, 'dirty.material')) {
|
|
525
|
-
cloneTransformNodeMaterial(node);
|
|
526
|
-
}
|
|
527
|
-
if (!has(node.metadata, 'dirty.material.roughness')) {
|
|
528
|
-
// inject initial value and mark as dirty
|
|
529
|
-
injectNodeMetadata(node, { dirty: { material: { roughness: oldValue } } });
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
setMaterialRoughness(node, roughness);
|
|
533
|
-
});
|
|
534
|
-
},
|
|
535
|
-
]);
|
|
536
|
-
this._parameterObservers.set(Parameter.HIGHLIGHT_COLOR, [
|
|
537
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
538
|
-
// trigger Parameter.HIGHLIGHTED observers
|
|
539
|
-
this._parameterObservers.get(Parameter.HIGHLIGHTED)?.forEach(observer => {
|
|
540
|
-
observer(element, !!element.highlighted, !!element.highlighted);
|
|
541
|
-
});
|
|
542
|
-
},
|
|
543
|
-
]);
|
|
544
|
-
this._parameterObservers.set(Parameter.HIGHLIGHT_ENABLED, [
|
|
545
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
546
|
-
let curr;
|
|
547
|
-
try {
|
|
548
|
-
curr = Parameter.parseBoolean(newValue);
|
|
549
|
-
} catch (e) {
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
switch (curr) {
|
|
553
|
-
case true:
|
|
554
|
-
if (element._highlightLayer) {
|
|
555
|
-
break;
|
|
556
|
-
}
|
|
557
|
-
element.highlightLayer = new HighlightLayer(
|
|
558
|
-
DottedPath.create(element.id).addPart('highlight').path,
|
|
559
|
-
element.variant.viewer.scene
|
|
560
|
-
);
|
|
561
|
-
break;
|
|
562
|
-
case false:
|
|
563
|
-
element.highlightLayer?.dispose();
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
},
|
|
567
|
-
]);
|
|
568
|
-
this._parameterObservers.set(Parameter.HIGHLIGHTED, [
|
|
569
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
570
|
-
if (!element.inheritedParameters[Parameter.HIGHLIGHT_ENABLED]) {
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
// trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
|
|
574
|
-
this._parameterObservers.get(Parameter.HIGHLIGHT_ENABLED)?.forEach(observer => {
|
|
575
|
-
observer(element, this.parameters[Parameter.HIGHLIGHT_ENABLED], true);
|
|
576
|
-
});
|
|
577
|
-
let highlighted;
|
|
578
|
-
try {
|
|
579
|
-
highlighted = Parameter.parseBoolean(newValue);
|
|
580
|
-
} catch (e) {
|
|
581
|
-
return;
|
|
582
|
-
}
|
|
583
|
-
// Add/Remove meshes to previously created highlight layers.
|
|
584
|
-
if (highlighted === true) {
|
|
585
|
-
element.nodes.forEach(node => {
|
|
586
|
-
assertTransformNode(node, (node: AbstractMesh) => {
|
|
587
|
-
if (node instanceof InstancedMesh) {
|
|
588
|
-
throw new Error(
|
|
589
|
-
`Changing parameter "${Parameter.HIGHLIGHTED}" ` +
|
|
590
|
-
`of an InstancedMesh is not supported. ` +
|
|
591
|
-
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
});
|
|
595
|
-
let color = Color3.Green();
|
|
596
|
-
if (element.inheritedParameters[Parameter.HIGHLIGHT_COLOR]) {
|
|
597
|
-
color = Parameter.parseColor(element.inheritedParameters[Parameter.HIGHLIGHT_COLOR]);
|
|
598
|
-
}
|
|
599
|
-
if (typeof element._highlightLayer !== 'undefined') {
|
|
600
|
-
addToHighlightLayer(element._highlightLayer, color, node);
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
} else if (highlighted === false) {
|
|
604
|
-
element.nodes.forEach(node => {
|
|
605
|
-
if (typeof element._highlightLayer !== 'undefined') {
|
|
606
|
-
removeFromHighlightLayer(element._highlightLayer, node);
|
|
607
|
-
}
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
},
|
|
611
|
-
]);
|
|
612
|
-
this._parameterObservers.set(Parameter.CAST_SHADOW, [
|
|
613
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
614
|
-
let castShadow;
|
|
615
|
-
try {
|
|
616
|
-
castShadow = Parameter.parseBoolean(newValue);
|
|
617
|
-
} catch (e) {
|
|
618
|
-
return;
|
|
619
|
-
}
|
|
620
|
-
let lightCsl = element.inheritedParameters[Parameter.CAST_SHADOW_FROM_LIGHTS];
|
|
621
|
-
if (!lightCsl) {
|
|
622
|
-
lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
|
|
623
|
-
}
|
|
624
|
-
if (castShadow === true) {
|
|
625
|
-
await this.castShadowValueHandler(lightCsl, addToShadowGenerator);
|
|
626
|
-
}
|
|
627
|
-
if (castShadow === false) {
|
|
628
|
-
await this.castShadowValueHandler(lightCsl, removeFromShadowGenerator);
|
|
629
|
-
}
|
|
630
|
-
},
|
|
631
|
-
]);
|
|
632
|
-
this._parameterObservers.set(Parameter.CAST_SHADOW_FROM_LIGHTS, [
|
|
633
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
634
|
-
// TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
|
|
635
|
-
// when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
|
|
636
|
-
const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
|
|
637
|
-
// cleanup all shadow generators
|
|
638
|
-
await this.castShadowValueHandler(lightCsl, removeFromShadowGenerator);
|
|
639
|
-
if (element.castShadow === true) {
|
|
640
|
-
// if newValue is undefined or '' then set newValue to lightCsl (use all lights)
|
|
641
|
-
if (!newValue) {
|
|
642
|
-
newValue = lightCsl;
|
|
643
|
-
}
|
|
644
|
-
await this.castShadowValueHandler(newValue, addToShadowGenerator);
|
|
645
|
-
}
|
|
646
|
-
},
|
|
647
|
-
]);
|
|
648
|
-
this._parameterObservers.set(Parameter.RECEIVE_SHADOWS, [
|
|
649
|
-
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
650
|
-
element.nodes.forEach(node => setReceiveShadows(node, Parameter.parseBoolean(newValue)));
|
|
651
|
-
},
|
|
652
|
-
]);
|
|
653
|
-
return this;
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
/**
|
|
657
|
-
* Filter for nodes that are not defined or excluded in the `pathDefinitions`.
|
|
658
|
-
*/
|
|
659
|
-
protected filterNode(node: TransformNode): boolean {
|
|
660
|
-
let includeTokens: string[] = [];
|
|
661
|
-
this.pathDefinitions.include.forEach(dottedPath => {
|
|
662
|
-
includeTokens = union(includeTokens, DottedPath.create(dottedPath).leafTokens);
|
|
663
|
-
});
|
|
664
|
-
const shallKeep = (dp: DottedPath) => {
|
|
665
|
-
const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
|
|
666
|
-
const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
|
|
667
|
-
return dp.path.startsWith(`${_dp}${DottedPath.DELIMITER}`);
|
|
668
|
-
});
|
|
669
|
-
return isDirectlyIncluded || isSubOfIncluded;
|
|
670
|
-
};
|
|
671
|
-
const shallExclude = (dp: DottedPath) => {
|
|
672
|
-
if (!this.pathDefinitions.exclude) {
|
|
673
|
-
return false;
|
|
674
|
-
}
|
|
675
|
-
return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
|
|
676
|
-
};
|
|
677
|
-
return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
/**
|
|
681
|
-
* Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
|
|
682
|
-
* @protected
|
|
683
|
-
*/
|
|
684
|
-
protected assertPathDefinitions() {
|
|
685
|
-
const dottedNodes = this.variant.inheritedDottedNodes;
|
|
686
|
-
if (dottedNodes.size === 0) {
|
|
687
|
-
throw new Error(`There are no mapped nodes for element "${this.id}".`);
|
|
688
|
-
}
|
|
689
|
-
const include = this.pathDefinitions.include.map(path => DottedPath.create(path).path);
|
|
690
|
-
const exclude = this.pathDefinitions.exclude?.map(path => DottedPath.create(path).path);
|
|
691
|
-
const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
|
|
692
|
-
union<string>(include, exclude).forEach(path => {
|
|
693
|
-
if (keys.indexOf(path) === -1) {
|
|
694
|
-
throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
/**
|
|
700
|
-
* Handles callback for given light parameter.
|
|
701
|
-
*/
|
|
702
|
-
private async castShadowValueHandler(lightCsl: ParameterValue, mutator: CallableFunction) {
|
|
703
|
-
const lights: Light[] = [];
|
|
704
|
-
for (const lightName of Parameter.parseCommaSeparatedList(lightCsl)) {
|
|
705
|
-
const viewerLight = await this.variant.getViewerLight(lightName);
|
|
706
|
-
if (viewerLight) {
|
|
707
|
-
lights.push(viewerLight.light);
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
const shadowGenerators = lights.map(light => light?.getShadowGenerator() as ShadowGenerator).filter(Boolean);
|
|
711
|
-
shadowGenerators.forEach(generator => {
|
|
712
|
-
this.nodes.forEach(node => {
|
|
713
|
-
mutator(generator, node);
|
|
714
|
-
});
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
activateTransformNode as activate,
|
|
3
|
+
addToHighlightLayer,
|
|
4
|
+
addToShadowGenerator,
|
|
5
|
+
assertTransformNode,
|
|
6
|
+
cloneTransformNode,
|
|
7
|
+
cloneTransformNodeMaterial,
|
|
8
|
+
deactivateTransformNode as deactivate,
|
|
9
|
+
getClientRectFromMesh,
|
|
10
|
+
injectNodeMetadata,
|
|
11
|
+
mapToDottedNodes,
|
|
12
|
+
removeFromHighlightLayer,
|
|
13
|
+
removeFromShadowGenerator,
|
|
14
|
+
setMaterial,
|
|
15
|
+
setMaterialColor,
|
|
16
|
+
setMaterialMetallness,
|
|
17
|
+
setMaterialRoughness,
|
|
18
|
+
setMaterialTexture,
|
|
19
|
+
setReceiveShadows,
|
|
20
|
+
transformTransformNode,
|
|
21
|
+
} from '../util/babylonHelper';
|
|
22
|
+
import { createImageFromImgSrc, createImageFromSvg } from '../util/resourceHelper';
|
|
23
|
+
import { addMissingMaterialObserver } from '../util/sceneLoaderHelper';
|
|
24
|
+
import { VariantParameterizable } from './../classes/variantParameterizable';
|
|
25
|
+
import { DottedPath } from './dottedPath';
|
|
26
|
+
import { Parameter } from './parameter';
|
|
27
|
+
import { Variant } from './variant';
|
|
28
|
+
import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
|
|
29
|
+
import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
|
|
30
|
+
import { Light } from '@babylonjs/core/Lights/light';
|
|
31
|
+
import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
|
|
32
|
+
import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
|
|
33
|
+
import { Material } from '@babylonjs/core/Materials/material';
|
|
34
|
+
import { Color3 } from '@babylonjs/core/Maths/math.color';
|
|
35
|
+
import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
|
|
36
|
+
import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
|
|
37
|
+
import { Mesh } from '@babylonjs/core/Meshes/mesh';
|
|
38
|
+
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
|
|
39
|
+
import { has, isArray, isNumber, isPlainObject, merge, union } from 'lodash-es';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
|
|
43
|
+
* {@link Variant}.
|
|
44
|
+
*
|
|
45
|
+
* When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
|
|
46
|
+
* [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
|
|
47
|
+
*/
|
|
48
|
+
export class Element extends VariantParameterizable {
|
|
49
|
+
public readonly nodes: TransformNode[] = [];
|
|
50
|
+
|
|
51
|
+
protected readonly _dottedNodes: Map<DottedPath, TransformNode> = new Map();
|
|
52
|
+
|
|
53
|
+
protected _highlightLayer?: HighlightLayer;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Constructor.
|
|
57
|
+
*/
|
|
58
|
+
protected constructor(public readonly variant: Variant, public readonly name: string) {
|
|
59
|
+
super(variant, name);
|
|
60
|
+
if (process.env.NODE_ENV?.toLowerCase().includes('dev')) {
|
|
61
|
+
this.assertPathDefinitions();
|
|
62
|
+
}
|
|
63
|
+
const nodes = this.variant.inheritedNodes.map(node => cloneTransformNode(node, this.filterNode.bind(this)));
|
|
64
|
+
this.nodes = nodes.filter(node => !!node) as TransformNode[];
|
|
65
|
+
this._dottedNodes = mapToDottedNodes<TransformNode>(this.nodes, node => node instanceof TransformNode);
|
|
66
|
+
this.addMissingMaterialObservers();
|
|
67
|
+
this.addParameterObservers();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Creates an {@link Element} with given name.
|
|
72
|
+
*/
|
|
73
|
+
public static async create(variant: Variant, name: string): Promise<Element> {
|
|
74
|
+
return new Element(variant, name);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The {@link DottedPath} in the built tree of {@link Element}s.
|
|
79
|
+
* E.g. "_.top-1.sub-2.sub-sub-3.el-1"
|
|
80
|
+
*/
|
|
81
|
+
get dottedPath(): DottedPath {
|
|
82
|
+
return DottedPath.create(this.variant.dottedPath).addPart(this.name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The id representing a {@link DottedPath}.
|
|
87
|
+
*/
|
|
88
|
+
get id(): string {
|
|
89
|
+
const dottedPath = DottedPath.create(this.dottedPath);
|
|
90
|
+
dottedPath.shiftPart(); // remove root
|
|
91
|
+
return dottedPath.path;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The {@link ElementDefinition} of the {@link Element}.
|
|
96
|
+
*/
|
|
97
|
+
get definition(): ElementDefinition {
|
|
98
|
+
const definition = this.variant.structureJson.elements![this.name];
|
|
99
|
+
if (isArray(definition)) {
|
|
100
|
+
return {
|
|
101
|
+
paths: {
|
|
102
|
+
include: definition,
|
|
103
|
+
},
|
|
104
|
+
} as ElementDefinition;
|
|
105
|
+
}
|
|
106
|
+
return definition as ElementDefinition;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The {@link PathDefinitions} of the {@link Element}.
|
|
111
|
+
*/
|
|
112
|
+
get pathDefinitions(): PathDefinitions {
|
|
113
|
+
return this.definition.paths;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The {@link TraceableDefinitions} of the {@link Element}.
|
|
118
|
+
*/
|
|
119
|
+
get traceableDefinitions(): TraceableDefinitions {
|
|
120
|
+
return this.definition.traceables || {};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The {@link PaintableDefinition} of the {@link Element}.
|
|
125
|
+
*/
|
|
126
|
+
get paintableDefinitions(): PaintableDefinitions {
|
|
127
|
+
return this.definition.paintables || {};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The inherited {@link ParameterBag}.
|
|
132
|
+
* Merges the {@link Variant}'s parameters and those from the {@link Element}.
|
|
133
|
+
*/
|
|
134
|
+
get inheritedParameters(): ParameterBag {
|
|
135
|
+
return merge({}, this.variant.inheritedParameters, this.parameters);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The actual {@link Mesh}es that make up this {@link Element}.
|
|
140
|
+
* Handy for e.g. creating a bounding box around an entire element.
|
|
141
|
+
*/
|
|
142
|
+
get meshesFlat(): Mesh[] {
|
|
143
|
+
const flatMeshes = this.nodes.reduce((accFlatMeshes, curNode) => {
|
|
144
|
+
const currMeshes = curNode.getChildMeshes(false, n => n instanceof Mesh) as Mesh[];
|
|
145
|
+
return [...accFlatMeshes, ...currMeshes];
|
|
146
|
+
}, [] as Mesh[]);
|
|
147
|
+
return flatMeshes;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The actual {@link AbstractMesh}es that make up this {@link Element}.
|
|
152
|
+
* I.e. (real) Meshes as well as InstancedMeshes.
|
|
153
|
+
*/
|
|
154
|
+
get abstractMeshesFlat(): AbstractMesh[] {
|
|
155
|
+
const allAbstractMeshes = this.nodes.reduce((acc, currNode) => {
|
|
156
|
+
const currAbstractMeshes = currNode.getChildMeshes(false, n => n instanceof AbstractMesh);
|
|
157
|
+
if (currNode instanceof AbstractMesh) {
|
|
158
|
+
currAbstractMeshes.unshift(currNode);
|
|
159
|
+
}
|
|
160
|
+
return [...acc, ...currAbstractMeshes];
|
|
161
|
+
}, [] as AbstractMesh[]); // initial accumulator value
|
|
162
|
+
return allAbstractMeshes;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The meshes of this {@link Element}.
|
|
167
|
+
*/
|
|
168
|
+
get meshes(): Mesh[] {
|
|
169
|
+
return this.nodes.filter(node => node instanceof Mesh) as Mesh[];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @protected
|
|
174
|
+
*/
|
|
175
|
+
set highlightLayer(layer: HighlightLayer | undefined) {
|
|
176
|
+
this._highlightLayer = layer;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get highlightLayer(): HighlightLayer | undefined {
|
|
180
|
+
return this._highlightLayer;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Destroys this {@link Element} and dispose all nodes.
|
|
185
|
+
*/
|
|
186
|
+
public destroy(): Element {
|
|
187
|
+
this.nodes.forEach(node => {
|
|
188
|
+
node.dispose();
|
|
189
|
+
});
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Gets a node by its {@link DottedPath}.
|
|
195
|
+
*/
|
|
196
|
+
public getNode(dottedPath: DottedPathArgument): TransformNode {
|
|
197
|
+
const _dottedPath = DottedPath.create(dottedPath);
|
|
198
|
+
const keys = Array.from(this._dottedNodes.keys()).map(dp => dp.path);
|
|
199
|
+
const values = Array.from(this._dottedNodes.values());
|
|
200
|
+
const node = values[keys.indexOf(_dottedPath.path)];
|
|
201
|
+
if (!node) {
|
|
202
|
+
throw new Error(`Node with path "${_dottedPath.path}" does not exist for element "${this.id}".`);
|
|
203
|
+
}
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Gets a mesh by its {@link DottedPath}.
|
|
209
|
+
*/
|
|
210
|
+
public async getMesh(dottedPath: DottedPathArgument): Promise<Mesh | null> {
|
|
211
|
+
const node = await this.getNode(dottedPath);
|
|
212
|
+
if (node instanceof Mesh) {
|
|
213
|
+
return node;
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Gets a {@link PaintableDefinition} by its name.
|
|
220
|
+
*/
|
|
221
|
+
public getPaintableDefinition(name: string): PaintableDefinition {
|
|
222
|
+
if (!this.paintableDefinitions || !this.paintableDefinitions[name]) {
|
|
223
|
+
throw new Error(`No paintable "${name}" defined for element "${this.id}".`);
|
|
224
|
+
}
|
|
225
|
+
return this.paintableDefinitions[name];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Gets a {@link TraceableDefinition} by its name.
|
|
230
|
+
*/
|
|
231
|
+
public getTraceableDefinition(name: string): TraceableDefinition {
|
|
232
|
+
if (!this.traceableDefinitions || !this.traceableDefinitions[name]) {
|
|
233
|
+
throw new Error(`No traceable "${name}" defined to add observer in element "${this.id}".`);
|
|
234
|
+
}
|
|
235
|
+
return this.traceableDefinitions[name];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Gets the paintable Node by its name.
|
|
240
|
+
*/
|
|
241
|
+
public getPaintableNode(name: string): TransformNode {
|
|
242
|
+
return this.getNode(this.getPaintableDefinition(name).path);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Gets the traceable Node by its name.
|
|
247
|
+
*/
|
|
248
|
+
public getTraceableNode(name: string): TransformNode {
|
|
249
|
+
return this.getNode(this.getTraceableDefinition(name).path);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @see {@link VariantParameterizable.commitParameters}
|
|
254
|
+
* @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
|
|
255
|
+
*/
|
|
256
|
+
public async commitParameters(parameters?: ParameterBag): Promise<VariantParameterizable> {
|
|
257
|
+
return super.commitParameters(parameters);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Adds an observer function for camera matrix changes.
|
|
262
|
+
* The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
|
|
263
|
+
*/
|
|
264
|
+
public addTraceableObserver(traceable: string, observer: CallableFunction, payload?: any): Element {
|
|
265
|
+
if (payload && !(payload instanceof HTMLElement)) {
|
|
266
|
+
throw new Error(`Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".`);
|
|
267
|
+
}
|
|
268
|
+
if (!this.traceableDefinitions || !this.traceableDefinitions[traceable]) {
|
|
269
|
+
throw new Error(`No traceable "${traceable}" defined to add observer in element "${this.id}".`);
|
|
270
|
+
}
|
|
271
|
+
const node = this.getTraceableNode(traceable);
|
|
272
|
+
if (!(node instanceof AbstractMesh)) {
|
|
273
|
+
throw new Error(`The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".`);
|
|
274
|
+
}
|
|
275
|
+
this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add((eventData, eventState) => {
|
|
276
|
+
const position = getClientRectFromMesh(node, this.variant.viewer.scene, this.variant.viewer.canvas);
|
|
277
|
+
if (payload) {
|
|
278
|
+
payload.style.top = `${position.top}px`;
|
|
279
|
+
payload.style.left = `${position.left}px`;
|
|
280
|
+
}
|
|
281
|
+
observer(node, position);
|
|
282
|
+
});
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
|
|
288
|
+
*/
|
|
289
|
+
public drawPaintable(paintable: string, imageSource: CanvasImageSource): Element {
|
|
290
|
+
// node and material checks and preperation
|
|
291
|
+
const node = this.getPaintableNode(paintable);
|
|
292
|
+
if (!(node instanceof AbstractMesh)) {
|
|
293
|
+
throw new Error(
|
|
294
|
+
`The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` + `in element "${this.id}".`
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (node.material && this.variant.viewer.cloneMaterialsOnMutation) {
|
|
299
|
+
if (node.material && !has(node.metadata, 'dirty.material')) {
|
|
300
|
+
cloneTransformNodeMaterial(node);
|
|
301
|
+
}
|
|
302
|
+
if (!has(node.metadata, 'dirty.material.texture')) {
|
|
303
|
+
// inject initial value and mark as dirty
|
|
304
|
+
injectNodeMetadata(node, { dirty: { material: { texture: true } } });
|
|
305
|
+
}
|
|
306
|
+
} else if (!node.material) {
|
|
307
|
+
// paintables should also work if no material is assigned to the node per default
|
|
308
|
+
// in this case a certain fallback material with fitting default values is defined
|
|
309
|
+
const pbrMat = new PBRMaterial(`${this.id}.${paintable}.material`, this.variant.viewer.scene);
|
|
310
|
+
pbrMat.roughness = 1.0;
|
|
311
|
+
pbrMat.metallic = 1.0;
|
|
312
|
+
node.material = pbrMat;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// TODO: Not sure if this really should be overwritten every time the paintable is redrawn.
|
|
316
|
+
// Maybe the 3D artist had something in mind when setting a certain transparency mode, which would get overwritten
|
|
317
|
+
// in this case.
|
|
318
|
+
// Applying it as a default if no material is set is fine though (see roughness/metallic some lines above).
|
|
319
|
+
// Removing this line will most likely break existing configurators, so be cautios!
|
|
320
|
+
node.material.transparencyMode = Material.MATERIAL_ALPHATESTANDBLEND;
|
|
321
|
+
|
|
322
|
+
// consider width and height of the paintable
|
|
323
|
+
const widthAndHeight = {
|
|
324
|
+
width: imageSource.width,
|
|
325
|
+
height: imageSource.height,
|
|
326
|
+
};
|
|
327
|
+
const paintableDefinition = this.getPaintableDefinition(paintable);
|
|
328
|
+
if (isNumber(paintableDefinition.textureOptions)) {
|
|
329
|
+
widthAndHeight.width = paintableDefinition.textureOptions;
|
|
330
|
+
widthAndHeight.height = paintableDefinition.textureOptions;
|
|
331
|
+
} else if (isPlainObject(paintableDefinition.textureOptions)) {
|
|
332
|
+
widthAndHeight.width = paintableDefinition.textureOptions.width;
|
|
333
|
+
widthAndHeight.height = paintableDefinition.textureOptions.height;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// create dynamic texture on which the image can be drawn
|
|
337
|
+
const texture = new DynamicTexture(
|
|
338
|
+
`${this.id}.${paintable}.texture`,
|
|
339
|
+
widthAndHeight,
|
|
340
|
+
this.variant.viewer.scene,
|
|
341
|
+
false
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// draw image on texture
|
|
345
|
+
const ctx = texture.getContext();
|
|
346
|
+
ctx.drawImage(imageSource, 0, 0);
|
|
347
|
+
texture.update(false);
|
|
348
|
+
|
|
349
|
+
// finally apply the texture on the desired node material
|
|
350
|
+
setMaterialTexture(node, texture, false);
|
|
351
|
+
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
|
|
357
|
+
*/
|
|
358
|
+
public async drawPaintableFromSvg(paintable: string, svgSource: string): Promise<Element> {
|
|
359
|
+
const image = await createImageFromSvg(svgSource);
|
|
360
|
+
return this.drawPaintable(paintable, image);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
|
|
365
|
+
*/
|
|
366
|
+
public async drawPaintableFromImgSrc(paintable: string, imgSource: string): Promise<Element> {
|
|
367
|
+
const image = await createImageFromImgSrc(imgSource);
|
|
368
|
+
return this.drawPaintable(paintable, image);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Adds observers for loading missing materials where needed
|
|
373
|
+
*/
|
|
374
|
+
protected addMissingMaterialObservers() {
|
|
375
|
+
for (const abstractMesh of this.abstractMeshesFlat) {
|
|
376
|
+
addMissingMaterialObserver(abstractMesh);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
|
|
382
|
+
*/
|
|
383
|
+
protected addParameterObservers(): Element {
|
|
384
|
+
this._parameterObservers.set(Parameter.VISIBLE, [
|
|
385
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
386
|
+
let visible;
|
|
387
|
+
try {
|
|
388
|
+
visible = Parameter.parseBoolean(newValue);
|
|
389
|
+
} catch (e) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (visible === true) {
|
|
393
|
+
element.nodes.forEach(node => {
|
|
394
|
+
injectNodeMetadata(node, { visibility: node.isEnabled() });
|
|
395
|
+
activate(node);
|
|
396
|
+
});
|
|
397
|
+
} else if (visible === false) {
|
|
398
|
+
element.nodes.forEach(node => {
|
|
399
|
+
injectNodeMetadata(node, { visibility: node.isEnabled() });
|
|
400
|
+
deactivate(node);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
]);
|
|
405
|
+
this._parameterObservers.set(Parameter.SCALING, [
|
|
406
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
407
|
+
// we have to deal just with root nodes here due to relative impacts in a node tree
|
|
408
|
+
element.nodes.forEach(node =>
|
|
409
|
+
transformTransformNode(node, {
|
|
410
|
+
scaling: Parameter.parseVector(newValue),
|
|
411
|
+
position: Parameter.parseVector(element.inheritedParameters[Parameter.POSITION] || '(0, 0, 0)'),
|
|
412
|
+
rotation: Parameter.parseRotation(element.inheritedParameters[Parameter.ROTATION] || '(0, 0, 0)'),
|
|
413
|
+
})
|
|
414
|
+
);
|
|
415
|
+
},
|
|
416
|
+
]);
|
|
417
|
+
this._parameterObservers.set(Parameter.POSITION, [
|
|
418
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
419
|
+
// we have to deal just with root nodes here due to relative impacts in a node tree
|
|
420
|
+
element.nodes.forEach(node =>
|
|
421
|
+
transformTransformNode(node, {
|
|
422
|
+
scaling: Parameter.parseVector(element.inheritedParameters[Parameter.SCALING] || '(1, 1, 1)'),
|
|
423
|
+
position: Parameter.parseVector(newValue),
|
|
424
|
+
rotation: Parameter.parseRotation(element.inheritedParameters[Parameter.ROTATION] || '(0, 0, 0)'),
|
|
425
|
+
})
|
|
426
|
+
);
|
|
427
|
+
},
|
|
428
|
+
]);
|
|
429
|
+
this._parameterObservers.set(Parameter.ROTATION, [
|
|
430
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
431
|
+
// we have to deal just with root nodes here due to relative impacts in a node tree
|
|
432
|
+
element.nodes.forEach(node =>
|
|
433
|
+
transformTransformNode(node, {
|
|
434
|
+
scaling: Parameter.parseVector(element.inheritedParameters[Parameter.SCALING] || '(1, 1, 1)'),
|
|
435
|
+
position: Parameter.parseVector(element.inheritedParameters[Parameter.POSITION] || '(0, 0, 0)'),
|
|
436
|
+
rotation: Parameter.parseRotation(newValue),
|
|
437
|
+
})
|
|
438
|
+
);
|
|
439
|
+
},
|
|
440
|
+
]);
|
|
441
|
+
this._parameterObservers.set(Parameter.MATERIAL, [
|
|
442
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
443
|
+
const materialName = newValue.toString();
|
|
444
|
+
element.nodes.forEach(node => {
|
|
445
|
+
assertTransformNode(node, (node: AbstractMesh) => {
|
|
446
|
+
if (node instanceof InstancedMesh) {
|
|
447
|
+
throw new Error(
|
|
448
|
+
`Changing parameter "${Parameter.MATERIAL}" ` +
|
|
449
|
+
`of an InstancedMesh is not supported. ` +
|
|
450
|
+
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
setMaterial(element.variant, node, materialName);
|
|
455
|
+
});
|
|
456
|
+
},
|
|
457
|
+
]);
|
|
458
|
+
this._parameterObservers.set(Parameter.MATERIAL_COLOR, [
|
|
459
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
460
|
+
const color = Parameter.parseColor(newValue);
|
|
461
|
+
element.nodes.forEach(node => {
|
|
462
|
+
assertTransformNode(node, (node: AbstractMesh) => {
|
|
463
|
+
if (node instanceof InstancedMesh) {
|
|
464
|
+
throw new Error(
|
|
465
|
+
`Changing parameter "${Parameter.MATERIAL_COLOR}" ` +
|
|
466
|
+
`of an InstancedMesh is not supported. ` +
|
|
467
|
+
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
if (this.variant.viewer.cloneMaterialsOnMutation) {
|
|
472
|
+
if (!has(node.metadata, 'dirty.material')) {
|
|
473
|
+
cloneTransformNodeMaterial(node);
|
|
474
|
+
}
|
|
475
|
+
if (!has(node.metadata, 'dirty.material.color')) {
|
|
476
|
+
// inject initial value and mark as dirty
|
|
477
|
+
injectNodeMetadata(node, { dirty: { material: { color: oldValue } } });
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
setMaterialColor(node, color);
|
|
481
|
+
});
|
|
482
|
+
},
|
|
483
|
+
]);
|
|
484
|
+
this._parameterObservers.set(Parameter.MATERIAL_METALLNESS, [
|
|
485
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
486
|
+
const metallness = Parameter.parseNumber(newValue);
|
|
487
|
+
element.nodes.forEach(node => {
|
|
488
|
+
assertTransformNode(node, (node: AbstractMesh) => {
|
|
489
|
+
if (node instanceof InstancedMesh) {
|
|
490
|
+
throw new Error(
|
|
491
|
+
`Changing parameter "${Parameter.MATERIAL_METALLNESS}" ` +
|
|
492
|
+
`of an InstancedMesh is not supported. ` +
|
|
493
|
+
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
if (this.variant.viewer.cloneMaterialsOnMutation) {
|
|
498
|
+
if (!has(node.metadata, 'dirty.material')) {
|
|
499
|
+
cloneTransformNodeMaterial(node);
|
|
500
|
+
}
|
|
501
|
+
if (!has(node.metadata, 'dirty.material.metallness')) {
|
|
502
|
+
// inject initial value and mark as dirty
|
|
503
|
+
injectNodeMetadata(node, { dirty: { material: { metallness: oldValue } } });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
setMaterialMetallness(node, metallness);
|
|
507
|
+
});
|
|
508
|
+
},
|
|
509
|
+
]);
|
|
510
|
+
this._parameterObservers.set(Parameter.MATERIAL_ROUGHNESS, [
|
|
511
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
512
|
+
const roughness = Parameter.parseNumber(newValue);
|
|
513
|
+
element.nodes.forEach(node => {
|
|
514
|
+
assertTransformNode(node, (node: AbstractMesh) => {
|
|
515
|
+
if (node instanceof InstancedMesh) {
|
|
516
|
+
throw new Error(
|
|
517
|
+
`Changing parameter "${Parameter.MATERIAL_ROUGHNESS}" ` +
|
|
518
|
+
`of an InstancedMesh is not supported. ` +
|
|
519
|
+
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
if (this.variant.viewer.cloneMaterialsOnMutation) {
|
|
524
|
+
if (!has(node.metadata, 'dirty.material')) {
|
|
525
|
+
cloneTransformNodeMaterial(node);
|
|
526
|
+
}
|
|
527
|
+
if (!has(node.metadata, 'dirty.material.roughness')) {
|
|
528
|
+
// inject initial value and mark as dirty
|
|
529
|
+
injectNodeMetadata(node, { dirty: { material: { roughness: oldValue } } });
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
setMaterialRoughness(node, roughness);
|
|
533
|
+
});
|
|
534
|
+
},
|
|
535
|
+
]);
|
|
536
|
+
this._parameterObservers.set(Parameter.HIGHLIGHT_COLOR, [
|
|
537
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
538
|
+
// trigger Parameter.HIGHLIGHTED observers
|
|
539
|
+
this._parameterObservers.get(Parameter.HIGHLIGHTED)?.forEach(observer => {
|
|
540
|
+
observer(element, !!element.highlighted, !!element.highlighted);
|
|
541
|
+
});
|
|
542
|
+
},
|
|
543
|
+
]);
|
|
544
|
+
this._parameterObservers.set(Parameter.HIGHLIGHT_ENABLED, [
|
|
545
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
546
|
+
let curr;
|
|
547
|
+
try {
|
|
548
|
+
curr = Parameter.parseBoolean(newValue);
|
|
549
|
+
} catch (e) {
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
switch (curr) {
|
|
553
|
+
case true:
|
|
554
|
+
if (element._highlightLayer) {
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
element.highlightLayer = new HighlightLayer(
|
|
558
|
+
DottedPath.create(element.id).addPart('highlight').path,
|
|
559
|
+
element.variant.viewer.scene
|
|
560
|
+
);
|
|
561
|
+
break;
|
|
562
|
+
case false:
|
|
563
|
+
element.highlightLayer?.dispose();
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
},
|
|
567
|
+
]);
|
|
568
|
+
this._parameterObservers.set(Parameter.HIGHLIGHTED, [
|
|
569
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
570
|
+
if (!element.inheritedParameters[Parameter.HIGHLIGHT_ENABLED]) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
// trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
|
|
574
|
+
this._parameterObservers.get(Parameter.HIGHLIGHT_ENABLED)?.forEach(observer => {
|
|
575
|
+
observer(element, this.parameters[Parameter.HIGHLIGHT_ENABLED], true);
|
|
576
|
+
});
|
|
577
|
+
let highlighted;
|
|
578
|
+
try {
|
|
579
|
+
highlighted = Parameter.parseBoolean(newValue);
|
|
580
|
+
} catch (e) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
// Add/Remove meshes to previously created highlight layers.
|
|
584
|
+
if (highlighted === true) {
|
|
585
|
+
element.nodes.forEach(node => {
|
|
586
|
+
assertTransformNode(node, (node: AbstractMesh) => {
|
|
587
|
+
if (node instanceof InstancedMesh) {
|
|
588
|
+
throw new Error(
|
|
589
|
+
`Changing parameter "${Parameter.HIGHLIGHTED}" ` +
|
|
590
|
+
`of an InstancedMesh is not supported. ` +
|
|
591
|
+
`Tried to change node "${node.id}" on element "${element.id}".`
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
let color = Color3.Green();
|
|
596
|
+
if (element.inheritedParameters[Parameter.HIGHLIGHT_COLOR]) {
|
|
597
|
+
color = Parameter.parseColor(element.inheritedParameters[Parameter.HIGHLIGHT_COLOR]);
|
|
598
|
+
}
|
|
599
|
+
if (typeof element._highlightLayer !== 'undefined') {
|
|
600
|
+
addToHighlightLayer(element._highlightLayer, color, node);
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
} else if (highlighted === false) {
|
|
604
|
+
element.nodes.forEach(node => {
|
|
605
|
+
if (typeof element._highlightLayer !== 'undefined') {
|
|
606
|
+
removeFromHighlightLayer(element._highlightLayer, node);
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
},
|
|
611
|
+
]);
|
|
612
|
+
this._parameterObservers.set(Parameter.CAST_SHADOW, [
|
|
613
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
614
|
+
let castShadow;
|
|
615
|
+
try {
|
|
616
|
+
castShadow = Parameter.parseBoolean(newValue);
|
|
617
|
+
} catch (e) {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
let lightCsl = element.inheritedParameters[Parameter.CAST_SHADOW_FROM_LIGHTS];
|
|
621
|
+
if (!lightCsl) {
|
|
622
|
+
lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
|
|
623
|
+
}
|
|
624
|
+
if (castShadow === true) {
|
|
625
|
+
await this.castShadowValueHandler(lightCsl, addToShadowGenerator);
|
|
626
|
+
}
|
|
627
|
+
if (castShadow === false) {
|
|
628
|
+
await this.castShadowValueHandler(lightCsl, removeFromShadowGenerator);
|
|
629
|
+
}
|
|
630
|
+
},
|
|
631
|
+
]);
|
|
632
|
+
this._parameterObservers.set(Parameter.CAST_SHADOW_FROM_LIGHTS, [
|
|
633
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
634
|
+
// TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
|
|
635
|
+
// when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
|
|
636
|
+
const lightCsl = element.variant.inheritedViewerLights.map(l => l.name).join(',');
|
|
637
|
+
// cleanup all shadow generators
|
|
638
|
+
await this.castShadowValueHandler(lightCsl, removeFromShadowGenerator);
|
|
639
|
+
if (element.castShadow === true) {
|
|
640
|
+
// if newValue is undefined or '' then set newValue to lightCsl (use all lights)
|
|
641
|
+
if (!newValue) {
|
|
642
|
+
newValue = lightCsl;
|
|
643
|
+
}
|
|
644
|
+
await this.castShadowValueHandler(newValue, addToShadowGenerator);
|
|
645
|
+
}
|
|
646
|
+
},
|
|
647
|
+
]);
|
|
648
|
+
this._parameterObservers.set(Parameter.RECEIVE_SHADOWS, [
|
|
649
|
+
async (element: Element, oldValue: ParameterValue, newValue: ParameterValue) => {
|
|
650
|
+
element.nodes.forEach(node => setReceiveShadows(node, Parameter.parseBoolean(newValue)));
|
|
651
|
+
},
|
|
652
|
+
]);
|
|
653
|
+
return this;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Filter for nodes that are not defined or excluded in the `pathDefinitions`.
|
|
658
|
+
*/
|
|
659
|
+
protected filterNode(node: TransformNode): boolean {
|
|
660
|
+
let includeTokens: string[] = [];
|
|
661
|
+
this.pathDefinitions.include.forEach(dottedPath => {
|
|
662
|
+
includeTokens = union(includeTokens, DottedPath.create(dottedPath).leafTokens);
|
|
663
|
+
});
|
|
664
|
+
const shallKeep = (dp: DottedPath) => {
|
|
665
|
+
const isDirectlyIncluded = !!includeTokens.find(_dp => _dp === dp.path);
|
|
666
|
+
const isSubOfIncluded = !!this.pathDefinitions.include.find(_dp => {
|
|
667
|
+
return dp.path.startsWith(`${_dp}${DottedPath.DELIMITER}`);
|
|
668
|
+
});
|
|
669
|
+
return isDirectlyIncluded || isSubOfIncluded;
|
|
670
|
+
};
|
|
671
|
+
const shallExclude = (dp: DottedPath) => {
|
|
672
|
+
if (!this.pathDefinitions.exclude) {
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
return !!this.pathDefinitions.exclude.find(_dp => dp.path === _dp);
|
|
676
|
+
};
|
|
677
|
+
return shallKeep(node.metadata.dottedPath) && !shallExclude(node.metadata.dottedPath);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
|
|
682
|
+
* @protected
|
|
683
|
+
*/
|
|
684
|
+
protected assertPathDefinitions() {
|
|
685
|
+
const dottedNodes = this.variant.inheritedDottedNodes;
|
|
686
|
+
if (dottedNodes.size === 0) {
|
|
687
|
+
throw new Error(`There are no mapped nodes for element "${this.id}".`);
|
|
688
|
+
}
|
|
689
|
+
const include = this.pathDefinitions.include.map(path => DottedPath.create(path).path);
|
|
690
|
+
const exclude = this.pathDefinitions.exclude?.map(path => DottedPath.create(path).path);
|
|
691
|
+
const keys = Array.from(dottedNodes.keys()).map(dottedPath => dottedPath.path);
|
|
692
|
+
union<string>(include, exclude).forEach(path => {
|
|
693
|
+
if (keys.indexOf(path) === -1) {
|
|
694
|
+
throw new Error(`Node with path "${path}" does not exist for element "${this.id}".`);
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Handles callback for given light parameter.
|
|
701
|
+
*/
|
|
702
|
+
private async castShadowValueHandler(lightCsl: ParameterValue, mutator: CallableFunction) {
|
|
703
|
+
const lights: Light[] = [];
|
|
704
|
+
for (const lightName of Parameter.parseCommaSeparatedList(lightCsl)) {
|
|
705
|
+
const viewerLight = await this.variant.getViewerLight(lightName);
|
|
706
|
+
if (viewerLight) {
|
|
707
|
+
lights.push(viewerLight.light);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
const shadowGenerators = lights.map(light => light?.getShadowGenerator() as ShadowGenerator).filter(Boolean);
|
|
711
|
+
shadowGenerators.forEach(generator => {
|
|
712
|
+
this.nodes.forEach(node => {
|
|
713
|
+
mutator(generator, node);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
}
|