@combeenation/3d-viewer 12.0.0 → 12.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@combeenation/3d-viewer",
3
- "version": "12.0.0",
3
+ "version": "12.0.2",
4
4
  "description": "Combeenation 3D Viewer",
5
5
  "homepage": "https://github.com/Combeenation/3d-viewer#readme",
6
6
  "bugs": {
@@ -47,11 +47,8 @@
47
47
  "prettier": "@combeenation/prettier-config",
48
48
  "dependencies": {
49
49
  "@babylonjs/core": "6.27.1",
50
- "@babylonjs/inspector": "6.27.1",
51
50
  "@babylonjs/loaders": "6.27.1",
52
51
  "@babylonjs/materials": "6.27.1",
53
- "@babylonjs/node-editor": "6.27.1",
54
- "@babylonjs/node-geometry-editor": "6.27.1",
55
52
  "@babylonjs/serializers": "6.27.1",
56
53
  "eventemitter3": "4.0.7",
57
54
  "gsap": "3.11.2",
@@ -81,5 +78,13 @@
81
78
  "webpack-cli": "4.10.0",
82
79
  "webpack-dev-server": "4.11.1",
83
80
  "webpack-merge": "5.8.0"
81
+ },
82
+ "optionalDependencies": {
83
+ "@babylonjs/inspector": "6.27.1",
84
+ "@babylonjs/node-editor": "6.27.1",
85
+ "@babylonjs/node-geometry-editor": "6.27.1"
86
+ },
87
+ "optionalDependenciesComments": {
88
+ "@babylonjs": "Defining the inspector package (and it's dependencies) as optional is required to omit these packages in the production build of the CBN react-app (see CB-9269)"
84
89
  }
85
90
  }
@@ -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
  /**
@@ -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
+ }