@combeenation/3d-viewer 14.0.0 → 14.0.1-rc1

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 (65) hide show
  1. package/README.md +9 -9
  2. package/dist/lib-cjs/buildinfo.json +3 -3
  3. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  4. package/dist/lib-cjs/index.d.ts +62 -62
  5. package/dist/lib-cjs/index.js +94 -94
  6. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.d.ts +10 -10
  7. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js +131 -131
  8. package/dist/lib-cjs/internal/cloning-helper.d.ts +19 -19
  9. package/dist/lib-cjs/internal/cloning-helper.js +163 -163
  10. package/dist/lib-cjs/internal/device-helper.d.ts +9 -9
  11. package/dist/lib-cjs/internal/device-helper.js +24 -24
  12. package/dist/lib-cjs/internal/geometry-helper.d.ts +21 -21
  13. package/dist/lib-cjs/internal/geometry-helper.js +145 -145
  14. package/dist/lib-cjs/internal/metadata-helper.d.ts +26 -26
  15. package/dist/lib-cjs/internal/metadata-helper.js +50 -50
  16. package/dist/lib-cjs/internal/paintable-helper.d.ts +40 -40
  17. package/dist/lib-cjs/internal/paintable-helper.js +286 -286
  18. package/dist/lib-cjs/internal/tags-helper.d.ts +12 -12
  19. package/dist/lib-cjs/internal/tags-helper.js +37 -37
  20. package/dist/lib-cjs/manager/camera-manager.d.ts +110 -110
  21. package/dist/lib-cjs/manager/camera-manager.js +206 -206
  22. package/dist/lib-cjs/manager/debug-manager.d.ts +60 -60
  23. package/dist/lib-cjs/manager/debug-manager.js +217 -217
  24. package/dist/lib-cjs/manager/event-manager.d.ts +52 -52
  25. package/dist/lib-cjs/manager/event-manager.js +71 -71
  26. package/dist/lib-cjs/manager/gltf-export-manager.d.ts +84 -75
  27. package/dist/lib-cjs/manager/gltf-export-manager.js +290 -278
  28. package/dist/lib-cjs/manager/gltf-export-manager.js.map +1 -1
  29. package/dist/lib-cjs/manager/material-manager.d.ts +35 -35
  30. package/dist/lib-cjs/manager/material-manager.js +125 -125
  31. package/dist/lib-cjs/manager/model-manager.d.ts +145 -145
  32. package/dist/lib-cjs/manager/model-manager.js +382 -382
  33. package/dist/lib-cjs/manager/parameter-manager.d.ts +210 -210
  34. package/dist/lib-cjs/manager/parameter-manager.js +514 -514
  35. package/dist/lib-cjs/manager/scene-manager.d.ts +45 -45
  36. package/dist/lib-cjs/manager/scene-manager.js +64 -64
  37. package/dist/lib-cjs/manager/texture-manager.d.ts +12 -12
  38. package/dist/lib-cjs/manager/texture-manager.js +43 -43
  39. package/dist/lib-cjs/viewer-error.d.ts +48 -48
  40. package/dist/lib-cjs/viewer-error.js +60 -60
  41. package/dist/lib-cjs/viewer.d.ts +115 -115
  42. package/dist/lib-cjs/viewer.js +217 -217
  43. package/package.json +91 -91
  44. package/src/buildinfo.json +3 -3
  45. package/src/dev.ts +47 -47
  46. package/src/global-types.d.ts +39 -39
  47. package/src/index.ts +81 -81
  48. package/src/internal/cbn-custom-babylon-loader-plugin.ts +159 -159
  49. package/src/internal/cloning-helper.ts +225 -225
  50. package/src/internal/device-helper.ts +25 -25
  51. package/src/internal/geometry-helper.ts +181 -181
  52. package/src/internal/metadata-helper.ts +63 -63
  53. package/src/internal/paintable-helper.ts +310 -310
  54. package/src/internal/tags-helper.ts +41 -41
  55. package/src/manager/camera-manager.ts +365 -365
  56. package/src/manager/debug-manager.ts +245 -245
  57. package/src/manager/event-manager.ts +72 -72
  58. package/src/manager/gltf-export-manager.ts +357 -341
  59. package/src/manager/material-manager.ts +135 -135
  60. package/src/manager/model-manager.ts +458 -458
  61. package/src/manager/parameter-manager.ts +652 -652
  62. package/src/manager/scene-manager.ts +101 -101
  63. package/src/manager/texture-manager.ts +32 -32
  64. package/src/viewer-error.ts +68 -68
  65. package/src/viewer.ts +290 -290
@@ -1,135 +1,135 @@
1
- import {
2
- AbstractMesh,
3
- BaseTexture,
4
- Material,
5
- StandardMaterial,
6
- Viewer,
7
- ViewerError,
8
- ViewerErrorIds,
9
- ViewerEvent,
10
- } from '../index';
11
- import {
12
- clearInternalMetadataValue,
13
- getInternalMetadataValue,
14
- setInternalMetadataValue,
15
- } from '../internal/metadata-helper';
16
-
17
- /**
18
- * Manager for material related tasks
19
- */
20
- export class MaterialManager {
21
- /**
22
- * CAUTION: this has to be in sync with the Combeenation backend!
23
- * @internal
24
- */
25
- public static readonly CBN_FALLBACK_MATERIAL_NAME = '$fallback';
26
-
27
- protected _createMaterialPromises: { [materialId: string]: Promise<Material | null> } = {};
28
-
29
- /** @internal */
30
- public constructor(protected viewer: Viewer) {}
31
-
32
- /**
33
- * Assigns material with certain id to desired mesh.\
34
- * Creates the material if not already available (see {@link getOrCreateMaterial}).
35
- */
36
- public async setMaterialOnMesh(materialId: string, mesh: AbstractMesh): Promise<void> {
37
- setInternalMetadataValue(mesh, 'materialToBeSet', materialId);
38
-
39
- const material = await this.getOrCreateMaterial(materialId, mesh);
40
-
41
- // there may have been later "setMaterialOnMesh" calls with faster material creation
42
- // make sure to assign the latest material!
43
- if (getInternalMetadataValue(mesh, 'materialToBeSet') === materialId) {
44
- mesh.material = material;
45
- clearInternalMetadataValue(mesh, 'materialToBeSet');
46
- clearInternalMetadataValue(mesh, 'deferredMaterial');
47
- }
48
- }
49
-
50
- /**
51
- * Returns material with certain id if already available in the scene.\
52
- * If this is not the case material is created from a
53
- * "[Material asset](https://docs.combeenation.com/docs/howto-create-and-use-babylon-and-material-asset)"
54
- * on the Combeenation server.\
55
- * Waits until textures of material are fully loaded and shader is compiled. In this way "flickering" effects
56
- * will be avoided, since the material would be incomplete without its loaded textures.
57
- *
58
- * @param mesh Required for shader compilation check, can be omitted if this check should not be done.\
59
- * Use {@link setMaterialOnMesh} instead if the material should be applied on the mesh immediately.
60
- */
61
- public async getOrCreateMaterial(materialId: string, mesh?: AbstractMesh): Promise<Material> {
62
- let chosenMaterial: Material | null = this.viewer.scene.materials.find(mat => mat.id === materialId) ?? null;
63
-
64
- if (!chosenMaterial) {
65
- // material not available yet, request it from Combeenation assets
66
- const existingCreationProm = this._createMaterialPromises[materialId];
67
- if (existingCreationProm !== undefined) {
68
- // request is already pending, just wait for the result
69
- chosenMaterial = await existingCreationProm;
70
- } else {
71
- this.viewer.eventManager.fireEvent(ViewerEvent.MaterialCreationStart, materialId);
72
-
73
- // request not pending, call the dedicated function
74
- const newCreationProm = this._createMaterial(materialId, mesh);
75
- // store the promise in a global map, so that subsequent requests can reference it
76
- this._createMaterialPromises[materialId] = newCreationProm;
77
- chosenMaterial = await newCreationProm;
78
-
79
- delete this._createMaterialPromises[materialId];
80
-
81
- const allMaterialsCreated = !Object.entries(this._createMaterialPromises).length;
82
- // signalize event system that material is created now
83
- // also check if there are other material creation processes still pending, as this is an important information
84
- // e.g. for showing load masks
85
- this.viewer.eventManager.fireEvent(ViewerEvent.MaterialCreationEnd, materialId, allMaterialsCreated);
86
- }
87
- }
88
-
89
- return chosenMaterial as Material;
90
- }
91
-
92
- protected async _createMaterial(materialId: string, mesh?: AbstractMesh): Promise<Material> {
93
- if (materialId === MaterialManager.CBN_FALLBACK_MATERIAL_NAME) {
94
- const fallbackMaterial = new StandardMaterial(MaterialManager.CBN_FALLBACK_MATERIAL_NAME, this.viewer.scene);
95
- fallbackMaterial.disableLighting = true;
96
-
97
- return fallbackMaterial;
98
- }
99
-
100
- const materialDefinition = await window.Cbn?.Assets.getMaterial(materialId);
101
- // The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
102
- // `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
103
- // within the material JSON definition
104
- const material = materialDefinition && Material.Parse(materialDefinition, this.viewer.scene, '');
105
-
106
- if (!material) {
107
- throw new ViewerError({
108
- id: ViewerErrorIds.MaterialCouldNotBeParsed,
109
- message: `Material with id "${materialId}" could not be parsed`,
110
- });
111
- }
112
-
113
- await this.viewer.parameterManager.applyParameterValuesToMaterial(material);
114
-
115
- const matReadyProms = [
116
- new Promise<void>(resolve => BaseTexture.WhenAllReady(material.getActiveTextures(), resolve)),
117
- ];
118
-
119
- if (mesh) {
120
- // this promise should only take some time on the first call of the corresponding shader (eg: PBRMaterial shader)
121
- // on each other call of the same material/shader type there should be not be a waiting time, or at maximum one
122
- // frame
123
- // https://forum.babylonjs.com/t/mesh-flickering-when-setting-material-initially/37312
124
- matReadyProms.push(material.forceCompilationAsync(mesh));
125
- }
126
-
127
- // material needs to fulfill 2 criterias before it's ready to use
128
- // - textures need to be "ready" => downloaded
129
- // - dedicated shader needs to be compiled
130
- // if this would not be the case you can see some "flickering" when setting the material
131
- await Promise.all(matReadyProms);
132
-
133
- return material;
134
- }
135
- }
1
+ import {
2
+ AbstractMesh,
3
+ BaseTexture,
4
+ Material,
5
+ StandardMaterial,
6
+ Viewer,
7
+ ViewerError,
8
+ ViewerErrorIds,
9
+ ViewerEvent,
10
+ } from '../index';
11
+ import {
12
+ clearInternalMetadataValue,
13
+ getInternalMetadataValue,
14
+ setInternalMetadataValue,
15
+ } from '../internal/metadata-helper';
16
+
17
+ /**
18
+ * Manager for material related tasks
19
+ */
20
+ export class MaterialManager {
21
+ /**
22
+ * CAUTION: this has to be in sync with the Combeenation backend!
23
+ * @internal
24
+ */
25
+ public static readonly CBN_FALLBACK_MATERIAL_NAME = '$fallback';
26
+
27
+ protected _createMaterialPromises: { [materialId: string]: Promise<Material | null> } = {};
28
+
29
+ /** @internal */
30
+ public constructor(protected viewer: Viewer) {}
31
+
32
+ /**
33
+ * Assigns material with certain id to desired mesh.\
34
+ * Creates the material if not already available (see {@link getOrCreateMaterial}).
35
+ */
36
+ public async setMaterialOnMesh(materialId: string, mesh: AbstractMesh): Promise<void> {
37
+ setInternalMetadataValue(mesh, 'materialToBeSet', materialId);
38
+
39
+ const material = await this.getOrCreateMaterial(materialId, mesh);
40
+
41
+ // there may have been later "setMaterialOnMesh" calls with faster material creation
42
+ // make sure to assign the latest material!
43
+ if (getInternalMetadataValue(mesh, 'materialToBeSet') === materialId) {
44
+ mesh.material = material;
45
+ clearInternalMetadataValue(mesh, 'materialToBeSet');
46
+ clearInternalMetadataValue(mesh, 'deferredMaterial');
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Returns material with certain id if already available in the scene.\
52
+ * If this is not the case material is created from a
53
+ * "[Material asset](https://docs.combeenation.com/docs/howto-create-and-use-babylon-and-material-asset)"
54
+ * on the Combeenation server.\
55
+ * Waits until textures of material are fully loaded and shader is compiled. In this way "flickering" effects
56
+ * will be avoided, since the material would be incomplete without its loaded textures.
57
+ *
58
+ * @param mesh Required for shader compilation check, can be omitted if this check should not be done.\
59
+ * Use {@link setMaterialOnMesh} instead if the material should be applied on the mesh immediately.
60
+ */
61
+ public async getOrCreateMaterial(materialId: string, mesh?: AbstractMesh): Promise<Material> {
62
+ let chosenMaterial: Material | null = this.viewer.scene.materials.find(mat => mat.id === materialId) ?? null;
63
+
64
+ if (!chosenMaterial) {
65
+ // material not available yet, request it from Combeenation assets
66
+ const existingCreationProm = this._createMaterialPromises[materialId];
67
+ if (existingCreationProm !== undefined) {
68
+ // request is already pending, just wait for the result
69
+ chosenMaterial = await existingCreationProm;
70
+ } else {
71
+ this.viewer.eventManager.fireEvent(ViewerEvent.MaterialCreationStart, materialId);
72
+
73
+ // request not pending, call the dedicated function
74
+ const newCreationProm = this._createMaterial(materialId, mesh);
75
+ // store the promise in a global map, so that subsequent requests can reference it
76
+ this._createMaterialPromises[materialId] = newCreationProm;
77
+ chosenMaterial = await newCreationProm;
78
+
79
+ delete this._createMaterialPromises[materialId];
80
+
81
+ const allMaterialsCreated = !Object.entries(this._createMaterialPromises).length;
82
+ // signalize event system that material is created now
83
+ // also check if there are other material creation processes still pending, as this is an important information
84
+ // e.g. for showing load masks
85
+ this.viewer.eventManager.fireEvent(ViewerEvent.MaterialCreationEnd, materialId, allMaterialsCreated);
86
+ }
87
+ }
88
+
89
+ return chosenMaterial as Material;
90
+ }
91
+
92
+ protected async _createMaterial(materialId: string, mesh?: AbstractMesh): Promise<Material> {
93
+ if (materialId === MaterialManager.CBN_FALLBACK_MATERIAL_NAME) {
94
+ const fallbackMaterial = new StandardMaterial(MaterialManager.CBN_FALLBACK_MATERIAL_NAME, this.viewer.scene);
95
+ fallbackMaterial.disableLighting = true;
96
+
97
+ return fallbackMaterial;
98
+ }
99
+
100
+ const materialDefinition = await window.Cbn?.Assets.getMaterial(materialId);
101
+ // The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
102
+ // `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
103
+ // within the material JSON definition
104
+ const material = materialDefinition && Material.Parse(materialDefinition, this.viewer.scene, '');
105
+
106
+ if (!material) {
107
+ throw new ViewerError({
108
+ id: ViewerErrorIds.MaterialCouldNotBeParsed,
109
+ message: `Material with id "${materialId}" could not be parsed`,
110
+ });
111
+ }
112
+
113
+ await this.viewer.parameterManager.applyParameterValuesToMaterial(material);
114
+
115
+ const matReadyProms = [
116
+ new Promise<void>(resolve => BaseTexture.WhenAllReady(material.getActiveTextures(), resolve)),
117
+ ];
118
+
119
+ if (mesh) {
120
+ // this promise should only take some time on the first call of the corresponding shader (eg: PBRMaterial shader)
121
+ // on each other call of the same material/shader type there should be not be a waiting time, or at maximum one
122
+ // frame
123
+ // https://forum.babylonjs.com/t/mesh-flickering-when-setting-material-initially/37312
124
+ matReadyProms.push(material.forceCompilationAsync(mesh));
125
+ }
126
+
127
+ // material needs to fulfill 2 criterias before it's ready to use
128
+ // - textures need to be "ready" => downloaded
129
+ // - dedicated shader needs to be compiled
130
+ // if this would not be the case you can see some "flickering" when setting the material
131
+ await Promise.all(matReadyProms);
132
+
133
+ return material;
134
+ }
135
+ }