@combeenation/3d-viewer 4.3.0 → 5.0.0-beta4

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.
@@ -0,0 +1,192 @@
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 { Scene } from '@babylonjs/core/scene';
6
+ import { GLTF2Export, IExportOptions } from '@babylonjs/serializers';
7
+ import { isMeshIncludedInExclusionList } from 'api/util/structureHelper';
8
+ import { merge } from 'lodash-es';
9
+
10
+ export class GltfExportManager {
11
+ /**
12
+ * Constructor.
13
+ */
14
+ protected constructor(protected viewer: Viewer) {}
15
+
16
+ /**
17
+ * Creates an {@link GltfExportManager}.
18
+ */
19
+ public static async create(viewer: Viewer): Promise<GltfExportManager> {
20
+ return new GltfExportManager(viewer);
21
+ }
22
+
23
+ /**
24
+ * Exports selected nodes to a file.
25
+ * @param filename optional name of the exported .GLB file.
26
+ * @param exportOptions export options to be merged with default options. See {@link gltfExportOptions}.
27
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export.
28
+ */
29
+ public async exportGlb(
30
+ filename = 'glb-export.glb',
31
+ exportOptions: IExportOptions = {},
32
+ excluded?: ExcludedGeometry
33
+ ): Promise<File | undefined> {
34
+ this.exportPreProcess();
35
+ const glbData = await GLTF2Export.GLBAsync(
36
+ this.viewer.scene,
37
+ 'dummy',
38
+ this.gltfExportOptions(exportOptions, excluded)
39
+ );
40
+ this.exportPostProcess();
41
+ const resBlob = glbData.glTFFiles['dummy.glb']; // should be only one file for glb
42
+
43
+ // check if result is valid, according to the typings this could also be a string
44
+ if (resBlob instanceof Blob) {
45
+ if (!filename.endsWith('.glb')) {
46
+ filename += '.glb';
47
+ }
48
+ return new File([resBlob], filename);
49
+ } else {
50
+ // result was not a valid blob
51
+ return undefined;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Exports selected nodes to GLTF. This may result in more than one file, since textures are exported separately.
57
+ * @param filename name of the main (text-based) .GLTF file referring to separate texture files.
58
+ * @param exportOptions export options to be merged with default options. See {@link gltfExportOptions}.
59
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export.
60
+ */
61
+ public async exportGltfToFile(filename: string, exportOptions: IExportOptions = {}, excluded?: ExcludedGeometry) {
62
+ this.exportPreProcess();
63
+ await GLTF2Export.GLTFAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
64
+ glb => {
65
+ glb.downloadFiles();
66
+ }
67
+ );
68
+ this.exportPostProcess();
69
+ }
70
+
71
+ /**
72
+ * Exports selected nodes to GLB. This results in one binary file.
73
+ * @param filename name of the .GLB file.
74
+ * @param exportOptions export options to be merged with default options. See {@link gltfExportOptions}.
75
+ * @param excluded optional list of geometry (meshes, elements, variants, variantInstances) to be excluded from export
76
+ */
77
+ public async exportGlbToFile(filename: string, exportOptions: IExportOptions = {}, excluded?: ExcludedGeometry) {
78
+ this.exportPreProcess();
79
+ await GLTF2Export.GLBAsync(this.viewer.scene, filename, this.gltfExportOptions(exportOptions, excluded)).then(
80
+ glb => {
81
+ glb.downloadFiles();
82
+ }
83
+ );
84
+ this.exportPostProcess();
85
+ }
86
+
87
+ /**
88
+ * Gets predefined {@link IExportOptions } merged with given ones.
89
+ */
90
+ protected gltfExportOptions(mergeWithOptions: IExportOptions = {}, excluded?: ExcludedGeometry): IExportOptions {
91
+ const defaultOptions = {
92
+ // includeCoordinateSystemConversionNodes: true,
93
+ shouldExportNode: function (node: any) {
94
+ if (!node.isEnabled()) {
95
+ return false;
96
+ }
97
+ if (node.isVisible === false) {
98
+ return false;
99
+ }
100
+ if (node.name === '__bounding_box__') {
101
+ return false;
102
+ }
103
+ if (excluded && node instanceof Mesh && isMeshIncludedInExclusionList(node, excluded)) {
104
+ return false;
105
+ }
106
+ return true;
107
+ },
108
+ };
109
+ return merge({}, defaultOptions, mergeWithOptions);
110
+ }
111
+
112
+ /**
113
+ * Stuff to be done before exporting to GLTF
114
+ */
115
+ protected exportPreProcess() {
116
+ this.viewer.pauseRendering();
117
+ this.exchangeRefractionMaterials();
118
+ }
119
+
120
+ /**
121
+ * Stuff to be done after the GLTF export
122
+ */
123
+ protected exportPostProcess() {
124
+ this.restoreRefractionMaterials();
125
+ this.viewer.resumeRendering();
126
+ }
127
+
128
+ /**
129
+ * Materials with refraction set are not exported properly.
130
+ * Exchange all such (relevant) materials with a more export-friendly version
131
+ */
132
+ protected exchangeRefractionMaterials() {
133
+ for (const n of this.viewer.scene.getNodes()) {
134
+ if (!(n instanceof AbstractMesh)) continue;
135
+ if (!(n.material instanceof PBRMaterial)) continue;
136
+ if (!(n.material as PBRMaterial).subSurface.isRefractionEnabled) continue;
137
+ if ((n.material as PBRMaterial).transparencyMode !== PBRMaterial.PBRMATERIAL_OPAQUE) continue;
138
+ // if we're here, we have a node holding a material with set refraction whose transparencyMode is set to PBRMATERIAL_OPAQUE
139
+ n.material = this.createRefractionMaterialReplacement(n.material);
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Restore original materials with set refraction.
145
+ */
146
+ protected restoreRefractionMaterials() {
147
+ for (const n of this.viewer.scene.getNodes()) {
148
+ if (!(n instanceof AbstractMesh)) continue;
149
+ if (!(n.material instanceof PBRMaterial)) continue;
150
+ if (!this.isMaterialClonedForExport(n.material)) continue;
151
+ // at this point we have a pbrmaterial tagged as cloned in its metadata that's set on a mesh
152
+
153
+ // restore and dispose
154
+ const currMaterial = n.material;
155
+ const prevMaterial = this.viewer.scene.getMaterialByUniqueID(n.material.metadata.clonedFrom);
156
+ if (prevMaterial) {
157
+ n.material = prevMaterial; // restore previous material
158
+ currMaterial.dispose(false, true); // dispose of clone
159
+ }
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Create an export-friendly replacement material for a material using refraction.
165
+ * @param mat Material to be replaced
166
+ */
167
+ protected createRefractionMaterialReplacement(mat: PBRMaterial): PBRMaterial {
168
+ // if we're dealing with a clone already, return it instead of cloning
169
+ if (this.isMaterialClonedForExport(mat)) return mat;
170
+
171
+ // change material according to https://www.notion.so/combeenation/Glas-materials-don-t-look-glasy-after-export-d5fda2c6515e4420a8772744d3e6b460
172
+ let clonedMaterial = mat.clone(mat.name); // clone material. clone uses same name
173
+ 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
174
+ clonedMaterial.refractionTexture = null;
175
+ clonedMaterial.metallicReflectanceTexture = null; // is this the correct one for metallic roughness?
176
+ clonedMaterial.alpha = 0.7;
177
+ clonedMaterial.albedoColor = new Color3(0.3, 0.3, 0.3);
178
+ clonedMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
179
+ clonedMaterial.metallic = 0.65;
180
+ clonedMaterial.roughness = 0.15;
181
+
182
+ return clonedMaterial;
183
+ }
184
+
185
+ /**
186
+ * Inspect if a material was temporarily cloned for GLB export
187
+ * @param mat Material to be inspected
188
+ */
189
+ protected isMaterialClonedForExport(mat: PBRMaterial): boolean {
190
+ return mat.metadata.clonedFrom;
191
+ }
192
+ }
@@ -29,7 +29,7 @@ type ArcRotateCamera = import('@babylonjs/core/Cameras/arcRotateCamera').ArcRota
29
29
  type IScreenshotSize = import('@babylonjs/core/Misc/interfaces/screenshotSize').IScreenshotSize;
30
30
  type BabylonAnimation = import('@babylonjs/core/Animations/animation').Animation;
31
31
  type CubeTexture = import('@babylonjs/core/Materials/Textures/cubeTexture').CubeTexture;
32
- type MeshBuilder = import('@babylonjs/core/Meshes/meshBuilder').MeshBuilder;
32
+ type MeshBuilder = typeof import('@babylonjs/core/Meshes/meshBuilder').MeshBuilder;
33
33
  type Texture = import('@babylonjs/core/Materials/Textures/texture').Texture;
34
34
  type HemisphericLight = import('@babylonjs/core/Lights/hemisphericLight').HemisphericLight;
35
35
  type DirectionalLight = import('@babylonjs/core/Lights/directionalLight').DirectionalLight;
@@ -232,6 +232,11 @@ type ScreenshotSettings = {
232
232
  renderSprites?: boolean;
233
233
  };
234
234
 
235
+ /**
236
+ * Use this to define geometry to be excluded from autofocus, GLB export, etc.
237
+ */
238
+ type ExcludedGeometry = (Mesh | VariantInstance | Variant | VariantElement)[];
239
+
235
240
  type AutofocusSettings = {
236
241
  /**
237
242
  * Can be used to customize the margins shown around the 3d model when calling {@link autofocusActiveCamera}.\
@@ -247,6 +252,8 @@ type AutofocusSettings = {
247
252
  beta?: number;
248
253
  /** Optional animation for the focusing camera movement */
249
254
  animation?: string | AnimationDefinition;
255
+ /** Optional list of geometry to be excluded from consideration */
256
+ exclude?: ExcludedGeometry;
250
257
  };
251
258
 
252
259
  type LightDefinitions = {
@@ -0,0 +1,43 @@
1
+ import { Mesh } from '@babylonjs/core/Meshes/mesh';
2
+ import { Variant } from '../classes/variant';
3
+ import { VariantInstance } from '../classes/variantInstance';
4
+ import { Element } from '../classes/element';
5
+
6
+ /**
7
+ * Find out if a mesh is part of a list of excluded geometry
8
+ * @param mesh BJS mesh
9
+ * @param list list of excluded geometry
10
+ * @returns boolean based on whether mesh was found in list
11
+ */
12
+ const isMeshIncludedInExclusionList = function (mesh: Mesh, list: ExcludedGeometry): boolean {
13
+ const checkMesh = (inputMesh: Mesh, meshToCheck: Mesh) => {
14
+ return inputMesh.uniqueId === meshToCheck.uniqueId;
15
+ };
16
+ const checkElement = (inputEl: Element, meshToCheck: Mesh) => {
17
+ return inputEl.meshesFlat.some(m => checkMesh(m, meshToCheck));
18
+ };
19
+ const checkVariant = (inputVariant: Variant, meshToCheck: Mesh) => {
20
+ return inputVariant.elements.some(el => checkElement(el, meshToCheck));
21
+ };
22
+ const checkVariantInstance = (inputVarInst: VariantInstance, meshToCheck: Mesh) => {
23
+ return inputVarInst.variant.elements.some(el => checkElement(el, meshToCheck));
24
+ };
25
+ const isExcluded = list.some(geometryToExclude => {
26
+ if (geometryToExclude instanceof VariantInstance) {
27
+ return checkVariantInstance(geometryToExclude, mesh);
28
+ }
29
+ if (geometryToExclude instanceof Variant) {
30
+ return checkVariant(geometryToExclude, mesh);
31
+ }
32
+ if (geometryToExclude instanceof Element) {
33
+ return checkElement(geometryToExclude, mesh);
34
+ }
35
+ if (geometryToExclude instanceof Mesh) {
36
+ return checkMesh(geometryToExclude, mesh);
37
+ }
38
+ return false;
39
+ });
40
+ return isExcluded;
41
+ };
42
+
43
+ export { isMeshIncludedInExclusionList };