@combeenation/3d-viewer 4.0.0-alpha7 → 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 +125 -180
  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/variant.d.ts +14 -48
  14. package/dist/lib-cjs/api/classes/variant.js +56 -315
  15. package/dist/lib-cjs/api/classes/variant.js.map +1 -1
  16. package/dist/lib-cjs/api/classes/variantInstance.d.ts +1 -5
  17. package/dist/lib-cjs/api/classes/variantInstance.js +0 -10
  18. package/dist/lib-cjs/api/classes/variantInstance.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/viewer.d.ts +1 -0
  20. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  21. package/dist/lib-cjs/api/classes/viewerLight.js +27 -19
  22. package/dist/lib-cjs/api/classes/viewerLight.js.map +1 -1
  23. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +1 -5
  24. package/dist/lib-cjs/api/internal/sceneSetup.js +71 -75
  25. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  26. package/dist/lib-cjs/api/util/babylonHelper.d.ts +4 -51
  27. package/dist/lib-cjs/api/util/babylonHelper.js +8 -141
  28. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  29. package/dist/lib-cjs/api/util/globalTypes.d.ts +11 -53
  30. package/dist/lib-cjs/api/util/resourceHelper.d.ts +8 -4
  31. package/dist/lib-cjs/api/util/resourceHelper.js +25 -59
  32. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  33. package/dist/lib-cjs/index.d.ts +22 -24
  34. package/dist/lib-cjs/index.js +38 -42
  35. package/dist/lib-cjs/index.js.map +1 -1
  36. package/package.json +1 -1
  37. package/src/api/classes/element.ts +112 -133
  38. package/src/api/classes/{parameterizable.ts → elementParameterizable.ts} +1 -12
  39. package/src/api/classes/event.ts +1 -16
  40. package/src/api/classes/parameter.ts +22 -153
  41. package/src/api/classes/parameterObservable.ts +31 -9
  42. package/src/api/classes/variant.ts +51 -184
  43. package/src/api/classes/variantInstance.ts +1 -8
  44. package/src/api/classes/viewer.ts +1 -0
  45. package/src/api/internal/sceneSetup.ts +109 -99
  46. package/src/api/util/babylonHelper.ts +9 -153
  47. package/src/api/util/globalTypes.ts +13 -64
  48. package/src/api/util/resourceHelper.ts +24 -26
  49. package/src/dev.ts +7 -5
  50. package/src/index.ts +23 -27
  51. package/src/pagesconfig.json +13 -8
  52. package/dist/lib-cjs/api/emitter.d.ts +0 -35
  53. package/dist/lib-cjs/api/emitter.js +0 -61
  54. package/dist/lib-cjs/api/emitter.js.map +0 -1
  55. package/src/api/classes/variantParameterizable.ts +0 -73
  56. package/src/api/classes/viewerLight.ts +0 -326
  57. package/src/api/util/stringHelper.ts +0 -26
@@ -1,326 +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 '@babylonjs/loaders/glTF/2.0/Extensions/KHR_lights_punctual';
8
- import { TransformNode } from 'index';
9
- import { get, isEmpty, isString, set } from 'lodash-es';
10
- import {
11
- cloneNodeWithParents,
12
- disableNodeWithParents,
13
- enableNodeWithParents,
14
- getDottedPathForNode,
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
- // we have to deal just with root nodes here due to relative impacts in a node tree
143
- const rootNode = getRootNode( light.light );
144
- if( rootNode instanceof TransformNode ) {
145
- rotateTransformNode( rootNode, Parameter.parseRotation( newValue ) );
146
- }
147
- }
148
- ] );
149
- this._parameterObservers.set( Parameter.SCALING, [
150
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
151
- // we have to deal just with root nodes here due to relative impacts in a node tree
152
- const rootNode = getRootNode( light.light );
153
- if( rootNode instanceof TransformNode ) {
154
- rootNode.scaling = Parameter.parseScaling( newValue );
155
- }
156
- }
157
- ] );
158
- this._parameterObservers.set( Parameter.DIRECTION, [
159
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
160
- if(
161
- (light.light instanceof ShadowLight && !(light.light instanceof PointLight))
162
- || light.light instanceof HemisphericLight
163
- ) {
164
- set( light.light, 'direction', Parameter.parseVector( newValue ) );
165
- }
166
- }
167
- ] );
168
- this._parameterObservers.set( Parameter.ANGLE, [
169
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
170
- if( light.light.getClassName() === 'SpotLight' ) {
171
- set( light.light, 'angle', Parameter.parseNumber( newValue ) );
172
- }
173
- }
174
- ] );
175
- this._parameterObservers.set( Parameter.EXPONENT, [
176
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
177
- if( light.light.getClassName() === 'SpotLight' ) {
178
- set( light.light, 'exponent', Parameter.parseNumber( newValue ) );
179
- }
180
- }
181
- ] );
182
- this._parameterObservers.set( Parameter.DIFFUSE, [
183
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
184
- set( light.light, 'diffuse', Parameter.parseColor( newValue ) );
185
- }
186
- ] );
187
- this._parameterObservers.set( Parameter.SPECULAR, [
188
- async ( light: ViewerLight, oldValue: ParameterValue, newValue: ParameterValue ) => {
189
- set( light.light, 'specular', Parameter.parseColor( newValue ) );
190
- }
191
- ] );
192
- return this;
193
- }
194
-
195
- /**
196
- * @param definition
197
- * @protected
198
- */
199
- protected async createBabylonLightFromDefinition( definition: LightDefinition ): Promise<Light> {
200
- const parameters = Parameter.parseFromDeclarations( Parameter.declarations, this.inheritedParameters );
201
- const scene = this.variant.viewer.scene;
202
- let lightId = this.id;
203
- let babylonLight;
204
- switch( definition.type ) {
205
- case 'baked':
206
- if( !definition['path'] ) {
207
- throw new Error( `The light "${lightId}" of type "${definition.type}" needs a "path".` );
208
- }
209
- const bakedLight = this.variant.inheritedLights.find( l => l.metadata.dottedPath.path === definition['path'] );
210
- if( bakedLight ) {
211
- lightId = bakedLight.metadata.dottedPath.clone().unshiftPart( this.id ).path;
212
- babylonLight = cloneNodeWithParents( bakedLight ) as Light;
213
- babylonLight!.name = lightId;
214
- babylonLight!.id = lightId;
215
- } else {
216
- throw new Error( `No light found for path "${definition['path']}" in ViewerLight "${lightId}".` );
217
- }
218
- break;
219
- case 'hemispheric':
220
- if( !parameters['direction'] ) {
221
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
222
- }
223
- // @ts-ignore
224
- const hemisphericLightModule = await import(/* webpackChunkName: "hemispheric-light" */ '@babylonjs/core/Lights/hemisphericLight');
225
- babylonLight = new hemisphericLightModule.HemisphericLight(
226
- lightId,
227
- parameters['direction'],
228
- scene
229
- );
230
- break;
231
- case 'point':
232
- if( !parameters['position'] ) {
233
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "position".` );
234
- }
235
- // @ts-ignore
236
- const pointLightModule = await import(/* webpackChunkName: "point-light" */ '@babylonjs/core/Lights/pointLight');
237
- babylonLight = new pointLightModule.PointLight(
238
- lightId,
239
- parameters['position'],
240
- scene
241
- );
242
- break;
243
- case 'directional':
244
- if( !parameters['direction'] ) {
245
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
246
- }
247
- // @ts-ignore
248
- const directionalLightModule = await import(/* webpackChunkName: "directional-light" */ '@babylonjs/core/Lights/directionalLight');
249
- babylonLight = new directionalLightModule.DirectionalLight(
250
- lightId,
251
- parameters['direction'],
252
- scene
253
- );
254
- break;
255
- case 'spot':
256
- if( !parameters['position'] ) {
257
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "position".` );
258
- }
259
- if( !parameters['direction'] ) {
260
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs a "direction".` );
261
- }
262
- if( !parameters['angle'] ) {
263
- throw new Error( `A ViewerLight of type "${definition.type}" needs an "angle".` );
264
- }
265
- if( !parameters['exponent'] ) {
266
- throw new Error( `The ViewerLight "${lightId}" of type "${definition.type}" needs an "exponent".` );
267
- }
268
- // @ts-ignore
269
- const spotLightModule = await import(/* webpackChunkName: "spot-light" */ '@babylonjs/core/Lights/spotLight');
270
- babylonLight = new spotLightModule.SpotLight(
271
- lightId,
272
- parameters['position'],
273
- parameters['direction'],
274
- parameters['angle'],
275
- parameters['exponent'],
276
- scene
277
- );
278
- break;
279
- default:
280
- throw new Error( `The type "${definition.type}" for ViewerLight "${lightId}" is not implemented (yet).` );
281
- }
282
- if( !babylonLight ) {
283
- throw new Error( `The Light for ViewerLight "${lightId}" of type "${definition.type}" could no be created ` +
284
- `or found.` );
285
- }
286
- if( !babylonLight.parent ) {
287
- // Create pseudo parent since lights do not implement mutations like "rotation" by itself.
288
- babylonLight.parent = new TransformNode( '__light__', this.variant.viewer.scene, true );
289
- }
290
- // disable/hide by default
291
- disableNodeWithParents( babylonLight );
292
- injectNodeMetadata( babylonLight, { dottedPath: getDottedPathForNode( babylonLight ) }, false );
293
- // process shadow generator
294
- const shadowGeneratorDefinition = get( definition, 'shadowGenerator' ) as ShadowGeneratorDefinition;
295
- if( !isEmpty( shadowGeneratorDefinition ) ) {
296
- if( !(babylonLight instanceof ShadowLight) ) {
297
- throw new Error( `Using a ShadowGenerator with light type "${definition.type}" is not supported for ` +
298
- `"${lightId}". Use lights deriving from ShadowLight.` );
299
- }
300
- await this.processShadowGenerator( babylonLight, shadowGeneratorDefinition );
301
- }
302
- return babylonLight;
303
- };
304
-
305
- /**
306
- * @param babylonLight
307
- * @param definition
308
- * @protected
309
- */
310
- protected async processShadowGenerator( babylonLight: ShadowLight,
311
- definition: ShadowGeneratorDefinition ): Promise<ShadowGenerator> {
312
- const parameterDeclarations: ParameterDeclarations = {};
313
- // define declarations here if needed in future
314
- const parameterBag = definition as {} as ParameterBag;
315
- const parameters = Parameter.parseFromDeclarations( parameterDeclarations, parameterBag );
316
- const shadowGenerator = new ShadowGenerator( parameters['mapSize'], babylonLight );
317
- for( const parameter in parameters ) {
318
- if( parameter === 'mapSize' ) {
319
- continue;
320
- }
321
- set( shadowGenerator, parameter, get( parameters, parameter ) );
322
- }
323
- return shadowGenerator;
324
- };
325
-
326
- }
@@ -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
- };