@combeenation/3d-viewer 20.0.0 → 21.0.0-alpha1

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 (30) hide show
  1. package/dist/lib-cjs/buildinfo.json +1 -1
  2. package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
  3. package/dist/lib-cjs/index.d.ts +0 -1
  4. package/dist/lib-cjs/index.js +0 -1
  5. package/dist/lib-cjs/index.js.map +1 -1
  6. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js +4 -4
  7. package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js.map +1 -1
  8. package/dist/lib-cjs/internal/parameter/texture-parameter-helper.d.ts +2 -2
  9. package/dist/lib-cjs/internal/parameter/texture-parameter-helper.js +2 -3
  10. package/dist/lib-cjs/internal/parameter/texture-parameter-helper.js.map +1 -1
  11. package/dist/lib-cjs/manager/debug-manager.d.ts +8 -1
  12. package/dist/lib-cjs/manager/debug-manager.js +16 -4
  13. package/dist/lib-cjs/manager/debug-manager.js.map +1 -1
  14. package/dist/lib-cjs/manager/material-manager.d.ts +16 -4
  15. package/dist/lib-cjs/manager/material-manager.js +29 -10
  16. package/dist/lib-cjs/manager/material-manager.js.map +1 -1
  17. package/dist/lib-cjs/manager/parameter-manager.js +1 -1
  18. package/dist/lib-cjs/manager/parameter-manager.js.map +1 -1
  19. package/dist/lib-cjs/utils/viewer-utils.d.ts +3 -3
  20. package/dist/lib-cjs/utils/viewer-utils.js +9 -5
  21. package/dist/lib-cjs/utils/viewer-utils.js.map +1 -1
  22. package/package.json +1 -1
  23. package/src/global-types.d.ts +7 -7
  24. package/src/index.ts +0 -1
  25. package/src/internal/cbn-custom-babylon-loader-plugin.ts +2 -2
  26. package/src/internal/parameter/texture-parameter-helper.ts +7 -2
  27. package/src/manager/debug-manager.ts +18 -4
  28. package/src/manager/material-manager.ts +32 -14
  29. package/src/manager/parameter-manager.ts +1 -1
  30. package/src/utils/viewer-utils.ts +7 -6
@@ -9,7 +9,7 @@ interface Window {
9
9
  Cbn:
10
10
  | undefined /* window.Cbn is only available when viewer runs inside Combeenation configurator */
11
11
  | {
12
- Assets: {
12
+ Assets?: {
13
13
  /**
14
14
  * Retrieve material definition from configurator.
15
15
  * Implementation depends on the Combeenation environment the viewer is used in
@@ -19,20 +19,20 @@ interface Window {
19
19
  * @return Undefined if no definition for the given material name exists, otherwise an "JSON object" which can
20
20
  * be passed to `BABYLON.Material.Parse` to create a runtime material object.
21
21
  */
22
- getMaterial(materialId: string): Promise<object | undefined>;
22
+ getMaterial?(materialId: string): Promise<object | undefined>;
23
23
 
24
24
  /**
25
- * Checks if a definition for a given material name exists in the configurator and issues a warning if not
25
+ * Checks if a definition for a given material name exists in the configurator and issues a warning if not.\
26
26
  */
27
- assertMaterialExists(materialId: string): boolean;
27
+ assertMaterialExists?(materialId: string): boolean;
28
28
  };
29
- utils: {
30
- Viewer3dHelper: {
29
+ utils?: {
30
+ Viewer3dHelper?: {
31
31
  /**
32
32
  * Returns parent DOM element of a Combeenation configurator, which can be used as root for the inspector.
33
33
  * Also adjusts styling of some configurator parent sub elements to fix the inspector layout.
34
34
  */
35
- getCfgrParentDomElementForInspector(): HTMLElement | undefined;
35
+ getCfgrParentDomElementForInspector?(): HTMLElement | undefined;
36
36
  };
37
37
  };
38
38
  };
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference path="global-types.d.ts" />
2
1
  // Babylon.js extensions
3
2
  // All gltf extensions to support maximum feature set
4
3
  import '@babylonjs/loaders/glTF/2.0/Extensions';
@@ -165,7 +165,7 @@ function _addMissingMaterialMetadata(dataParsed: unknown, container: AssetContai
165
165
  const materialOnOriginalMesh = parsedMesh?.materialId;
166
166
 
167
167
  if (materialOnOriginalMesh && materialOnImportedMesh !== materialOnOriginalMesh) {
168
- window.Cbn?.Assets.assertMaterialExists(materialOnOriginalMesh);
168
+ window.Cbn?.Assets?.assertMaterialExists?.(materialOnOriginalMesh);
169
169
  setInternalMetadataValue(importedMesh, 'deferredMaterial', materialOnOriginalMesh);
170
170
  }
171
171
  });
@@ -230,7 +230,7 @@ function _createDecals(dataParsed: unknown, container: AssetContainer): void {
230
230
 
231
231
  // optionally set material and tags directly
232
232
  if (materialId) {
233
- window.Cbn?.Assets.assertMaterialExists(materialId);
233
+ window.Cbn?.Assets?.assertMaterialExists?.(materialId);
234
234
  setInternalMetadataValue(decalMesh, 'deferredMaterial', materialId);
235
235
  }
236
236
 
@@ -2,6 +2,7 @@ import {
2
2
  BaseTexture,
3
3
  BuiltInParameter,
4
4
  Material,
5
+ MaterialManager,
5
6
  PBRMaterial,
6
7
  ParameterManager,
7
8
  Scene,
@@ -50,7 +51,11 @@ export const ParameterTextureChannels = {
50
51
  };
51
52
  export type ParameterTextureChannelsKeys = keyof typeof ParameterTextureChannels;
52
53
 
53
- export function createBuiltInTextureParameter(parameterManager: ParameterManager, scene: Scene): void {
54
+ export function createBuiltInTextureParameter(
55
+ parameterManager: ParameterManager,
56
+ materialManager: MaterialManager,
57
+ scene: Scene
58
+ ): void {
54
59
  // create parameter observer for each channel
55
60
  (Object.values(ParameterTextureChannels) as ParameterTextureChannelsKeys[]).forEach(channel => {
56
61
  parameterManager.setParameterObserver(
@@ -100,7 +105,7 @@ export function createBuiltInTextureParameter(parameterManager: ParameterManager
100
105
  } else if (url) {
101
106
  // no texture, or wrong type => create texture from scratch
102
107
  // first we check if some settings were provided in the material definition
103
- const addMatSettings = await window.Cbn?.Assets.getMaterial(pbrMaterial.id);
108
+ const addMatSettings = await materialManager.getMaterialDefinition(pbrMaterial.id);
104
109
  const textureSettings = addMatSettings ? _getTextureObjFromParameterChannel(addMatSettings, channel) : {};
105
110
 
106
111
  // overwrite the name with the url, as this is what the texture parser expects
@@ -25,6 +25,7 @@ export class DebugManager {
25
25
 
26
26
  protected _axesViewer: AxesViewer | null = null;
27
27
  protected _showBoundingSphereForAutofocus: boolean = false;
28
+ protected _parentDomElementForInspector: HTMLElement | null = null;
28
29
 
29
30
  /** @internal */
30
31
  public constructor(protected viewer: Viewer) {}
@@ -40,7 +41,16 @@ export class DebugManager {
40
41
  }
41
42
 
42
43
  /**
43
- * Enables the Babylon.js [Inspector](https://doc.babylonjs.com/legacy/inspector/).\
44
+ * Registers a parent DOM element to use as root for the Babylon.js inspector.
45
+ * When set and no explicit `options` are passed to {@link showInspector}, this element will be used
46
+ * as `globalRoot`, which avoids interference with the surrounding application layout.
47
+ */
48
+ public registerParentDomElementForInspector(el: HTMLElement): void {
49
+ this._parentDomElementForInspector = el;
50
+ }
51
+
52
+ /**
53
+ * Enables the Babylon.js [Inspector](https://doc.babylonjs.com/legacy/inspector()).\
44
54
  * Due to the additional size of the inspector, this function is only available in "development" builds!
45
55
  *
46
56
  * @returns Signalizes if inspector could be loaded
@@ -49,12 +59,16 @@ export class DebugManager {
49
59
  if (process.env.NODE_ENV === 'development') {
50
60
  const inspModule = await import(/* webpackChunkName: "inspector"*/ '@babylonjs/inspector');
51
61
 
52
- if (!options && window.Cbn) {
62
+ const parentEl =
63
+ this._parentDomElementForInspector ??
64
+ // fallback to legacy `window.Cbn` call to support "old" cfgrs (pre Editor V2)
65
+ window.Cbn?.utils?.Viewer3dHelper?.getCfgrParentDomElementForInspector?.();
66
+
67
+ if (!options && parentEl) {
53
68
  // special case for usage in Combeenation configurator:
54
69
  // if no options are set the inspector will be rendered into the parent element of the configurator
55
70
  // in this way the configurator layout itself will not be affected by the inspector
56
- const cfgrParentEl = window.Cbn.utils.Viewer3dHelper.getCfgrParentDomElementForInspector();
57
- inspModule.Inspector.Show(this.viewer.scene, { globalRoot: cfgrParentEl });
71
+ inspModule.Inspector.Show(this.viewer.scene, { globalRoot: parentEl });
58
72
  } else {
59
73
  inspModule.Inspector.Show(this.viewer.scene, options ?? {});
60
74
  }
@@ -30,12 +30,17 @@ export class MaterialManager {
30
30
  */
31
31
  public static readonly CBN_FALLBACK_MATERIAL_NAME = '$fallback';
32
32
 
33
+ protected _materialDefinitions: { [materialId: string]: object } = {};
33
34
  protected _clonedMaterials: { [name: string]: Material } = {};
34
35
  protected _createMaterialPromises: { [materialId: string]: Promise<Material | null> } = {};
35
36
 
36
37
  /** @internal */
37
38
  public constructor(protected viewer: Viewer) {}
38
39
 
40
+ public registerMaterials(materialDefinitions: { [materialId: string]: object }): void {
41
+ this._materialDefinitions = materialDefinitions;
42
+ }
43
+
39
44
  /**
40
45
  * Assigns material with certain id to desired mesh.\
41
46
  * Creates the material if not already available (see {@link getOrCreateMaterial}).
@@ -56,9 +61,9 @@ export class MaterialManager {
56
61
 
57
62
  /**
58
63
  * Returns material with certain id if already available in the scene.\
59
- * If this is not the case material is created from a
60
- * "[Material asset](https://docs.combeenation.com/docs/howto-create-and-use-babylon-and-material-asset)"
61
- * on the Combeenation server.\
64
+ * If this is not the case, material is created from registered material definitions, which are most likely coming
65
+ * from "[Material assets](https://docs.combeenation.com/docs/howto-create-and-use-babylon-and-material-asset)" on the
66
+ * Combeenation server.\
62
67
  * Waits until textures of material are fully loaded and shader is compiled. In this way "flickering" effects
63
68
  * will be avoided, since the material would be incomplete without its loaded textures.
64
69
  *
@@ -78,7 +83,7 @@ export class MaterialManager {
78
83
  this.viewer.eventManager.fireEvent(ViewerEvent.MaterialCreationStart, materialId);
79
84
 
80
85
  // request not pending, call the dedicated function
81
- const newCreationProm = this._createFromMaterialAsset(materialId, mesh);
86
+ const newCreationProm = this._createFromMaterialDefinition(materialId, mesh);
82
87
  // store the promise in a global map, so that subsequent requests can reference it
83
88
  this._createMaterialPromises[materialId] = newCreationProm;
84
89
  chosenMaterial = await newCreationProm;
@@ -153,7 +158,28 @@ export class MaterialManager {
153
158
  Object.keys(this._clonedMaterials).forEach(materialId => this.deleteClonedMaterial(materialId));
154
159
  }
155
160
 
156
- protected async _createFromMaterialAsset(materialId: string, mesh?: AbstractMesh): Promise<Material | null> {
161
+ /**
162
+ * Get material defintion from registered material definitions or from legacy `Cbn.Assets.getMaterial` call
163
+ *
164
+ * @internal
165
+ */
166
+ public async getMaterialDefinition(materialId: string): Promise<object | undefined> {
167
+ let definition: object | undefined = this._materialDefinitions[materialId];
168
+ if (!definition && window.Cbn?.Assets?.getMaterial) {
169
+ // fallback to legacy `Cbn.Assets.getMaterial` call to support "old" cfgrs (pre Editor V2)
170
+ definition = await window.Cbn.Assets.getMaterial(materialId);
171
+ }
172
+
173
+ if (!definition) {
174
+ console.warn(
175
+ `Material definition for "${materialId}" couldn't be found. Material might not be registered or Material Asset from Combeenation server does not exist or is not connected to the configurator.`
176
+ );
177
+ }
178
+
179
+ return definition;
180
+ }
181
+
182
+ protected async _createFromMaterialDefinition(materialId: string, mesh?: AbstractMesh): Promise<Material | null> {
157
183
  if (materialId === MaterialManager.CBN_FALLBACK_MATERIAL_NAME) {
158
184
  const fallbackMaterial = new StandardMaterial(MaterialManager.CBN_FALLBACK_MATERIAL_NAME, this.viewer.scene);
159
185
  fallbackMaterial.disableLighting = true;
@@ -161,16 +187,8 @@ export class MaterialManager {
161
187
  return fallbackMaterial;
162
188
  }
163
189
 
164
- if (!window.Cbn) {
165
- return null;
166
- }
167
-
168
- const materialDefinition = await window.Cbn.Assets.getMaterial(materialId);
169
-
190
+ const materialDefinition = await this.getMaterialDefinition(materialId);
170
191
  if (!materialDefinition) {
171
- console.warn(
172
- `Trying to create material "${materialId}" from Combeenation asset but according asset does not exist or is not connected to the configurator.`
173
- );
174
192
  return null;
175
193
  }
176
194
 
@@ -584,7 +584,7 @@ export class ParameterManager {
584
584
  */
585
585
  protected _addBuiltInParameterObservers(): void {
586
586
  createBuiltInParameter(this, this.viewer.materialManager);
587
- createBuiltInTextureParameter(this, this.viewer.scene);
587
+ createBuiltInTextureParameter(this, this.viewer.materialManager, this.viewer.scene);
588
588
  createPaintableParameter(this, this.viewer.scene);
589
589
  }
590
590
 
@@ -5,6 +5,7 @@ import {
5
5
  MorphTarget,
6
6
  MorphTargetManager,
7
7
  Node,
8
+ Nullable,
8
9
  Tags,
9
10
  TransformNode,
10
11
  Vector3,
@@ -17,7 +18,7 @@ export type BakeGeometrySettings = {
17
18
  };
18
19
 
19
20
  export type CreateTransformNodeSettings = {
20
- parentNode?: TransformNode;
21
+ parentNode?: Nullable<Node>;
21
22
  position?: Vector3;
22
23
  rotation?: Vector3;
23
24
  scaling?: Vector3;
@@ -111,7 +112,7 @@ export class ViewerUtils {
111
112
 
112
113
  /**
113
114
  * Get a `TransformNode` by name or create one if not already available.\
114
- * Optionally set a parent node for the new `TransformNode` right away.
115
+ * Optionally set some settings of the existing or new `TransformNode` right away.
115
116
  */
116
117
  public getOrCreateTransformNode(nodeName: string, settings?: CreateTransformNodeSettings): TransformNode {
117
118
  const { parentNode, position, rotation, scaling } = settings ?? {};
@@ -122,10 +123,10 @@ export class ViewerUtils {
122
123
  node = new TransformNode(nodeName, scene);
123
124
  }
124
125
 
125
- node.parent = parentNode ?? null;
126
- node.position = position ?? Vector3.Zero();
127
- node.rotation = rotation ?? Vector3.Zero();
128
- node.scaling = scaling ?? Vector3.One();
126
+ if (parentNode !== undefined) node.parent = parentNode;
127
+ if (position !== undefined) node.position = position;
128
+ if (rotation !== undefined) node.rotation = rotation;
129
+ if (scaling !== undefined) node.scaling = scaling;
129
130
 
130
131
  return node;
131
132
  }