@combeenation/3d-viewer 4.0.0-beta5 → 4.1.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.
@@ -13,6 +13,7 @@ import { Mesh } from '@babylonjs/core/Meshes/mesh';
13
13
  import { TransformNode } from '@babylonjs/core/Meshes/transformNode';
14
14
  import { Node } from '@babylonjs/core/node';
15
15
  import { Scene } from '@babylonjs/core/scene';
16
+ import { Nullable } from '@babylonjs/core/types';
16
17
  import { cloneDeep, get, has, merge } from 'lodash-es';
17
18
  import { DottedPath } from '../classes/dottedPath';
18
19
 
@@ -98,9 +99,9 @@ const cloneTransformNode = function( node: TransformNode,
98
99
  const cloneNodeWithParents = function( node: Node | null ): Node | null {
99
100
  let clone = null;
100
101
  if( node instanceof TransformNode ) {
101
- clone = node.clone( node.name, cloneNodeWithParents( node.parent ), true );
102
+ clone = node.clone( node.name, cloneNodeWithParents( node.parent ) as Nullable<Node>, true );
102
103
  } else if( node instanceof Light ) {
103
- clone = node.clone( node.name, cloneNodeWithParents( node.parent ) );
104
+ clone = node.clone( node.name, cloneNodeWithParents( node.parent ) as Nullable<Node> );
104
105
  } else if( node ) {
105
106
  throw new Error( `Cloning of "${node?.constructor.name}" is not implemented (yet).` );
106
107
  }
@@ -215,56 +216,37 @@ const disableNodeWithParents = function( node: Node ) {
215
216
  };
216
217
 
217
218
  /**
218
- * Attention: this function mutates the position of a node. Keep in mind that there are dependencies to other
219
- * functions moving nodes around.
219
+ * Applies a {@link TransformationDefinition} consecutively to ensure dependencies in positioning etc.
220
220
  * @param node
221
- * @param rotation
221
+ * @param transformation
222
222
  */
223
- const rotateTransformNode = function( node: TransformNode, rotation: Vector3 ): TransformNode {
224
- // remember absolute rotation and reset it before translating
225
- if( !has( node.metadata, 'rotation.initial' ) ) {
226
- injectNodeMetadata( node, { 'rotation.initial': node.rotationQuaternion?.asArray() }, false );
227
- }
228
- if( !has( node.metadata, 'rotation.position' ) || get( node.metadata, 'position.dirty' ) ) {
229
- let rotationPosition = node.absolutePosition.clone();
230
- if( has( node.metadata, 'rotation.offset' ) ) {
231
- rotationPosition = rotationPosition.subtract( get( node.metadata, 'rotation.offset' ) as Vector3 );
232
- }
233
- injectNodeMetadata( node, { 'rotation.position': rotationPosition }, false );
234
- injectNodeMetadata( node, { 'position.dirty': false }, false );
235
- }
236
- node.setAbsolutePosition( get( node.metadata, 'rotation.position' ) );
237
- if( node.metadata.rotation ) {
238
- node.rotationQuaternion = Quaternion.FromArray( get( node.metadata, 'rotation.initial' ) as [] );
239
- }
240
- node.rotateAround( Vector3.Zero(), Axis.X, rotation.x );
241
- node.rotateAround( Vector3.Zero(), Axis.Y, rotation.y );
242
- node.rotateAround( Vector3.Zero(), Axis.Z, rotation.z );
243
- node.computeWorldMatrix( true );
244
- injectNodeMetadata( node, {
245
- 'rotation.offset': node.absolutePosition.subtract( get( node.metadata, 'rotation.position' ) as Vector3 )
246
- }, false );
247
- return node;
248
- };
249
-
250
- /**
251
- * Attention: this function mutates the position of a node. Keep in mind that there are dependencies to other
252
- * functions moving nodes around.
253
- * @param node
254
- * @param distance
255
- */
256
- const moveTransformNode = function( node: TransformNode, distance: Vector3 ): TransformNode {
257
- // remember absolute position and reset it before translating
223
+ const transformTransformNode = function( node: TransformNode, transformation: TransformationDefinition ) {
224
+ // scaling
225
+ if( !has( node.metadata, 'scaling.initial' ) ) {
226
+ injectNodeMetadata( node, { 'scaling.initial': node.scaling }, false );
227
+ }
228
+ const initialScaling = get( node.metadata, 'scaling.initial' ) as Vector3;
229
+ node.scaling = initialScaling.multiply( transformation.scaling );
230
+ // position
258
231
  if( !has( node.metadata, 'position.initial' ) ) {
259
232
  injectNodeMetadata( node, { 'position.initial': node.absolutePosition.clone() }, false );
260
233
  }
261
- let position = get( node.metadata, 'position.initial' ) as Vector3;
262
- if( has( node.metadata, 'rotation.offset' ) ) {
263
- position = position.add( get( node.metadata, 'rotation.offset' ) as Vector3 );
234
+ const initialPosition = get( node.metadata, 'position.initial' ) as Vector3;
235
+ node.setAbsolutePosition( initialPosition.add( transformation.position ).multiply( transformation.scaling ) );
236
+ // rotation
237
+ if( !has( node.metadata, 'rotation.initial' ) ) {
238
+ let rotationQuaternion = node.rotationQuaternion;
239
+ if( !rotationQuaternion ) {
240
+ rotationQuaternion = Quaternion.RotationYawPitchRoll( node.rotation.x, node.rotation.y, node.rotation.z );
241
+ }
242
+ injectNodeMetadata( node, { 'rotation.initial': rotationQuaternion.asArray() }, false );
264
243
  }
265
- node.setAbsolutePosition( position.add( distance ) );
266
- injectNodeMetadata( node, { 'position.dirty': true }, false );
267
- return node;
244
+ const initialRotationQuaternion = Quaternion.FromArray( get( node.metadata, 'rotation.initial' ) as [] );
245
+ node.rotationQuaternion = initialRotationQuaternion;
246
+ node.rotateAround( Vector3.Zero(), Axis.X, transformation.rotation.x );
247
+ node.rotateAround( Vector3.Zero(), Axis.Y, transformation.rotation.y );
248
+ node.rotateAround( Vector3.Zero(), Axis.Z, transformation.rotation.z );
249
+ node.computeWorldMatrix( true );
268
250
  };
269
251
 
270
252
  /**
@@ -536,8 +518,7 @@ export {
536
518
  deactivateTransformNode,
537
519
  enableNodeWithParents,
538
520
  disableNodeWithParents,
539
- rotateTransformNode,
540
- moveTransformNode,
521
+ transformTransformNode,
541
522
  setMaterial,
542
523
  setSourceNodeMaterial,
543
524
  setMaterialColor,
@@ -79,6 +79,12 @@ type ElementDefinition = {
79
79
  paintables?: PaintableDefinitions
80
80
  };
81
81
 
82
+ type TransformationDefinition = {
83
+ scaling: Vector3,
84
+ position: Vector3,
85
+ rotation: Vector3
86
+ };
87
+
82
88
  type StructureJson = {
83
89
  /**
84
90
  * `scene` describes the visualisation of the Babylon `scene` such as the incidence of light and camera position. If a
package/src/dev.ts CHANGED
@@ -4,7 +4,7 @@ import { set } from 'lodash-es';
4
4
 
5
5
  import { Emitter, Viewer } from '.';
6
6
 
7
- import { createSpec, beforeBootstrap, afterBootstrap, createUIelements } from '../assets/CB-6062-async-check/main';
7
+ import { createSpec, beforeBootstrap, afterBootstrap, createUIelements } from '../assets/index';
8
8
 
9
9
  const loadingElement = document.getElementById( 'loading' ) as HTMLDivElement;
10
10
 
@@ -22,7 +22,7 @@ document.addEventListener('DOMContentLoaded', main );
22
22
  async function main() {
23
23
  const viewer = await bootstrapViewer();
24
24
  // "Export" for console testing...
25
- set( window, 'viewerInstance', viewer);
25
+ set( window, 'viewer', viewer);
26
26
  }
27
27
 
28
28
  async function bootstrapViewer() {