@combeenation/3d-viewer 7.1.0 → 7.1.2
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.
- package/README.md +111 -111
- package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
- package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
- package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
- package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
- package/dist/lib-cjs/api/classes/element.d.ts +153 -153
- package/dist/lib-cjs/api/classes/element.js +672 -670
- package/dist/lib-cjs/api/classes/element.js.map +1 -1
- package/dist/lib-cjs/api/classes/event.d.ts +401 -396
- package/dist/lib-cjs/api/classes/event.js +424 -419
- package/dist/lib-cjs/api/classes/event.js.map +1 -1
- package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
- package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
- package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
- package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
- package/dist/lib-cjs/api/classes/parameter.d.ts +351 -351
- package/dist/lib-cjs/api/classes/parameter.js +524 -517
- package/dist/lib-cjs/api/classes/parameter.js.map +1 -1
- package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
- package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
- package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
- package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
- package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
- package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
- package/dist/lib-cjs/api/classes/variant.d.ts +253 -253
- package/dist/lib-cjs/api/classes/variant.js +858 -843
- package/dist/lib-cjs/api/classes/variant.js.map +1 -1
- package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
- package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
- package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
- package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -88
- package/dist/lib-cjs/api/classes/variantParameterizable.js.map +1 -1
- package/dist/lib-cjs/api/classes/viewer.d.ts +204 -199
- package/dist/lib-cjs/api/classes/viewer.js +682 -670
- package/dist/lib-cjs/api/classes/viewer.js.map +1 -1
- package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -0
- package/dist/lib-cjs/api/classes/viewerError.js +56 -0
- package/dist/lib-cjs/api/classes/viewerError.js.map +1 -0
- package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
- package/dist/lib-cjs/api/classes/viewerLight.js +348 -348
- package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
- package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
- package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
- package/dist/lib-cjs/api/internal/sceneSetup.js +226 -226
- package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
- package/dist/lib-cjs/api/manager/animationManager.js +126 -126
- package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +79 -78
- package/dist/lib-cjs/api/manager/gltfExportManager.js +242 -241
- package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
- package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
- package/dist/lib-cjs/api/manager/sceneManager.js +128 -130
- package/dist/lib-cjs/api/manager/sceneManager.js.map +1 -1
- package/dist/lib-cjs/api/manager/tagManager.d.ts +116 -108
- package/dist/lib-cjs/api/manager/tagManager.js +444 -419
- package/dist/lib-cjs/api/manager/tagManager.js.map +1 -1
- package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
- package/dist/lib-cjs/api/manager/textureLoadManager.js +97 -97
- package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -102
- package/dist/lib-cjs/api/manager/variantInstanceManager.js +292 -284
- package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +1 -1
- package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
- package/dist/lib-cjs/api/store/specStorage.js +65 -65
- package/dist/lib-cjs/api/util/babylonHelper.d.ts +235 -235
- package/dist/lib-cjs/api/util/babylonHelper.js +753 -753
- package/dist/lib-cjs/api/util/globalTypes.d.ts +441 -436
- package/dist/lib-cjs/api/util/globalTypes.js +1 -1
- package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
- package/dist/lib-cjs/api/util/resourceHelper.js +203 -203
- package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +44 -44
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js +175 -175
- package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
- package/dist/lib-cjs/api/util/stringHelper.js +32 -32
- package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/structureHelper.js +57 -48
- package/dist/lib-cjs/api/util/structureHelper.js.map +1 -1
- package/dist/lib-cjs/buildinfo.json +3 -3
- package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
- package/dist/lib-cjs/index.d.ts +54 -53
- package/dist/lib-cjs/index.js +117 -114
- package/dist/lib-cjs/index.js.map +1 -1
- package/package.json +81 -81
- package/src/api/classes/animationInterface.ts +10 -10
- package/src/api/classes/dottedPath.ts +181 -181
- package/src/api/classes/element.ts +733 -731
- package/src/api/classes/event.ts +457 -452
- package/src/api/classes/eventBroadcaster.ts +52 -52
- package/src/api/classes/fuzzyMap.ts +21 -21
- package/src/api/classes/parameter.ts +561 -554
- package/src/api/classes/parameterObservable.ts +73 -73
- package/src/api/classes/parameterizable.ts +87 -87
- package/src/api/classes/placementAnimation.ts +162 -162
- package/src/api/classes/variant.ts +949 -933
- package/src/api/classes/variantInstance.ts +123 -123
- package/src/api/classes/variantParameterizable.ts +83 -85
- package/src/api/classes/viewer.ts +760 -743
- package/src/api/classes/viewerError.ts +63 -0
- package/src/api/classes/viewerLight.ts +339 -339
- package/src/api/internal/debugViewer.ts +90 -90
- package/src/api/internal/lensRendering.ts +9 -9
- package/src/api/internal/sceneSetup.ts +205 -205
- package/src/api/manager/animationManager.ts +143 -143
- package/src/api/manager/gltfExportManager.ts +237 -236
- package/src/api/manager/sceneManager.ts +134 -136
- package/src/api/manager/tagManager.ts +477 -451
- package/src/api/manager/textureLoadManager.ts +95 -95
- package/src/api/manager/variantInstanceManager.ts +309 -297
- package/src/api/store/specStorage.ts +68 -68
- package/src/api/util/babylonHelper.ts +823 -823
- package/src/api/util/globalTypes.ts +508 -503
- package/src/api/util/resourceHelper.ts +191 -191
- package/src/api/util/sceneLoaderHelper.ts +170 -170
- package/src/api/util/stringHelper.ts +30 -30
- package/src/api/util/structureHelper.ts +58 -49
- package/src/buildinfo.json +3 -3
- package/src/dev.ts +62 -62
- package/src/index.ts +103 -100
- package/src/types.d.ts +38 -38
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
import { Event, emitter } from '../classes/event';
|
|
2
|
-
import { applyMaterial, getOrCreateMaterial, injectNodeMetadata } from './babylonHelper';
|
|
3
|
-
import { sleep } from './resourceHelper';
|
|
4
|
-
import { ISceneLoaderPlugin } from '@babylonjs/core/Loading/sceneLoader';
|
|
5
|
-
import { Material } from '@babylonjs/core/Materials/material';
|
|
6
|
-
import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
|
|
7
|
-
import { Observer } from '@babylonjs/core/Misc/observable';
|
|
8
|
-
import { AssetContainer } from '@babylonjs/core/assetContainer';
|
|
9
|
-
//! overload DOM API Node due to name-clash with BJS
|
|
10
|
-
import { Node as BjsNode } from '@babylonjs/core/node';
|
|
11
|
-
import { Scene } from '@babylonjs/core/scene';
|
|
12
|
-
import { Nullable } from '@babylonjs/core/types';
|
|
13
|
-
import has from 'lodash-es/has';
|
|
14
|
-
|
|
15
|
-
// map for keeping track of active "node enable" observers
|
|
16
|
-
const enableObserverMap: {
|
|
17
|
-
[concerningNodeId: string]: { currNodeId: string; observer: Nullable<Observer<boolean>> }[];
|
|
18
|
-
} = {};
|
|
19
|
-
|
|
20
|
-
export const missingMaterialMetadataName = 'missingMaterial';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create and return a custom loader plugin to be registered with SceneLoader, that allows
|
|
24
|
-
* us to run our own code against the input data before using the standard procedure to
|
|
25
|
-
* import.
|
|
26
|
-
* @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
|
|
27
|
-
* @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
|
|
28
|
-
*/
|
|
29
|
-
export const getCustomCbnBabylonLoaderPlugin = function (previousLoaderPlugin: ISceneLoaderPlugin): ISceneLoaderPlugin {
|
|
30
|
-
const customLoader: ISceneLoaderPlugin = {
|
|
31
|
-
name: 'cbnCustomBabylonLoader',
|
|
32
|
-
extensions: '.babylon',
|
|
33
|
-
importMesh: previousLoaderPlugin.importMesh,
|
|
34
|
-
load: previousLoaderPlugin.load,
|
|
35
|
-
loadAssetContainer: function (scene, data, rootUrl, onError) {
|
|
36
|
-
//* 1) --- manipulate ORIGINAL data
|
|
37
|
-
const dataParsed = JSON.parse(data);
|
|
38
|
-
//* 2) --- call default (non-custom) loading method
|
|
39
|
-
const importedContainer = previousLoaderPlugin.loadAssetContainer(scene, data, rootUrl);
|
|
40
|
-
//* 3) --- manipulate IMPORTED data
|
|
41
|
-
addMissingMaterialMetadata(dataParsed, importedContainer);
|
|
42
|
-
//* 4) --- return imported data
|
|
43
|
-
return importedContainer;
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
return customLoader;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Return an observer to be applied to meshes in order to post-load missing materials
|
|
51
|
-
* upon set enabled/visible.
|
|
52
|
-
*
|
|
53
|
-
* @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
|
|
54
|
-
* @returns observer
|
|
55
|
-
*/
|
|
56
|
-
export const getMaterialPostLoadObserver = function (concerningMesh: Mesh) {
|
|
57
|
-
return async () => {
|
|
58
|
-
const scene = concerningMesh.getScene();
|
|
59
|
-
|
|
60
|
-
// can't check `isEnabled` immediatly, since the enabled state of parents and childs is not synced yet
|
|
61
|
-
// postpone one cycle to ensure a correct parent-child enable relation
|
|
62
|
-
await sleep(0);
|
|
63
|
-
|
|
64
|
-
const hasBeenEnabled = concerningMesh.isEnabled(true);
|
|
65
|
-
const materialMissing = has(concerningMesh.metadata, missingMaterialMetadataName);
|
|
66
|
-
if (!hasBeenEnabled || !materialMissing) return;
|
|
67
|
-
// get id of missing material
|
|
68
|
-
const missingMatId = concerningMesh.metadata[missingMaterialMetadataName];
|
|
69
|
-
// get material and apply it on the concerning mesh after all textures have been loaded
|
|
70
|
-
const material = await getOrCreateMaterial(concerningMesh.getScene(), missingMatId);
|
|
71
|
-
applyMaterial(material, concerningMesh).then(() =>
|
|
72
|
-
emitter.emit(Event.MESH_MATERIAL_APPLIED, concerningMesh, material)
|
|
73
|
-
);
|
|
74
|
-
// since the material is there now, we do not need the related metadata tag anymore
|
|
75
|
-
delete concerningMesh.metadata[missingMaterialMetadataName];
|
|
76
|
-
|
|
77
|
-
// remove all "enable" observers that were assigned to the concerning mesh
|
|
78
|
-
// the mesh got visible and therefore the observers are not needed anymore
|
|
79
|
-
enableObserverMap[concerningMesh.id]?.forEach(entry => {
|
|
80
|
-
const currNode = scene.getMeshById(entry.currNodeId);
|
|
81
|
-
currNode?.onEnabledStateChangedObservable.remove(entry.observer);
|
|
82
|
-
});
|
|
83
|
-
// also remove from the local observer map
|
|
84
|
-
delete enableObserverMap[concerningMesh.id];
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Internal function that compares the original meshes on a .babylon file with what was loaded,
|
|
90
|
-
* and tags missing materials with respective metadata on respective meshes.
|
|
91
|
-
* @param dataParsed original data
|
|
92
|
-
* @param container loaded data
|
|
93
|
-
*/
|
|
94
|
-
export const addMissingMaterialMetadata = function (dataParsed: any, container: AssetContainer) {
|
|
95
|
-
container.meshes.forEach(currMeshImported => {
|
|
96
|
-
for (const currMeshOriginal of dataParsed.meshes) {
|
|
97
|
-
if (currMeshOriginal.name !== currMeshImported.name) continue;
|
|
98
|
-
// we're dealing with the original version of the current imported mesh now
|
|
99
|
-
const materialOnImportedMesh = currMeshImported.material?.id;
|
|
100
|
-
const materialOnOriginalMesh = currMeshOriginal.materialId;
|
|
101
|
-
if (!materialOnOriginalMesh || materialOnImportedMesh === materialOnOriginalMesh) continue;
|
|
102
|
-
// if we're here, the imported mesh has different material than original one
|
|
103
|
-
window.Cbn?.Assets.assertMaterialExists(materialOnOriginalMesh);
|
|
104
|
-
injectNodeMetadata(currMeshImported, { [missingMaterialMetadataName]: materialOnOriginalMesh }, false);
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
|
|
112
|
-
* The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
|
|
113
|
-
* enabled.
|
|
114
|
-
*/
|
|
115
|
-
export const addMissingMaterialObserver = function (node: BjsNode) {
|
|
116
|
-
// set the concerning node, i.e. the node the observer should check for missing material.
|
|
117
|
-
// for instanced meshes, we want the sourcemesh here.
|
|
118
|
-
const concerningNode = node instanceof InstancedMesh ? node.sourceMesh : (node as Mesh);
|
|
119
|
-
|
|
120
|
-
// observer is pointless if concerning node has no missing material
|
|
121
|
-
if (!has(concerningNode.metadata, missingMaterialMetadataName)) return;
|
|
122
|
-
|
|
123
|
-
// for each of our AbstractMeshes, set an observer on the AbstractMesh itself and all of its parents.
|
|
124
|
-
let currNode: Nullable<BjsNode> = node;
|
|
125
|
-
while (currNode) {
|
|
126
|
-
const callback = getMaterialPostLoadObserver(concerningNode);
|
|
127
|
-
const observer = currNode.onEnabledStateChangedObservable.add(callback);
|
|
128
|
-
|
|
129
|
-
// store the observer in a local map to keep track of the active "enable" observers
|
|
130
|
-
// observers will be removed when the concerning node gets enabled
|
|
131
|
-
if (!enableObserverMap[concerningNode.id]) {
|
|
132
|
-
enableObserverMap[concerningNode.id] = [];
|
|
133
|
-
}
|
|
134
|
-
enableObserverMap[concerningNode.id].push({ currNodeId: currNode.id, observer });
|
|
135
|
-
|
|
136
|
-
currNode = currNode.parent;
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
export const removeMissingMaterialObserver = function (node: BjsNode) {
|
|
141
|
-
// set the concerning node, i.e. the node the observer should check for missing material.
|
|
142
|
-
// for instanced meshes, we want the sourcemesh here.
|
|
143
|
-
const concerningNode = node instanceof InstancedMesh ? node.sourceMesh : (node as Mesh);
|
|
144
|
-
let currNode: Nullable<BjsNode> = node;
|
|
145
|
-
while (currNode) {
|
|
146
|
-
enableObserverMap[concerningNode.id]?.forEach(entry => {
|
|
147
|
-
if (entry.currNodeId === currNode?.id) {
|
|
148
|
-
currNode.onEnabledStateChangedObservable.remove(entry.observer);
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
currNode = currNode.parent;
|
|
152
|
-
}
|
|
153
|
-
delete enableObserverMap[concerningNode.id];
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Look up the provided materials (see library import) and create and return one if found.
|
|
158
|
-
*
|
|
159
|
-
* @param materialId Babylon.js material-id. E.g. 'concrete".
|
|
160
|
-
* @param scene Babylon.js scene
|
|
161
|
-
* @returns PBRMaterial | null
|
|
162
|
-
*/
|
|
163
|
-
export const createMaterialFromCbnAssets = async function (materialId: string, scene: Scene): Promise<Material | null> {
|
|
164
|
-
const materialDefinition = await window.Cbn?.Assets.getMaterial(materialId);
|
|
165
|
-
// The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
|
|
166
|
-
// `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
|
|
167
|
-
// within the material JSON definition
|
|
168
|
-
const material = materialDefinition && Material.Parse(materialDefinition, scene, '');
|
|
169
|
-
return material || null;
|
|
170
|
-
};
|
|
1
|
+
import { Event, emitter } from '../classes/event';
|
|
2
|
+
import { applyMaterial, getOrCreateMaterial, injectNodeMetadata } from './babylonHelper';
|
|
3
|
+
import { sleep } from './resourceHelper';
|
|
4
|
+
import { ISceneLoaderPlugin } from '@babylonjs/core/Loading/sceneLoader';
|
|
5
|
+
import { Material } from '@babylonjs/core/Materials/material';
|
|
6
|
+
import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
|
|
7
|
+
import { Observer } from '@babylonjs/core/Misc/observable';
|
|
8
|
+
import { AssetContainer } from '@babylonjs/core/assetContainer';
|
|
9
|
+
//! overload DOM API Node due to name-clash with BJS
|
|
10
|
+
import { Node as BjsNode } from '@babylonjs/core/node';
|
|
11
|
+
import { Scene } from '@babylonjs/core/scene';
|
|
12
|
+
import { Nullable } from '@babylonjs/core/types';
|
|
13
|
+
import has from 'lodash-es/has';
|
|
14
|
+
|
|
15
|
+
// map for keeping track of active "node enable" observers
|
|
16
|
+
const enableObserverMap: {
|
|
17
|
+
[concerningNodeId: string]: { currNodeId: string; observer: Nullable<Observer<boolean>> }[];
|
|
18
|
+
} = {};
|
|
19
|
+
|
|
20
|
+
export const missingMaterialMetadataName = 'missingMaterial';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create and return a custom loader plugin to be registered with SceneLoader, that allows
|
|
24
|
+
* us to run our own code against the input data before using the standard procedure to
|
|
25
|
+
* import.
|
|
26
|
+
* @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
|
|
27
|
+
* @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
|
|
28
|
+
*/
|
|
29
|
+
export const getCustomCbnBabylonLoaderPlugin = function (previousLoaderPlugin: ISceneLoaderPlugin): ISceneLoaderPlugin {
|
|
30
|
+
const customLoader: ISceneLoaderPlugin = {
|
|
31
|
+
name: 'cbnCustomBabylonLoader',
|
|
32
|
+
extensions: '.babylon',
|
|
33
|
+
importMesh: previousLoaderPlugin.importMesh,
|
|
34
|
+
load: previousLoaderPlugin.load,
|
|
35
|
+
loadAssetContainer: function (scene, data, rootUrl, onError) {
|
|
36
|
+
//* 1) --- manipulate ORIGINAL data
|
|
37
|
+
const dataParsed = JSON.parse(data);
|
|
38
|
+
//* 2) --- call default (non-custom) loading method
|
|
39
|
+
const importedContainer = previousLoaderPlugin.loadAssetContainer(scene, data, rootUrl);
|
|
40
|
+
//* 3) --- manipulate IMPORTED data
|
|
41
|
+
addMissingMaterialMetadata(dataParsed, importedContainer);
|
|
42
|
+
//* 4) --- return imported data
|
|
43
|
+
return importedContainer;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
return customLoader;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Return an observer to be applied to meshes in order to post-load missing materials
|
|
51
|
+
* upon set enabled/visible.
|
|
52
|
+
*
|
|
53
|
+
* @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
|
|
54
|
+
* @returns observer
|
|
55
|
+
*/
|
|
56
|
+
export const getMaterialPostLoadObserver = function (concerningMesh: Mesh) {
|
|
57
|
+
return async () => {
|
|
58
|
+
const scene = concerningMesh.getScene();
|
|
59
|
+
|
|
60
|
+
// can't check `isEnabled` immediatly, since the enabled state of parents and childs is not synced yet
|
|
61
|
+
// postpone one cycle to ensure a correct parent-child enable relation
|
|
62
|
+
await sleep(0);
|
|
63
|
+
|
|
64
|
+
const hasBeenEnabled = concerningMesh.isEnabled(true);
|
|
65
|
+
const materialMissing = has(concerningMesh.metadata, missingMaterialMetadataName);
|
|
66
|
+
if (!hasBeenEnabled || !materialMissing) return;
|
|
67
|
+
// get id of missing material
|
|
68
|
+
const missingMatId = concerningMesh.metadata[missingMaterialMetadataName];
|
|
69
|
+
// get material and apply it on the concerning mesh after all textures have been loaded
|
|
70
|
+
const material = await getOrCreateMaterial(concerningMesh.getScene(), missingMatId);
|
|
71
|
+
applyMaterial(material, concerningMesh).then(() =>
|
|
72
|
+
emitter.emit(Event.MESH_MATERIAL_APPLIED, concerningMesh, material)
|
|
73
|
+
);
|
|
74
|
+
// since the material is there now, we do not need the related metadata tag anymore
|
|
75
|
+
delete concerningMesh.metadata[missingMaterialMetadataName];
|
|
76
|
+
|
|
77
|
+
// remove all "enable" observers that were assigned to the concerning mesh
|
|
78
|
+
// the mesh got visible and therefore the observers are not needed anymore
|
|
79
|
+
enableObserverMap[concerningMesh.id]?.forEach(entry => {
|
|
80
|
+
const currNode = scene.getMeshById(entry.currNodeId);
|
|
81
|
+
currNode?.onEnabledStateChangedObservable.remove(entry.observer);
|
|
82
|
+
});
|
|
83
|
+
// also remove from the local observer map
|
|
84
|
+
delete enableObserverMap[concerningMesh.id];
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Internal function that compares the original meshes on a .babylon file with what was loaded,
|
|
90
|
+
* and tags missing materials with respective metadata on respective meshes.
|
|
91
|
+
* @param dataParsed original data
|
|
92
|
+
* @param container loaded data
|
|
93
|
+
*/
|
|
94
|
+
export const addMissingMaterialMetadata = function (dataParsed: any, container: AssetContainer) {
|
|
95
|
+
container.meshes.forEach(currMeshImported => {
|
|
96
|
+
for (const currMeshOriginal of dataParsed.meshes) {
|
|
97
|
+
if (currMeshOriginal.name !== currMeshImported.name) continue;
|
|
98
|
+
// we're dealing with the original version of the current imported mesh now
|
|
99
|
+
const materialOnImportedMesh = currMeshImported.material?.id;
|
|
100
|
+
const materialOnOriginalMesh = currMeshOriginal.materialId;
|
|
101
|
+
if (!materialOnOriginalMesh || materialOnImportedMesh === materialOnOriginalMesh) continue;
|
|
102
|
+
// if we're here, the imported mesh has different material than original one
|
|
103
|
+
window.Cbn?.Assets.assertMaterialExists(materialOnOriginalMesh);
|
|
104
|
+
injectNodeMetadata(currMeshImported, { [missingMaterialMetadataName]: materialOnOriginalMesh }, false);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
|
|
112
|
+
* The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
|
|
113
|
+
* enabled.
|
|
114
|
+
*/
|
|
115
|
+
export const addMissingMaterialObserver = function (node: BjsNode) {
|
|
116
|
+
// set the concerning node, i.e. the node the observer should check for missing material.
|
|
117
|
+
// for instanced meshes, we want the sourcemesh here.
|
|
118
|
+
const concerningNode = node instanceof InstancedMesh ? node.sourceMesh : (node as Mesh);
|
|
119
|
+
|
|
120
|
+
// observer is pointless if concerning node has no missing material
|
|
121
|
+
if (!has(concerningNode.metadata, missingMaterialMetadataName)) return;
|
|
122
|
+
|
|
123
|
+
// for each of our AbstractMeshes, set an observer on the AbstractMesh itself and all of its parents.
|
|
124
|
+
let currNode: Nullable<BjsNode> = node;
|
|
125
|
+
while (currNode) {
|
|
126
|
+
const callback = getMaterialPostLoadObserver(concerningNode);
|
|
127
|
+
const observer = currNode.onEnabledStateChangedObservable.add(callback);
|
|
128
|
+
|
|
129
|
+
// store the observer in a local map to keep track of the active "enable" observers
|
|
130
|
+
// observers will be removed when the concerning node gets enabled
|
|
131
|
+
if (!enableObserverMap[concerningNode.id]) {
|
|
132
|
+
enableObserverMap[concerningNode.id] = [];
|
|
133
|
+
}
|
|
134
|
+
enableObserverMap[concerningNode.id].push({ currNodeId: currNode.id, observer });
|
|
135
|
+
|
|
136
|
+
currNode = currNode.parent;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const removeMissingMaterialObserver = function (node: BjsNode) {
|
|
141
|
+
// set the concerning node, i.e. the node the observer should check for missing material.
|
|
142
|
+
// for instanced meshes, we want the sourcemesh here.
|
|
143
|
+
const concerningNode = node instanceof InstancedMesh ? node.sourceMesh : (node as Mesh);
|
|
144
|
+
let currNode: Nullable<BjsNode> = node;
|
|
145
|
+
while (currNode) {
|
|
146
|
+
enableObserverMap[concerningNode.id]?.forEach(entry => {
|
|
147
|
+
if (entry.currNodeId === currNode?.id) {
|
|
148
|
+
currNode.onEnabledStateChangedObservable.remove(entry.observer);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
currNode = currNode.parent;
|
|
152
|
+
}
|
|
153
|
+
delete enableObserverMap[concerningNode.id];
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Look up the provided materials (see library import) and create and return one if found.
|
|
158
|
+
*
|
|
159
|
+
* @param materialId Babylon.js material-id. E.g. 'concrete".
|
|
160
|
+
* @param scene Babylon.js scene
|
|
161
|
+
* @returns PBRMaterial | null
|
|
162
|
+
*/
|
|
163
|
+
export const createMaterialFromCbnAssets = async function (materialId: string, scene: Scene): Promise<Material | null> {
|
|
164
|
+
const materialDefinition = await window.Cbn?.Assets.getMaterial(materialId);
|
|
165
|
+
// The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
|
|
166
|
+
// `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
|
|
167
|
+
// within the material JSON definition
|
|
168
|
+
const material = materialDefinition && Material.Parse(materialDefinition, scene, '');
|
|
169
|
+
return material || null;
|
|
170
|
+
};
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a random uuidv4.
|
|
3
|
-
*/
|
|
4
|
-
const uuidv4 = function () {
|
|
5
|
-
return ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => {
|
|
6
|
-
const cNum = parseInt(c);
|
|
7
|
-
return (cNum ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (cNum / 4)))).toString(16);
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Converts a string from camel case to snake case.
|
|
13
|
-
*/
|
|
14
|
-
const camelToSnakeCase = function (str: string): string {
|
|
15
|
-
return str
|
|
16
|
-
.replace(/([A-Z])/g, ' $1')
|
|
17
|
-
.trim()
|
|
18
|
-
.split(' ')
|
|
19
|
-
.join('_')
|
|
20
|
-
.toLowerCase();
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Replaces all dots from the input string with a desired character ('/' by default)
|
|
25
|
-
*/
|
|
26
|
-
const replaceDots = function (str: string, replaceChar = '/'): string {
|
|
27
|
-
return str.split('.').join(replaceChar);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export { uuidv4, camelToSnakeCase, replaceDots };
|
|
1
|
+
/**
|
|
2
|
+
* Creates a random uuidv4.
|
|
3
|
+
*/
|
|
4
|
+
const uuidv4 = function () {
|
|
5
|
+
return ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => {
|
|
6
|
+
const cNum = parseInt(c);
|
|
7
|
+
return (cNum ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (cNum / 4)))).toString(16);
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts a string from camel case to snake case.
|
|
13
|
+
*/
|
|
14
|
+
const camelToSnakeCase = function (str: string): string {
|
|
15
|
+
return str
|
|
16
|
+
.replace(/([A-Z])/g, ' $1')
|
|
17
|
+
.trim()
|
|
18
|
+
.split(' ')
|
|
19
|
+
.join('_')
|
|
20
|
+
.toLowerCase();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Replaces all dots from the input string with a desired character ('/' by default)
|
|
25
|
+
*/
|
|
26
|
+
const replaceDots = function (str: string, replaceChar = '/'): string {
|
|
27
|
+
return str.split('.').join(replaceChar);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { uuidv4, camelToSnakeCase, replaceDots };
|
|
@@ -1,49 +1,58 @@
|
|
|
1
|
-
import { Element } from '../classes/element';
|
|
2
|
-
import { Variant } from '../classes/variant';
|
|
3
|
-
import { VariantInstance } from '../classes/variantInstance';
|
|
4
|
-
import { Mesh } from '@babylonjs/core/Meshes/mesh';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @param
|
|
10
|
-
* @
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (geometryToExclude instanceof
|
|
33
|
-
return
|
|
34
|
-
}
|
|
35
|
-
if (geometryToExclude instanceof
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
1
|
+
import { Element } from '../classes/element';
|
|
2
|
+
import { Variant } from '../classes/variant';
|
|
3
|
+
import { VariantInstance } from '../classes/variantInstance';
|
|
4
|
+
import { Mesh } from '@babylonjs/core/Meshes/mesh';
|
|
5
|
+
import { Tags } from '@babylonjs/core/Misc/tags';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Find out if a mesh is part of a list of excluded geometry
|
|
9
|
+
* @param mesh BJS mesh
|
|
10
|
+
* @param list list of excluded geometry
|
|
11
|
+
* @returns boolean based on whether mesh (or one of its parents) was found in list
|
|
12
|
+
*/
|
|
13
|
+
const isMeshIncludedInExclusionList = function (mesh: Mesh, list: ExcludedGeometryList): boolean {
|
|
14
|
+
const checkMesh = (inputMesh: Mesh, meshToCheck: Mesh) => {
|
|
15
|
+
return inputMesh.uniqueId === meshToCheck.uniqueId;
|
|
16
|
+
};
|
|
17
|
+
const checkElement = (inputEl: Element, meshToCheck: Mesh) => {
|
|
18
|
+
return inputEl.meshesFlat.some(m => checkMesh(m, meshToCheck));
|
|
19
|
+
};
|
|
20
|
+
const checkVariant = (inputVariant: Variant, meshToCheck: Mesh) => {
|
|
21
|
+
return inputVariant.elements.some(el => checkElement(el, meshToCheck));
|
|
22
|
+
};
|
|
23
|
+
const checkVariantInstance = (inputVarInst: VariantInstance, meshToCheck: Mesh) => {
|
|
24
|
+
return inputVarInst.variant.elements.some(el => checkElement(el, meshToCheck));
|
|
25
|
+
};
|
|
26
|
+
const checkTagManagerSubject = (inputSubject: TagManagerSubject, meshToCheck: Mesh) => {
|
|
27
|
+
const nameMatches = inputSubject.nodeName && inputSubject.nodeName === meshToCheck.name;
|
|
28
|
+
const tagMatches = inputSubject.tagName && Tags.MatchesQuery(mesh, inputSubject.tagName);
|
|
29
|
+
return nameMatches || tagMatches;
|
|
30
|
+
};
|
|
31
|
+
const check = (geometryToExclude: ExcludedGeometry, mesh: Mesh) => {
|
|
32
|
+
if (geometryToExclude instanceof VariantInstance) {
|
|
33
|
+
return checkVariantInstance(geometryToExclude, mesh);
|
|
34
|
+
}
|
|
35
|
+
if (geometryToExclude instanceof Variant) {
|
|
36
|
+
return checkVariant(geometryToExclude, mesh);
|
|
37
|
+
}
|
|
38
|
+
if (geometryToExclude instanceof Element) {
|
|
39
|
+
return checkElement(geometryToExclude, mesh);
|
|
40
|
+
}
|
|
41
|
+
if (geometryToExclude instanceof Mesh) {
|
|
42
|
+
return checkMesh(geometryToExclude, mesh);
|
|
43
|
+
}
|
|
44
|
+
if ((geometryToExclude as TagManagerSubject).tagName || (geometryToExclude as TagManagerSubject).nodeName) {
|
|
45
|
+
return checkTagManagerSubject(geometryToExclude as TagManagerSubject, mesh);
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
let isExcluded = list.some(geometryToExclude => check(geometryToExclude, mesh));
|
|
51
|
+
if (!isExcluded && mesh.parent instanceof Mesh) {
|
|
52
|
+
isExcluded = isMeshIncludedInExclusionList(mesh.parent, list);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return isExcluded;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { isMeshIncludedInExclusionList };
|
package/src/buildinfo.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "@VERSION@"
|
|
3
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"version": "@VERSION@"
|
|
3
|
+
}
|
package/src/dev.ts
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import { Emitter, Event, Viewer } from '.';
|
|
2
|
-
import {
|
|
3
|
-
afterBootstrap,
|
|
4
|
-
beforeBootstrap,
|
|
5
|
-
createSpec,
|
|
6
|
-
createTagManagerParameterValues,
|
|
7
|
-
createUIelements,
|
|
8
|
-
getMaterial,
|
|
9
|
-
mockMaterials,
|
|
10
|
-
} from '../assets/index';
|
|
11
|
-
import { set } from 'lodash-es';
|
|
12
|
-
|
|
13
|
-
const loadingElement = document.getElementById('loading') as HTMLDivElement;
|
|
14
|
-
|
|
15
|
-
Emitter.on(Event.BOOTSTRAP_START, () => {
|
|
16
|
-
loadingElement!.style.display = 'block';
|
|
17
|
-
});
|
|
18
|
-
Emitter.on(Event.BOOTSTRAP_END, () => {
|
|
19
|
-
loadingElement!.style.display = 'none';
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
document.addEventListener('DOMContentLoaded', main);
|
|
23
|
-
|
|
24
|
-
window.Cbn = {
|
|
25
|
-
Assets: {
|
|
26
|
-
async getMaterial(materialId: string) {
|
|
27
|
-
//! this creates a new function on the object that uses the imported function of the same name..
|
|
28
|
-
const material = getMaterial(materialId);
|
|
29
|
-
if (material) return material;
|
|
30
|
-
|
|
31
|
-
// Fallback to random mock material
|
|
32
|
-
return mockMaterials[Math.floor(Math.random() * mockMaterials.length)];
|
|
33
|
-
},
|
|
34
|
-
assertMaterialExists(materialId: string): boolean {
|
|
35
|
-
return true;
|
|
36
|
-
},
|
|
37
|
-
reportDuplicateNodeName(nodeNameOrArrayOfNodeNames: string | string[]): void {},
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
async function main() {
|
|
42
|
-
const viewer = await bootstrapViewer();
|
|
43
|
-
// "Export" for console testing...
|
|
44
|
-
set(window, 'viewer', viewer);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function bootstrapViewer() {
|
|
48
|
-
const canvas = document.getElementById('babylon-canvas') as HTMLCanvasElement;
|
|
49
|
-
if (!canvas) {
|
|
50
|
-
throw new Error('Missing "canvas" element');
|
|
51
|
-
}
|
|
52
|
-
const viewer = new Viewer(canvas, createSpec());
|
|
53
|
-
await beforeBootstrap(viewer);
|
|
54
|
-
try {
|
|
55
|
-
await viewer.bootstrap(createTagManagerParameterValues(viewer));
|
|
56
|
-
} catch (e) {
|
|
57
|
-
console.error(e);
|
|
58
|
-
}
|
|
59
|
-
await afterBootstrap(viewer);
|
|
60
|
-
await createUIelements(viewer);
|
|
61
|
-
return viewer;
|
|
62
|
-
}
|
|
1
|
+
import { Emitter, Event, Viewer } from '.';
|
|
2
|
+
import {
|
|
3
|
+
afterBootstrap,
|
|
4
|
+
beforeBootstrap,
|
|
5
|
+
createSpec,
|
|
6
|
+
createTagManagerParameterValues,
|
|
7
|
+
createUIelements,
|
|
8
|
+
getMaterial,
|
|
9
|
+
mockMaterials,
|
|
10
|
+
} from '../assets/index';
|
|
11
|
+
import { set } from 'lodash-es';
|
|
12
|
+
|
|
13
|
+
const loadingElement = document.getElementById('loading') as HTMLDivElement;
|
|
14
|
+
|
|
15
|
+
Emitter.on(Event.BOOTSTRAP_START, () => {
|
|
16
|
+
loadingElement!.style.display = 'block';
|
|
17
|
+
});
|
|
18
|
+
Emitter.on(Event.BOOTSTRAP_END, () => {
|
|
19
|
+
loadingElement!.style.display = 'none';
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
document.addEventListener('DOMContentLoaded', main);
|
|
23
|
+
|
|
24
|
+
window.Cbn = {
|
|
25
|
+
Assets: {
|
|
26
|
+
async getMaterial(materialId: string) {
|
|
27
|
+
//! this creates a new function on the object that uses the imported function of the same name..
|
|
28
|
+
const material = getMaterial(materialId);
|
|
29
|
+
if (material) return material;
|
|
30
|
+
|
|
31
|
+
// Fallback to random mock material
|
|
32
|
+
return mockMaterials[Math.floor(Math.random() * mockMaterials.length)];
|
|
33
|
+
},
|
|
34
|
+
assertMaterialExists(materialId: string): boolean {
|
|
35
|
+
return true;
|
|
36
|
+
},
|
|
37
|
+
reportDuplicateNodeName(nodeNameOrArrayOfNodeNames: string | string[]): void {},
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
async function main() {
|
|
42
|
+
const viewer = await bootstrapViewer();
|
|
43
|
+
// "Export" for console testing...
|
|
44
|
+
set(window, 'viewer', viewer);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function bootstrapViewer() {
|
|
48
|
+
const canvas = document.getElementById('babylon-canvas') as HTMLCanvasElement;
|
|
49
|
+
if (!canvas) {
|
|
50
|
+
throw new Error('Missing "canvas" element');
|
|
51
|
+
}
|
|
52
|
+
const viewer = new Viewer(canvas, createSpec());
|
|
53
|
+
await beforeBootstrap(viewer);
|
|
54
|
+
try {
|
|
55
|
+
await viewer.bootstrap(createTagManagerParameterValues(viewer));
|
|
56
|
+
} catch (e) {
|
|
57
|
+
console.error(e);
|
|
58
|
+
}
|
|
59
|
+
await afterBootstrap(viewer);
|
|
60
|
+
await createUIelements(viewer);
|
|
61
|
+
return viewer;
|
|
62
|
+
}
|