@combeenation/3d-viewer 4.0.0-beta1 → 4.0.0-beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +1 -0
  2. package/dist/lib-cjs/api/classes/element.d.ts +12 -16
  3. package/dist/lib-cjs/api/classes/element.js +129 -194
  4. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  5. package/dist/lib-cjs/api/classes/event.d.ts +1 -15
  6. package/dist/lib-cjs/api/classes/event.js +1 -15
  7. package/dist/lib-cjs/api/classes/event.js.map +1 -1
  8. package/dist/lib-cjs/api/classes/parameter.d.ts +7 -101
  9. package/dist/lib-cjs/api/classes/parameter.js +21 -141
  10. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  11. package/dist/lib-cjs/api/classes/parameterObservable.js +36 -11
  12. package/dist/lib-cjs/api/classes/parameterObservable.js.map +1 -1
  13. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +2 -2
  14. package/dist/lib-cjs/api/classes/placementAnimation.js +0 -11
  15. package/dist/lib-cjs/api/classes/placementAnimation.js.map +1 -1
  16. package/dist/lib-cjs/api/classes/variant.d.ts +14 -48
  17. package/dist/lib-cjs/api/classes/variant.js +56 -320
  18. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/variantInstance.d.ts +1 -5
  20. package/dist/lib-cjs/api/classes/variantInstance.js +0 -10
  21. package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
  22. package/dist/lib-cjs/api/classes/viewer.d.ts +3 -6
  23. package/dist/lib-cjs/api/classes/viewer.js +59 -133
  24. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  25. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +1 -5
  26. package/dist/lib-cjs/api/internal/sceneSetup.js +71 -75
  27. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  28. package/dist/lib-cjs/api/util/babylonHelper.d.ts +4 -54
  29. package/dist/lib-cjs/api/util/babylonHelper.js +8 -160
  30. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  31. package/dist/lib-cjs/api/util/globalTypes.d.ts +12 -62
  32. package/dist/lib-cjs/api/util/resourceHelper.d.ts +8 -8
  33. package/dist/lib-cjs/api/util/resourceHelper.js +24 -63
  34. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  35. package/dist/lib-cjs/index.d.ts +22 -24
  36. package/dist/lib-cjs/index.js +38 -42
  37. package/dist/lib-cjs/index.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/api/classes/element.ts +117 -146
  40. package/src/api/classes/{parameterizable.ts → elementParameterizable.ts} +1 -12
  41. package/src/api/classes/event.ts +1 -16
  42. package/src/api/classes/parameter.ts +22 -153
  43. package/src/api/classes/parameterObservable.ts +31 -9
  44. package/src/api/classes/placementAnimation.ts +0 -10
  45. package/src/api/classes/variant.ts +51 -187
  46. package/src/api/classes/variantInstance.ts +1 -8
  47. package/src/api/classes/viewer.ts +11 -68
  48. package/src/api/internal/sceneSetup.ts +109 -99
  49. package/src/api/util/babylonHelper.ts +10 -171
  50. package/src/api/util/globalTypes.ts +14 -71
  51. package/src/api/util/resourceHelper.ts +23 -31
  52. package/src/dev.ts +6 -2
  53. package/src/index.ts +23 -27
  54. package/src/pagesconfig.json +13 -8
  55. package/src/api/classes/variantParameterizable.ts +0 -73
  56. package/src/api/classes/viewerLight.ts +0 -330
  57. package/src/api/util/stringHelper.ts +0 -26
@@ -1,330 +0,0 @@
1
- import { PointLight } from '@babylonjs/core';
2
- import { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight';
3
- import { Light } from '@babylonjs/core/Lights/light';
4
- import { ShadowLight } from '@babylonjs/core/Lights/shadowLight';
5
- import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
6
- import '@babylonjs/core/Lights/Shadows/shadowGeneratorSceneComponent';
7
- import { Vector3 } from '@babylonjs/core/Maths/math.vector';
8
- import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
9
- import '@babylonjs/loaders/glTF/2.0/Extensions/KHR_lights_punctual';
10
- import { get, isEmpty, isString, set } from 'lodash-es';
11
- import {
12
- cloneNodeWithParents,
13
- disableNodeWithParents,
14
- enableNodeWithParents,
15
- getRootNode,
16
- injectNodeMetadata,
17
- moveTransformNode,
18
- rotateTransformNode
19
- } from './../util/babylonHelper';
20
- import { DottedPath } from './dottedPath';
21
- import { Parameter } from './parameter';
22
- import { Variant } from './variant';
23
- import { VariantParameterizable } from './variantParameterizable';
24
-
25
- /**
26
- * A {@link ViewerLight} of a {@link Variant}. Acts as a container for BabylonJS lights. Lives only in the context of a
27
- * {@link Variant}.
28
- */
29
- export class ViewerLight extends VariantParameterizable {
30
-
31
- protected _light: Light | undefined;
32
-
33
- /**
34
- * Constructor.
35
- */
36
- protected constructor( public readonly variant: Variant,
37
- public readonly name: string ) {
38
- super( variant, name );
39
- this.addParameterObservers();
40
- }
41
-
42
- /**
43
- * Creates a {@link ViewerLight} with given name.
44
- */
45
- public static async create( variant: Variant, name: string ): Promise<ViewerLight> {
46
- const viewerLight = new ViewerLight( variant, name );
47
- viewerLight._light = await viewerLight.createBabylonLightFromDefinition( viewerLight.definition );
48
- return viewerLight;
49
- }
50
-
51
- /**
52
- * The wrapped Light.
53
- */
54
- get light(): Light {
55
- if( !this._light ) {
56
- throw new Error( `Light for ViewerLight "${this.name}" has not been properly initialized.` );
57
- }
58
- return this._light!;
59
- }
60
-
61
- /**
62
- * The {@link DottedPath} in the built tree of {@link ViewerLight}s.
63
- * E.g. "_.top-1.sub-2.sub-sub-3.el-1"
64
- */
65
- get dottedPath(): DottedPath {
66
- return DottedPath.create( this.variant.dottedPath ).addPart( this.name );
67
- }
68
-
69
- /**
70
- * The id representing a {@link DottedPath}.
71
- */
72
- get id(): string {
73
- const dottedPath = DottedPath.create( this.dottedPath );
74
- dottedPath.shiftPart(); // remove root
75
- return dottedPath.path;
76
- }
77
-
78
- /**
79
- * The {@link LightDefinition} of the {@link ViewerLight}.
80
- */
81
- get definition(): LightDefinition {
82
- const definition = this.variant.structureJson.lights![this.name];
83
- if( isString( definition ) ) {
84
- return {
85
- type: 'baked',
86
- path: definition
87
- } as LightDefinition;
88
- }
89
- return definition as LightDefinition;
90
- }
91
-
92
- /**
93
- * The type of the {@link ViewerLight}'s light.
94
- */
95
- get type(): string {
96
- return this.light.constructor.name.replace( /light/i, '' ).toLowerCase();
97
- }
98
-
99
- /**
100
- * @see {@link VariantParameterizable.commitParameters}
101
- * @emit {@link Event.VIEWER_LIGHT_PARAMETER_COMMITTED}
102
- */
103
- public async commitParameters( parameters?: ParameterBag ): Promise<VariantParameterizable> {
104
- return super.commitParameters( parameters );
105
- }
106
-
107
- /**
108
- * Adds the default {@link ParameterObserver}s which are called every time {@link commitParameters} is called.
109
- */
110
- protected addParameterObservers(): ViewerLight {
111
- this._parameterObservers.set( Parameter.VISIBLE, [
112
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
113
- let visible;
114
- try {
115
- visible = Parameter.parseBoolean( newValue );
116
- } catch( e ) {
117
- return;
118
- }
119
- if( visible === true ) {
120
- enableNodeWithParents( light.light );
121
- } else if( visible === false ) {
122
- disableNodeWithParents( light.light );
123
- }
124
- }
125
- ] );
126
- this._parameterObservers.set( Parameter.INTENSITY, [
127
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
128
- set( light.light, 'intensity', Parameter.parseNumber( newValue ) );
129
- }
130
- ] );
131
- this._parameterObservers.set( Parameter.POSITION, [
132
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
133
- // we have to deal just with root nodes here due to relative impacts in a node tree
134
- const rootNode = getRootNode( light.light );
135
- if( rootNode instanceof TransformNode ) {
136
- moveTransformNode( rootNode, Parameter.parseVector( newValue ) );
137
- }
138
- }
139
- ] );
140
- this._parameterObservers.set( Parameter.ROTATION, [
141
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
142
- // The current implementation (rotating around coordinates 0,0,0) implicitly mutates the position of a node.
143
- // Since a user expects the rotation after the positioning, we have to manually fire the position observers.
144
- // Without calling these observers, the pivot and the position of a node is initially the same before rotating,
145
- // so there is no rotation happening at all.
146
- if( Parameter.POSITION in light.inheritedParameters ) {
147
- await light.commitParameter( Parameter.POSITION, light.inheritedParameters[Parameter.POSITION] );
148
- }
149
- // we have to deal just with root nodes here due to relative impacts in a node tree
150
- const rootNode = getRootNode( light.light );
151
- if( rootNode instanceof TransformNode ) {
152
- rotateTransformNode( rootNode, Parameter.parseRotation( newValue ) );
153
- }
154
- }
155
- ] );
156
- this._parameterObservers.set( Parameter.SCALING, [
157
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
158
- // we have to deal just with root nodes here due to relative impacts in a node tree
159
- const rootNode = getRootNode( light.light );
160
- if( rootNode instanceof TransformNode ) {
161
- rootNode.scaling = Parameter.parseScaling( newValue );
162
- }
163
- }
164
- ] );
165
- this._parameterObservers.set( Parameter.DIRECTION, [
166
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
167
- if(
168
- (light.light instanceof ShadowLight && !(light.light instanceof PointLight))
169
- || light.light instanceof HemisphericLight
170
- ) {
171
- set( light.light, 'direction', Parameter.parseVector( newValue ) );
172
- }
173
- }
174
- ] );
175
- this._parameterObservers.set( Parameter.ANGLE, [
176
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
177
- if( light.light.getClassName() === 'SpotLight' ) {
178
- set( light.light, 'angle', Parameter.parseNumber( newValue ) );
179
- }
180
- }
181
- ] );
182
- this._parameterObservers.set( Parameter.EXPONENT, [
183
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
184
- if( light.light.getClassName() === 'SpotLight' ) {
185
- set( light.light, 'exponent', Parameter.parseNumber( newValue ) );
186
- }
187
- }
188
- ] );
189
- this._parameterObservers.set( Parameter.DIFFUSE, [
190
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
191
- set( light.light, 'diffuse', Parameter.parseColor( newValue ) );
192
- }
193
- ] );
194
- this._parameterObservers.set( Parameter.SPECULAR, [
195
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
196
- set( light.light, 'specular', Parameter.parseColor( newValue ) );
197
- }
198
- ] );
199
- return this;
200
- }
201
-
202
- /**
203
- * @param definition
204
- * @protected
205
- */
206
- protected async createBabylonLightFromDefinition( definition: LightDefinition ): Promise<Light> {
207
- const parameters = Parameter.parseFromDeclarations( Parameter.declarations, this.inheritedParameters );
208
- const scene = this.variant.viewer.scene;
209
- let lightId = this.id;
210
- let babylonLight;
211
- switch( definition.type ) {
212
- case 'baked':
213
- if( !definition['path'] ) {
214
- throw new Error( `The light "${lightId}" of type "${definition.type}" needs a "path".` );
215
- }
216
- const bakedLight = this.variant.inheritedLights.find( l => l.metadata.dottedPath.path === definition['path'] );
217
- if( bakedLight ) {
218
- lightId = bakedLight.metadata.dottedPath.clone().unshiftPart( this.id ).path;
219
- babylonLight = cloneNodeWithParents( bakedLight ) as Light;
220
- babylonLight!.name = lightId;
221
- babylonLight!.id = lightId;
222
- } else {
223
- throw new Error( `No light found for path "${definition['path']}" in ViewerLight "${lightId}".` );
224
- }
225
- break;
226
- case 'hemispheric':
227
- if( !parameters['direction'] ) {
228
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
229
- }
230
- // @ts-ignore
231
- const hemisphericLightModule = await import(/* webpackChunkName: "hemispheric-light" */ '@babylonjs/core/Lights/hemisphericLight');
232
- babylonLight = new hemisphericLightModule.HemisphericLight(
233
- lightId,
234
- parameters['direction'],
235
- scene
236
- );
237
- break;
238
- case 'point':
239
- // @ts-ignore
240
- const pointLightModule = await import(/* webpackChunkName: "point-light" */ '@babylonjs/core/Lights/pointLight');
241
- babylonLight = new pointLightModule.PointLight(
242
- lightId,
243
- Vector3.Zero(), // position is set via parent TransformNode
244
- scene
245
- );
246
- break;
247
- case 'directional':
248
- if( !parameters['direction'] ) {
249
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
250
- }
251
- // @ts-ignore
252
- const directionalLightModule = await import(/* webpackChunkName: "directional-light" */ '@babylonjs/core/Lights/directionalLight');
253
- babylonLight = new directionalLightModule.DirectionalLight(
254
- lightId,
255
- parameters['direction'],
256
- scene
257
- );
258
- break;
259
- case 'spot':
260
- if( !parameters['direction'] ) {
261
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
262
- }
263
- if( !parameters['angle'] ) {
264
- throw new Error( `A ViewerLight of type "${definition.type}" needs an "angle".` );
265
- }
266
- if( !parameters['exponent'] ) {
267
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs an "exponent".` );
268
- }
269
- // @ts-ignore
270
- const spotLightModule = await import(/* webpackChunkName: "spot-light" */ '@babylonjs/core/Lights/spotLight');
271
- babylonLight = new spotLightModule.SpotLight(
272
- lightId,
273
- Vector3.Zero(), // position is set via parent TransformNode
274
- parameters['direction'],
275
- parameters['angle'],
276
- parameters['exponent'],
277
- scene
278
- );
279
- break;
280
- default:
281
- throw new Error( `The type "${definition.type}" for ViewerLight "${lightId}" is not implemented (yet).` );
282
- }
283
- if( !babylonLight ) {
284
- throw new Error( `The Light for ViewerLight "${lightId}" of type "${definition.type}" could no be created ` +
285
- `or found.` );
286
- }
287
- if( !babylonLight.parent ) {
288
- // Create pseudo parent since lights do not implement mutations like "rotation" by itself.
289
- babylonLight.parent = new TransformNode( '__light__', this.variant.viewer.scene, true );
290
- }
291
- injectNodeMetadata( babylonLight.parent, {
292
- variant: this.variant,
293
- variantParameterizable: this
294
- } );
295
- // disable/hide by default
296
- disableNodeWithParents( babylonLight );
297
- // process shadow generator
298
- const shadowGeneratorDefinition = get( definition, 'shadowGenerator' ) as ShadowGeneratorDefinition;
299
- if( !isEmpty( shadowGeneratorDefinition ) ) {
300
- if( !(babylonLight instanceof ShadowLight) ) {
301
- throw new Error( `Using a ShadowGenerator with light type "${definition.type}" is not supported for ` +
302
- `"${lightId}". Use lights deriving from ShadowLight.` );
303
- }
304
- await this.processShadowGenerator( babylonLight, shadowGeneratorDefinition );
305
- }
306
- return babylonLight;
307
- };
308
-
309
- /**
310
- * @param babylonLight
311
- * @param definition
312
- * @protected
313
- */
314
- protected async processShadowGenerator( babylonLight: ShadowLight,
315
- definition: ShadowGeneratorDefinition ): Promise<ShadowGenerator> {
316
- const parameterDeclarations: ParameterDeclarations = {};
317
- // define declarations here if needed in future
318
- const parameterBag = definition as {} as ParameterBag;
319
- const parameters = Parameter.parseFromDeclarations( parameterDeclarations, parameterBag );
320
- const shadowGenerator = new ShadowGenerator( parameters['mapSize'], babylonLight );
321
- for( const parameter in parameters ) {
322
- if( parameter === 'mapSize' ) {
323
- continue;
324
- }
325
- set( shadowGenerator, parameter, get( parameters, parameter ) );
326
- }
327
- return shadowGenerator;
328
- };
329
-
330
- }
@@ -1,26 +0,0 @@
1
- /**
2
- * Creates a random uuidv4.
3
- */
4
- const uuidv4 = function() {
5
- // @ts-ignore
6
- return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace( /[018]/g, c =>
7
- (c ^ crypto.getRandomValues( new Uint8Array( 1 ) )[0] & 15 >> c / 4).toString( 16 )
8
- );
9
- };
10
-
11
- /**
12
- * Converts a string from camel case to snake case.
13
- */
14
- const camelToSnakeCase = function( str: string ): string {
15
- return str
16
- .replace( /([A-Z])/g, " $1" )
17
- .trim()
18
- .split( ' ' )
19
- .join('_')
20
- .toLowerCase();
21
- };
22
-
23
- export {
24
- uuidv4,
25
- camelToSnakeCase,
26
- };