@combeenation/3d-viewer 12.0.1 → 12.1.0-beta1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@combeenation/3d-viewer",
3
- "version": "12.0.1",
3
+ "version": "12.1.0-beta1",
4
4
  "description": "Combeenation 3D Viewer",
5
5
  "homepage": "https://github.com/Combeenation/3d-viewer#readme",
6
6
  "bugs": {
@@ -71,6 +71,41 @@ export class Element extends VariantParameterizable {
71
71
  };
72
72
  return cloneTransformNode(node, nodeNamingStrategy, this.filterNode.bind(this), true);
73
73
  });
74
+
75
+ // when cloning an instanced mesh the source mesh is still referencing to the source of the original mesh
76
+ // this is wrong, as we want to reference the cloned source
77
+ // this can only be done after the cloning process of the whole tree is finished, as we can't be sure if all
78
+ // instanced meshes or all sources meshes are cloning prior
79
+ const reassignSourceMesh = (node: TransformNode) => {
80
+ if (node.constructor.name === 'InstancedMesh') {
81
+ // find the cloned instance and source mesh
82
+ const clonedInstance = node as InstancedMesh;
83
+ const originalInstance = clonedInstance.metadata.cloneSource as InstancedMesh;
84
+ const originalSource = originalInstance.sourceMesh;
85
+ const clonedSource = originalSource.metadata.cloneTarget;
86
+ // it's not possible to just exchange the source mesh => readonly property
87
+ // we have to create a new clone with the correct source mesh
88
+ const exchangedInstance = clonedInstance.clone(clonedInstance.name, clonedInstance.parent, true, clonedSource);
89
+
90
+ // re-reference the childs of the cloned instance as well
91
+ const childs = clonedInstance.getChildTransformNodes(true);
92
+ childs.forEach(child => (child.parent = exchangedInstance));
93
+
94
+ // old instance mesh is not needed anymore and can be disposed
95
+ clonedInstance.dispose();
96
+ }
97
+
98
+ // recurse into childs
99
+ const children = node.getChildTransformNodes(true);
100
+ children.forEach(reassignSourceMesh);
101
+ };
102
+
103
+ nodes.forEach(node => {
104
+ if (node) {
105
+ reassignSourceMesh(node);
106
+ }
107
+ });
108
+
74
109
  this.nodes = nodes.filter(node => !!node) as TransformNode[];
75
110
  this._dottedNodes = mapToDottedNodes<TransformNode>(this.nodes, node => node instanceof TransformNode);
76
111
  variant.broadcastEvent(Event.ELEMENT_NODES_CLONED, this);
@@ -7,6 +7,7 @@ import { TagManager } from '../manager/tagManager';
7
7
  import { VariantInstanceManager } from '../manager/variantInstanceManager';
8
8
  import { SpecStorage } from '../store/specStorage';
9
9
  import { backgroundDomeName, envHelperMetadataName } from '../util/babylonHelper';
10
+ import { showWorldCoordinates } from '../util/debugHelper';
10
11
  import { getIsScaledDownDevice } from '../util/deviceHelper';
11
12
  import { debounce, loadJson } from '../util/resourceHelper';
12
13
  import { getCustomCbnBabylonLoaderPlugin } from '../util/sceneLoaderHelper';
@@ -27,9 +28,6 @@ import { EnvironmentHelper } from '@babylonjs/core/Helpers/environmentHelper';
27
28
  import { PhotoDome } from '@babylonjs/core/Helpers/photoDome';
28
29
  import { HighlightLayer } from '@babylonjs/core/Layers/highlightLayer';
29
30
  import { ISceneLoaderPlugin, SceneLoader } from '@babylonjs/core/Loading/sceneLoader';
30
- import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
31
- import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
32
- import { Color3 } from '@babylonjs/core/Maths/math.color';
33
31
  import { Vector3 } from '@babylonjs/core/Maths/math.vector';
34
32
  import { Mesh } from '@babylonjs/core/Meshes/mesh';
35
33
  import { ScreenshotTools } from '@babylonjs/core/Misc/screenshotTools';
@@ -632,64 +630,7 @@ The inspector can only be used in development builds.`);
632
630
  * Show coordinate system with given dimension (for debugging purpose).
633
631
  */
634
632
  public showWorldCoordinates(dimension: number) {
635
- const scene = this.scene;
636
- const makeTextPlane = function (text: string, color: string, size: number) {
637
- const dynamicTexture = new DynamicTexture('DynamicTexture', 50, scene, true);
638
- dynamicTexture.hasAlpha = true;
639
- dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true);
640
- const plane = Mesh.CreatePlane('TextPlane', size, scene, true);
641
- plane.material = new StandardMaterial('TextPlaneMaterial', scene);
642
- plane.material.backFaceCulling = false;
643
- (plane.material as StandardMaterial).specularColor = new Color3(0, 0, 0);
644
- (plane.material as StandardMaterial).diffuseTexture = dynamicTexture;
645
- return plane;
646
- };
647
-
648
- const axisX = Mesh.CreateLines(
649
- 'axisX',
650
- [
651
- Vector3.Zero(),
652
- new Vector3(dimension, 0, 0),
653
- new Vector3(dimension * 0.95, 0.05 * dimension, 0),
654
- new Vector3(dimension, 0, 0),
655
- new Vector3(dimension * 0.95, -0.05 * dimension, 0),
656
- ],
657
- scene,
658
- false
659
- );
660
- axisX.color = new Color3(1, 0, 0);
661
- const xChar = makeTextPlane('X', 'red', dimension / 10);
662
- xChar.position = new Vector3(0.9 * dimension, -0.05 * dimension, 0);
663
- const axisY = Mesh.CreateLines(
664
- 'axisY',
665
- [
666
- Vector3.Zero(),
667
- new Vector3(0, dimension, 0),
668
- new Vector3(-0.05 * dimension, dimension * 0.95, 0),
669
- new Vector3(0, dimension, 0),
670
- new Vector3(0.05 * dimension, dimension * 0.95, 0),
671
- ],
672
- scene,
673
- false
674
- );
675
- axisY.color = new Color3(0, 1, 0);
676
- const yChar = makeTextPlane('Y', 'green', dimension / 10);
677
- yChar.position = new Vector3(0, 0.9 * dimension, -0.05 * dimension);
678
- const axisZ = Mesh.CreateLines(
679
- 'axisZ',
680
- [
681
- Vector3.Zero(),
682
- new Vector3(0, 0, dimension),
683
- new Vector3(0, -0.05 * dimension, dimension * 0.95),
684
- new Vector3(0, 0, dimension),
685
- new Vector3(0, 0.05 * dimension, dimension * 0.95),
686
- ],
687
- scene,
688
- false
689
- );
690
- axisZ.color = new Color3(0, 0, 1);
691
- const zChar = makeTextPlane('Z', 'blue', dimension / 10);
692
- zChar.position = new Vector3(0, 0.05 * dimension, 0.9 * dimension);
633
+ showWorldCoordinates(this.scene, dimension);
693
634
  }
694
635
 
695
636
  /**
@@ -110,17 +110,13 @@ const cloneTransformNode = function (
110
110
  clone.id = newName;
111
111
  clone.metadata = cloneDeep(node.metadata);
112
112
  injectMetadata(clone, { cloneSource: node }, false);
113
+ injectMetadata(node, { cloneTarget: clone }, false);
113
114
  // if the cloned node is of type InstancedMesh, due to a bug(?),
114
115
  // the InstancedMesh.isEnabled state may have changed after cloning.
115
116
  // in that case, set the clone's enabled state to the original's state
116
117
  if (node.constructor.name === 'InstancedMesh') {
117
118
  clone.setEnabled(node.isEnabled(false));
118
119
  }
119
- // copying tags is a temporary workaround, as it doesn't work for TransformNodes ATM
120
- // see https://forum.babylonjs.com/t/transformnode-clone-doesnt-contain-original-tags/38429/1
121
- if (Tags?.HasTags(node)) {
122
- Tags.AddTagsTo(clone, Tags.GetTags(node, true));
123
- }
124
120
  }
125
121
  if (deep) {
126
122
  const children = node.getChildTransformNodes(true);
@@ -0,0 +1,102 @@
1
+ import { Vector3 } from '@babylonjs/core';
2
+ import { AxesViewer } from '@babylonjs/core/Debug/axesViewer';
3
+ import { DynamicTexture } from '@babylonjs/core/Materials/Textures/dynamicTexture';
4
+ import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
5
+ import { Color3 } from '@babylonjs/core/Maths/math.color';
6
+ import { MeshBuilder } from '@babylonjs/core/Meshes/meshBuilder';
7
+ import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
8
+
9
+ type DebugAxisKeys = 'X' | 'Y' | 'Z';
10
+ type DebugAxisConfig = { color: Color3; position: Vector3 };
11
+
12
+ const _DEBUG_AXIS_MAP: Record<DebugAxisKeys, DebugAxisConfig> = {
13
+ X: { color: Color3.Red().scale(0.5), position: new Vector3(1, 0, 0) },
14
+ Y: { color: Color3.Green().scale(0.5), position: new Vector3(0, 1, 0) },
15
+ Z: { color: Color3.Blue().scale(0.5), position: new Vector3(0, 0, 1) },
16
+ };
17
+
18
+ const _WORLD_COORD_ROOT_KEY = '__world_coordinates__';
19
+
20
+ /**
21
+ * Create debug coordinate system located in world origin.
22
+ * This function is based on Babylon.js `AxesViewer`, axis texts are added manually.
23
+ */
24
+ export function showWorldCoordinates(scene: Scene, dimension: number) {
25
+ const existingRoot = scene.getTransformNodeByName(_WORLD_COORD_ROOT_KEY);
26
+ if (existingRoot) {
27
+ // make sure to remove already existing debug coordinate systems
28
+ existingRoot.dispose();
29
+ }
30
+
31
+ const worldCoordRoot = new TransformNode(_WORLD_COORD_ROOT_KEY, scene);
32
+ // axes viewer coordinate system is a bit too large
33
+ // multiply with unify factor to create arrows which exactly match the length of the input dimension
34
+ const factor = _getWorldCoordinatesAxesUnifyFactor();
35
+ const axesViewer = new AxesViewer(scene, dimension * factor);
36
+
37
+ _prepareWorldCoordinateAxis('X', axesViewer.xAxis, worldCoordRoot, dimension, scene);
38
+ _prepareWorldCoordinateAxis('Y', axesViewer.yAxis, worldCoordRoot, dimension, scene);
39
+ _prepareWorldCoordinateAxis('Z', axesViewer.zAxis, worldCoordRoot, dimension, scene);
40
+ }
41
+
42
+ /**
43
+ * Adjust and enhance coordinate axes to fulfill our needs.
44
+ * - moves nodes into common root
45
+ * - adds text node
46
+ */
47
+ function _prepareWorldCoordinateAxis(
48
+ text: 'X' | 'Y' | 'Z',
49
+ axis: TransformNode,
50
+ root: TransformNode,
51
+ dimension: number,
52
+ scene: Scene
53
+ ) {
54
+ // create unique names
55
+ axis.name = `${_WORLD_COORD_ROOT_KEY}.${text}`;
56
+ axis.parent = root;
57
+ axis.getChildMeshes().forEach((child, idx) => (child.name = `${_WORLD_COORD_ROOT_KEY}.${text}.cylinder_${idx}`));
58
+
59
+ // create text mesh via dynamic texture
60
+ const dynamicTexture = new DynamicTexture(`${_WORLD_COORD_ROOT_KEY}.${text}`, 50, scene, true);
61
+ dynamicTexture.hasAlpha = true;
62
+ // 42.5 is a magic offset, so that the text is vertically centered for font size 50px
63
+ // horizontal centering works well with the standard behaviour of Babylon.js (setting "null" as "width")
64
+ dynamicTexture.drawText(text, null, 42.5, 'bold 50px Arial', 'white', 'transparent', true);
65
+
66
+ const material = new StandardMaterial(`${_WORLD_COORD_ROOT_KEY}.${text}`, scene);
67
+ material.disableLighting = true;
68
+ material.emissiveColor = _DEBUG_AXIS_MAP[text].color;
69
+ material.diffuseTexture = dynamicTexture;
70
+
71
+ const plane = MeshBuilder.CreatePlane(`${_WORLD_COORD_ROOT_KEY}.${text}.TextPlane`, { size: dimension / 10 }, scene);
72
+ // make sure that text is located outside of arrow
73
+ plane.position = _DEBUG_AXIS_MAP[text].position.multiplyByFloats(
74
+ dimension * 1.05,
75
+ dimension * 1.05,
76
+ dimension * 1.05
77
+ );
78
+ plane.parent = root;
79
+ plane.material = material;
80
+ // will be rendered on top of "default" meshes => taken from Babylon.js repo "AxesViewer" implementation
81
+ plane.renderingGroupId = 2;
82
+ // setting billboard mode ensures, that the text is always readable
83
+ plane.billboardMode = TransformNode.BILLBOARDMODE_ALL;
84
+ }
85
+
86
+ /**
87
+ * Calculate factor for creating world coordinate axes with exactly one unit in length
88
+ */
89
+ function _getWorldCoordinatesAxesUnifyFactor() {
90
+ // These constants are taken from the Babylon.js repository
91
+ const coneZPosOffset = 0.3;
92
+ const coneHeight = 0.075;
93
+ const baseScaling = 4;
94
+
95
+ // cone is moved 0.3 units away from the center
96
+ // the origin of the cone will be located on 1.2, if combined with the base scaling of 4
97
+ // invert that offset to get the origin to 1
98
+ // also consider the cone height to move the tip of the cone to 1 instead the center
99
+ const factor = 1 / ((coneZPosOffset + coneHeight / 2) * baseScaling);
100
+
101
+ return factor;
102
+ }
@@ -48,6 +48,20 @@ const bakeGeometryOfMesh = function (mesh: Mesh) {
48
48
  // geometries can be shared across multiple meshes, first make them unique to avoid side-effects
49
49
  mesh.makeGeometryUnique();
50
50
 
51
+ // Babylon.js already provides a function for baking the current skeleton changes into the geometry
52
+ if (mesh.skeleton) {
53
+ mesh.applySkeleton(mesh.skeleton);
54
+ mesh.skeleton = null;
55
+ }
56
+
57
+ // NOTE: in difference to skeletons and transformations there is no baking function for morph targets (yet)
58
+ // however another approach could be to re-apply the position and normals data, as there are nice functions for it
59
+ // - `getPositionData(applySkeleton: boolean = false, applyMorph: boolean = false)`
60
+ // - `getNormalsData(applySkeleton: boolean = false, applyMorph: boolean = false)`
61
+ // you can decide if skeletons and morph targets can be added, which is exactly what we want
62
+ // I'm still hesitant to use it because "tangent" and "UV" kinds are not supported, whereas I'm not sure if it's
63
+ // required
64
+ // => try it out when there is enough time for detailed regression tests!
51
65
  const morphTargetManager = mesh.morphTargetManager;
52
66
  const geometry = mesh.geometry;
53
67
 
@@ -18,6 +18,7 @@ const enableObserverMap: {
18
18
  } = {};
19
19
 
20
20
  export const missingMaterialMetadataName = 'missingMaterial';
21
+ export const parsedMaterialIdMetadataName = 'parsedMaterialId';
21
22
 
22
23
  /**
23
24
  * Create and return a custom loader plugin to be registered with SceneLoader, that allows
@@ -96,6 +97,8 @@ export const addMissingMaterialMetadata = function (dataParsed: any, container:
96
97
  for (const currMeshOriginal of dataParsed.meshes) {
97
98
  if (currMeshOriginal.name !== currMeshImported.name) continue;
98
99
  // we're dealing with the original version of the current imported mesh now
100
+ // first save original material id of the imported babylon or GLB file
101
+ injectMetadata(currMeshImported, { [parsedMaterialIdMetadataName]: currMeshOriginal.materialId });
99
102
  const materialOnImportedMesh = currMeshImported.material?.id;
100
103
  const materialOnOriginalMesh = currMeshOriginal.materialId;
101
104
  if (!materialOnOriginalMesh || materialOnImportedMesh === materialOnOriginalMesh) continue;