@combeenation/3d-viewer 3.0.0-rc2 → 4.0.0-alpha6

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.
Files changed (212) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +111 -111
  3. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  4. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  5. package/dist/lib-cjs/api/classes/animationInterface.js.map +0 -0
  6. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  7. package/dist/lib-cjs/api/classes/dottedPath.js +190 -190
  8. package/dist/lib-cjs/api/classes/dottedPath.js.map +0 -0
  9. package/dist/lib-cjs/api/classes/element.d.ts +130 -125
  10. package/dist/lib-cjs/api/classes/element.js +752 -638
  11. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  12. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -14
  13. package/dist/lib-cjs/api/classes/elementParameterizable.js +134 -134
  14. package/dist/lib-cjs/api/classes/elementParameterizable.js.map +0 -0
  15. package/dist/lib-cjs/api/classes/event.d.ts +326 -312
  16. package/dist/lib-cjs/api/classes/event.js +371 -357
  17. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  18. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  19. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  20. package/dist/lib-cjs/api/classes/eventBroadcaster.js.map +0 -0
  21. package/dist/lib-cjs/api/classes/parameter.d.ts +259 -161
  22. package/dist/lib-cjs/api/classes/parameter.js +387 -261
  23. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  24. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  25. package/dist/lib-cjs/api/classes/parameterObservable.js +101 -126
  26. package/dist/lib-cjs/api/classes/parameterObservable.js.map +1 -1
  27. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -0
  28. package/dist/lib-cjs/api/classes/parameterizable.js +150 -0
  29. package/dist/lib-cjs/api/classes/parameterizable.js.map +1 -0
  30. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +38 -38
  31. package/dist/lib-cjs/api/classes/placementAnimation.js +138 -138
  32. package/dist/lib-cjs/api/classes/placementAnimation.js.map +0 -0
  33. package/dist/lib-cjs/api/classes/variant.d.ts +224 -176
  34. package/dist/lib-cjs/api/classes/variant.js +1126 -770
  35. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  36. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -41
  37. package/dist/lib-cjs/api/classes/variantInstance.js +108 -98
  38. package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
  39. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -0
  40. package/dist/lib-cjs/api/classes/variantParameterizable.js +93 -0
  41. package/dist/lib-cjs/api/classes/variantParameterizable.js.map +1 -0
  42. package/dist/lib-cjs/api/classes/viewer.d.ts +127 -128
  43. package/dist/lib-cjs/api/classes/viewer.js +486 -486
  44. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  45. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -0
  46. package/dist/lib-cjs/api/classes/viewerLight.js +350 -0
  47. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -0
  48. package/dist/lib-cjs/api/emitter.d.ts +35 -0
  49. package/dist/lib-cjs/api/emitter.js +61 -0
  50. package/dist/lib-cjs/api/emitter.js.map +1 -0
  51. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  52. package/dist/lib-cjs/api/internal/debugViewer.js +87 -87
  53. package/dist/lib-cjs/api/internal/debugViewer.js.map +0 -0
  54. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  55. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  56. package/dist/lib-cjs/api/internal/lensRendering.js.map +0 -0
  57. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +10 -6
  58. package/dist/lib-cjs/api/internal/sceneSetup.js +231 -227
  59. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  60. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  61. package/dist/lib-cjs/api/manager/animationManager.js +121 -121
  62. package/dist/lib-cjs/api/manager/animationManager.js.map +0 -0
  63. package/dist/lib-cjs/api/manager/sceneManager.d.ts +32 -32
  64. package/dist/lib-cjs/api/manager/sceneManager.js +132 -132
  65. package/dist/lib-cjs/api/manager/sceneManager.js.map +0 -0
  66. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +90 -90
  67. package/dist/lib-cjs/api/manager/variantInstanceManager.js +321 -321
  68. package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +0 -0
  69. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  70. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  71. package/dist/lib-cjs/api/store/specStorage.js.map +0 -0
  72. package/dist/lib-cjs/api/util/babylonHelper.d.ts +160 -125
  73. package/dist/lib-cjs/api/util/babylonHelper.js +465 -368
  74. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  75. package/dist/lib-cjs/api/util/globalTypes.d.ts +321 -275
  76. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  77. package/dist/lib-cjs/api/util/globalTypes.js.map +0 -0
  78. package/dist/lib-cjs/api/util/resourceHelper.d.ts +26 -30
  79. package/dist/lib-cjs/api/util/resourceHelper.js +237 -247
  80. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  81. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -0
  82. package/dist/lib-cjs/api/util/stringHelper.js +26 -0
  83. package/dist/lib-cjs/api/util/stringHelper.js.map +1 -0
  84. package/dist/lib-cjs/buildinfo.json +3 -3
  85. package/dist/lib-cjs/index.d.ts +48 -46
  86. package/dist/lib-cjs/index.js +86 -82
  87. package/dist/lib-cjs/index.js.map +1 -1
  88. package/package.json +83 -83
  89. package/src/api/classes/animationInterface.ts +11 -11
  90. package/src/api/classes/dottedPath.ts +189 -189
  91. package/src/api/classes/element.ts +648 -606
  92. package/src/api/classes/event.ts +370 -355
  93. package/src/api/classes/eventBroadcaster.ts +54 -54
  94. package/src/api/classes/parameter.ts +408 -270
  95. package/src/api/classes/parameterObservable.ts +99 -121
  96. package/src/api/classes/{elementParameterizable.ts → parameterizable.ts} +89 -78
  97. package/src/api/classes/placementAnimation.ts +133 -133
  98. package/src/api/classes/variant.ts +799 -652
  99. package/src/api/classes/variantInstance.ts +88 -81
  100. package/src/api/classes/variantParameterizable.ts +73 -0
  101. package/src/api/classes/viewer.ts +420 -421
  102. package/src/api/classes/viewerLight.ts +295 -0
  103. package/src/api/internal/debugViewer.ts +81 -81
  104. package/src/api/internal/lensRendering.ts +10 -10
  105. package/src/api/internal/sceneSetup.ts +194 -204
  106. package/src/api/manager/animationManager.ts +116 -116
  107. package/src/api/manager/sceneManager.ts +105 -105
  108. package/src/api/manager/variantInstanceManager.ts +236 -236
  109. package/src/api/store/specStorage.ts +53 -53
  110. package/src/api/util/babylonHelper.ts +497 -392
  111. package/src/api/util/globalTypes.ts +369 -314
  112. package/src/api/util/resourceHelper.ts +157 -168
  113. package/src/api/util/stringHelper.ts +26 -0
  114. package/src/buildinfo.json +2 -2
  115. package/src/commonjs.tsconfig.json +13 -13
  116. package/src/declaration.tsconfig.json +10 -10
  117. package/src/dev.ts +45 -60
  118. package/src/es6.tsconfig.json +13 -13
  119. package/src/index.ts +91 -87
  120. package/src/pagesconfig.json +51 -47
  121. package/src/tsconfig.json +43 -43
  122. package/src/tsconfig.types.json +9 -9
  123. package/src/types.d.ts +4 -4
  124. package/src/assets/02_environment.env +0 -0
  125. package/src/assets/02_test_hdri_flipped.hdr +0 -0
  126. package/src/assets/07rDvxP2xTk.glb +0 -0
  127. package/src/assets/08L8DrdZt8y.glb +0 -0
  128. package/src/assets/CB-6250/main.js +0 -427
  129. package/src/assets/CB-6250/models/.gitkeep +0 -1
  130. package/src/assets/CB-6250/models/candle.glb +0 -0
  131. package/src/assets/CB-6250/models/lamp.glb +0 -0
  132. package/src/assets/CB-6250/models/molto-bido.glb +0 -0
  133. package/src/assets/CB-6250/models/mtron.glb +0 -0
  134. package/src/assets/CB-6250/models/mtron_split_geometry.glb +0 -0
  135. package/src/assets/CB-6250/models/mtron_split_material.glb +0 -0
  136. package/src/assets/KTM1290SA/AvatarAdv.glb +0 -0
  137. package/src/assets/KTM1290SA/ktm1290SA.ts +0 -77
  138. package/src/assets/KTM1290SA/scene.json +0 -16
  139. package/src/assets/_draft/main.js +0 -117
  140. package/src/assets/_draft/models/.gitkeep +0 -1
  141. package/src/assets/_draft/models/_demo.glb +0 -0
  142. package/src/assets/_draft/studio.env +0 -0
  143. package/src/assets/bike/CompleteBike_Optim_986K_01.glb +0 -0
  144. package/src/assets/bike/complete.json +0 -19
  145. package/src/assets/bike/index.json +0 -9
  146. package/src/assets/bike/setup.json +0 -11
  147. package/src/assets/ctrls-helper.ts +0 -89
  148. package/src/assets/cube/10 environment.env +0 -0
  149. package/src/assets/cube/bin_svg_logo_test.glb +0 -0
  150. package/src/assets/cube/cube.ts +0 -42
  151. package/src/assets/cube/cube_v2.glb +0 -0
  152. package/src/assets/cube/scene.ts +0 -83
  153. package/src/assets/cube/testCylinderUVs.glb +0 -0
  154. package/src/assets/environment.env +0 -0
  155. package/src/assets/hoferkerzen/hoferkerzen.glb +0 -0
  156. package/src/assets/hoferkerzen/index.json +0 -9
  157. package/src/assets/hoferkerzen/setup.json +0 -11
  158. package/src/assets/hoferkerzen/variant_kgl.json +0 -41
  159. package/src/assets/husqvarna-instanced-meshes/husqvarna.env +0 -0
  160. package/src/assets/husqvarna-instanced-meshes/husqvarna.glb +0 -0
  161. package/src/assets/husqvarna-instanced-meshes/main.ts +0 -108
  162. package/src/assets/index.html +0 -144
  163. package/src/assets/index.json +0 -9
  164. package/src/assets/linkeddups/example_2_instances.glb +0 -0
  165. package/src/assets/linkeddups/spec.ts +0 -32
  166. package/src/assets/molto-mova-multi-instance-shown-bug/main.ts +0 -106
  167. package/src/assets/molto-mova-multi-instance-shown-bug/molto-mova-rc82.glb +0 -0
  168. package/src/assets/molto-mova-multi-instance-shown-bug/molto-mova-volare.glb +0 -0
  169. package/src/assets/molto-mova-multi-instance-shown-bug/studio.env +0 -0
  170. package/src/assets/moltomova/10 environment.env +0 -0
  171. package/src/assets/moltomova/RC65.glb +0 -0
  172. package/src/assets/moltomova/RC82.glb +0 -0
  173. package/src/assets/moltomova/TPH67.glb +0 -0
  174. package/src/assets/moltomova/TPH82.glb +0 -0
  175. package/src/assets/moltomova/Volare.glb +0 -0
  176. package/src/assets/moltomova/moltomova-cl3d-spec-rc65.ts +0 -159
  177. package/src/assets/moltomova/moltomova-cl3d-spec-rc82.ts +0 -118
  178. package/src/assets/moltomova/moltomova-cl3d-spec-scene.ts +0 -57
  179. package/src/assets/moltomova/moltomova-cl3d-spec-tph67.ts +0 -199
  180. package/src/assets/moltomova/moltomova-cl3d-spec-tph82.ts +0 -173
  181. package/src/assets/moltomova/moltomova-cl3d-spec-volare65.ts +0 -173
  182. package/src/assets/moltomova/moltomova.ts +0 -175
  183. package/src/assets/mova.json +0 -18
  184. package/src/assets/mova_3_phase_rail.glb +0 -0
  185. package/src/assets/mova_3_phase_rail.json +0 -86
  186. package/src/assets/mova_recessed_luminaire.glb +0 -0
  187. package/src/assets/mova_recessed_luminaire.json +0 -81
  188. package/src/assets/nessie/gltf-test-no-material.gltf +0 -142
  189. package/src/assets/nessie/gltf-test-no-material_data.bin +0 -0
  190. package/src/assets/nessie/gltf-test-rotation.gltf +0 -142
  191. package/src/assets/nessie/gltf-test-rotation_data.bin +0 -0
  192. package/src/assets/nessie/gltf-test.gltf +0 -178
  193. package/src/assets/nessie/gltf-test_data.bin +0 -0
  194. package/src/assets/nessie/index.json +0 -9
  195. package/src/assets/nessie/nessie_basic.json +0 -24
  196. package/src/assets/nessie/setup.json +0 -12
  197. package/src/assets/scene.json +0 -64
  198. package/src/assets/setup.json +0 -25
  199. package/src/assets/small_cave_1k.hdr +0 -0
  200. package/src/assets/svgTo3D/09aJssVZrjk.env +0 -0
  201. package/src/assets/svgTo3D/09f0zfBQBWK.glb +0 -0
  202. package/src/assets/svgTo3D/9bae062709f8a7803769ba955cde3c4a.jpg +0 -0
  203. package/src/assets/svgTo3D/logo-cyledge.svg +0 -19
  204. package/src/assets/svgTo3D/spec.ts +0 -56
  205. package/src/assets/svgTo3D/svg_assets/09fmVjc59hQ.png +0 -0
  206. package/src/assets/svgTo3D/svg_assets/LDI2apCSOBg7S-QT7pb0EPOreec.woff2 +0 -0
  207. package/src/assets/svgTo3D/svg_assets/unineue_regular.woff2 +0 -0
  208. package/src/assets/svgTo3D/testCube.glb +0 -0
  209. package/src/assets/svgTo3D/testCube001.glb +0 -0
  210. package/src/assets/svgTo3D/testCubeSpec.ts +0 -49
  211. package/src/assets/textures/grass.png +0 -0
  212. package/src/assets/textures/seamless-grunge-scratched-texture.jpg +0 -0
@@ -1,606 +1,648 @@
1
- import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
2
- import { Material } from '@babylonjs/core/Materials/material';
3
- import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
4
- import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
5
- import { Axis, Space } from '@babylonjs/core/Maths/math.axis';
6
- import { Color3 } from '@babylonjs/core/Maths/math.color';
7
- import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
8
- import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
9
- import { Mesh } from '@babylonjs/core/Meshes/mesh';
10
- import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
11
- import { cloneDeep, concat, get, isArray, isNumber, isPlainObject, merge, union } from 'lodash-es';
12
- import {
13
- activateTransformNode as activate,
14
- addToHighlightLayer,
15
- assertTransformNode,
16
- cloneTransformNode,
17
- cloneTransformNodeMaterial,
18
- deactivateTransformNode as deactivate,
19
- getClientRectFromMesh,
20
- injectTransformNodeMetadata,
21
- mapToDottedNodes,
22
- removeFromHighlightLayer,
23
- setMaterial,
24
- setMaterialColor,
25
- setMaterialMetallness,
26
- setMaterialRoughness,
27
- setMaterialTexture
28
- } from '../util/babylonHelper';
29
- import { createImageBitmapFromImgSrc, createImageBitmapFromSvg, mergeMaps } from '../util/resourceHelper';
30
- import { DottedPath } from './dottedPath';
31
- import { ElementParameterizable } from './elementParameterizable';
32
- import { Event } from './event';
33
- import { Parameter } from './parameter';
34
- import { Variant } from './variant';
35
-
36
- /**
37
- * An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
38
- * {@link Variant}.
39
- *
40
- * When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
41
- * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
42
- */
43
- export class Element extends ElementParameterizable {
44
-
45
- protected readonly _dottedNodes: Map<DottedPath, TransformNode> = new Map();
46
-
47
- protected readonly _parameterObservers: Map<string, ParameterObserver[]> = new Map();
48
-
49
- protected _highlightLayer?: HighlightLayer;
50
-
51
- public readonly nodes: TransformNode[] = [];
52
-
53
- /**
54
- * Constructor.
55
- */
56
- public constructor( public readonly variant: Variant,
57
- public readonly name: string ) {
58
- super();
59
- if ( process.env.NODE_ENV?.toLowerCase().includes('dev')) {
60
- this.assertPathDefinitions();
61
- }
62
- const nodes = this.variant.inheritedNodes.map(
63
- node => cloneTransformNode( node, this.filterNode.bind(this) )
64
- );
65
- this.nodes = nodes.filter( node => !!node ) as TransformNode[];
66
- this._dottedNodes = mapToDottedNodes<TransformNode>(
67
- this.nodes,
68
- node => node instanceof TransformNode
69
- );
70
- this.addParameterObservers();
71
- }
72
-
73
- /**
74
- * The {@link DottedPath} in the built tree of {@link Element}s.
75
- * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
76
- */
77
- get dottedPath(): DottedPath {
78
- return DottedPath.create( this.variant.dottedPath ).addPart( this.name );
79
- }
80
-
81
- /**
82
- * The id representing a {@link DottedPath}.
83
- */
84
- get id(): string {
85
- const dottedPath = DottedPath.create( this.dottedPath );
86
- dottedPath.shiftPart(); // remove root
87
- return dottedPath.path;
88
- }
89
-
90
- /**
91
- * The {@link ElementDefinition} of the {@link Element}.
92
- */
93
- get definition(): ElementDefinition {
94
- const definition = this.variant.structureJson.elements![this.name];
95
- if( isArray( definition ) ) {
96
- return {
97
- paths: {
98
- include: definition
99
- }
100
- } as ElementDefinition;
101
- }
102
- return definition as ElementDefinition;
103
- }
104
-
105
- /**
106
- * The {@link PathDefinitions} of the {@link Element}.
107
- */
108
- get pathDefinitions(): PathDefinitions {
109
- return this.definition.paths;
110
- }
111
-
112
- /**
113
- * The {@link TraceableDefinitions} of the {@link Element}.
114
- */
115
- get traceableDefinitions(): TraceableDefinitions {
116
- return this.definition.traceables || {};
117
- }
118
-
119
- /**
120
- * The {@link PaintableDefinition} of the {@link Element}.
121
- */
122
- get paintableDefinitions(): PaintableDefinitions {
123
- return this.definition.paintables || {};
124
- }
125
-
126
- /**
127
- * The inherited {@link ParameterBag}.
128
- * Merges the {@link Variant}'s parameters and those from the {@link Element}.
129
- */
130
- get inheritedParameters(): ParameterBag {
131
- return merge( {}, this.variant.inheritedParameters, this.parameters );
132
- }
133
-
134
- /**
135
- * @protected
136
- */
137
- set highlightLayer(layer: HighlightLayer | undefined) {
138
- this._highlightLayer = layer;
139
- }
140
-
141
- get highlightLayer(): HighlightLayer | undefined {
142
- return this._highlightLayer;
143
- }
144
-
145
- /**
146
- * Destroys this {@link Element} and dispose all nodes.
147
- */
148
- public destroy(): Element {
149
- this.nodes.forEach( node => {
150
- node.dispose();
151
- } );
152
- return this;
153
- }
154
-
155
- /**
156
- * Gets a node by its {@link DottedPath}.
157
- */
158
- public getNode( dottedPath: DottedPathArgument ): TransformNode {
159
- const _dottedPath = DottedPath.create( dottedPath );
160
- const node = this._dottedNodes.get( _dottedPath );
161
- if( !node ) {
162
- throw new Error( `Node with path "${_dottedPath.path}" does not exist for element "${this.id}".` );
163
- }
164
- return node;
165
- }
166
-
167
- /**
168
- * Gets a mesh by its {@link DottedPath}.
169
- */
170
- public async getMesh( dottedPath: DottedPathArgument ): Promise<Mesh|null> {
171
- const node = await this.getNode( dottedPath );
172
- if( node instanceof Mesh ) {
173
- return node;
174
- }
175
- return null;
176
- }
177
-
178
- /**
179
- * Gets a {@link PaintableDefinition} by its name.
180
- */
181
- public getPaintableDefinition( name: string ): PaintableDefinition {
182
- if( !this.paintableDefinitions || !this.paintableDefinitions[name] ) {
183
- throw new Error( `No paintable "${name}" defined for element "${this.id}".` );
184
- }
185
- return this.paintableDefinitions[name];
186
- }
187
-
188
- /**
189
- * Gets a {@link TraceableDefinition} by its name.
190
- */
191
- public getTraceableDefinition( name: string ): TraceableDefinition {
192
- if( !this.traceableDefinitions || !this.traceableDefinitions[name] ) {
193
- throw new Error( `No traceable "${name}" defined to add observer in element "${this.id}".` );
194
- }
195
- return this.traceableDefinitions[name];
196
- }
197
-
198
- /**
199
- * Gets the paintable Node by its name.
200
- */
201
- public getPaintableNode( name: string ): TransformNode {
202
- return this.getNode( this.getPaintableDefinition( name ).path );
203
- }
204
-
205
- /**
206
- * Gets the traceable Node by its name.
207
- */
208
- public getTraceableNode( name: string ): TransformNode {
209
- return this.getNode( this.getTraceableDefinition( name ).path );
210
- }
211
-
212
- /**
213
- * Places the given {@link ParameterBag} in the {@link Element}'s parameters, replaces all patterns in the
214
- * {@link StructureJson} and broadcasts all {@link ParameterObserver}s.
215
- *
216
- * @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
217
- */
218
- public async commitParameters( parameters?: ParameterBag ): Promise<Element> {
219
- if( !parameters ) {
220
- parameters = {};
221
- }
222
- const oldParameters = cloneDeep( this.parameters );
223
- merge( this.parameters, parameters );
224
- // handle parameter observers
225
- let observerPromises: Promise<void | ParameterObserver>[] = [];
226
- for( const parameter in this.parameters ) {
227
- const oldParameterValue = oldParameters[parameter];
228
- const newParameterValue = this.parameters[parameter];
229
- this.variant.assertParameter( this.variant.inheritedParameterDeclaration, parameter, newParameterValue );
230
- if( oldParameterValue === newParameterValue ) {
231
- continue;
232
- }
233
- // parameter changed
234
- const parameterObservers = mergeMaps( this._parameterObservers, this.parameterObservers );
235
- if( parameterObservers.has( parameter ) ) {
236
- const observers = parameterObservers.get( parameter )!;
237
- observerPromises = concat(observerPromises, observers.map( observer => {
238
- const observerResult = observer( this, oldParameterValue, newParameterValue );
239
- return Promise.resolve( observerResult ).then( () => {
240
- this.broadcastEvent( Event.ELEMENT_PARAMETER_COMMITTED,
241
- this, parameter, oldParameterValue, newParameterValue );
242
- } );
243
- } ) );
244
- }
245
- }
246
- await Promise.all( observerPromises );
247
- return this;
248
- }
249
-
250
- /**
251
- * Adds an observer function for camera matrix changes.
252
- * The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
253
- */
254
- public addTraceableObserver( traceable: string, observer: CallableFunction, payload?: any ): Element {
255
- if( payload && !(payload instanceof HTMLElement) ) {
256
- throw new Error( `Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".` );
257
- }
258
- if( !this.traceableDefinitions || !this.traceableDefinitions[traceable] ) {
259
- throw new Error( `No traceable "${traceable}" defined to add observer in element "${this.id}".` );
260
- }
261
- const node = this.getTraceableNode( traceable );
262
- if( !(node instanceof AbstractMesh) ) {
263
- throw new Error( `The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".` );
264
- }
265
- this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add(
266
- ( eventData, eventState ) => {
267
- const position = getClientRectFromMesh( node, this.variant.viewer.scene, this.variant.viewer.canvas );
268
- if( payload ) {
269
- payload.style.top = `${position.top}px`;
270
- payload.style.left = `${position.left}px`;
271
- }
272
- observer( node, position );
273
- }
274
- );
275
- return this;
276
- }
277
-
278
- /**
279
- * Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
280
- */
281
- public drawPaintable( paintable: string, imageSource: CanvasImageSource ): Element {
282
- const paintableDefinition = this.getPaintableDefinition( paintable );
283
- const node = this.getPaintableNode( paintable );
284
- if( !(node instanceof AbstractMesh) ) {
285
- throw new Error( `The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` +
286
- `in element "${this.id}".` );
287
- }
288
- if( node.material && !get( node.metadata, 'dirty.material' ) ) {
289
- cloneTransformNodeMaterial( node );
290
- }
291
- if( !node.material ) {
292
- node.material = new StandardMaterial( `${this.id}.${paintable}.material`, this.variant.viewer.scene );
293
- }
294
- node.material.transparencyMode = Material.MATERIAL_ALPHATESTANDBLEND;
295
- if( !get( node.metadata, 'dirty.material.texture' ) ) {
296
- // inject initial value and mark as dirty
297
- injectTransformNodeMetadata( node, { dirty: { material: { texture: true } } } );
298
- }
299
- const widthAndHeight = {
300
- width: imageSource.width,
301
- height: imageSource.height
302
- };
303
- if( isNumber( paintableDefinition.textureOptions ) ) {
304
- widthAndHeight.width = paintableDefinition.textureOptions;
305
- widthAndHeight.height = paintableDefinition.textureOptions;
306
- } else if( isPlainObject( paintableDefinition.textureOptions ) ) {
307
- widthAndHeight.width = paintableDefinition.textureOptions.width;
308
- widthAndHeight.height = paintableDefinition.textureOptions.height;
309
- }
310
- const texture = new DynamicTexture(
311
- `${this.id}.${paintable}.texture`,
312
- widthAndHeight,
313
- this.variant.viewer.scene,
314
- false
315
- );
316
- setMaterialTexture( node, texture, false );
317
- const ctx = texture.getContext();
318
- // flip texture context horizontal with translate and scale
319
- ctx.drawImage( imageSource, 0, 0);
320
- texture.update( false );
321
- return this;
322
- }
323
-
324
- /**
325
- * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
326
- */
327
- public async drawPaintableFromSvg( paintable: string, svgSource: string ): Promise<Element> {
328
- const source = await createImageBitmapFromSvg( svgSource );
329
- return this.drawPaintable( paintable, source );
330
- }
331
-
332
- /**
333
- * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
334
- */
335
- public async drawPaintableFromImgSrc( paintable: string, imgSource: string ): Promise<Element> {
336
- const source = await createImageBitmapFromImgSrc( imgSource );
337
- return this.drawPaintable( paintable, source );
338
- }
339
-
340
- /**
341
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
342
- */
343
- protected addParameterObservers(): Element {
344
- this._parameterObservers.set( Parameter.VISIBLE, [
345
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
346
- let visible;
347
- try {
348
- visible = Parameter.parseBoolean( newValue );
349
- } catch( e ) {
350
- return;
351
- }
352
- if( visible === true ) {
353
- element.nodes.forEach( node => {
354
- injectTransformNodeMetadata( node, { visibility: node.isEnabled() } );
355
- activate( node );
356
- } );
357
- } else if( visible === false ) {
358
- element.nodes.forEach( node => {
359
- injectTransformNodeMetadata( node, { visibility: node.isEnabled() } );
360
- deactivate( node );
361
- } );
362
- }
363
- }
364
- ] );
365
- this._parameterObservers.set( Parameter.SCALING, [
366
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
367
- // we have to deal just with root nodes here due to relative impacts in a node tree
368
- element.nodes.forEach( node => node.scaling = Parameter.parseScaling( newValue ) );
369
- }
370
- ] );
371
- this._parameterObservers.set( Parameter.MATERIAL, [
372
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
373
- const material = element.variant.getMaterial( newValue.toString() );
374
- element.nodes.forEach( node => {
375
- assertTransformNode(node, (node: TransformNode) => {
376
- if( node instanceof InstancedMesh ) {
377
- throw new Error( `Changing parameter "${Parameter.MATERIAL}" ` +
378
- `of an InstancedMesh is not supported. ` +
379
- `Tried to change node "${node.id}" on element "${element.id}".` );
380
- }
381
- });
382
- setMaterial( node, material );
383
- } );
384
- }
385
- ] );
386
- this._parameterObservers.set( Parameter.MATERIAL_COLOR, [
387
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
388
- const color = Parameter.parseColor( newValue );
389
- element.nodes.forEach( node => {
390
- assertTransformNode(node, (node: TransformNode) => {
391
- if( node instanceof InstancedMesh ) {
392
- throw new Error( `Changing parameter "${Parameter.MATERIAL_COLOR}" ` +
393
- `of an InstancedMesh is not supported. ` +
394
- `Tried to change node "${node.id}" on element "${element.id}".` );
395
- }
396
- });
397
- if( !get( node.metadata, 'dirty.material' ) ) {
398
- cloneTransformNodeMaterial( node );
399
- }
400
- if( !get( node.metadata, 'dirty.material.color' ) ) {
401
- // inject initial value and mark as dirty
402
- injectTransformNodeMetadata( node, { dirty: { material: { color: oldValue } } } );
403
- }
404
- setMaterialColor( node, color );
405
- } );
406
- }
407
- ] );
408
- this._parameterObservers.set( Parameter.MATERIAL_METALLNESS, [
409
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
410
- const metallness = Parameter.parseNumber( newValue );
411
- element.nodes.forEach( node => {
412
- assertTransformNode(node, (node: TransformNode) => {
413
- if( node instanceof InstancedMesh ) {
414
- throw new Error( `Changing parameter "${Parameter.MATERIAL_METALLNESS}" ` +
415
- `of an InstancedMesh is not supported. ` +
416
- `Tried to change node "${node.id}" on element "${element.id}".` );
417
- }
418
- });
419
- if( !get( node.metadata, 'dirty.material' ) ) {
420
- cloneTransformNodeMaterial( node );
421
- }
422
- if( !get( node.metadata, 'dirty.material.metallness' ) ) {
423
- // inject initial value and mark as dirty
424
- injectTransformNodeMetadata( node, { dirty: { material: { metallness: oldValue } } } );
425
- }
426
- setMaterialMetallness( node, metallness );
427
- } );
428
- }
429
- ] );
430
- this._parameterObservers.set( Parameter.MATERIAL_ROUGHNESS, [
431
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
432
- const roughness = Parameter.parseNumber( newValue );
433
- element.nodes.forEach( node => {
434
- assertTransformNode(node, (node: TransformNode) => {
435
- if( node instanceof InstancedMesh ) {
436
- throw new Error( `Changing parameter "${Parameter.MATERIAL_ROUGHNESS}" ` +
437
- `of an InstancedMesh is not supported. ` +
438
- `Tried to change node "${node.id}" on element "${element.id}".` );
439
- }
440
- });
441
- if( !get( node.metadata, 'dirty.material' ) ) {
442
- cloneTransformNodeMaterial( node );
443
- }
444
- if( !get( node.metadata, 'dirty.material.roughness' ) ) {
445
- // inject initial value and mark as dirty
446
- injectTransformNodeMetadata( node, { dirty: { material: { roughness: oldValue } } } );
447
- }
448
- setMaterialRoughness( node, roughness );
449
- } );
450
- }
451
- ] );
452
- this._parameterObservers.set( Parameter.HIGHLIGHT_COLOR, [
453
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
454
- // trigger Parameter.HIGHLIGHTED observers
455
- this._parameterObservers.get( Parameter.HIGHLIGHTED )?.forEach( observer => {
456
- observer( element, !!element.highlighted, !!element.highlighted );
457
- } );
458
- }
459
- ] );
460
- this._parameterObservers.set( Parameter.HIGHLIGHT_ENABLED, [
461
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
462
- let curr;
463
- try {
464
- curr = Parameter.parseBoolean( newValue );
465
- } catch( e ) {
466
- return;
467
- }
468
- switch( curr ) {
469
- case true:
470
- if( element._highlightLayer ) {
471
- break;
472
- }
473
- element.highlightLayer = new HighlightLayer(
474
- DottedPath.create( element.id ).addPart( 'highlight' ).path,
475
- element.variant.viewer.scene
476
- );
477
- break;
478
- case false:
479
- element.highlightLayer?.dispose();
480
- break;
481
- }
482
- }
483
- ] );
484
- this._parameterObservers.set( Parameter.HIGHLIGHTED, [
485
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
486
- if( !element.inheritedParameters[Parameter.HIGHLIGHT_ENABLED] ) {
487
- return;
488
- }
489
- // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
490
- this._parameterObservers.get( Parameter.HIGHLIGHT_ENABLED )?.forEach( observer => {
491
- observer( element, this.parameters[Parameter.HIGHLIGHT_ENABLED], true );
492
- } );
493
- let highlighted;
494
- try {
495
- highlighted = Parameter.parseBoolean( newValue );
496
- } catch( e ) {
497
- return;
498
- }
499
- // Add/Remove meshes to previously created highlight layers.
500
- if( highlighted === true ) {
501
- element.nodes.forEach( node => {
502
- assertTransformNode(node, (node: TransformNode) => {
503
- if( node instanceof InstancedMesh ) {
504
- throw new Error( `Changing parameter "${Parameter.HIGHLIGHTED}" ` +
505
- `of an InstancedMesh is not supported. ` +
506
- `Tried to change node "${node.id}" on element "${element.id}".` );
507
- }
508
- });
509
- let color = Color3.Green();
510
- if( element.inheritedParameters[Parameter.HIGHLIGHT_COLOR] ) {
511
- color = Parameter.parseColor( element.inheritedParameters[Parameter.HIGHLIGHT_COLOR] );
512
- }
513
- if ( typeof element._highlightLayer !== "undefined" ) {
514
- addToHighlightLayer( element._highlightLayer, color, node );
515
- }
516
- } );
517
- } else if( highlighted === false ) {
518
- element.nodes.forEach( node => {
519
- if ( typeof element._highlightLayer !== "undefined" ) {
520
- removeFromHighlightLayer( element._highlightLayer, node );
521
- }
522
- } );
523
- }
524
- }
525
- ] );
526
- this._parameterObservers.set( Parameter.POSITION, [
527
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
528
- // we have to deal just with root nodes here due to relative impacts in a node tree
529
- element.nodes.forEach( node => {
530
- // remember absolute position and reset it before translating
531
- if( !get( node.metadata, 'position' ) ) {
532
- node.metadata.position = node.absolutePosition.clone();
533
- }
534
- node.setAbsolutePosition( node.metadata.position );
535
- // move
536
- const distance = Parameter.parseVector( newValue );
537
- node.translate( Axis.X, distance.x, Space.WORLD );
538
- node.translate( Axis.Y, distance.y, Space.WORLD );
539
- node.translate( Axis.Z, distance.z, Space.WORLD );
540
- } );
541
- }
542
- ] );
543
- this._parameterObservers.set( Parameter.ROTATION, [
544
- ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
545
- // we have to deal just with root nodes here due to relative impacts in a node tree
546
- element.nodes.forEach( node => {
547
- // remember absolute rotation and reset it before translating
548
- if(!get( node.metadata, 'rotation' ) ) {
549
- node.metadata.rotation = node.rotation.clone();
550
- }
551
- node.rotation = node.metadata.rotation;
552
- // rotate
553
- const rotation = Parameter.parseRotation( newValue );
554
- node.rotate( Axis.X, rotation.x, Space.WORLD );
555
- node.rotate( Axis.Y, rotation.y, Space.WORLD );
556
- node.rotate( Axis.Z, rotation.z, Space.WORLD );
557
- } );
558
- }
559
- ] );
560
- return this;
561
- }
562
-
563
- /**
564
- * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
565
- */
566
- protected filterNode( node: TransformNode ): boolean {
567
- let includeTokens: string[] = [];
568
- this.pathDefinitions.include.forEach( dottedPath => {
569
- includeTokens = union( includeTokens, DottedPath.create( dottedPath ).leafTokens );
570
- } );
571
- const shallKeep = ( dp: DottedPath ) => {
572
- const isDirectlyIncluded = !!includeTokens.find( _dp => _dp === dp.path );
573
- const isSubOfIncluded = !!this.pathDefinitions.include.find( _dp => {
574
- return dp.path.startsWith( `${_dp}${DottedPath.DELIMITER}` );
575
- } );
576
- return isDirectlyIncluded || isSubOfIncluded;
577
- };
578
- const shallExclude = ( dp: DottedPath ) => {
579
- if( !this.pathDefinitions.exclude ) {
580
- return false;
581
- }
582
- return !!this.pathDefinitions.exclude.find( _dp => dp.path === _dp );
583
- };
584
- return shallKeep( node.metadata.dottedPath ) && !shallExclude( node.metadata.dottedPath );
585
- }
586
-
587
- /**
588
- * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
589
- * @protected
590
- */
591
- protected assertPathDefinitions() {
592
- const dottedNodes = this.variant.inheritedDottedNodes;
593
- if( dottedNodes.size === 0 ) {
594
- throw new Error( `There are no mapped nodes for element "${this.id}".` );
595
- }
596
- const include = this.pathDefinitions.include.map( path => DottedPath.create( path ).path );
597
- const exclude = this.pathDefinitions.exclude?.map( path => DottedPath.create( path ).path );
598
- const keys = Array.from( dottedNodes.keys() ).map( dottedPath => dottedPath.path );
599
- union<string>( include, exclude ).forEach( path => {
600
- if( keys.indexOf( path ) === -1 ) {
601
- throw new Error( `Node with path "${path}" does not exist for element "${this.id}".` );
602
- }
603
- } );
604
- }
605
-
606
- }
1
+ import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
2
+ import { Light } from '@babylonjs/core/Lights/light';
3
+ import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
4
+ import { Material } from '@babylonjs/core/Materials/material';
5
+ import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
6
+ import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
7
+ import { Axis, Space } from '@babylonjs/core/Maths/math.axis';
8
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
9
+ import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
10
+ import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
11
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
12
+ import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
13
+ import { get, isArray, isNumber, isPlainObject, merge, union } from 'lodash-es';
14
+ import {
15
+ activateTransformNode as activate,
16
+ addToHighlightLayer,
17
+ addToShadowGenerator,
18
+ assertTransformNode,
19
+ cloneTransformNode,
20
+ cloneTransformNodeMaterial,
21
+ deactivateTransformNode as deactivate,
22
+ getClientRectFromMesh,
23
+ injectNodeMetadata,
24
+ mapToDottedNodes,
25
+ removeFromHighlightLayer,
26
+ removeFromShadowGenerator,
27
+ setMaterial,
28
+ setMaterialColor,
29
+ setMaterialMetallness,
30
+ setMaterialRoughness,
31
+ setMaterialTexture,
32
+ setReceiveShadows
33
+ } from '../util/babylonHelper';
34
+ import { createImageBitmapFromImgSrc, createImageBitmapFromSvg } from '../util/resourceHelper';
35
+ import { VariantParameterizable } from './../classes/variantParameterizable';
36
+ import { DottedPath } from './dottedPath';
37
+ import { Parameter } from './parameter';
38
+ import { Variant } from './variant';
39
+
40
+ /**
41
+ * An {@link Element} of a {@link Variant}. Acts as a container for diverse definitions. Lives only in the context of a
42
+ * {@link Variant}.
43
+ *
44
+ * When used in typings, refer to via its alias {@link VariantElement} to prevent name clashes with the web APIs
45
+ * [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) class
46
+ */
47
+ export class Element extends VariantParameterizable {
48
+
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,
59
+ public readonly name: string ) {
60
+ super( variant, name );
61
+ if ( process.env.NODE_ENV?.toLowerCase().includes('dev')) {
62
+ this.assertPathDefinitions();
63
+ }
64
+ const nodes = this.variant.inheritedNodes.map(
65
+ node => cloneTransformNode( node, this.filterNode.bind(this) )
66
+ );
67
+ this.nodes = nodes.filter( node => !!node ) as TransformNode[];
68
+ this._dottedNodes = mapToDottedNodes<TransformNode>(
69
+ this.nodes,
70
+ node => node instanceof TransformNode
71
+ );
72
+ this.addParameterObservers();
73
+ }
74
+
75
+ /**
76
+ * Creates an {@link Element} with given name.
77
+ */
78
+ public static async create( variant: Variant, name: string ): Promise<Element> {
79
+ return new Element( variant, name );
80
+ }
81
+
82
+ /**
83
+ * The {@link DottedPath} in the built tree of {@link Element}s.
84
+ * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
85
+ */
86
+ get dottedPath(): DottedPath {
87
+ return DottedPath.create( this.variant.dottedPath ).addPart( this.name );
88
+ }
89
+
90
+ /**
91
+ * The id representing a {@link DottedPath}.
92
+ */
93
+ get id(): string {
94
+ const dottedPath = DottedPath.create( this.dottedPath );
95
+ dottedPath.shiftPart(); // remove root
96
+ return dottedPath.path;
97
+ }
98
+
99
+ /**
100
+ * The {@link ElementDefinition} of the {@link Element}.
101
+ */
102
+ get definition(): ElementDefinition {
103
+ const definition = this.variant.structureJson.elements![this.name];
104
+ if( isArray( definition ) ) {
105
+ return {
106
+ paths: {
107
+ include: definition
108
+ }
109
+ } as ElementDefinition;
110
+ }
111
+ return definition as ElementDefinition;
112
+ }
113
+
114
+ /**
115
+ * The {@link PathDefinitions} of the {@link Element}.
116
+ */
117
+ get pathDefinitions(): PathDefinitions {
118
+ return this.definition.paths;
119
+ }
120
+
121
+ /**
122
+ * The {@link TraceableDefinitions} of the {@link Element}.
123
+ */
124
+ get traceableDefinitions(): TraceableDefinitions {
125
+ return this.definition.traceables || {};
126
+ }
127
+
128
+ /**
129
+ * The {@link PaintableDefinition} of the {@link Element}.
130
+ */
131
+ get paintableDefinitions(): PaintableDefinitions {
132
+ return this.definition.paintables || {};
133
+ }
134
+
135
+ /**
136
+ * The inherited {@link ParameterBag}.
137
+ * Merges the {@link Variant}'s parameters and those from the {@link Element}.
138
+ */
139
+ get inheritedParameters(): ParameterBag {
140
+ return merge( {}, this.variant.inheritedParameters, this.parameters );
141
+ }
142
+
143
+ /**
144
+ * @protected
145
+ */
146
+ set highlightLayer(layer: HighlightLayer | undefined) {
147
+ this._highlightLayer = layer;
148
+ }
149
+
150
+ get highlightLayer(): HighlightLayer | undefined {
151
+ return this._highlightLayer;
152
+ }
153
+
154
+ /**
155
+ * Destroys this {@link Element} and dispose all nodes.
156
+ */
157
+ public destroy(): Element {
158
+ this.nodes.forEach( node => {
159
+ node.dispose();
160
+ } );
161
+ return this;
162
+ }
163
+
164
+ /**
165
+ * Gets a node by its {@link DottedPath}.
166
+ */
167
+ public getNode( dottedPath: DottedPathArgument ): TransformNode {
168
+ // TODO: fix, currently broken
169
+ const _dottedPath = DottedPath.create( dottedPath );
170
+ const node = this._dottedNodes.get( _dottedPath );
171
+ if( !node ) {
172
+ throw new Error( `Node with path "${_dottedPath.path}" does not exist for element "${this.id}".` );
173
+ }
174
+ return node;
175
+ }
176
+
177
+ /**
178
+ * Gets a mesh by its {@link DottedPath}.
179
+ */
180
+ public async getMesh( dottedPath: DottedPathArgument ): Promise<Mesh|null> {
181
+ const node = await this.getNode( dottedPath );
182
+ if( node instanceof Mesh ) {
183
+ return node;
184
+ }
185
+ return null;
186
+ }
187
+
188
+ /**
189
+ * Gets a {@link PaintableDefinition} by its name.
190
+ */
191
+ public getPaintableDefinition( name: string ): PaintableDefinition {
192
+ if( !this.paintableDefinitions || !this.paintableDefinitions[name] ) {
193
+ throw new Error( `No paintable "${name}" defined for element "${this.id}".` );
194
+ }
195
+ return this.paintableDefinitions[name];
196
+ }
197
+
198
+ /**
199
+ * Gets a {@link TraceableDefinition} by its name.
200
+ */
201
+ public getTraceableDefinition( name: string ): TraceableDefinition {
202
+ if( !this.traceableDefinitions || !this.traceableDefinitions[name] ) {
203
+ throw new Error( `No traceable "${name}" defined to add observer in element "${this.id}".` );
204
+ }
205
+ return this.traceableDefinitions[name];
206
+ }
207
+
208
+ /**
209
+ * Gets the paintable Node by its name.
210
+ */
211
+ public getPaintableNode( name: string ): TransformNode {
212
+ return this.getNode( this.getPaintableDefinition( name ).path );
213
+ }
214
+
215
+ /**
216
+ * Gets the traceable Node by its name.
217
+ */
218
+ public getTraceableNode( name: string ): TransformNode {
219
+ return this.getNode( this.getTraceableDefinition( name ).path );
220
+ }
221
+
222
+ /**
223
+ * @see {@link VariantParameterizable.commitParameters}
224
+ * @emit {@link Event.ELEMENT_PARAMETER_COMMITTED}
225
+ */
226
+ public async commitParameters( parameters?: ParameterBag ): Promise<VariantParameterizable> {
227
+ return super.commitParameters( parameters );
228
+ }
229
+
230
+ /**
231
+ * Adds an observer function for camera matrix changes.
232
+ * The `observer` gets 2 parameters: the `AbstractMesh` and a `ClientRect` object.
233
+ */
234
+ public addTraceableObserver( traceable: string, observer: CallableFunction, payload?: any ): Element {
235
+ if( payload && !(payload instanceof HTMLElement) ) {
236
+ throw new Error( `Payload for camera observer must be an instance of "HTMLElement" in element "${this.id}".` );
237
+ }
238
+ if( !this.traceableDefinitions || !this.traceableDefinitions[traceable] ) {
239
+ throw new Error( `No traceable "${traceable}" defined to add observer in element "${this.id}".` );
240
+ }
241
+ const node = this.getTraceableNode( traceable );
242
+ if( !(node instanceof AbstractMesh) ) {
243
+ throw new Error( `The path must be an instance of "AbstractMesh" for camera observer in element "${this.id}".` );
244
+ }
245
+ this.variant.viewer.sceneManager.activeCamera.onViewMatrixChangedObservable.add(
246
+ ( eventData, eventState ) => {
247
+ const position = getClientRectFromMesh( node, this.variant.viewer.scene, this.variant.viewer.canvas );
248
+ if( payload ) {
249
+ payload.style.top = `${position.top}px`;
250
+ payload.style.left = `${position.left}px`;
251
+ }
252
+ observer( node, position );
253
+ }
254
+ );
255
+ return this;
256
+ }
257
+
258
+ /**
259
+ * Draws a `ImageBitmap` or `OffscreenCanvas` onto a `paintable` defined via {@link PaintableDefinition}.
260
+ */
261
+ public drawPaintable( paintable: string, imageSource: CanvasImageSource ): Element {
262
+ const paintableDefinition = this.getPaintableDefinition( paintable );
263
+ const node = this.getPaintableNode( paintable );
264
+ if( !(node instanceof AbstractMesh) ) {
265
+ throw new Error( `The path must be an instance of "AbstractMesh" for paintable "${paintable}" ` +
266
+ `in element "${this.id}".` );
267
+ }
268
+ if( node.material && !get( node.metadata, 'dirty.material' ) ) {
269
+ cloneTransformNodeMaterial( node );
270
+ }
271
+ if( !node.material ) {
272
+ node.material = new StandardMaterial( `${this.id}.${paintable}.material`, this.variant.viewer.scene );
273
+ }
274
+ node.material.transparencyMode = Material.MATERIAL_ALPHATESTANDBLEND;
275
+ if( !get( node.metadata, 'dirty.material.texture' ) ) {
276
+ // inject initial value and mark as dirty
277
+ injectNodeMetadata( node, { dirty: { material: { texture: true } } } );
278
+ }
279
+ const widthAndHeight = {
280
+ width: imageSource.width,
281
+ height: imageSource.height
282
+ };
283
+ if( isNumber( paintableDefinition.textureOptions ) ) {
284
+ widthAndHeight.width = paintableDefinition.textureOptions;
285
+ widthAndHeight.height = paintableDefinition.textureOptions;
286
+ } else if( isPlainObject( paintableDefinition.textureOptions ) ) {
287
+ widthAndHeight.width = paintableDefinition.textureOptions.width;
288
+ widthAndHeight.height = paintableDefinition.textureOptions.height;
289
+ }
290
+ const texture = new DynamicTexture(
291
+ `${this.id}.${paintable}.texture`,
292
+ widthAndHeight,
293
+ this.variant.viewer.scene,
294
+ false
295
+ );
296
+ setMaterialTexture( node, texture, false );
297
+ const ctx = texture.getContext();
298
+ // flip texture context horizontal with translate and scale
299
+ ctx.drawImage( imageSource, 0, 0);
300
+ texture.update( false );
301
+ return this;
302
+ }
303
+
304
+ /**
305
+ * Draws a SVG string onto a `paintable` defined via {@link PaintableDefinition}.
306
+ */
307
+ public async drawPaintableFromSvg( paintable: string, svgSource: string ): Promise<Element> {
308
+ const source = await createImageBitmapFromSvg( svgSource );
309
+ return this.drawPaintable( paintable, source );
310
+ }
311
+
312
+ /**
313
+ * Draws an Image from source (URL/URI) onto a `paintable` defined via {@link PaintableDefinition}.
314
+ */
315
+ public async drawPaintableFromImgSrc( paintable: string, imgSource: string ): Promise<Element> {
316
+ const source = await createImageBitmapFromImgSrc( imgSource );
317
+ return this.drawPaintable( paintable, source );
318
+ }
319
+
320
+ /**
321
+ * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
322
+ */
323
+ protected addParameterObservers(): Element {
324
+ this._parameterObservers.set( Parameter.VISIBLE, [
325
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
326
+ let visible;
327
+ try {
328
+ visible = Parameter.parseBoolean( newValue );
329
+ } catch( e ) {
330
+ return;
331
+ }
332
+ if( visible === true ) {
333
+ element.nodes.forEach( node => {
334
+ injectNodeMetadata( node, { visibility: node.isEnabled() } );
335
+ activate( node );
336
+ } );
337
+ } else if( visible === false ) {
338
+ element.nodes.forEach( node => {
339
+ injectNodeMetadata( node, { visibility: node.isEnabled() } );
340
+ deactivate( node );
341
+ } );
342
+ }
343
+ }
344
+ ] );
345
+ this._parameterObservers.set( Parameter.SCALING, [
346
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
347
+ // we have to deal just with root nodes here due to relative impacts in a node tree
348
+ element.nodes.forEach( node => node.scaling = Parameter.parseScaling( newValue ) );
349
+ }
350
+ ] );
351
+ this._parameterObservers.set( Parameter.MATERIAL, [
352
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
353
+ const material = element.variant.getMaterial( newValue.toString() );
354
+ element.nodes.forEach( node => {
355
+ assertTransformNode(node, (node: AbstractMesh) => {
356
+ if( node instanceof InstancedMesh ) {
357
+ throw new Error( `Changing parameter "${Parameter.MATERIAL}" ` +
358
+ `of an InstancedMesh is not supported. ` +
359
+ `Tried to change node "${node.id}" on element "${element.id}".` );
360
+ }
361
+ });
362
+ setMaterial( node, material );
363
+ } );
364
+ }
365
+ ] );
366
+ this._parameterObservers.set( Parameter.MATERIAL_COLOR, [
367
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
368
+ const color = Parameter.parseColor( newValue );
369
+ element.nodes.forEach( node => {
370
+ assertTransformNode(node, (node: AbstractMesh) => {
371
+ if( node instanceof InstancedMesh ) {
372
+ throw new Error( `Changing parameter "${Parameter.MATERIAL_COLOR}" ` +
373
+ `of an InstancedMesh is not supported. ` +
374
+ `Tried to change node "${node.id}" on element "${element.id}".` );
375
+ }
376
+ });
377
+ if( !get( node.metadata, 'dirty.material' ) ) {
378
+ cloneTransformNodeMaterial( node );
379
+ }
380
+ if( !get( node.metadata, 'dirty.material.color' ) ) {
381
+ // inject initial value and mark as dirty
382
+ injectNodeMetadata( node, { dirty: { material: { color: oldValue } } } );
383
+ }
384
+ setMaterialColor( node, color );
385
+ } );
386
+ }
387
+ ] );
388
+ this._parameterObservers.set( Parameter.MATERIAL_METALLNESS, [
389
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
390
+ const metallness = Parameter.parseNumber( newValue );
391
+ element.nodes.forEach( node => {
392
+ assertTransformNode(node, (node: AbstractMesh) => {
393
+ if( node instanceof InstancedMesh ) {
394
+ throw new Error( `Changing parameter "${Parameter.MATERIAL_METALLNESS}" ` +
395
+ `of an InstancedMesh is not supported. ` +
396
+ `Tried to change node "${node.id}" on element "${element.id}".` );
397
+ }
398
+ });
399
+ if( !get( node.metadata, 'dirty.material' ) ) {
400
+ cloneTransformNodeMaterial( node );
401
+ }
402
+ if( !get( node.metadata, 'dirty.material.metallness' ) ) {
403
+ // inject initial value and mark as dirty
404
+ injectNodeMetadata( node, { dirty: { material: { metallness: oldValue } } } );
405
+ }
406
+ setMaterialMetallness( node, metallness );
407
+ } );
408
+ }
409
+ ] );
410
+ this._parameterObservers.set( Parameter.MATERIAL_ROUGHNESS, [
411
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
412
+ const roughness = Parameter.parseNumber( newValue );
413
+ element.nodes.forEach( node => {
414
+ assertTransformNode(node, (node: AbstractMesh) => {
415
+ if( node instanceof InstancedMesh ) {
416
+ throw new Error( `Changing parameter "${Parameter.MATERIAL_ROUGHNESS}" ` +
417
+ `of an InstancedMesh is not supported. ` +
418
+ `Tried to change node "${node.id}" on element "${element.id}".` );
419
+ }
420
+ });
421
+ if( !get( node.metadata, 'dirty.material' ) ) {
422
+ cloneTransformNodeMaterial( node );
423
+ }
424
+ if( !get( node.metadata, 'dirty.material.roughness' ) ) {
425
+ // inject initial value and mark as dirty
426
+ injectNodeMetadata( node, { dirty: { material: { roughness: oldValue } } } );
427
+ }
428
+ setMaterialRoughness( node, roughness );
429
+ } );
430
+ }
431
+ ] );
432
+ this._parameterObservers.set( Parameter.HIGHLIGHT_COLOR, [
433
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
434
+ // trigger Parameter.HIGHLIGHTED observers
435
+ this._parameterObservers.get( Parameter.HIGHLIGHTED )?.forEach( observer => {
436
+ observer( element, !!element.highlighted, !!element.highlighted );
437
+ } );
438
+ }
439
+ ] );
440
+ this._parameterObservers.set( Parameter.HIGHLIGHT_ENABLED, [
441
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
442
+ let curr;
443
+ try {
444
+ curr = Parameter.parseBoolean( newValue );
445
+ } catch( e ) {
446
+ return;
447
+ }
448
+ switch( curr ) {
449
+ case true:
450
+ if( element._highlightLayer ) {
451
+ break;
452
+ }
453
+ element.highlightLayer = new HighlightLayer(
454
+ DottedPath.create( element.id ).addPart( 'highlight' ).path,
455
+ element.variant.viewer.scene
456
+ );
457
+ break;
458
+ case false:
459
+ element.highlightLayer?.dispose();
460
+ break;
461
+ }
462
+ }
463
+ ] );
464
+ this._parameterObservers.set( Parameter.HIGHLIGHTED, [
465
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
466
+ if( !element.inheritedParameters[Parameter.HIGHLIGHT_ENABLED] ) {
467
+ return;
468
+ }
469
+ // trigger Parameter.HIGHLIGHT_ENABLED observers to ensure HighlightLayer
470
+ this._parameterObservers.get( Parameter.HIGHLIGHT_ENABLED )?.forEach( observer => {
471
+ observer( element, this.parameters[Parameter.HIGHLIGHT_ENABLED], true );
472
+ } );
473
+ let highlighted;
474
+ try {
475
+ highlighted = Parameter.parseBoolean( newValue );
476
+ } catch( e ) {
477
+ return;
478
+ }
479
+ // Add/Remove meshes to previously created highlight layers.
480
+ if( highlighted === true ) {
481
+ element.nodes.forEach( node => {
482
+ assertTransformNode(node, (node: AbstractMesh) => {
483
+ if( node instanceof InstancedMesh ) {
484
+ throw new Error( `Changing parameter "${Parameter.HIGHLIGHTED}" ` +
485
+ `of an InstancedMesh is not supported. ` +
486
+ `Tried to change node "${node.id}" on element "${element.id}".` );
487
+ }
488
+ });
489
+ let color = Color3.Green();
490
+ if( element.inheritedParameters[Parameter.HIGHLIGHT_COLOR] ) {
491
+ color = Parameter.parseColor( element.inheritedParameters[Parameter.HIGHLIGHT_COLOR] );
492
+ }
493
+ if ( typeof element._highlightLayer !== "undefined" ) {
494
+ addToHighlightLayer( element._highlightLayer, color, node );
495
+ }
496
+ } );
497
+ } else if( highlighted === false ) {
498
+ element.nodes.forEach( node => {
499
+ if ( typeof element._highlightLayer !== "undefined" ) {
500
+ removeFromHighlightLayer( element._highlightLayer, node );
501
+ }
502
+ } );
503
+ }
504
+ }
505
+ ] );
506
+ this._parameterObservers.set( Parameter.POSITION, [
507
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
508
+ // we have to deal just with root nodes here due to relative impacts in a node tree
509
+ element.nodes.forEach( node => {
510
+ // remember absolute position and reset it before translating
511
+ if( !get( node.metadata, 'position' ) ) {
512
+ node.metadata.position = node.absolutePosition.clone();
513
+ }
514
+ node.setAbsolutePosition( node.metadata.position );
515
+ // move
516
+ const distance = Parameter.parseVector( newValue );
517
+ node.translate( Axis.X, distance.x, Space.WORLD );
518
+ node.translate( Axis.Y, distance.y, Space.WORLD );
519
+ node.translate( Axis.Z, distance.z, Space.WORLD );
520
+ } );
521
+ }
522
+ ] );
523
+ this._parameterObservers.set( Parameter.ROTATION, [
524
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
525
+ // we have to deal just with root nodes here due to relative impacts in a node tree
526
+ element.nodes.forEach( node => {
527
+ // remember absolute rotation and reset it before translating
528
+ if(!get( node.metadata, 'rotation' ) ) {
529
+ node.metadata.rotation = node.rotation.clone();
530
+ }
531
+ node.rotation = node.metadata.rotation;
532
+ // rotate
533
+ const rotation = Parameter.parseRotation( newValue );
534
+ node.rotate( Axis.X, rotation.x, Space.WORLD );
535
+ node.rotate( Axis.Y, rotation.y, Space.WORLD );
536
+ node.rotate( Axis.Z, rotation.z, Space.WORLD );
537
+ } );
538
+ }
539
+ ] );
540
+ this._parameterObservers.set( Parameter.CAST_SHADOW, [
541
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
542
+ let castShadow;
543
+ try {
544
+ castShadow = Parameter.parseBoolean( newValue );
545
+ } catch( e ) {
546
+ return;
547
+ }
548
+ let lightCsl = element.inheritedParameters[Parameter.CAST_SHADOW_FROM_LIGHTS];
549
+ if( ! lightCsl ) {
550
+ lightCsl = element.variant.inheritedViewerLights.map( l => l.name ).join( ',' );
551
+ }
552
+ if( castShadow === true ) {
553
+ await this.castShadowValueHandler( lightCsl, addToShadowGenerator );
554
+ }
555
+ if( castShadow === false ) {
556
+ await this.castShadowValueHandler( lightCsl, removeFromShadowGenerator );
557
+ }
558
+ }
559
+ ] );
560
+ this._parameterObservers.set( Parameter.CAST_SHADOW_FROM_LIGHTS, [
561
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
562
+ // TODO: Possible performance issue in combination with CAST_SHADOW, since both observers are initially called
563
+ // when CAST_SHADOW and CAST_SHADOW_FROM_LIGHTS are defined in the spec.
564
+ const lightCsl = element.variant.inheritedViewerLights.map( l => l.name ).join( ',' );
565
+ // cleanup all shadow generators
566
+ await this.castShadowValueHandler( lightCsl, removeFromShadowGenerator );
567
+ if( element.castShadow === true ) {
568
+ // if newValue is undefined or '' then set newValue to lightCsl (use all lights)
569
+ if( !newValue ) {
570
+ newValue = lightCsl;
571
+ }
572
+ await this.castShadowValueHandler( newValue, addToShadowGenerator );
573
+ }
574
+ }
575
+ ] );
576
+ this._parameterObservers.set( Parameter.RECEIVE_SHADOWS, [
577
+ async ( element: Element, oldValue: ParameterValue, newValue: ParameterValue ) => {
578
+ element.nodes.forEach( node => setReceiveShadows( node, Parameter.parseBoolean( newValue ) ) );
579
+ }
580
+ ] );
581
+ return this;
582
+ }
583
+
584
+ /**
585
+ * Filter for nodes that are not defined or excluded in the `pathDefinitions`.
586
+ */
587
+ protected filterNode( node: TransformNode ): boolean {
588
+ let includeTokens: string[] = [];
589
+ this.pathDefinitions.include.forEach( dottedPath => {
590
+ includeTokens = union( includeTokens, DottedPath.create( dottedPath ).leafTokens );
591
+ } );
592
+ const shallKeep = ( dp: DottedPath ) => {
593
+ const isDirectlyIncluded = !!includeTokens.find( _dp => _dp === dp.path );
594
+ const isSubOfIncluded = !!this.pathDefinitions.include.find( _dp => {
595
+ return dp.path.startsWith( `${_dp}${DottedPath.DELIMITER}` );
596
+ } );
597
+ return isDirectlyIncluded || isSubOfIncluded;
598
+ };
599
+ const shallExclude = ( dp: DottedPath ) => {
600
+ if( !this.pathDefinitions.exclude ) {
601
+ return false;
602
+ }
603
+ return !!this.pathDefinitions.exclude.find( _dp => dp.path === _dp );
604
+ };
605
+ return shallKeep( node.metadata.dottedPath ) && !shallExclude( node.metadata.dottedPath );
606
+ }
607
+
608
+ /**
609
+ * Asserts whether all {@link pathDefinitions} link to a valid `Node` path.
610
+ * @protected
611
+ */
612
+ protected assertPathDefinitions() {
613
+ const dottedNodes = this.variant.inheritedDottedNodes;
614
+ if( dottedNodes.size === 0 ) {
615
+ throw new Error( `There are no mapped nodes for element "${this.id}".` );
616
+ }
617
+ const include = this.pathDefinitions.include.map( path => DottedPath.create( path ).path );
618
+ const exclude = this.pathDefinitions.exclude?.map( path => DottedPath.create( path ).path );
619
+ const keys = Array.from( dottedNodes.keys() ).map( dottedPath => dottedPath.path );
620
+ union<string>( include, exclude ).forEach( path => {
621
+ if( keys.indexOf( path ) === -1 ) {
622
+ throw new Error( `Node with path "${path}" does not exist for element "${this.id}".` );
623
+ }
624
+ } );
625
+ }
626
+
627
+ /**
628
+ * Handles callback for given light parameter.
629
+ */
630
+ private async castShadowValueHandler( lightCsl: ParameterValue, mutator: CallableFunction ) {
631
+ let lights: Light[] = [];
632
+ for( const lightName of Parameter.parseCommaSeparatedList( lightCsl ) ) {
633
+ const viewerLight = await this.variant.getViewerLight( lightName );
634
+ if( viewerLight ) {
635
+ lights.push( viewerLight.light );
636
+ }
637
+ }
638
+ const shadowGenerators = lights
639
+ .map( light => light?.getShadowGenerator() as ShadowGenerator )
640
+ .filter( Boolean );
641
+ shadowGenerators.forEach( generator => {
642
+ this.nodes.forEach( node => {
643
+ mutator( generator, node );
644
+ } );
645
+ } );
646
+ }
647
+
648
+ }