@combeenation/3d-viewer 5.0.1 → 5.0.3-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 (109) hide show
  1. package/README.md +111 -114
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +1 -1
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +187 -187
  6. package/dist/lib-cjs/api/classes/element.d.ts +139 -139
  7. package/dist/lib-cjs/api/classes/element.js +794 -794
  8. package/dist/lib-cjs/api/classes/element.js.map +1 -1
  9. package/dist/lib-cjs/api/classes/elementParameterizable.d.ts +14 -0
  10. package/dist/lib-cjs/api/classes/elementParameterizable.js +135 -0
  11. package/dist/lib-cjs/api/classes/elementParameterizable.js.map +1 -0
  12. package/dist/lib-cjs/api/classes/event.d.ts +326 -326
  13. package/dist/lib-cjs/api/classes/event.js +371 -371
  14. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  15. package/dist/lib-cjs/api/classes/eventBroadcaster.js +53 -53
  16. package/dist/lib-cjs/api/classes/parameter.d.ts +316 -259
  17. package/dist/lib-cjs/api/classes/parameter.js +451 -388
  18. package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
  19. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  20. package/dist/lib-cjs/api/classes/parameterObservable.js +101 -101
  21. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  22. package/dist/lib-cjs/api/classes/parameterizable.js +149 -149
  23. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +44 -44
  24. package/dist/lib-cjs/api/classes/placementAnimation.js +163 -163
  25. package/dist/lib-cjs/api/classes/variant.d.ts +234 -234
  26. package/dist/lib-cjs/api/classes/variant.js +1154 -1154
  27. package/dist/lib-cjs/api/classes/variantInstance.d.ts +45 -45
  28. package/dist/lib-cjs/api/classes/variantInstance.js +108 -108
  29. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  30. package/dist/lib-cjs/api/classes/variantParameterizable.js +99 -99
  31. package/dist/lib-cjs/api/classes/viewer.d.ts +177 -175
  32. package/dist/lib-cjs/api/classes/viewer.js +717 -701
  33. package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
  34. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  35. package/dist/lib-cjs/api/classes/viewerLight.js +392 -392
  36. package/dist/lib-cjs/api/internal/debugViewer.d.ts +13 -13
  37. package/dist/lib-cjs/api/internal/debugViewer.js +86 -86
  38. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  39. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  40. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -10
  41. package/dist/lib-cjs/api/internal/sceneSetup.js +238 -234
  42. package/dist/lib-cjs/api/internal/sceneSetup.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/animationManager.d.ts +29 -29
  44. package/dist/lib-cjs/api/manager/animationManager.js +130 -130
  45. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +65 -65
  46. package/dist/lib-cjs/api/manager/gltfExportManager.js +223 -222
  47. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  48. package/dist/lib-cjs/api/manager/sceneManager.d.ts +31 -32
  49. package/dist/lib-cjs/api/manager/sceneManager.js +153 -132
  50. package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
  51. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +92 -92
  52. package/dist/lib-cjs/api/manager/variantInstanceManager.js +335 -335
  53. package/dist/lib-cjs/api/store/specStorage.d.ts +24 -24
  54. package/dist/lib-cjs/api/store/specStorage.js +51 -51
  55. package/dist/lib-cjs/api/util/babylonHelper.d.ts +174 -166
  56. package/dist/lib-cjs/api/util/babylonHelper.js +575 -497
  57. package/dist/lib-cjs/api/util/babylonHelper.js.map +1 -1
  58. package/dist/lib-cjs/api/util/globalTypes.d.ts +366 -356
  59. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  60. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -53
  61. package/dist/lib-cjs/api/util/resourceHelper.js +257 -242
  62. package/dist/lib-cjs/api/util/resourceHelper.js.map +1 -1
  63. package/dist/lib-cjs/api/util/stringHelper.d.ts +9 -9
  64. package/dist/lib-cjs/api/util/stringHelper.js +25 -25
  65. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  66. package/dist/lib-cjs/api/util/structureHelper.js +48 -44
  67. package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
  68. package/dist/lib-cjs/buildinfo.json +3 -3
  69. package/dist/lib-cjs/index.d.ts +49 -49
  70. package/dist/lib-cjs/index.js +89 -89
  71. package/dist/webpack-stats.json +0 -0
  72. package/package.json +87 -87
  73. package/src/api/classes/animationInterface.ts +10 -10
  74. package/src/api/classes/dottedPath.ts +181 -181
  75. package/src/api/classes/element.ts +690 -692
  76. package/src/api/classes/event.ts +367 -367
  77. package/src/api/classes/eventBroadcaster.ts +52 -52
  78. package/src/api/classes/parameter.ts +474 -405
  79. package/src/api/classes/parameterObservable.ts +100 -100
  80. package/src/api/classes/parameterizable.ts +87 -87
  81. package/src/api/classes/placementAnimation.ts +160 -160
  82. package/src/api/classes/variant.ts +845 -845
  83. package/src/api/classes/variantInstance.ts +97 -97
  84. package/src/api/classes/variantParameterizable.ts +85 -85
  85. package/src/api/classes/viewer.ts +650 -624
  86. package/src/api/classes/viewerLight.ts +334 -334
  87. package/src/api/internal/debugViewer.ts +90 -90
  88. package/src/api/internal/lensRendering.ts +10 -10
  89. package/src/api/internal/sceneSetup.ts +204 -201
  90. package/src/api/manager/animationManager.ts +142 -142
  91. package/src/api/manager/gltfExportManager.ts +191 -191
  92. package/src/api/manager/sceneManager.ts +128 -102
  93. package/src/api/manager/variantInstanceManager.ts +265 -265
  94. package/src/api/store/specStorage.ts +51 -51
  95. package/src/api/util/babylonHelper.ts +628 -538
  96. package/src/api/util/globalTypes.ts +413 -402
  97. package/src/api/util/resourceHelper.ts +189 -173
  98. package/src/api/util/stringHelper.ts +23 -23
  99. package/src/api/util/structureHelper.ts +49 -43
  100. package/src/buildinfo.json +3 -3
  101. package/src/commonjs.tsconfig.json +10 -10
  102. package/src/declaration.tsconfig.json +8 -8
  103. package/src/dev.ts +42 -42
  104. package/src/es6.tsconfig.json +10 -10
  105. package/src/index.ts +94 -94
  106. package/src/pagesconfig.json +77 -73
  107. package/src/tsconfig.json +32 -32
  108. package/src/tsconfig.types.json +9 -9
  109. package/src/types.d.ts +3 -3
@@ -1,142 +1,142 @@
1
- import { Animation as BabylonAnimation } from '@babylonjs/core/Animations/animation';
2
- import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
3
- import { Scene as BabylonScene, Scene } from '@babylonjs/core/scene';
4
- import { Nullable } from '@babylonjs/core/types';
5
- //import { CustomEase } from 'gsap/CustomEase';
6
- //import { ExpoScaleEase, RoughEase, SlowMo } from 'gsap/EasePack';
7
- import { remove } from 'lodash-es';
8
- import { PlacementAnimation } from '../classes/placementAnimation';
9
-
10
- //gsap.registerPlugin( CustomEase, ExpoScaleEase, RoughEase, SlowMo );
11
-
12
- export class AnimationManager {
13
- public animations: AnimationInterface[] = [];
14
-
15
- /**
16
- * Constructor.
17
- */
18
- protected constructor(protected scene: Scene) {}
19
-
20
- /**
21
- * Creates an {@link AnimationManager} based on given BabylonJS scene.
22
- */
23
- public static async create(scene: BabylonScene): Promise<AnimationManager> {
24
- return new AnimationManager(scene);
25
- }
26
-
27
- /**
28
- * @deprecated Use viewer.moveCameraTo().
29
- */
30
- public resetCamera(animate: boolean = false) {
31
- //TODO currently only works for arcrotate. are there any other cameras?
32
- let activeCamera = this.scene.activeCamera as Nullable<ArcRotateCamera>;
33
- if (!activeCamera) {
34
- throw new Error('There is no active camera');
35
- }
36
-
37
- const initAlpha = activeCamera.metadata.alpha;
38
- const initBeta = activeCamera.metadata.beta;
39
- const initRadius = activeCamera.metadata.radius;
40
-
41
- if (animate) {
42
- this.animateArcRotateCamera(initAlpha, initBeta, initRadius);
43
- } else {
44
- activeCamera.alpha = initAlpha;
45
- activeCamera.beta = initBeta;
46
- activeCamera.radius = initRadius;
47
- }
48
- }
49
-
50
- /**
51
- * @deprecated Use viewer.moveCameraTo().
52
- */
53
- public animateArcRotateCamera(targetAlpha: number, targetBeta: number, targetRadius: number) {
54
- let activeCamera = this.scene.activeCamera as Nullable<ArcRotateCamera>;
55
- if (!activeCamera) {
56
- throw new Error('There is no active camera');
57
- }
58
- const alphaAnimation = new BabylonAnimation(
59
- 'alphaAnimation',
60
- 'alpha',
61
- 30,
62
- BabylonAnimation.ANIMATIONTYPE_FLOAT,
63
- BabylonAnimation.ANIMATIONLOOPMODE_CONSTANT
64
- );
65
- const betaAnimation = new BabylonAnimation(
66
- 'betaAnimation',
67
- 'beta',
68
- 30,
69
- BabylonAnimation.ANIMATIONTYPE_FLOAT,
70
- BabylonAnimation.ANIMATIONLOOPMODE_CONSTANT
71
- );
72
- const radiusAnimation = new BabylonAnimation(
73
- 'radiusAnimation',
74
- 'radius',
75
- 30,
76
- BabylonAnimation.ANIMATIONTYPE_FLOAT,
77
- BabylonAnimation.ANIMATIONLOOPMODE_CONSTANT
78
- );
79
- alphaAnimation.setKeys([
80
- {
81
- frame: 0,
82
- value: activeCamera.alpha,
83
- },
84
- {
85
- frame: 100,
86
- value: targetAlpha,
87
- },
88
- ]);
89
- betaAnimation.setKeys([
90
- {
91
- frame: 0,
92
- value: activeCamera.beta,
93
- },
94
- {
95
- frame: 100,
96
- value: targetBeta,
97
- },
98
- ]);
99
- radiusAnimation.setKeys([
100
- {
101
- frame: 0,
102
- value: activeCamera.radius,
103
- },
104
- {
105
- frame: 100,
106
- value: targetRadius,
107
- },
108
- ]);
109
- activeCamera.animations.push(alphaAnimation);
110
- activeCamera.animations.push(betaAnimation);
111
- activeCamera.animations.push(radiusAnimation);
112
-
113
- let animation = this.scene.beginAnimation(activeCamera, 0, 100, false, 10);
114
- animation.disposeOnEnd = true;
115
- }
116
-
117
- /**
118
- * Stops and kills all running animations.
119
- */
120
- public killAllAnimations() {
121
- this.animations.forEach(animation => animation.kill());
122
- this.animations = [];
123
- }
124
-
125
- /**
126
- * @see {@link PlacementAnimation.play}
127
- */
128
- public async animateToPlacement(
129
- mutable: object,
130
- placement: PlacementDefinition | string,
131
- animation?: AnimationDefinition | string
132
- ): Promise<AnimationInterface> {
133
- const _animation = new PlacementAnimation(mutable, placement, animation);
134
- this.animations.push(_animation);
135
- const animationPromise = _animation.play();
136
- animationPromise.then(a => {
137
- remove(this.animations, _a => _a === a);
138
- a.kill();
139
- });
140
- return await animationPromise;
141
- }
142
- }
1
+ import { Animation as BabylonAnimation } from '@babylonjs/core/Animations/animation';
2
+ import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera';
3
+ import { Scene as BabylonScene, Scene } from '@babylonjs/core/scene';
4
+ import { Nullable } from '@babylonjs/core/types';
5
+ //import { CustomEase } from 'gsap/CustomEase';
6
+ //import { ExpoScaleEase, RoughEase, SlowMo } from 'gsap/EasePack';
7
+ import { remove } from 'lodash-es';
8
+ import { PlacementAnimation } from '../classes/placementAnimation';
9
+
10
+ //gsap.registerPlugin( CustomEase, ExpoScaleEase, RoughEase, SlowMo );
11
+
12
+ export class AnimationManager {
13
+ public animations: AnimationInterface[] = [];
14
+
15
+ /**
16
+ * Constructor.
17
+ */
18
+ protected constructor(protected scene: Scene) {}
19
+
20
+ /**
21
+ * Creates an {@link AnimationManager} based on given BabylonJS scene.
22
+ */
23
+ public static async create(scene: BabylonScene): Promise<AnimationManager> {
24
+ return new AnimationManager(scene);
25
+ }
26
+
27
+ /**
28
+ * @deprecated Use viewer.moveCameraTo().
29
+ */
30
+ public resetCamera(animate: boolean = false) {
31
+ //TODO currently only works for arcrotate. are there any other cameras?
32
+ let activeCamera = this.scene.activeCamera as Nullable<ArcRotateCamera>;
33
+ if (!activeCamera) {
34
+ throw new Error('There is no active camera');
35
+ }
36
+
37
+ const initAlpha = activeCamera.metadata.alpha;
38
+ const initBeta = activeCamera.metadata.beta;
39
+ const initRadius = activeCamera.metadata.radius;
40
+
41
+ if (animate) {
42
+ this.animateArcRotateCamera(initAlpha, initBeta, initRadius);
43
+ } else {
44
+ activeCamera.alpha = initAlpha;
45
+ activeCamera.beta = initBeta;
46
+ activeCamera.radius = initRadius;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * @deprecated Use viewer.moveCameraTo().
52
+ */
53
+ public animateArcRotateCamera(targetAlpha: number, targetBeta: number, targetRadius: number) {
54
+ let activeCamera = this.scene.activeCamera as Nullable<ArcRotateCamera>;
55
+ if (!activeCamera) {
56
+ throw new Error('There is no active camera');
57
+ }
58
+ const alphaAnimation = new BabylonAnimation(
59
+ 'alphaAnimation',
60
+ 'alpha',
61
+ 30,
62
+ BabylonAnimation.ANIMATIONTYPE_FLOAT,
63
+ BabylonAnimation.ANIMATIONLOOPMODE_CONSTANT
64
+ );
65
+ const betaAnimation = new BabylonAnimation(
66
+ 'betaAnimation',
67
+ 'beta',
68
+ 30,
69
+ BabylonAnimation.ANIMATIONTYPE_FLOAT,
70
+ BabylonAnimation.ANIMATIONLOOPMODE_CONSTANT
71
+ );
72
+ const radiusAnimation = new BabylonAnimation(
73
+ 'radiusAnimation',
74
+ 'radius',
75
+ 30,
76
+ BabylonAnimation.ANIMATIONTYPE_FLOAT,
77
+ BabylonAnimation.ANIMATIONLOOPMODE_CONSTANT
78
+ );
79
+ alphaAnimation.setKeys([
80
+ {
81
+ frame: 0,
82
+ value: activeCamera.alpha,
83
+ },
84
+ {
85
+ frame: 100,
86
+ value: targetAlpha,
87
+ },
88
+ ]);
89
+ betaAnimation.setKeys([
90
+ {
91
+ frame: 0,
92
+ value: activeCamera.beta,
93
+ },
94
+ {
95
+ frame: 100,
96
+ value: targetBeta,
97
+ },
98
+ ]);
99
+ radiusAnimation.setKeys([
100
+ {
101
+ frame: 0,
102
+ value: activeCamera.radius,
103
+ },
104
+ {
105
+ frame: 100,
106
+ value: targetRadius,
107
+ },
108
+ ]);
109
+ activeCamera.animations.push(alphaAnimation);
110
+ activeCamera.animations.push(betaAnimation);
111
+ activeCamera.animations.push(radiusAnimation);
112
+
113
+ let animation = this.scene.beginAnimation(activeCamera, 0, 100, false, 10);
114
+ animation.disposeOnEnd = true;
115
+ }
116
+
117
+ /**
118
+ * Stops and kills all running animations.
119
+ */
120
+ public killAllAnimations() {
121
+ this.animations.forEach(animation => animation.kill());
122
+ this.animations = [];
123
+ }
124
+
125
+ /**
126
+ * @see {@link PlacementAnimation.play}
127
+ */
128
+ public async animateToPlacement(
129
+ mutable: object,
130
+ placement: PlacementDefinition | string,
131
+ animation?: AnimationDefinition | string
132
+ ): Promise<AnimationInterface> {
133
+ const _animation = new PlacementAnimation(mutable, placement, animation);
134
+ this.animations.push(_animation);
135
+ const animationPromise = _animation.play();
136
+ animationPromise.then(a => {
137
+ remove(this.animations, _a => _a === a);
138
+ a.kill();
139
+ });
140
+ return await animationPromise;
141
+ }
142
+ }
@@ -1,191 +1,191 @@
1
- import { Mesh } from '@babylonjs/core';
2
- import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
3
- import { Color3 } from '@babylonjs/core/Maths/math.color';
4
- import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
5
- import { GLTF2Export, IExportOptions } from '@babylonjs/serializers';
6
- import { isMeshIncludedInExclusionList } from '../util/structureHelper';
7
- import { merge } from 'lodash-es';
8
-
9
- export class GltfExportManager {
10
- /**
11
- * Constructor.
12
- */
13
- protected constructor(protected viewer: Viewer) {}
14
-
15
- /**
16
- * Creates an {@link GltfExportManager}.
17
- */
18
- public static async create(viewer: Viewer): Promise<GltfExportManager> {
19
- return new GltfExportManager(viewer);
20
- }
21
-
22
- /**
23
- * Exports selected nodes to a file.
24
- * @param filename optional name of the exported .GLB file.
25
- * @param exportOptions export options to be merged with default options.
26
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export.
27
- */
28
- public async exportGlb(
29
- filename = 'glb-export.glb',
30
- exportOptions: IExportOptions = {},
31
- excluded?: ExcludedGeometry
32
- ): Promise<File | undefined> {
33
- this.exportPreProcess();
34
- const glbData = await GLTF2Export.GLBAsync(
35
- this.viewer.scene,
36
- 'dummy',
37
- this.gltfExportOptions(exportOptions, excluded)
38
- );
39
- this.exportPostProcess();
40
- const resBlob = glbData.glTFFiles['dummy.glb']; // should be only one file for glb
41
-
42
- // check if result is valid, according to the typings this could also be a string
43
- if (resBlob instanceof Blob) {
44
- if (!filename.endsWith('.glb')) {
45
- filename += '.glb';
46
- }
47
- return new File([resBlob], filename);
48
- } else {
49
- // result was not a valid blob
50
- return undefined;
51
- }
52
- }
53
-
54
- /**
55
- * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
56
- * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
57
- * @param exportOptions export options to be merged with default options.
58
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export.
59
- */
60
- public async exportGltfToFile(filename: string, exportOptions: IExportOptions = {}, excluded?: ExcludedGeometry) {
61
- this.exportPreProcess();
62
- await GLTF2Export.GLTFAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
63
- glb => {
64
- glb.downloadFiles();
65
- }
66
- );
67
- this.exportPostProcess();
68
- }
69
-
70
- /**
71
- * Exports selected nodes to GLB. This results in one binary file.
72
- * @param filename name of the .GLB file.
73
- * @param exportOptions export options to be merged with default options.
74
- * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
75
- */
76
- public async exportGlbToFile(filename: string, exportOptions: IExportOptions = {}, excluded?: ExcludedGeometry) {
77
- this.exportPreProcess();
78
- await GLTF2Export.GLBAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
79
- glb => {
80
- glb.downloadFiles();
81
- }
82
- );
83
- this.exportPostProcess();
84
- }
85
-
86
- /**
87
- * Gets predefined {@link IExportOptions } merged with given ones.
88
- */
89
- protected gltfExportOptions(mergeWithOptions: IExportOptions = {}, excluded?: ExcludedGeometry): IExportOptions {
90
- const defaultOptions = {
91
- // includeCoordinateSystemConversionNodes: true,
92
- shouldExportNode: function (node: any) {
93
- if (!node.isEnabled()) {
94
- return false;
95
- }
96
- if (node.isVisible === false) {
97
- return false;
98
- }
99
- if (node.name === '__bounding_box__') {
100
- return false;
101
- }
102
- if (excluded && node instanceof Mesh && isMeshIncludedInExclusionList(node, excluded)) {
103
- return false;
104
- }
105
- return true;
106
- },
107
- };
108
- return merge({}, defaultOptions, mergeWithOptions);
109
- }
110
-
111
- /**
112
- * Stuff to be done before exporting to GLTF
113
- */
114
- protected exportPreProcess() {
115
- this.viewer.pauseRendering();
116
- this.exchangeRefractionMaterials();
117
- }
118
-
119
- /**
120
- * Stuff to be done after the GLTF export
121
- */
122
- protected exportPostProcess() {
123
- this.restoreRefractionMaterials();
124
- this.viewer.resumeRendering();
125
- }
126
-
127
- /**
128
- * Materials with refraction set are not exported properly.
129
- * Exchange all such (relevant) materials with a more export-friendly version
130
- */
131
- protected exchangeRefractionMaterials() {
132
- for (const n of this.viewer.scene.getNodes()) {
133
- if (!(n instanceof AbstractMesh)) continue;
134
- if (!(n.material instanceof PBRMaterial)) continue;
135
- if (!(n.material as PBRMaterial).subSurface.isRefractionEnabled) continue;
136
- if ((n.material as PBRMaterial).transparencyMode !== PBRMaterial.PBRMATERIAL_OPAQUE) continue;
137
- // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to PBRMATERIAL_OPAQUE
138
- n.material = this.createRefractionMaterialReplacement(n.material);
139
- }
140
- }
141
-
142
- /**
143
- * Restore original materials with set refraction.
144
- */
145
- protected restoreRefractionMaterials() {
146
- for (const n of this.viewer.scene.getNodes()) {
147
- if (!(n instanceof AbstractMesh)) continue;
148
- if (!(n.material instanceof PBRMaterial)) continue;
149
- if (!this.isMaterialClonedForExport(n.material)) continue;
150
- // at this point we have a pbrmaterial tagged as cloned in its metadata that's set on a mesh
151
-
152
- // restore and dispose
153
- const currMaterial = n.material;
154
- const prevMaterial = this.viewer.scene.getMaterialByUniqueID(n.material.metadata.clonedFrom);
155
- if (prevMaterial) {
156
- n.material = prevMaterial; // restore previous material
157
- currMaterial.dispose(false, true); // dispose of clone
158
- }
159
- }
160
- }
161
-
162
- /**
163
- * Create an export-friendly replacement material for a material using refraction.
164
- * @param mat Material to be replaced
165
- */
166
- protected createRefractionMaterialReplacement(mat: PBRMaterial): PBRMaterial {
167
- // if we're dealing with a clone already, return it instead of cloning
168
- if (this.isMaterialClonedForExport(mat)) return mat;
169
-
170
- // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
171
- let clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
172
- clonedMaterial.metadata = { ...mat.metadata, clonedFrom: mat.uniqueId }; // create shallow copy of metadata on clone. see https://forum.babylonjs.com/t/the-metadata-of-the-mesh-cloned-by-the-instantiatemodelstoscene-method-is-a-shallow-copy/21563
173
- clonedMaterial.refractionTexture = null;
174
- clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
175
- clonedMaterial.alpha = 0.7;
176
- clonedMaterial.albedoColor = new Color3(0.3, 0.3, 0.3);
177
- clonedMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
178
- clonedMaterial.metallic = 0.65;
179
- clonedMaterial.roughness = 0.15;
180
-
181
- return clonedMaterial;
182
- }
183
-
184
- /**
185
- * Inspect if a material was temporarily cloned for GLB export
186
- * @param mat Material to be inspected
187
- */
188
- protected isMaterialClonedForExport(mat: PBRMaterial): boolean {
189
- return mat.metadata.clonedFrom;
190
- }
191
- }
1
+ import { PBRMaterial } from '@babylonjs/core/Materials/PBR/pbrMaterial';
2
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
3
+ import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';
4
+ import { GLTF2Export, IExportOptions } from '@babylonjs/serializers/glTF/2.0';
5
+ import { isMeshIncludedInExclusionList } from '../util/structureHelper';
6
+ import { merge } from 'lodash-es';
7
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
8
+
9
+ export class GltfExportManager {
10
+ /**
11
+ * Constructor.
12
+ */
13
+ protected constructor(protected viewer: Viewer) {}
14
+
15
+ /**
16
+ * Creates an {@link GltfExportManager}.
17
+ */
18
+ public static async create(viewer: Viewer): Promise<GltfExportManager> {
19
+ return new GltfExportManager(viewer);
20
+ }
21
+
22
+ /**
23
+ * Exports selected nodes to a file.
24
+ * @param filename optional name of the exported .GLB file.
25
+ * @param exportOptions export options to be merged with default options.
26
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export.
27
+ */
28
+ public async exportGlb(
29
+ filename = 'glb-export.glb',
30
+ exportOptions: IExportOptions = {},
31
+ excluded?: ExcludedGeometryList
32
+ ): Promise<File | undefined> {
33
+ this.exportPreProcess();
34
+ const glbData = await GLTF2Export.GLBAsync(
35
+ this.viewer.scene,
36
+ 'dummy',
37
+ this.gltfExportOptions(exportOptions, excluded)
38
+ );
39
+ this.exportPostProcess();
40
+ const resBlob = glbData.glTFFiles['dummy.glb']; // should be only one file for glb
41
+
42
+ // check if result is valid, according to the typings this could also be a string
43
+ if (resBlob instanceof Blob) {
44
+ if (!filename.endsWith('.glb')) {
45
+ filename += '.glb';
46
+ }
47
+ return new File([resBlob], filename);
48
+ } else {
49
+ // result was not a valid blob
50
+ return undefined;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
56
+ * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
57
+ * @param exportOptions export options to be merged with default options.
58
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export.
59
+ */
60
+ public async exportGltfToFile(filename: string, exportOptions: IExportOptions = {}, excluded?: ExcludedGeometryList) {
61
+ this.exportPreProcess();
62
+ await GLTF2Export.GLTFAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
63
+ glb => {
64
+ glb.downloadFiles();
65
+ }
66
+ );
67
+ this.exportPostProcess();
68
+ }
69
+
70
+ /**
71
+ * Exports selected nodes to GLB. This results in one binary file.
72
+ * @param filename name of the .GLB file.
73
+ * @param exportOptions export options to be merged with default options.
74
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
75
+ */
76
+ public async exportGlbToFile(filename: string, exportOptions: IExportOptions = {}, excluded?: ExcludedGeometryList) {
77
+ this.exportPreProcess();
78
+ await GLTF2Export.GLBAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
79
+ glb => {
80
+ glb.downloadFiles();
81
+ }
82
+ );
83
+ this.exportPostProcess();
84
+ }
85
+
86
+ /**
87
+ * Gets predefined {@link IExportOptions } merged with given ones.
88
+ */
89
+ protected gltfExportOptions(mergeWithOptions: IExportOptions = {}, excluded?: ExcludedGeometryList): IExportOptions {
90
+ const defaultOptions = {
91
+ // includeCoordinateSystemConversionNodes: true,
92
+ shouldExportNode: function (node: any) {
93
+ if (!node.isEnabled()) {
94
+ return false;
95
+ }
96
+ if (node.isVisible === false) {
97
+ return false;
98
+ }
99
+ if (node.name === '__bounding_box__') {
100
+ return false;
101
+ }
102
+ if (excluded && node instanceof Mesh && isMeshIncludedInExclusionList(node, excluded)) {
103
+ return false;
104
+ }
105
+ return true;
106
+ },
107
+ };
108
+ return merge({}, defaultOptions, mergeWithOptions);
109
+ }
110
+
111
+ /**
112
+ * Stuff to be done before exporting to GLTF
113
+ */
114
+ protected exportPreProcess() {
115
+ this.viewer.pauseRendering();
116
+ this.exchangeRefractionMaterials();
117
+ }
118
+
119
+ /**
120
+ * Stuff to be done after the GLTF export
121
+ */
122
+ protected exportPostProcess() {
123
+ this.restoreRefractionMaterials();
124
+ this.viewer.resumeRendering();
125
+ }
126
+
127
+ /**
128
+ * Materials with refraction set are not exported properly.
129
+ * Exchange all such (relevant) materials with a more export-friendly version
130
+ */
131
+ protected exchangeRefractionMaterials() {
132
+ for (const n of this.viewer.scene.getNodes()) {
133
+ if (!(n instanceof AbstractMesh)) continue;
134
+ if (!(n.material instanceof PBRMaterial)) continue;
135
+ if (!(n.material as PBRMaterial).subSurface.isRefractionEnabled) continue;
136
+ if ((n.material as PBRMaterial).transparencyMode !== PBRMaterial.PBRMATERIAL_OPAQUE) continue;
137
+ // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to PBRMATERIAL_OPAQUE
138
+ n.material = this.createRefractionMaterialReplacement(n.material);
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Restore original materials with set refraction.
144
+ */
145
+ protected restoreRefractionMaterials() {
146
+ for (const n of this.viewer.scene.getNodes()) {
147
+ if (!(n instanceof AbstractMesh)) continue;
148
+ if (!(n.material instanceof PBRMaterial)) continue;
149
+ if (!this.isMaterialClonedForExport(n.material)) continue;
150
+ // at this point we have a pbrmaterial tagged as cloned in its metadata that's set on a mesh
151
+
152
+ // restore and dispose
153
+ const currMaterial = n.material;
154
+ const prevMaterial = this.viewer.scene.getMaterialByUniqueID(n.material.metadata.clonedFrom);
155
+ if (prevMaterial) {
156
+ n.material = prevMaterial; // restore previous material
157
+ currMaterial.dispose(false, true); // dispose of clone
158
+ }
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Create an export-friendly replacement material for a material using refraction.
164
+ * @param mat Material to be replaced
165
+ */
166
+ protected createRefractionMaterialReplacement(mat: PBRMaterial): PBRMaterial {
167
+ // if we're dealing with a clone already, return it instead of cloning
168
+ if (this.isMaterialClonedForExport(mat)) return mat;
169
+
170
+ // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
171
+ let clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
172
+ clonedMaterial.metadata = { ...mat.metadata, clonedFrom: mat.uniqueId }; // create shallow copy of metadata on clone. see https://forum.babylonjs.com/t/the-metadata-of-the-mesh-cloned-by-the-instantiatemodelstoscene-method-is-a-shallow-copy/21563
173
+ clonedMaterial.refractionTexture = null;
174
+ clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
175
+ clonedMaterial.alpha = 0.7;
176
+ clonedMaterial.albedoColor = new Color3(0.3, 0.3, 0.3);
177
+ clonedMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
178
+ clonedMaterial.metallic = 0.65;
179
+ clonedMaterial.roughness = 0.15;
180
+
181
+ return clonedMaterial;
182
+ }
183
+
184
+ /**
185
+ * Inspect if a material was temporarily cloned for GLB export
186
+ * @param mat Material to be inspected
187
+ */
188
+ protected isMaterialClonedForExport(mat: PBRMaterial): boolean {
189
+ return !!mat.metadata?.clonedFrom;
190
+ }
191
+ }