@combeenation/3d-viewer 12.4.1 → 12.4.3

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 (114) hide show
  1. package/README.md +9 -9
  2. package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
  3. package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
  4. package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
  5. package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
  6. package/dist/lib-cjs/api/classes/element.d.ts +153 -153
  7. package/dist/lib-cjs/api/classes/element.js +702 -702
  8. package/dist/lib-cjs/api/classes/event.d.ts +401 -401
  9. package/dist/lib-cjs/api/classes/event.js +424 -424
  10. package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
  11. package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
  12. package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
  13. package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
  14. package/dist/lib-cjs/api/classes/parameter.d.ts +410 -410
  15. package/dist/lib-cjs/api/classes/parameter.js +642 -642
  16. package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
  17. package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
  18. package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
  19. package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
  20. package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
  21. package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
  22. package/dist/lib-cjs/api/classes/variant.d.ts +261 -261
  23. package/dist/lib-cjs/api/classes/variant.js +872 -872
  24. package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
  25. package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
  26. package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
  27. package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -86
  28. package/dist/lib-cjs/api/classes/viewer.d.ts +215 -215
  29. package/dist/lib-cjs/api/classes/viewer.js +708 -708
  30. package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -43
  31. package/dist/lib-cjs/api/classes/viewerError.js +55 -55
  32. package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
  33. package/dist/lib-cjs/api/classes/viewerLight.js +344 -344
  34. package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
  35. package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
  36. package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
  37. package/dist/lib-cjs/api/internal/sceneSetup.js +227 -227
  38. package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
  39. package/dist/lib-cjs/api/manager/animationManager.js +126 -126
  40. package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +80 -80
  41. package/dist/lib-cjs/api/manager/gltfExportManager.js +300 -299
  42. package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
  43. package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
  44. package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
  45. package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
  46. package/dist/lib-cjs/api/manager/tagManager.js +530 -530
  47. package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
  48. package/dist/lib-cjs/api/manager/textureLoadManager.js +107 -107
  49. package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
  50. package/dist/lib-cjs/api/manager/variantInstanceManager.js +290 -290
  51. package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
  52. package/dist/lib-cjs/api/store/specStorage.js +65 -65
  53. package/dist/lib-cjs/api/util/babylonHelper.d.ts +238 -238
  54. package/dist/lib-cjs/api/util/babylonHelper.js +825 -825
  55. package/dist/lib-cjs/api/util/debugHelper.d.ts +9 -9
  56. package/dist/lib-cjs/api/util/debugHelper.js +93 -93
  57. package/dist/lib-cjs/api/util/deviceHelper.d.ts +9 -9
  58. package/dist/lib-cjs/api/util/deviceHelper.js +28 -28
  59. package/dist/lib-cjs/api/util/geometryHelper.d.ts +17 -17
  60. package/dist/lib-cjs/api/util/geometryHelper.js +112 -112
  61. package/dist/lib-cjs/api/util/globalTypes.d.ts +490 -490
  62. package/dist/lib-cjs/api/util/globalTypes.js +1 -1
  63. package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
  64. package/dist/lib-cjs/api/util/resourceHelper.js +214 -214
  65. package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +58 -58
  66. package/dist/lib-cjs/api/util/sceneLoaderHelper.js +228 -228
  67. package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
  68. package/dist/lib-cjs/api/util/stringHelper.js +32 -32
  69. package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
  70. package/dist/lib-cjs/api/util/structureHelper.js +57 -57
  71. package/dist/lib-cjs/buildinfo.json +3 -3
  72. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  73. package/dist/lib-cjs/index.d.ts +63 -63
  74. package/dist/lib-cjs/index.js +128 -128
  75. package/package.json +93 -92
  76. package/src/api/classes/animationInterface.ts +10 -10
  77. package/src/api/classes/dottedPath.ts +181 -181
  78. package/src/api/classes/element.ts +766 -766
  79. package/src/api/classes/event.ts +457 -457
  80. package/src/api/classes/eventBroadcaster.ts +52 -52
  81. package/src/api/classes/fuzzyMap.ts +21 -21
  82. package/src/api/classes/parameter.ts +686 -686
  83. package/src/api/classes/parameterObservable.ts +73 -73
  84. package/src/api/classes/parameterizable.ts +87 -87
  85. package/src/api/classes/placementAnimation.ts +162 -162
  86. package/src/api/classes/variant.ts +965 -965
  87. package/src/api/classes/variantInstance.ts +123 -123
  88. package/src/api/classes/variantParameterizable.ts +83 -83
  89. package/src/api/classes/viewer.ts +751 -751
  90. package/src/api/classes/viewerError.ts +63 -63
  91. package/src/api/classes/viewerLight.ts +335 -335
  92. package/src/api/internal/debugViewer.ts +90 -90
  93. package/src/api/internal/lensRendering.ts +9 -9
  94. package/src/api/internal/sceneSetup.ts +208 -208
  95. package/src/api/manager/animationManager.ts +143 -143
  96. package/src/api/manager/gltfExportManager.ts +337 -334
  97. package/src/api/manager/sceneManager.ts +134 -134
  98. package/src/api/manager/tagManager.ts +572 -572
  99. package/src/api/manager/textureLoadManager.ts +107 -107
  100. package/src/api/manager/variantInstanceManager.ts +306 -306
  101. package/src/api/store/specStorage.ts +68 -68
  102. package/src/api/util/babylonHelper.ts +915 -915
  103. package/src/api/util/debugHelper.ts +121 -121
  104. package/src/api/util/deviceHelper.ts +31 -31
  105. package/src/api/util/geometryHelper.ts +142 -142
  106. package/src/api/util/globalTypes.ts +566 -566
  107. package/src/api/util/resourceHelper.ts +201 -201
  108. package/src/api/util/sceneLoaderHelper.ts +247 -247
  109. package/src/api/util/stringHelper.ts +30 -30
  110. package/src/api/util/structureHelper.ts +62 -62
  111. package/src/buildinfo.json +3 -3
  112. package/src/dev.ts +70 -70
  113. package/src/index.ts +116 -116
  114. package/src/types.d.ts +49 -49
@@ -1,58 +1,58 @@
1
- import { ISceneLoaderPlugin } from '@babylonjs/core/Loading/sceneLoader';
2
- import { Material } from '@babylonjs/core/Materials/material';
3
- import { AssetContainer } from '@babylonjs/core/assetContainer';
4
- import { Node as BjsNode } from '@babylonjs/core/node';
5
- import { Scene } from '@babylonjs/core/scene';
6
- export declare const missingMaterialMetadataName = "missingMaterial";
7
- export declare const parsedMaterialIdMetadataName = "parsedMaterialId";
8
- /**
9
- * Create and return a custom loader plugin to be registered with SceneLoader, that allows
10
- * us to run our own code against the input data before using the standard procedure to
11
- * import.
12
- * @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
13
- * @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
14
- */
15
- export declare const getCustomCbnBabylonLoaderPlugin: (previousLoaderPlugin: ISceneLoaderPlugin) => ISceneLoaderPlugin;
16
- /**
17
- * Return an observer to be applied to meshes in order to post-load missing materials
18
- * upon set enabled/visible.
19
- *
20
- * @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
21
- * @returns observer
22
- */
23
- export declare const getMaterialPostLoadObserver: (concerningMesh: Mesh) => () => Promise<void>;
24
- /**
25
- * Internal function that compares the original meshes on a .babylon file with what was loaded,
26
- * and tags missing materials with respective metadata on respective meshes.
27
- * @param dataParsed original data
28
- * @param container loaded data
29
- */
30
- export declare const addMissingMaterialMetadata: (dataParsed: unknown, container: AssetContainer) => void;
31
- /**
32
- * Help function for manipulating tags of instances meshes after parsing.
33
- * Per default babylon attaches the tags of the source mesh to the instance, **but only** if no tags are set for the
34
- * instanced mesh. If the instanced mesh has dedicated tags set, the ones from the source mesh are **not** copied over.
35
- * In this case it's not possible to have tags on the source mesh but not on the instance, which is a problem with our
36
- * tagging system in the viewer and the Combeenation asset editor as well.
37
- * This function rejects the default tag import algorithm and just copies the tags of the original parsed node without
38
- * any parent synchronization.
39
- *
40
- * @param dataParsed original data
41
- * @param container loaded data
42
- */
43
- export declare const reconstructTagsForInstancedMeshes: (dataParsed: unknown, container: AssetContainer) => void;
44
- /**
45
- * Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
46
- * The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
47
- * enabled.
48
- */
49
- export declare const addMissingMaterialObserver: (node: BjsNode) => void;
50
- export declare const removeMissingMaterialObserver: (node: BjsNode) => void;
51
- /**
52
- * Look up the provided materials (see library import) and create and return one if found.
53
- *
54
- * @param materialId Babylon.js material-id. E.g. 'concrete".
55
- * @param scene Babylon.js scene
56
- * @returns PBRMaterial | null
57
- */
58
- export declare const createMaterialFromCbnAssets: (materialId: string, scene: Scene) => Promise<Material | null>;
1
+ import { ISceneLoaderPlugin } from '@babylonjs/core/Loading/sceneLoader';
2
+ import { Material } from '@babylonjs/core/Materials/material';
3
+ import { AssetContainer } from '@babylonjs/core/assetContainer';
4
+ import { Node as BjsNode } from '@babylonjs/core/node';
5
+ import { Scene } from '@babylonjs/core/scene';
6
+ export declare const missingMaterialMetadataName = "missingMaterial";
7
+ export declare const parsedMaterialIdMetadataName = "parsedMaterialId";
8
+ /**
9
+ * Create and return a custom loader plugin to be registered with SceneLoader, that allows
10
+ * us to run our own code against the input data before using the standard procedure to
11
+ * import.
12
+ * @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
13
+ * @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
14
+ */
15
+ export declare const getCustomCbnBabylonLoaderPlugin: (previousLoaderPlugin: ISceneLoaderPlugin) => ISceneLoaderPlugin;
16
+ /**
17
+ * Return an observer to be applied to meshes in order to post-load missing materials
18
+ * upon set enabled/visible.
19
+ *
20
+ * @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
21
+ * @returns observer
22
+ */
23
+ export declare const getMaterialPostLoadObserver: (concerningMesh: Mesh) => () => Promise<void>;
24
+ /**
25
+ * Internal function that compares the original meshes on a .babylon file with what was loaded,
26
+ * and tags missing materials with respective metadata on respective meshes.
27
+ * @param dataParsed original data
28
+ * @param container loaded data
29
+ */
30
+ export declare const addMissingMaterialMetadata: (dataParsed: unknown, container: AssetContainer) => void;
31
+ /**
32
+ * Help function for manipulating tags of instances meshes after parsing.
33
+ * Per default babylon attaches the tags of the source mesh to the instance, **but only** if no tags are set for the
34
+ * instanced mesh. If the instanced mesh has dedicated tags set, the ones from the source mesh are **not** copied over.
35
+ * In this case it's not possible to have tags on the source mesh but not on the instance, which is a problem with our
36
+ * tagging system in the viewer and the Combeenation asset editor as well.
37
+ * This function rejects the default tag import algorithm and just copies the tags of the original parsed node without
38
+ * any parent synchronization.
39
+ *
40
+ * @param dataParsed original data
41
+ * @param container loaded data
42
+ */
43
+ export declare const reconstructTagsForInstancedMeshes: (dataParsed: unknown, container: AssetContainer) => void;
44
+ /**
45
+ * Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
46
+ * The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
47
+ * enabled.
48
+ */
49
+ export declare const addMissingMaterialObserver: (node: BjsNode) => void;
50
+ export declare const removeMissingMaterialObserver: (node: BjsNode) => void;
51
+ /**
52
+ * Look up the provided materials (see library import) and create and return one if found.
53
+ *
54
+ * @param materialId Babylon.js material-id. E.g. 'concrete".
55
+ * @param scene Babylon.js scene
56
+ * @returns PBRMaterial | null
57
+ */
58
+ export declare const createMaterialFromCbnAssets: (materialId: string, scene: Scene) => Promise<Material | null>;
@@ -1,229 +1,229 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.createMaterialFromCbnAssets = exports.removeMissingMaterialObserver = exports.addMissingMaterialObserver = exports.reconstructTagsForInstancedMeshes = exports.addMissingMaterialMetadata = exports.getMaterialPostLoadObserver = exports.getCustomCbnBabylonLoaderPlugin = exports.parsedMaterialIdMetadataName = exports.missingMaterialMetadataName = void 0;
16
- const event_1 = require("../classes/event");
17
- const babylonHelper_1 = require("./babylonHelper");
18
- const resourceHelper_1 = require("./resourceHelper");
19
- const material_1 = require("@babylonjs/core/Materials/material");
20
- const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
21
- const tags_1 = require("@babylonjs/core/Misc/tags");
22
- const lodash_es_1 = require("lodash-es");
23
- const has_1 = __importDefault(require("lodash-es/has"));
24
- // map for keeping track of active "node enable" observers
25
- const enableObserverMap = {};
26
- exports.missingMaterialMetadataName = 'missingMaterial';
27
- exports.parsedMaterialIdMetadataName = 'parsedMaterialId';
28
- /**
29
- * Create and return a custom loader plugin to be registered with SceneLoader, that allows
30
- * us to run our own code against the input data before using the standard procedure to
31
- * import.
32
- * @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
33
- * @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
34
- */
35
- const getCustomCbnBabylonLoaderPlugin = function (previousLoaderPlugin) {
36
- const customLoader = {
37
- name: 'cbnCustomBabylonLoader',
38
- extensions: '.babylon',
39
- importMesh: previousLoaderPlugin.importMesh,
40
- load: previousLoaderPlugin.load,
41
- loadAssetContainer: function (scene, data, rootUrl, onError) {
42
- //* 1) --- manipulate ORIGINAL data
43
- const dataParsed = JSON.parse(data);
44
- //* 2) --- call default (non-custom) loading method
45
- const importedContainer = previousLoaderPlugin.loadAssetContainer(scene, data, rootUrl);
46
- //* 3) --- manipulate IMPORTED data
47
- (0, exports.addMissingMaterialMetadata)(dataParsed, importedContainer);
48
- (0, exports.reconstructTagsForInstancedMeshes)(dataParsed, importedContainer);
49
- //* 4) --- return imported data
50
- return importedContainer;
51
- },
52
- };
53
- return customLoader;
54
- };
55
- exports.getCustomCbnBabylonLoaderPlugin = getCustomCbnBabylonLoaderPlugin;
56
- /**
57
- * Return an observer to be applied to meshes in order to post-load missing materials
58
- * upon set enabled/visible.
59
- *
60
- * @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
61
- * @returns observer
62
- */
63
- const getMaterialPostLoadObserver = function (concerningMesh) {
64
- return () => __awaiter(this, void 0, void 0, function* () {
65
- var _a;
66
- const scene = concerningMesh.getScene();
67
- // can't check `isEnabled` immediatly, since the enabled state of parents and childs is not synced yet
68
- // postpone one cycle to ensure a correct parent-child enable relation
69
- yield (0, resourceHelper_1.sleep)(0);
70
- const hasBeenEnabled = concerningMesh.isEnabled(true);
71
- const materialMissing = (0, has_1.default)(concerningMesh.metadata, exports.missingMaterialMetadataName);
72
- if (!hasBeenEnabled || !materialMissing)
73
- return;
74
- // get id of missing material
75
- const missingMatId = concerningMesh.metadata[exports.missingMaterialMetadataName];
76
- // get material and apply it on the concerning mesh after all textures have been loaded
77
- const material = yield (0, babylonHelper_1.getOrCreateMaterial)(concerningMesh.getScene(), missingMatId);
78
- (0, babylonHelper_1.applyMaterial)(material, concerningMesh).then(() => event_1.emitter.emit(event_1.Event.MESH_MATERIAL_APPLIED, concerningMesh, material));
79
- // since the material is there now, we do not need the related metadata tag anymore
80
- delete concerningMesh.metadata[exports.missingMaterialMetadataName];
81
- // remove all "enable" observers that were assigned to the concerning mesh
82
- // the mesh got visible and therefore the observers are not needed anymore
83
- (_a = enableObserverMap[concerningMesh.id]) === null || _a === void 0 ? void 0 : _a.forEach(entry => {
84
- const currNode = scene.getMeshById(entry.currNodeId);
85
- currNode === null || currNode === void 0 ? void 0 : currNode.onEnabledStateChangedObservable.remove(entry.observer);
86
- });
87
- // also remove from the local observer map
88
- delete enableObserverMap[concerningMesh.id];
89
- });
90
- };
91
- exports.getMaterialPostLoadObserver = getMaterialPostLoadObserver;
92
- function _isMeshInstanceData(data) {
93
- const hasName = (0, lodash_es_1.isString)(data.name);
94
- const hasValidTags = !data.tags || (0, lodash_es_1.isString)(data.tags);
95
- return hasName && hasValidTags;
96
- }
97
- function _isMeshData(data) {
98
- const hasName = (0, lodash_es_1.isString)(data.name);
99
- const hasValidMaterialId = !data.materialId || (0, lodash_es_1.isString)(data.materialId);
100
- return hasName && hasValidMaterialId;
101
- }
102
- function _isDataWithMeshes(data) {
103
- return data && (0, lodash_es_1.isArray)(data.meshes);
104
- }
105
- /**
106
- * Internal function that compares the original meshes on a .babylon file with what was loaded,
107
- * and tags missing materials with respective metadata on respective meshes.
108
- * @param dataParsed original data
109
- * @param container loaded data
110
- */
111
- const addMissingMaterialMetadata = function (dataParsed, container) {
112
- if (!_isDataWithMeshes(dataParsed))
113
- return;
114
- const validatedMeshes = dataParsed.meshes.filter(_isMeshData);
115
- container.meshes.forEach(importedMesh => {
116
- var _a, _b;
117
- const parsedMesh = validatedMeshes.find(mesh => mesh.name === importedMesh.name);
118
- // save original material id of the imported babylon or GLB file
119
- (0, babylonHelper_1.injectMetadata)(importedMesh, { [exports.parsedMaterialIdMetadataName]: parsedMesh === null || parsedMesh === void 0 ? void 0 : parsedMesh.materialId }, false);
120
- const materialOnImportedMesh = (_a = importedMesh.material) === null || _a === void 0 ? void 0 : _a.id;
121
- const materialOnOriginalMesh = parsedMesh === null || parsedMesh === void 0 ? void 0 : parsedMesh.materialId;
122
- if (materialOnOriginalMesh && materialOnImportedMesh !== materialOnOriginalMesh) {
123
- (_b = window.Cbn) === null || _b === void 0 ? void 0 : _b.Assets.assertMaterialExists(materialOnOriginalMesh);
124
- (0, babylonHelper_1.injectMetadata)(importedMesh, { [exports.missingMaterialMetadataName]: materialOnOriginalMesh }, false);
125
- }
126
- });
127
- };
128
- exports.addMissingMaterialMetadata = addMissingMaterialMetadata;
129
- /**
130
- * Help function for manipulating tags of instances meshes after parsing.
131
- * Per default babylon attaches the tags of the source mesh to the instance, **but only** if no tags are set for the
132
- * instanced mesh. If the instanced mesh has dedicated tags set, the ones from the source mesh are **not** copied over.
133
- * In this case it's not possible to have tags on the source mesh but not on the instance, which is a problem with our
134
- * tagging system in the viewer and the Combeenation asset editor as well.
135
- * This function rejects the default tag import algorithm and just copies the tags of the original parsed node without
136
- * any parent synchronization.
137
- *
138
- * @param dataParsed original data
139
- * @param container loaded data
140
- */
141
- const reconstructTagsForInstancedMeshes = function (dataParsed, container) {
142
- if (!_isDataWithMeshes(dataParsed))
143
- return;
144
- const validatedMeshes = dataParsed.meshes.filter(_isMeshData);
145
- container.meshes.forEach(importedMesh => {
146
- var _a;
147
- if (importedMesh instanceof instancedMesh_1.InstancedMesh) {
148
- // remove all tags from the imported mesh if there are some, since these tags are probably coming from the
149
- // source mesh, if no tags are set there is no need for further operation though
150
- const importedTags = tags_1.Tags.GetTags(importedMesh);
151
- if (importedTags) {
152
- tags_1.Tags.RemoveTagsFrom(importedMesh, importedTags);
153
- // get tags of parsed instanced mesh and set them on the imported instanced mesh
154
- const parsedSourceMesh = validatedMeshes.find(mesh => mesh.name === importedMesh.sourceMesh.name);
155
- const validatedSourceMeshInstances = (_a = parsedSourceMesh === null || parsedSourceMesh === void 0 ? void 0 : parsedSourceMesh.instances) === null || _a === void 0 ? void 0 : _a.filter(_isMeshInstanceData);
156
- const parsedInstancedMesh = validatedSourceMeshInstances === null || validatedSourceMeshInstances === void 0 ? void 0 : validatedSourceMeshInstances.find(mesh => mesh.name === importedMesh.name);
157
- const parsedTags = parsedInstancedMesh === null || parsedInstancedMesh === void 0 ? void 0 : parsedInstancedMesh.tags;
158
- if (parsedTags) {
159
- tags_1.Tags.AddTagsTo(importedMesh, parsedTags);
160
- }
161
- }
162
- }
163
- });
164
- };
165
- exports.reconstructTagsForInstancedMeshes = reconstructTagsForInstancedMeshes;
166
- /**
167
- * Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
168
- * The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
169
- * enabled.
170
- */
171
- const addMissingMaterialObserver = function (node) {
172
- // set the concerning node, i.e. the node the observer should check for missing material.
173
- // for instanced meshes, we want the sourcemesh here.
174
- const concerningNode = node instanceof instancedMesh_1.InstancedMesh ? node.sourceMesh : node;
175
- // observer is pointless if concerning node has no missing material
176
- if (!(0, has_1.default)(concerningNode.metadata, exports.missingMaterialMetadataName))
177
- return;
178
- // for each of our AbstractMeshes, set an observer on the AbstractMesh itself and all of its parents.
179
- let currNode = node;
180
- while (currNode) {
181
- const callback = (0, exports.getMaterialPostLoadObserver)(concerningNode);
182
- const observer = currNode.onEnabledStateChangedObservable.add(callback);
183
- // store the observer in a local map to keep track of the active "enable" observers
184
- // observers will be removed when the concerning node gets enabled
185
- if (!enableObserverMap[concerningNode.id]) {
186
- enableObserverMap[concerningNode.id] = [];
187
- }
188
- enableObserverMap[concerningNode.id].push({ currNodeId: currNode.id, observer });
189
- currNode = currNode.parent;
190
- }
191
- };
192
- exports.addMissingMaterialObserver = addMissingMaterialObserver;
193
- const removeMissingMaterialObserver = function (node) {
194
- var _a;
195
- // set the concerning node, i.e. the node the observer should check for missing material.
196
- // for instanced meshes, we want the sourcemesh here.
197
- const concerningNode = node instanceof instancedMesh_1.InstancedMesh ? node.sourceMesh : node;
198
- let currNode = node;
199
- while (currNode) {
200
- (_a = enableObserverMap[concerningNode.id]) === null || _a === void 0 ? void 0 : _a.forEach(entry => {
201
- if (entry.currNodeId === (currNode === null || currNode === void 0 ? void 0 : currNode.id)) {
202
- currNode.onEnabledStateChangedObservable.remove(entry.observer);
203
- }
204
- });
205
- currNode = currNode.parent;
206
- }
207
- delete enableObserverMap[concerningNode.id];
208
- };
209
- exports.removeMissingMaterialObserver = removeMissingMaterialObserver;
210
- /**
211
- * Look up the provided materials (see library import) and create and return one if found.
212
- *
213
- * @param materialId Babylon.js material-id. E.g. 'concrete".
214
- * @param scene Babylon.js scene
215
- * @returns PBRMaterial | null
216
- */
217
- const createMaterialFromCbnAssets = function (materialId, scene) {
218
- var _a;
219
- return __awaiter(this, void 0, void 0, function* () {
220
- const materialDefinition = yield ((_a = window.Cbn) === null || _a === void 0 ? void 0 : _a.Assets.getMaterial(materialId));
221
- // The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
222
- // `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
223
- // within the material JSON definition
224
- const material = materialDefinition && material_1.Material.Parse(materialDefinition, scene, '');
225
- return material || null;
226
- });
227
- };
228
- exports.createMaterialFromCbnAssets = createMaterialFromCbnAssets;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.createMaterialFromCbnAssets = exports.removeMissingMaterialObserver = exports.addMissingMaterialObserver = exports.reconstructTagsForInstancedMeshes = exports.addMissingMaterialMetadata = exports.getMaterialPostLoadObserver = exports.getCustomCbnBabylonLoaderPlugin = exports.parsedMaterialIdMetadataName = exports.missingMaterialMetadataName = void 0;
16
+ const event_1 = require("../classes/event");
17
+ const babylonHelper_1 = require("./babylonHelper");
18
+ const resourceHelper_1 = require("./resourceHelper");
19
+ const material_1 = require("@babylonjs/core/Materials/material");
20
+ const instancedMesh_1 = require("@babylonjs/core/Meshes/instancedMesh");
21
+ const tags_1 = require("@babylonjs/core/Misc/tags");
22
+ const lodash_es_1 = require("lodash-es");
23
+ const has_1 = __importDefault(require("lodash-es/has"));
24
+ // map for keeping track of active "node enable" observers
25
+ const enableObserverMap = {};
26
+ exports.missingMaterialMetadataName = 'missingMaterial';
27
+ exports.parsedMaterialIdMetadataName = 'parsedMaterialId';
28
+ /**
29
+ * Create and return a custom loader plugin to be registered with SceneLoader, that allows
30
+ * us to run our own code against the input data before using the standard procedure to
31
+ * import.
32
+ * @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
33
+ * @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
34
+ */
35
+ const getCustomCbnBabylonLoaderPlugin = function (previousLoaderPlugin) {
36
+ const customLoader = {
37
+ name: 'cbnCustomBabylonLoader',
38
+ extensions: '.babylon',
39
+ importMesh: previousLoaderPlugin.importMesh,
40
+ load: previousLoaderPlugin.load,
41
+ loadAssetContainer: function (scene, data, rootUrl, onError) {
42
+ //* 1) --- manipulate ORIGINAL data
43
+ const dataParsed = JSON.parse(data);
44
+ //* 2) --- call default (non-custom) loading method
45
+ const importedContainer = previousLoaderPlugin.loadAssetContainer(scene, data, rootUrl);
46
+ //* 3) --- manipulate IMPORTED data
47
+ (0, exports.addMissingMaterialMetadata)(dataParsed, importedContainer);
48
+ (0, exports.reconstructTagsForInstancedMeshes)(dataParsed, importedContainer);
49
+ //* 4) --- return imported data
50
+ return importedContainer;
51
+ },
52
+ };
53
+ return customLoader;
54
+ };
55
+ exports.getCustomCbnBabylonLoaderPlugin = getCustomCbnBabylonLoaderPlugin;
56
+ /**
57
+ * Return an observer to be applied to meshes in order to post-load missing materials
58
+ * upon set enabled/visible.
59
+ *
60
+ * @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
61
+ * @returns observer
62
+ */
63
+ const getMaterialPostLoadObserver = function (concerningMesh) {
64
+ return () => __awaiter(this, void 0, void 0, function* () {
65
+ var _a;
66
+ const scene = concerningMesh.getScene();
67
+ // can't check `isEnabled` immediatly, since the enabled state of parents and childs is not synced yet
68
+ // postpone one cycle to ensure a correct parent-child enable relation
69
+ yield (0, resourceHelper_1.sleep)(0);
70
+ const hasBeenEnabled = concerningMesh.isEnabled(true);
71
+ const materialMissing = (0, has_1.default)(concerningMesh.metadata, exports.missingMaterialMetadataName);
72
+ if (!hasBeenEnabled || !materialMissing)
73
+ return;
74
+ // get id of missing material
75
+ const missingMatId = concerningMesh.metadata[exports.missingMaterialMetadataName];
76
+ // get material and apply it on the concerning mesh after all textures have been loaded
77
+ const material = yield (0, babylonHelper_1.getOrCreateMaterial)(concerningMesh.getScene(), missingMatId);
78
+ (0, babylonHelper_1.applyMaterial)(material, concerningMesh).then(() => event_1.emitter.emit(event_1.Event.MESH_MATERIAL_APPLIED, concerningMesh, material));
79
+ // since the material is there now, we do not need the related metadata tag anymore
80
+ delete concerningMesh.metadata[exports.missingMaterialMetadataName];
81
+ // remove all "enable" observers that were assigned to the concerning mesh
82
+ // the mesh got visible and therefore the observers are not needed anymore
83
+ (_a = enableObserverMap[concerningMesh.id]) === null || _a === void 0 ? void 0 : _a.forEach(entry => {
84
+ const currNode = scene.getMeshById(entry.currNodeId);
85
+ currNode === null || currNode === void 0 ? void 0 : currNode.onEnabledStateChangedObservable.remove(entry.observer);
86
+ });
87
+ // also remove from the local observer map
88
+ delete enableObserverMap[concerningMesh.id];
89
+ });
90
+ };
91
+ exports.getMaterialPostLoadObserver = getMaterialPostLoadObserver;
92
+ function _isMeshInstanceData(data) {
93
+ const hasName = (0, lodash_es_1.isString)(data.name);
94
+ const hasValidTags = !data.tags || (0, lodash_es_1.isString)(data.tags);
95
+ return hasName && hasValidTags;
96
+ }
97
+ function _isMeshData(data) {
98
+ const hasName = (0, lodash_es_1.isString)(data.name);
99
+ const hasValidMaterialId = !data.materialId || (0, lodash_es_1.isString)(data.materialId);
100
+ return hasName && hasValidMaterialId;
101
+ }
102
+ function _isDataWithMeshes(data) {
103
+ return data && (0, lodash_es_1.isArray)(data.meshes);
104
+ }
105
+ /**
106
+ * Internal function that compares the original meshes on a .babylon file with what was loaded,
107
+ * and tags missing materials with respective metadata on respective meshes.
108
+ * @param dataParsed original data
109
+ * @param container loaded data
110
+ */
111
+ const addMissingMaterialMetadata = function (dataParsed, container) {
112
+ if (!_isDataWithMeshes(dataParsed))
113
+ return;
114
+ const validatedMeshes = dataParsed.meshes.filter(_isMeshData);
115
+ container.meshes.forEach(importedMesh => {
116
+ var _a, _b;
117
+ const parsedMesh = validatedMeshes.find(mesh => mesh.name === importedMesh.name);
118
+ // save original material id of the imported babylon or GLB file
119
+ (0, babylonHelper_1.injectMetadata)(importedMesh, { [exports.parsedMaterialIdMetadataName]: parsedMesh === null || parsedMesh === void 0 ? void 0 : parsedMesh.materialId }, false);
120
+ const materialOnImportedMesh = (_a = importedMesh.material) === null || _a === void 0 ? void 0 : _a.id;
121
+ const materialOnOriginalMesh = parsedMesh === null || parsedMesh === void 0 ? void 0 : parsedMesh.materialId;
122
+ if (materialOnOriginalMesh && materialOnImportedMesh !== materialOnOriginalMesh) {
123
+ (_b = window.Cbn) === null || _b === void 0 ? void 0 : _b.Assets.assertMaterialExists(materialOnOriginalMesh);
124
+ (0, babylonHelper_1.injectMetadata)(importedMesh, { [exports.missingMaterialMetadataName]: materialOnOriginalMesh }, false);
125
+ }
126
+ });
127
+ };
128
+ exports.addMissingMaterialMetadata = addMissingMaterialMetadata;
129
+ /**
130
+ * Help function for manipulating tags of instances meshes after parsing.
131
+ * Per default babylon attaches the tags of the source mesh to the instance, **but only** if no tags are set for the
132
+ * instanced mesh. If the instanced mesh has dedicated tags set, the ones from the source mesh are **not** copied over.
133
+ * In this case it's not possible to have tags on the source mesh but not on the instance, which is a problem with our
134
+ * tagging system in the viewer and the Combeenation asset editor as well.
135
+ * This function rejects the default tag import algorithm and just copies the tags of the original parsed node without
136
+ * any parent synchronization.
137
+ *
138
+ * @param dataParsed original data
139
+ * @param container loaded data
140
+ */
141
+ const reconstructTagsForInstancedMeshes = function (dataParsed, container) {
142
+ if (!_isDataWithMeshes(dataParsed))
143
+ return;
144
+ const validatedMeshes = dataParsed.meshes.filter(_isMeshData);
145
+ container.meshes.forEach(importedMesh => {
146
+ var _a;
147
+ if (importedMesh instanceof instancedMesh_1.InstancedMesh) {
148
+ // remove all tags from the imported mesh if there are some, since these tags are probably coming from the
149
+ // source mesh, if no tags are set there is no need for further operation though
150
+ const importedTags = tags_1.Tags.GetTags(importedMesh);
151
+ if (importedTags) {
152
+ tags_1.Tags.RemoveTagsFrom(importedMesh, importedTags);
153
+ // get tags of parsed instanced mesh and set them on the imported instanced mesh
154
+ const parsedSourceMesh = validatedMeshes.find(mesh => mesh.name === importedMesh.sourceMesh.name);
155
+ const validatedSourceMeshInstances = (_a = parsedSourceMesh === null || parsedSourceMesh === void 0 ? void 0 : parsedSourceMesh.instances) === null || _a === void 0 ? void 0 : _a.filter(_isMeshInstanceData);
156
+ const parsedInstancedMesh = validatedSourceMeshInstances === null || validatedSourceMeshInstances === void 0 ? void 0 : validatedSourceMeshInstances.find(mesh => mesh.name === importedMesh.name);
157
+ const parsedTags = parsedInstancedMesh === null || parsedInstancedMesh === void 0 ? void 0 : parsedInstancedMesh.tags;
158
+ if (parsedTags) {
159
+ tags_1.Tags.AddTagsTo(importedMesh, parsedTags);
160
+ }
161
+ }
162
+ }
163
+ });
164
+ };
165
+ exports.reconstructTagsForInstancedMeshes = reconstructTagsForInstancedMeshes;
166
+ /**
167
+ * Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
168
+ * The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
169
+ * enabled.
170
+ */
171
+ const addMissingMaterialObserver = function (node) {
172
+ // set the concerning node, i.e. the node the observer should check for missing material.
173
+ // for instanced meshes, we want the sourcemesh here.
174
+ const concerningNode = node instanceof instancedMesh_1.InstancedMesh ? node.sourceMesh : node;
175
+ // observer is pointless if concerning node has no missing material
176
+ if (!(0, has_1.default)(concerningNode.metadata, exports.missingMaterialMetadataName))
177
+ return;
178
+ // for each of our AbstractMeshes, set an observer on the AbstractMesh itself and all of its parents.
179
+ let currNode = node;
180
+ while (currNode) {
181
+ const callback = (0, exports.getMaterialPostLoadObserver)(concerningNode);
182
+ const observer = currNode.onEnabledStateChangedObservable.add(callback);
183
+ // store the observer in a local map to keep track of the active "enable" observers
184
+ // observers will be removed when the concerning node gets enabled
185
+ if (!enableObserverMap[concerningNode.id]) {
186
+ enableObserverMap[concerningNode.id] = [];
187
+ }
188
+ enableObserverMap[concerningNode.id].push({ currNodeId: currNode.id, observer });
189
+ currNode = currNode.parent;
190
+ }
191
+ };
192
+ exports.addMissingMaterialObserver = addMissingMaterialObserver;
193
+ const removeMissingMaterialObserver = function (node) {
194
+ var _a;
195
+ // set the concerning node, i.e. the node the observer should check for missing material.
196
+ // for instanced meshes, we want the sourcemesh here.
197
+ const concerningNode = node instanceof instancedMesh_1.InstancedMesh ? node.sourceMesh : node;
198
+ let currNode = node;
199
+ while (currNode) {
200
+ (_a = enableObserverMap[concerningNode.id]) === null || _a === void 0 ? void 0 : _a.forEach(entry => {
201
+ if (entry.currNodeId === (currNode === null || currNode === void 0 ? void 0 : currNode.id)) {
202
+ currNode.onEnabledStateChangedObservable.remove(entry.observer);
203
+ }
204
+ });
205
+ currNode = currNode.parent;
206
+ }
207
+ delete enableObserverMap[concerningNode.id];
208
+ };
209
+ exports.removeMissingMaterialObserver = removeMissingMaterialObserver;
210
+ /**
211
+ * Look up the provided materials (see library import) and create and return one if found.
212
+ *
213
+ * @param materialId Babylon.js material-id. E.g. 'concrete".
214
+ * @param scene Babylon.js scene
215
+ * @returns PBRMaterial | null
216
+ */
217
+ const createMaterialFromCbnAssets = function (materialId, scene) {
218
+ var _a;
219
+ return __awaiter(this, void 0, void 0, function* () {
220
+ const materialDefinition = yield ((_a = window.Cbn) === null || _a === void 0 ? void 0 : _a.Assets.getMaterial(materialId));
221
+ // The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
222
+ // `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
223
+ // within the material JSON definition
224
+ const material = materialDefinition && material_1.Material.Parse(materialDefinition, scene, '');
225
+ return material || null;
226
+ });
227
+ };
228
+ exports.createMaterialFromCbnAssets = createMaterialFromCbnAssets;
229
229
  //# sourceMappingURL=sceneLoaderHelper.js.map
@@ -1,13 +1,13 @@
1
- /**
2
- * Creates a random uuidv4.
3
- */
4
- declare const uuidv4: () => string;
5
- /**
6
- * Converts a string from camel case to snake case.
7
- */
8
- declare const camelToSnakeCase: (str: string) => string;
9
- /**
10
- * Replaces all dots from the input string with a desired character ('/' by default)
11
- */
12
- declare const replaceDots: (str: string, replaceChar?: string) => string;
13
- export { uuidv4, camelToSnakeCase, replaceDots };
1
+ /**
2
+ * Creates a random uuidv4.
3
+ */
4
+ declare const uuidv4: () => string;
5
+ /**
6
+ * Converts a string from camel case to snake case.
7
+ */
8
+ declare const camelToSnakeCase: (str: string) => string;
9
+ /**
10
+ * Replaces all dots from the input string with a desired character ('/' by default)
11
+ */
12
+ declare const replaceDots: (str: string, replaceChar?: string) => string;
13
+ export { uuidv4, camelToSnakeCase, replaceDots };