@combeenation/3d-viewer 12.4.1 → 13.0.0
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/dist/lib-cjs/buildinfo.json +1 -1
- package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
- package/dist/lib-cjs/index.d.ts +63 -63
- package/dist/lib-cjs/index.js +81 -114
- package/dist/lib-cjs/index.js.map +1 -1
- package/dist/lib-cjs/internal/cbnCustomBabylonLoaderPlugin.d.ts +10 -0
- package/dist/lib-cjs/internal/cbnCustomBabylonLoaderPlugin.js +124 -0
- package/dist/lib-cjs/internal/cbnCustomBabylonLoaderPlugin.js.map +1 -0
- package/dist/lib-cjs/internal/cloningHelper.d.ts +19 -0
- package/dist/lib-cjs/internal/cloningHelper.js +165 -0
- package/dist/lib-cjs/internal/cloningHelper.js.map +1 -0
- package/dist/lib-cjs/internal/deviceHelper.d.ts +9 -0
- package/dist/lib-cjs/{api/util → internal}/deviceHelper.js +6 -10
- package/dist/lib-cjs/internal/deviceHelper.js.map +1 -0
- package/dist/lib-cjs/internal/geometryHelper.d.ts +21 -0
- package/dist/lib-cjs/{api/util → internal}/geometryHelper.js +57 -24
- package/dist/lib-cjs/internal/geometryHelper.js.map +1 -0
- package/dist/lib-cjs/internal/metadataHelper.d.ts +26 -0
- package/dist/lib-cjs/internal/metadataHelper.js +51 -0
- package/dist/lib-cjs/internal/metadataHelper.js.map +1 -0
- package/dist/lib-cjs/internal/paintableHelper.d.ts +40 -0
- package/dist/lib-cjs/internal/paintableHelper.js +287 -0
- package/dist/lib-cjs/internal/paintableHelper.js.map +1 -0
- package/dist/lib-cjs/internal/tagsHelper.d.ts +12 -0
- package/dist/lib-cjs/internal/tagsHelper.js +38 -0
- package/dist/lib-cjs/internal/tagsHelper.js.map +1 -0
- package/dist/lib-cjs/manager/cameraManager.d.ts +51 -0
- package/dist/lib-cjs/manager/cameraManager.js +154 -0
- package/dist/lib-cjs/manager/cameraManager.js.map +1 -0
- package/dist/lib-cjs/manager/debugManager.d.ts +60 -0
- package/dist/lib-cjs/manager/debugManager.js +218 -0
- package/dist/lib-cjs/manager/debugManager.js.map +1 -0
- package/dist/lib-cjs/manager/eventManager.d.ts +52 -0
- package/dist/lib-cjs/manager/eventManager.js +72 -0
- package/dist/lib-cjs/manager/eventManager.js.map +1 -0
- package/dist/lib-cjs/{api/manager → manager}/gltfExportManager.d.ts +29 -34
- package/dist/lib-cjs/{api/manager → manager}/gltfExportManager.js +99 -120
- package/dist/lib-cjs/manager/gltfExportManager.js.map +1 -0
- package/dist/lib-cjs/manager/materialManager.d.ts +35 -0
- package/dist/lib-cjs/manager/materialManager.js +126 -0
- package/dist/lib-cjs/manager/materialManager.js.map +1 -0
- package/dist/lib-cjs/manager/modelManager.d.ts +145 -0
- package/dist/lib-cjs/manager/modelManager.js +381 -0
- package/dist/lib-cjs/manager/modelManager.js.map +1 -0
- package/dist/lib-cjs/manager/parameterManager.d.ts +210 -0
- package/dist/lib-cjs/manager/parameterManager.js +515 -0
- package/dist/lib-cjs/manager/parameterManager.js.map +1 -0
- package/dist/lib-cjs/manager/sceneManager.d.ts +45 -0
- package/dist/lib-cjs/manager/sceneManager.js +65 -0
- package/dist/lib-cjs/manager/sceneManager.js.map +1 -0
- package/dist/lib-cjs/manager/screenshotManager.d.ts +36 -0
- package/dist/lib-cjs/manager/screenshotManager.js +40 -0
- package/dist/lib-cjs/manager/screenshotManager.js.map +1 -0
- package/dist/lib-cjs/manager/textureManager.d.ts +12 -0
- package/dist/lib-cjs/manager/textureManager.js +44 -0
- package/dist/lib-cjs/manager/textureManager.js.map +1 -0
- package/dist/lib-cjs/viewer.d.ts +117 -0
- package/dist/lib-cjs/viewer.js +222 -0
- package/dist/lib-cjs/viewer.js.map +1 -0
- package/dist/lib-cjs/{api/classes/viewerError.d.ts → viewerError.d.ts} +6 -1
- package/dist/lib-cjs/{api/classes/viewerError.js → viewerError.js} +6 -1
- package/dist/lib-cjs/viewerError.js.map +1 -0
- package/package.json +10 -11
- package/src/dev.ts +14 -37
- package/src/{types.d.ts → globalTypes.d.ts} +8 -18
- package/src/index.ts +79 -113
- package/src/internal/cbnCustomBabylonLoaderPlugin.ts +149 -0
- package/src/internal/cloningHelper.ts +225 -0
- package/src/internal/deviceHelper.ts +25 -0
- package/src/{api/util → internal}/geometryHelper.ts +63 -24
- package/src/internal/metadataHelper.ts +63 -0
- package/src/internal/paintableHelper.ts +310 -0
- package/src/internal/tagsHelper.ts +41 -0
- package/src/manager/cameraManager.ts +236 -0
- package/src/manager/debugManager.ts +245 -0
- package/src/manager/eventManager.ts +72 -0
- package/src/{api/manager → manager}/gltfExportManager.ts +132 -125
- package/src/manager/materialManager.ts +135 -0
- package/src/manager/modelManager.ts +456 -0
- package/src/manager/parameterManager.ts +652 -0
- package/src/manager/sceneManager.ts +101 -0
- package/src/manager/screenshotManager.ts +59 -0
- package/src/manager/textureManager.ts +32 -0
- package/src/viewer.ts +296 -0
- package/src/{api/classes/viewerError.ts → viewerError.ts} +6 -1
- package/dist/lib-cjs/api/classes/animationInterface.d.ts +0 -8
- package/dist/lib-cjs/api/classes/animationInterface.js +0 -3
- package/dist/lib-cjs/api/classes/animationInterface.js.map +0 -1
- package/dist/lib-cjs/api/classes/dottedPath.d.ts +0 -79
- package/dist/lib-cjs/api/classes/dottedPath.js +0 -167
- package/dist/lib-cjs/api/classes/dottedPath.js.map +0 -1
- package/dist/lib-cjs/api/classes/element.d.ts +0 -153
- package/dist/lib-cjs/api/classes/element.js +0 -703
- package/dist/lib-cjs/api/classes/element.js.map +0 -1
- package/dist/lib-cjs/api/classes/event.d.ts +0 -401
- package/dist/lib-cjs/api/classes/event.js +0 -425
- package/dist/lib-cjs/api/classes/event.js.map +0 -1
- package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +0 -26
- package/dist/lib-cjs/api/classes/eventBroadcaster.js +0 -50
- package/dist/lib-cjs/api/classes/eventBroadcaster.js.map +0 -1
- package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +0 -7
- package/dist/lib-cjs/api/classes/fuzzyMap.js +0 -22
- package/dist/lib-cjs/api/classes/fuzzyMap.js.map +0 -1
- package/dist/lib-cjs/api/classes/parameter.d.ts +0 -410
- package/dist/lib-cjs/api/classes/parameter.js +0 -643
- package/dist/lib-cjs/api/classes/parameter.js.map +0 -1
- package/dist/lib-cjs/api/classes/parameterObservable.d.ts +0 -36
- package/dist/lib-cjs/api/classes/parameterObservable.js +0 -73
- package/dist/lib-cjs/api/classes/parameterObservable.js.map +0 -1
- package/dist/lib-cjs/api/classes/parameterizable.d.ts +0 -15
- package/dist/lib-cjs/api/classes/parameterizable.js +0 -103
- package/dist/lib-cjs/api/classes/parameterizable.js.map +0 -1
- package/dist/lib-cjs/api/classes/placementAnimation.d.ts +0 -45
- package/dist/lib-cjs/api/classes/placementAnimation.js +0 -177
- package/dist/lib-cjs/api/classes/placementAnimation.js.map +0 -1
- package/dist/lib-cjs/api/classes/variant.d.ts +0 -261
- package/dist/lib-cjs/api/classes/variant.js +0 -873
- package/dist/lib-cjs/api/classes/variant.js.map +0 -1
- package/dist/lib-cjs/api/classes/variantInstance.d.ts +0 -53
- package/dist/lib-cjs/api/classes/variantInstance.js +0 -126
- package/dist/lib-cjs/api/classes/variantInstance.js.map +0 -1
- package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +0 -17
- package/dist/lib-cjs/api/classes/variantParameterizable.js +0 -87
- package/dist/lib-cjs/api/classes/variantParameterizable.js.map +0 -1
- package/dist/lib-cjs/api/classes/viewer.d.ts +0 -215
- package/dist/lib-cjs/api/classes/viewer.js +0 -709
- package/dist/lib-cjs/api/classes/viewer.js.map +0 -1
- package/dist/lib-cjs/api/classes/viewerError.js.map +0 -1
- package/dist/lib-cjs/api/classes/viewerLight.d.ts +0 -66
- package/dist/lib-cjs/api/classes/viewerLight.js +0 -345
- package/dist/lib-cjs/api/classes/viewerLight.js.map +0 -1
- package/dist/lib-cjs/api/internal/lensRendering.d.ts +0 -8
- package/dist/lib-cjs/api/internal/lensRendering.js +0 -12
- package/dist/lib-cjs/api/internal/lensRendering.js.map +0 -1
- package/dist/lib-cjs/api/internal/sceneSetup.d.ts +0 -13
- package/dist/lib-cjs/api/internal/sceneSetup.js +0 -228
- package/dist/lib-cjs/api/internal/sceneSetup.js.map +0 -1
- package/dist/lib-cjs/api/manager/animationManager.d.ts +0 -30
- package/dist/lib-cjs/api/manager/animationManager.js +0 -127
- package/dist/lib-cjs/api/manager/animationManager.js.map +0 -1
- package/dist/lib-cjs/api/manager/gltfExportManager.js.map +0 -1
- package/dist/lib-cjs/api/manager/sceneManager.d.ts +0 -33
- package/dist/lib-cjs/api/manager/sceneManager.js +0 -129
- package/dist/lib-cjs/api/manager/sceneManager.js.map +0 -1
- package/dist/lib-cjs/api/manager/tagManager.d.ts +0 -118
- package/dist/lib-cjs/api/manager/tagManager.js +0 -531
- package/dist/lib-cjs/api/manager/tagManager.js.map +0 -1
- package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +0 -22
- package/dist/lib-cjs/api/manager/textureLoadManager.js +0 -108
- package/dist/lib-cjs/api/manager/textureLoadManager.js.map +0 -1
- package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +0 -106
- package/dist/lib-cjs/api/manager/variantInstanceManager.js +0 -291
- package/dist/lib-cjs/api/manager/variantInstanceManager.js.map +0 -1
- package/dist/lib-cjs/api/store/specStorage.d.ts +0 -32
- package/dist/lib-cjs/api/store/specStorage.js +0 -66
- package/dist/lib-cjs/api/store/specStorage.js.map +0 -1
- package/dist/lib-cjs/api/util/babylonHelper.d.ts +0 -238
- package/dist/lib-cjs/api/util/babylonHelper.js +0 -826
- package/dist/lib-cjs/api/util/babylonHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/debugHelper.d.ts +0 -9
- package/dist/lib-cjs/api/util/debugHelper.js +0 -94
- package/dist/lib-cjs/api/util/debugHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/deviceHelper.d.ts +0 -9
- package/dist/lib-cjs/api/util/deviceHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/geometryHelper.d.ts +0 -17
- package/dist/lib-cjs/api/util/geometryHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/globalTypes.d.ts +0 -490
- package/dist/lib-cjs/api/util/globalTypes.js +0 -2
- package/dist/lib-cjs/api/util/globalTypes.js.map +0 -1
- package/dist/lib-cjs/api/util/resourceHelper.d.ts +0 -58
- package/dist/lib-cjs/api/util/resourceHelper.js +0 -215
- package/dist/lib-cjs/api/util/resourceHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +0 -58
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js +0 -229
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/stringHelper.d.ts +0 -13
- package/dist/lib-cjs/api/util/stringHelper.js +0 -33
- package/dist/lib-cjs/api/util/stringHelper.js.map +0 -1
- package/dist/lib-cjs/api/util/structureHelper.d.ts +0 -9
- package/dist/lib-cjs/api/util/structureHelper.js +0 -58
- package/dist/lib-cjs/api/util/structureHelper.js.map +0 -1
- package/src/api/classes/animationInterface.ts +0 -10
- package/src/api/classes/dottedPath.ts +0 -181
- package/src/api/classes/element.ts +0 -766
- package/src/api/classes/event.ts +0 -457
- package/src/api/classes/eventBroadcaster.ts +0 -52
- package/src/api/classes/fuzzyMap.ts +0 -21
- package/src/api/classes/parameter.ts +0 -686
- package/src/api/classes/parameterObservable.ts +0 -73
- package/src/api/classes/parameterizable.ts +0 -87
- package/src/api/classes/placementAnimation.ts +0 -162
- package/src/api/classes/variant.ts +0 -965
- package/src/api/classes/variantInstance.ts +0 -123
- package/src/api/classes/variantParameterizable.ts +0 -83
- package/src/api/classes/viewer.ts +0 -751
- package/src/api/classes/viewerLight.ts +0 -335
- package/src/api/internal/debugViewer.ts +0 -90
- package/src/api/internal/lensRendering.ts +0 -9
- package/src/api/internal/sceneSetup.ts +0 -208
- package/src/api/manager/animationManager.ts +0 -143
- package/src/api/manager/sceneManager.ts +0 -134
- package/src/api/manager/tagManager.ts +0 -572
- package/src/api/manager/textureLoadManager.ts +0 -107
- package/src/api/manager/variantInstanceManager.ts +0 -306
- package/src/api/store/specStorage.ts +0 -68
- package/src/api/util/babylonHelper.ts +0 -915
- package/src/api/util/debugHelper.ts +0 -121
- package/src/api/util/deviceHelper.ts +0 -31
- package/src/api/util/globalTypes.ts +0 -566
- package/src/api/util/resourceHelper.ts +0 -201
- package/src/api/util/sceneLoaderHelper.ts +0 -247
- package/src/api/util/stringHelper.ts +0 -30
- package/src/api/util/structureHelper.ts +0 -62
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { Event, emitter } from '../classes/event';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Sleeps for a certain amount of microseconds.
|
|
5
|
-
*/
|
|
6
|
-
const sleep = (ms: number) => new Promise(resolve => window.setTimeout(resolve, ms));
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Loads any kind of response from given path.
|
|
10
|
-
* @emits {@link Event.LOADING_START}
|
|
11
|
-
* @emits {@link Event.LOADING_END}
|
|
12
|
-
*/
|
|
13
|
-
const load = async function (path: string): Promise<Response> {
|
|
14
|
-
emitter.emit(Event.LOADING_START, path);
|
|
15
|
-
const data = await fetch(path);
|
|
16
|
-
emitter.emit(Event.LOADING_END, path);
|
|
17
|
-
return data;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Loads a JSON from a given path.
|
|
22
|
-
* @emits {@link Event.LOADING_START}
|
|
23
|
-
* @emits {@link Event.LOADING_END}
|
|
24
|
-
*/
|
|
25
|
-
const loadJson = async function <T>(path: string): Promise<T> {
|
|
26
|
-
const response = await load(path);
|
|
27
|
-
return response.json();
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Loads a Text from a given path.
|
|
32
|
-
* @emits {@link Event.LOADING_START}
|
|
33
|
-
* @emits {@link Event.LOADING_END}
|
|
34
|
-
*/
|
|
35
|
-
const loadText = async function (path: string): Promise<string> {
|
|
36
|
-
const response = await load(path);
|
|
37
|
-
return response.text();
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Loads a Javascript ressource.\
|
|
42
|
-
* Usefull for including packages via CDN (eg: Babylon.js Inspector)
|
|
43
|
-
*/
|
|
44
|
-
const loadJavascript = (url: string): Promise<void> =>
|
|
45
|
-
new Promise(resolve => {
|
|
46
|
-
const scriptEl = document.createElement('script');
|
|
47
|
-
|
|
48
|
-
scriptEl.setAttribute('src', url);
|
|
49
|
-
scriptEl.setAttribute('type', 'text/javascript');
|
|
50
|
-
scriptEl.setAttribute('crossorigin', 'anonymous');
|
|
51
|
-
scriptEl.onload = () => resolve();
|
|
52
|
-
|
|
53
|
-
document.head.appendChild(scriptEl);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Debounces: it limits the rate at which a function can fire.
|
|
58
|
-
*/
|
|
59
|
-
const debounce = function (func: Function, wait: number, immediate: boolean = false) {
|
|
60
|
-
let timeout: any;
|
|
61
|
-
return function () {
|
|
62
|
-
// @ts-ignore this function is not very "ESLint friendly", also see ESLint disable comment 2 lines below
|
|
63
|
-
// => take some time to refactor this function when there is time
|
|
64
|
-
const context = this;
|
|
65
|
-
// eslint-disable-next-line prefer-rest-params
|
|
66
|
-
const args = arguments;
|
|
67
|
-
const later = function () {
|
|
68
|
-
timeout = null;
|
|
69
|
-
if (!immediate) {
|
|
70
|
-
func.apply(context, args);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
const callNow = immediate && !timeout;
|
|
74
|
-
clearTimeout(timeout);
|
|
75
|
-
timeout = setTimeout(later, wait);
|
|
76
|
-
if (callNow) {
|
|
77
|
-
func.apply(context, args);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Merges multiple maps.
|
|
84
|
-
*/
|
|
85
|
-
const mergeMaps = function <TKey, TValue>(...maps: Map<TKey, TValue>[]): Map<TKey, TValue> {
|
|
86
|
-
const resultMap = new Map<TKey, TValue>();
|
|
87
|
-
maps.forEach(map => {
|
|
88
|
-
map.forEach((v, k) => {
|
|
89
|
-
resultMap.set(k, v);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
return resultMap;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Creates a HTML image element based on a SVG string, whereas all the embedded assets in the SVG (eg: fonts, images)
|
|
97
|
-
* are already loaded and exchanged by their base64 representation.\
|
|
98
|
-
* There the output image can exist as "standalone" image and may be used for example as a paintable.
|
|
99
|
-
*
|
|
100
|
-
* !!CAUTION!!: The used functions within this code section are very well evaluated since most alternatives
|
|
101
|
-
* somehow don't work in Safari, as mentioned in the following BJS forum entries:
|
|
102
|
-
* - https://forum.babylonjs.com/t/drawing-svg-content-text-into-dynamictexture-doesnt-work-in-safari-v15/25048
|
|
103
|
-
* - https://forum.babylonjs.com/t/texture-createfrombase64string-doesnt-seem-to-work-for-ios-devices-initially/25502
|
|
104
|
-
*/
|
|
105
|
-
const createImageFromSvg = async function (svgSrc: string): Promise<HTMLImageElement> {
|
|
106
|
-
// replace assets with their base64 versions in svg source code
|
|
107
|
-
const svgWithAssetsEmbedded = await _embedAssets(svgSrc);
|
|
108
|
-
|
|
109
|
-
// create data string which can be used as an image source
|
|
110
|
-
const svgEncoded = 'data:image/svg+xml,' + encodeURIComponent(svgWithAssetsEmbedded);
|
|
111
|
-
|
|
112
|
-
return createImageFromImgSrc(svgEncoded);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Creates an HTML image element from a dedicated image source.\
|
|
117
|
-
* Also waits until the image has loaded.
|
|
118
|
-
*
|
|
119
|
-
* !!CAUTION!!: The `setTimeout` after loading is finished is required due to a Safari bug:
|
|
120
|
-
* - https://bugs.webkit.org/show_bug.cgi?id=39059
|
|
121
|
-
* - https://bugs.webkit.org/show_bug.cgi?id=219770
|
|
122
|
-
*
|
|
123
|
-
* It's not 100% ensured that the timeout solves the issue in every case, but there is no other way unfortunately.\
|
|
124
|
-
* => Keep an eye on it in future projects
|
|
125
|
-
*
|
|
126
|
-
* @param imgSrc Theoretically every source is valid which is also supported by
|
|
127
|
-
* [HTMLImageElement.src](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/src).\
|
|
128
|
-
* Known exceptions are SVGs with embedded assets that are provided as object URL. See comments in
|
|
129
|
-
* {@link createImageFromSvg} for further details.
|
|
130
|
-
*/
|
|
131
|
-
const createImageFromImgSrc = async function (imgSrc: string): Promise<HTMLImageElement> {
|
|
132
|
-
const img = new Image();
|
|
133
|
-
|
|
134
|
-
await new Promise((resolve, reject) => {
|
|
135
|
-
img.onload = () => {
|
|
136
|
-
setTimeout(resolve, 0);
|
|
137
|
-
};
|
|
138
|
-
img.onerror = () => {
|
|
139
|
-
reject();
|
|
140
|
-
};
|
|
141
|
-
img.crossOrigin = 'anonymous';
|
|
142
|
-
img.src = imgSrc;
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
return img;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Replaces all supported image & font URLs in the given SVG with their base64 representation.
|
|
150
|
-
*/
|
|
151
|
-
const _embedAssets = async function (svgSrc: string): Promise<string> {
|
|
152
|
-
const _imageExtensions = ['png', 'gif', 'jpg', 'jpeg', 'svg', 'bmp'];
|
|
153
|
-
const _fontExtensions = ['woff2', 'woff', 'ttf', 'otf'];
|
|
154
|
-
const _assetExtensions = [..._imageExtensions, ..._fontExtensions];
|
|
155
|
-
// Regex copied from https://stackoverflow.com/a/8943487/1273551, not "stress tested"...
|
|
156
|
-
const urlRegex = /(\bhttps?:\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/gi;
|
|
157
|
-
const allUrls = svgSrc.match(urlRegex) as string[];
|
|
158
|
-
|
|
159
|
-
const assetUrls = allUrls.filter(url => {
|
|
160
|
-
const indexParam = url.indexOf('?');
|
|
161
|
-
// remove url parameter to recognize extension
|
|
162
|
-
if (indexParam > -1) {
|
|
163
|
-
url = url.substring(0, indexParam);
|
|
164
|
-
}
|
|
165
|
-
return _assetExtensions.some(extension => url.toLowerCase().endsWith(`.${extension}`));
|
|
166
|
-
});
|
|
167
|
-
const assetBase64Fetcher = assetUrls.map(_fetchBase64AssetUrl);
|
|
168
|
-
const assetFetcherResults = await Promise.all(assetBase64Fetcher);
|
|
169
|
-
return assetFetcherResults.reduce((svgSrc, x) => svgSrc.replace(x.url, x.base64), svgSrc);
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Fetch asset (image or font) and convert it to base64 string representation.
|
|
174
|
-
*/
|
|
175
|
-
const _fetchBase64AssetUrl = async function (assetUrl: string): Promise<{ url: string; base64: string }> {
|
|
176
|
-
// TODO: Cache known base64 representation and only fetch/convert when not already known.
|
|
177
|
-
// Usually the fetch shouldn't hit the network but the browser cache since the SVG was already drawn.
|
|
178
|
-
// See comments of "createImageBitmapFromSvg" for some considerations...
|
|
179
|
-
return new Promise(async (resolve, reject) => {
|
|
180
|
-
const resp = await fetch(assetUrl);
|
|
181
|
-
const blob = await resp.blob();
|
|
182
|
-
const reader = new FileReader();
|
|
183
|
-
reader.onloadend = event => {
|
|
184
|
-
const target = event.target;
|
|
185
|
-
if (!target) {
|
|
186
|
-
return reject(`Asset with URL "${assetUrl}" could not be loaded.`);
|
|
187
|
-
}
|
|
188
|
-
const result = target.result;
|
|
189
|
-
if (!result) {
|
|
190
|
-
return reject(`Asset with URL "${assetUrl}" returned an empty result.`);
|
|
191
|
-
}
|
|
192
|
-
resolve({
|
|
193
|
-
url: assetUrl,
|
|
194
|
-
base64: result.toString() as string,
|
|
195
|
-
});
|
|
196
|
-
};
|
|
197
|
-
reader.readAsDataURL(blob);
|
|
198
|
-
});
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
export { sleep, loadJson, loadText, loadJavascript, debounce, mergeMaps, createImageFromSvg, createImageFromImgSrc };
|
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import { Event, emitter } from '../classes/event';
|
|
2
|
-
import { applyMaterial, getOrCreateMaterial, injectMetadata } 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 { Tags } from '@babylonjs/core/Misc/tags';
|
|
9
|
-
import { AssetContainer } from '@babylonjs/core/assetContainer';
|
|
10
|
-
//! overload DOM API Node due to name-clash with BJS
|
|
11
|
-
import { Node as BjsNode } from '@babylonjs/core/node';
|
|
12
|
-
import { Scene } from '@babylonjs/core/scene';
|
|
13
|
-
import { Nullable } from '@babylonjs/core/types';
|
|
14
|
-
import { isArray, isString } from 'lodash-es';
|
|
15
|
-
import has from 'lodash-es/has';
|
|
16
|
-
|
|
17
|
-
// map for keeping track of active "node enable" observers
|
|
18
|
-
const enableObserverMap: {
|
|
19
|
-
[concerningNodeId: string]: { currNodeId: string; observer: Nullable<Observer<boolean>> }[];
|
|
20
|
-
} = {};
|
|
21
|
-
|
|
22
|
-
export const missingMaterialMetadataName = 'missingMaterial';
|
|
23
|
-
export const parsedMaterialIdMetadataName = 'parsedMaterialId';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Create and return a custom loader plugin to be registered with SceneLoader, that allows
|
|
27
|
-
* us to run our own code against the input data before using the standard procedure to
|
|
28
|
-
* import.
|
|
29
|
-
* @param previousLoaderPlugin the actual loader that's executed after manipulating the input data
|
|
30
|
-
* @returns Custom loader plugin to be registered with SceneLoader.RegisterPlugin()
|
|
31
|
-
*/
|
|
32
|
-
export const getCustomCbnBabylonLoaderPlugin = function (previousLoaderPlugin: ISceneLoaderPlugin): ISceneLoaderPlugin {
|
|
33
|
-
const customLoader: ISceneLoaderPlugin = {
|
|
34
|
-
name: 'cbnCustomBabylonLoader',
|
|
35
|
-
extensions: '.babylon',
|
|
36
|
-
importMesh: previousLoaderPlugin.importMesh,
|
|
37
|
-
load: previousLoaderPlugin.load,
|
|
38
|
-
loadAssetContainer: function (scene, data, rootUrl, onError) {
|
|
39
|
-
//* 1) --- manipulate ORIGINAL data
|
|
40
|
-
const dataParsed = JSON.parse(data);
|
|
41
|
-
//* 2) --- call default (non-custom) loading method
|
|
42
|
-
const importedContainer = previousLoaderPlugin.loadAssetContainer(scene, data, rootUrl);
|
|
43
|
-
//* 3) --- manipulate IMPORTED data
|
|
44
|
-
addMissingMaterialMetadata(dataParsed, importedContainer);
|
|
45
|
-
reconstructTagsForInstancedMeshes(dataParsed, importedContainer);
|
|
46
|
-
//* 4) --- return imported data
|
|
47
|
-
return importedContainer;
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
return customLoader;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Return an observer to be applied to meshes in order to post-load missing materials
|
|
55
|
-
* upon set enabled/visible.
|
|
56
|
-
*
|
|
57
|
-
* @param concerningMesh Mesh to look for missing materials on, and create/apply to (when found).
|
|
58
|
-
* @returns observer
|
|
59
|
-
*/
|
|
60
|
-
export const getMaterialPostLoadObserver = function (concerningMesh: Mesh) {
|
|
61
|
-
return async () => {
|
|
62
|
-
const scene = concerningMesh.getScene();
|
|
63
|
-
|
|
64
|
-
// can't check `isEnabled` immediatly, since the enabled state of parents and childs is not synced yet
|
|
65
|
-
// postpone one cycle to ensure a correct parent-child enable relation
|
|
66
|
-
await sleep(0);
|
|
67
|
-
|
|
68
|
-
const hasBeenEnabled = concerningMesh.isEnabled(true);
|
|
69
|
-
const materialMissing = has(concerningMesh.metadata, missingMaterialMetadataName);
|
|
70
|
-
if (!hasBeenEnabled || !materialMissing) return;
|
|
71
|
-
// get id of missing material
|
|
72
|
-
const missingMatId = concerningMesh.metadata[missingMaterialMetadataName];
|
|
73
|
-
// get material and apply it on the concerning mesh after all textures have been loaded
|
|
74
|
-
const material = await getOrCreateMaterial(concerningMesh.getScene(), missingMatId);
|
|
75
|
-
applyMaterial(material, concerningMesh).then(() =>
|
|
76
|
-
emitter.emit(Event.MESH_MATERIAL_APPLIED, concerningMesh, material)
|
|
77
|
-
);
|
|
78
|
-
// since the material is there now, we do not need the related metadata tag anymore
|
|
79
|
-
delete concerningMesh.metadata[missingMaterialMetadataName];
|
|
80
|
-
|
|
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
|
-
enableObserverMap[concerningMesh.id]?.forEach(entry => {
|
|
84
|
-
const currNode = scene.getMeshById(entry.currNodeId);
|
|
85
|
-
currNode?.onEnabledStateChangedObservable.remove(entry.observer);
|
|
86
|
-
});
|
|
87
|
-
// also remove from the local observer map
|
|
88
|
-
delete enableObserverMap[concerningMesh.id];
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
type InstanceData = {
|
|
93
|
-
name: string;
|
|
94
|
-
tags?: string;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
function _isMeshInstanceData(data: any): data is InstanceData {
|
|
98
|
-
const hasName = isString(data.name);
|
|
99
|
-
const hasValidTags = !data.tags || isString(data.tags);
|
|
100
|
-
return hasName && hasValidTags;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
type MeshData = {
|
|
104
|
-
name: string;
|
|
105
|
-
materialId?: string;
|
|
106
|
-
instances?: unknown[];
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
function _isMeshData(data: any): data is MeshData {
|
|
110
|
-
const hasName = isString(data.name);
|
|
111
|
-
const hasValidMaterialId = !data.materialId || isString(data.materialId);
|
|
112
|
-
|
|
113
|
-
return hasName && hasValidMaterialId;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
type DataWithMeshes = { meshes: unknown[] };
|
|
117
|
-
|
|
118
|
-
function _isDataWithMeshes(data: any): data is DataWithMeshes {
|
|
119
|
-
return data && isArray(data.meshes);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Internal function that compares the original meshes on a .babylon file with what was loaded,
|
|
124
|
-
* and tags missing materials with respective metadata on respective meshes.
|
|
125
|
-
* @param dataParsed original data
|
|
126
|
-
* @param container loaded data
|
|
127
|
-
*/
|
|
128
|
-
export const addMissingMaterialMetadata = function (dataParsed: unknown, container: AssetContainer) {
|
|
129
|
-
if (!_isDataWithMeshes(dataParsed)) return;
|
|
130
|
-
|
|
131
|
-
const validatedMeshes = dataParsed.meshes.filter(_isMeshData);
|
|
132
|
-
|
|
133
|
-
container.meshes.forEach(importedMesh => {
|
|
134
|
-
const parsedMesh = validatedMeshes.find(mesh => mesh.name === importedMesh.name);
|
|
135
|
-
|
|
136
|
-
// save original material id of the imported babylon or GLB file
|
|
137
|
-
injectMetadata(importedMesh, { [parsedMaterialIdMetadataName]: parsedMesh?.materialId }, false);
|
|
138
|
-
|
|
139
|
-
const materialOnImportedMesh = importedMesh.material?.id;
|
|
140
|
-
const materialOnOriginalMesh = parsedMesh?.materialId;
|
|
141
|
-
|
|
142
|
-
if (materialOnOriginalMesh && materialOnImportedMesh !== materialOnOriginalMesh) {
|
|
143
|
-
window.Cbn?.Assets.assertMaterialExists(materialOnOriginalMesh);
|
|
144
|
-
injectMetadata(importedMesh, { [missingMaterialMetadataName]: materialOnOriginalMesh }, false);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Help function for manipulating tags of instances meshes after parsing.
|
|
151
|
-
* Per default babylon attaches the tags of the source mesh to the instance, **but only** if no tags are set for the
|
|
152
|
-
* instanced mesh. If the instanced mesh has dedicated tags set, the ones from the source mesh are **not** copied over.
|
|
153
|
-
* 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
|
|
154
|
-
* tagging system in the viewer and the Combeenation asset editor as well.
|
|
155
|
-
* This function rejects the default tag import algorithm and just copies the tags of the original parsed node without
|
|
156
|
-
* any parent synchronization.
|
|
157
|
-
*
|
|
158
|
-
* @param dataParsed original data
|
|
159
|
-
* @param container loaded data
|
|
160
|
-
*/
|
|
161
|
-
export const reconstructTagsForInstancedMeshes = function (dataParsed: unknown, container: AssetContainer) {
|
|
162
|
-
if (!_isDataWithMeshes(dataParsed)) return;
|
|
163
|
-
|
|
164
|
-
const validatedMeshes = dataParsed.meshes.filter(_isMeshData);
|
|
165
|
-
|
|
166
|
-
container.meshes.forEach(importedMesh => {
|
|
167
|
-
if (importedMesh instanceof InstancedMesh) {
|
|
168
|
-
// remove all tags from the imported mesh if there are some, since these tags are probably coming from the
|
|
169
|
-
// source mesh, if no tags are set there is no need for further operation though
|
|
170
|
-
const importedTags = Tags.GetTags(importedMesh);
|
|
171
|
-
if (importedTags) {
|
|
172
|
-
Tags.RemoveTagsFrom(importedMesh, importedTags);
|
|
173
|
-
|
|
174
|
-
// get tags of parsed instanced mesh and set them on the imported instanced mesh
|
|
175
|
-
const parsedSourceMesh = validatedMeshes.find(mesh => mesh.name === importedMesh.sourceMesh.name);
|
|
176
|
-
const validatedSourceMeshInstances = parsedSourceMesh?.instances?.filter(_isMeshInstanceData);
|
|
177
|
-
const parsedInstancedMesh = validatedSourceMeshInstances?.find(mesh => mesh.name === importedMesh.name);
|
|
178
|
-
const parsedTags = parsedInstancedMesh?.tags;
|
|
179
|
-
if (parsedTags) {
|
|
180
|
-
Tags.AddTagsTo(importedMesh, parsedTags);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Adds an "onEnabledStateChanged" observer to the given mesh and all its parents:
|
|
189
|
-
* The added observer (`getMaterialPostLoadObserver`) handles creation of missing materials once the given node is
|
|
190
|
-
* enabled.
|
|
191
|
-
*/
|
|
192
|
-
export const addMissingMaterialObserver = function (node: BjsNode) {
|
|
193
|
-
// set the concerning node, i.e. the node the observer should check for missing material.
|
|
194
|
-
// for instanced meshes, we want the sourcemesh here.
|
|
195
|
-
const concerningNode = node instanceof InstancedMesh ? node.sourceMesh : (node as Mesh);
|
|
196
|
-
|
|
197
|
-
// observer is pointless if concerning node has no missing material
|
|
198
|
-
if (!has(concerningNode.metadata, missingMaterialMetadataName)) return;
|
|
199
|
-
|
|
200
|
-
// for each of our AbstractMeshes, set an observer on the AbstractMesh itself and all of its parents.
|
|
201
|
-
let currNode: Nullable<BjsNode> = node;
|
|
202
|
-
while (currNode) {
|
|
203
|
-
const callback = getMaterialPostLoadObserver(concerningNode);
|
|
204
|
-
const observer = currNode.onEnabledStateChangedObservable.add(callback);
|
|
205
|
-
|
|
206
|
-
// store the observer in a local map to keep track of the active "enable" observers
|
|
207
|
-
// observers will be removed when the concerning node gets enabled
|
|
208
|
-
if (!enableObserverMap[concerningNode.id]) {
|
|
209
|
-
enableObserverMap[concerningNode.id] = [];
|
|
210
|
-
}
|
|
211
|
-
enableObserverMap[concerningNode.id].push({ currNodeId: currNode.id, observer });
|
|
212
|
-
|
|
213
|
-
currNode = currNode.parent;
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
export const removeMissingMaterialObserver = function (node: BjsNode) {
|
|
218
|
-
// set the concerning node, i.e. the node the observer should check for missing material.
|
|
219
|
-
// for instanced meshes, we want the sourcemesh here.
|
|
220
|
-
const concerningNode = node instanceof InstancedMesh ? node.sourceMesh : (node as Mesh);
|
|
221
|
-
let currNode: Nullable<BjsNode> = node;
|
|
222
|
-
while (currNode) {
|
|
223
|
-
enableObserverMap[concerningNode.id]?.forEach(entry => {
|
|
224
|
-
if (entry.currNodeId === currNode?.id) {
|
|
225
|
-
currNode.onEnabledStateChangedObservable.remove(entry.observer);
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
currNode = currNode.parent;
|
|
229
|
-
}
|
|
230
|
-
delete enableObserverMap[concerningNode.id];
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Look up the provided materials (see library import) and create and return one if found.
|
|
235
|
-
*
|
|
236
|
-
* @param materialId Babylon.js material-id. E.g. 'concrete".
|
|
237
|
-
* @param scene Babylon.js scene
|
|
238
|
-
* @returns PBRMaterial | null
|
|
239
|
-
*/
|
|
240
|
-
export const createMaterialFromCbnAssets = async function (materialId: string, scene: Scene): Promise<Material | null> {
|
|
241
|
-
const materialDefinition = await window.Cbn?.Assets.getMaterial(materialId);
|
|
242
|
-
// The generic `Material.Parse` actually returns a more specific material like `BABYLON.StandardMaterial`,
|
|
243
|
-
// `BABYLON.PBRMaterial` or stuff like `BABYLON.PBRMetallicRoughnessMaterial` etc. based on the given `customType`
|
|
244
|
-
// within the material JSON definition
|
|
245
|
-
const material = materialDefinition && Material.Parse(materialDefinition, scene, '');
|
|
246
|
-
return material || null;
|
|
247
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
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,62 +0,0 @@
|
|
|
1
|
-
import { Element } from '../classes/element';
|
|
2
|
-
import { Variant } from '../classes/variant';
|
|
3
|
-
import { VariantInstance } from '../classes/variantInstance';
|
|
4
|
-
import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
|
|
5
|
-
import { Tags } from '@babylonjs/core/Misc/tags';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Find out if a node is part of a list of excluded geometry
|
|
9
|
-
* @param node BJS node
|
|
10
|
-
* @param list list of excluded geometry
|
|
11
|
-
* @returns boolean based on whether node (or one of its parents) was found in list
|
|
12
|
-
*/
|
|
13
|
-
const isNodeIncludedInExclusionList = function (
|
|
14
|
-
node: TransformNode,
|
|
15
|
-
list: ExcludedGeometryList,
|
|
16
|
-
skipParentCheck?: boolean
|
|
17
|
-
): boolean {
|
|
18
|
-
const checkNode = (inputNode: TransformNode, nodeToCheck: TransformNode) => {
|
|
19
|
-
return inputNode.name === nodeToCheck.name;
|
|
20
|
-
};
|
|
21
|
-
const checkElement = (inputEl: Element, nodeToCheck: TransformNode) => {
|
|
22
|
-
return inputEl.nodesFlat.some(m => checkNode(m, nodeToCheck));
|
|
23
|
-
};
|
|
24
|
-
const checkVariant = (inputVariant: Variant, nodeToCheck: TransformNode) => {
|
|
25
|
-
return inputVariant.elements.some(el => checkElement(el, nodeToCheck));
|
|
26
|
-
};
|
|
27
|
-
const checkVariantInstance = (inputVarInst: VariantInstance, nodeToCheck: TransformNode) => {
|
|
28
|
-
return inputVarInst.variant.elements.some(el => checkElement(el, nodeToCheck));
|
|
29
|
-
};
|
|
30
|
-
const checkTagManagerSubject = (inputSubject: TagManagerSubject, nodeToCheck: TransformNode) => {
|
|
31
|
-
const nameMatches = inputSubject.nodeName && inputSubject.nodeName === nodeToCheck.name;
|
|
32
|
-
const tagMatches = inputSubject.tagName && Tags.MatchesQuery(nodeToCheck, inputSubject.tagName);
|
|
33
|
-
return nameMatches || tagMatches;
|
|
34
|
-
};
|
|
35
|
-
const check = (geometryToExclude: ExcludedGeometry, node: TransformNode) => {
|
|
36
|
-
if (geometryToExclude instanceof VariantInstance) {
|
|
37
|
-
return checkVariantInstance(geometryToExclude, node);
|
|
38
|
-
}
|
|
39
|
-
if (geometryToExclude instanceof Variant) {
|
|
40
|
-
return checkVariant(geometryToExclude, node);
|
|
41
|
-
}
|
|
42
|
-
if (geometryToExclude instanceof Element) {
|
|
43
|
-
return checkElement(geometryToExclude, node);
|
|
44
|
-
}
|
|
45
|
-
if (geometryToExclude instanceof TransformNode) {
|
|
46
|
-
return checkNode(geometryToExclude, node);
|
|
47
|
-
}
|
|
48
|
-
if ((geometryToExclude as TagManagerSubject).tagName || (geometryToExclude as TagManagerSubject).nodeName) {
|
|
49
|
-
return checkTagManagerSubject(geometryToExclude as TagManagerSubject, node);
|
|
50
|
-
}
|
|
51
|
-
return false;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
let isExcluded = list.some(geometryToExclude => check(geometryToExclude, node));
|
|
55
|
-
if (!isExcluded && !skipParentCheck && node.parent instanceof TransformNode) {
|
|
56
|
-
isExcluded = isNodeIncludedInExclusionList(node.parent, list);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return isExcluded;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export { isNodeIncludedInExclusionList };
|