three-cad-viewer 0.9.16 → 0.9.17

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.
@@ -54585,7 +54585,7 @@ class Controls {
54585
54585
  this.controls.panSpeed = this.panSpeed;
54586
54586
 
54587
54587
  // save default view for reset
54588
- this.controls.saveState();
54588
+ this.saveState();
54589
54589
  this.update();
54590
54590
  }
54591
54591
 
@@ -54597,6 +54597,13 @@ class Controls {
54597
54597
  this.controls = null;
54598
54598
  }
54599
54599
 
54600
+ /**
54601
+ * Save state for reset.
54602
+ */
54603
+ saveState() {
54604
+ this.controls.saveState();
54605
+ }
54606
+
54600
54607
  /**
54601
54608
  * Initialize Trackball Controls.
54602
54609
  * @param {boolean} [holroyd=true] - enable holroyd (non tumbling) mode.
@@ -54854,10 +54861,11 @@ class Camera {
54854
54861
  /**
54855
54862
  * Setup the current camera.
54856
54863
  * @param {boolean} relative - flag whether the position is a relative (e.g. [1,1,1] for iso) or absolute point.
54857
- * @param {Vector3} position - the position (relative or absolute).
54864
+ * @param {THREE.Vector3} position - the camera position (relative or absolute).
54865
+ * @param {THREE.Quaternion} quaternion - the camera rotation expressed by a quaternion.
54858
54866
  * @param {number} zoom - zoom value.
54859
54867
  **/
54860
- setupCamera(relative, position, zoom) {
54868
+ setupCamera(relative, position = null, quaternion = null, zoom = null) {
54861
54869
  if (position != null) {
54862
54870
  var cameraPosition = relative
54863
54871
  ? position
@@ -54870,11 +54878,14 @@ class Camera {
54870
54878
  this.camera.position.set(...cameraPosition.toArray());
54871
54879
  }
54872
54880
 
54881
+ if (quaternion != null) {
54882
+ this.camera.quaternion.set(...quaternion.toArray());
54883
+ }
54884
+
54873
54885
  if (zoom != null) {
54874
54886
  this.setZoom(zoom);
54875
54887
  }
54876
54888
 
54877
- this.camera.lookAt(this.target);
54878
54889
  this.updateProjectionMatrix();
54879
54890
  }
54880
54891
 
@@ -54886,7 +54897,9 @@ class Camera {
54886
54897
  if (zoom == null) {
54887
54898
  zoom = this.camera.zoom;
54888
54899
  }
54889
- this.setupCamera(true, defaultDirections[dir], zoom);
54900
+ // For the default directions quaternion can be ignored, it will be reset automatically
54901
+ this.setupCamera(true, defaultDirections[dir], null, zoom);
54902
+ this.lookAtTarget();
54890
54903
  }
54891
54904
 
54892
54905
  /**
@@ -54929,19 +54942,19 @@ class Camera {
54929
54942
 
54930
54943
  /**
54931
54944
  * Set camera position.
54932
- * @param {relative} - flag whether the position is a relative (e.g. [1,1,1] for iso) or absolute point.
54945
+ * @param {boolean} relative - flag whether the position is a relative (e.g. [1,1,1] for iso) or absolute point.
54933
54946
  * @param {(Array(3) | THREE.Vector3)} position - position as 3 dim Array [x,y,z] or as Vector3.
54934
54947
  **/
54935
54948
  setPosition(position, relative) {
54936
54949
  const scope = this;
54937
54950
 
54938
- scope.setupCamera(
54939
- relative,
54940
- position instanceof Vector3
54941
- ? position
54942
- : new Vector3(...position)
54943
- );
54944
- this.updateProjectionMatrix();
54951
+ if (Array.isArray(position) && position.length === 3) {
54952
+ scope.setupCamera(relative, new Vector3(...position));
54953
+ } else if (position instanceof Vector3) {
54954
+ scope.setupCamera(relative, position);
54955
+ } else {
54956
+ console.error("wrong type for position", position);
54957
+ }
54945
54958
  }
54946
54959
 
54947
54960
  /**
@@ -54960,9 +54973,9 @@ class Camera {
54960
54973
  const scope = this;
54961
54974
 
54962
54975
  if (Array.isArray(quaternion) && quaternion.length === 4) {
54963
- scope.camera.quaternion.set(...quaternion);
54976
+ scope.setupCamera(null, null, new Quaternion(...quaternion));
54964
54977
  } else if (quaternion instanceof Quaternion) {
54965
- scope.camera.quaternion.set(...quaternion.toArray());
54978
+ scope.setupCamera(null, null, quaternion);
54966
54979
  } else {
54967
54980
  console.error("wrong type for quaternion", quaternion);
54968
54981
  }
@@ -55090,8 +55103,6 @@ class Viewer {
55090
55103
  this[option] = options[option];
55091
55104
  }
55092
55105
  }
55093
-
55094
- // this.edgeColor = this.blackEdges ? 0x000000 : this.edgeColor;
55095
55106
  }
55096
55107
 
55097
55108
  /**
@@ -55306,12 +55317,12 @@ class Viewer {
55306
55317
  * Render a CAD object and build the navigation tree
55307
55318
  * @param {Shapes} shapes - the shapes of the CAD object to be rendered
55308
55319
  * @param {States} states - the visibility state of meshes and edges
55309
- * @param {Vector3} [position=null] - the camera position.
55320
+ * @param {number[]} [position=null] - the camera position.
55321
+ * @param {number[]} [quaternion=null] - the camera rotation as quaternion. Only relevant for TrackballControls and needs position to be set, too.
55310
55322
  * @param {number} [zoom=null] - zoom value.
55311
55323
  */
55312
- render(shapes, states, position = null, zoom = null) {
55324
+ render(shapes, states, position = null, quaternion = null, zoom = null) {
55313
55325
  this.states = states;
55314
-
55315
55326
  this.scene = new Scene();
55316
55327
 
55317
55328
  const timer = new Timer("viewer", this.timeit);
@@ -55335,6 +55346,12 @@ class Viewer {
55335
55346
 
55336
55347
  timer.split("bounding box");
55337
55348
 
55349
+ //
55350
+ // add Info box
55351
+ //
55352
+
55353
+ this.info = new Info(this.display.cadInfo);
55354
+
55338
55355
  //
55339
55356
  // create cameras
55340
55357
  //
@@ -55347,16 +55364,9 @@ class Viewer {
55347
55364
  this.control
55348
55365
  );
55349
55366
 
55350
- if (position == null) {
55351
- this.presetCamera("iso", zoom);
55352
- } else {
55353
- this.setCamera(position, zoom);
55354
- }
55355
-
55356
55367
  //
55357
55368
  // build mouse/touch controls
55358
55369
  //
55359
-
55360
55370
  this.controls = new Controls(
55361
55371
  this.control,
55362
55372
  this.camera.getCamera(),
@@ -55368,6 +55378,28 @@ class Viewer {
55368
55378
  );
55369
55379
  this.controls.enableKeys = false;
55370
55380
 
55381
+ // this needs to happen after the controls have been established
55382
+ if (position == null && quaternion == null) {
55383
+ this.presetCamera("iso", zoom);
55384
+ } else if (position != null) {
55385
+ this.setCamera(false, position, quaternion, zoom);
55386
+ if (quaternion == null) {
55387
+ this.camera.lookAtTarget();
55388
+ }
55389
+ } else {
55390
+ this.info.addHtml(
55391
+ "<b>quaternion needs position to be provided, falling back to ISO view</b>"
55392
+ );
55393
+ this.presetCamera("iso", zoom);
55394
+ }
55395
+
55396
+ // Save the new state again
55397
+ this.controls.saveState();
55398
+
55399
+ //
55400
+ // Register update event if needed
55401
+ //
55402
+
55371
55403
  if (!this.needsAnimationLoop) {
55372
55404
  this.controls.addChangeListener(() => this.update(true, true, true));
55373
55405
  }
@@ -55474,13 +55506,6 @@ class Viewer {
55474
55506
 
55475
55507
  timer.split("scene done");
55476
55508
 
55477
- //
55478
- // add Info box
55479
- //
55480
-
55481
- this.info = new Info(this.display.cadInfo);
55482
- this.info.readyMsg(this.gridHelper.ticks, this.control);
55483
-
55484
55509
  //
55485
55510
  // show the rendering
55486
55511
  //
@@ -55493,7 +55518,7 @@ class Viewer {
55493
55518
  this.update(true, false);
55494
55519
  }
55495
55520
 
55496
- this.reset();
55521
+ this.info.readyMsg(this.gridHelper.ticks, this.control);
55497
55522
 
55498
55523
  timer.stop();
55499
55524
  }
@@ -55505,14 +55530,23 @@ class Viewer {
55505
55530
  /**
55506
55531
  * Move the camera to a given locations
55507
55532
  * @function
55508
- * @param {Vector3} position - the camera position.
55533
+ * @param {number[]} position - the camera position as 3 dim array [x,y,z]
55534
+ * @param {relative} [relative=false] - flag whether the position is a relative (e.g. [1,1,1] for iso) or absolute point.
55535
+ * @param {number[]} quaternion - the camera rotation expressed by a quaternion array [x,y,z,w].
55509
55536
  * @param {number} [zoom=null] - zoom value.
55510
55537
  * @param {boolean} [notify=true] - whether to send notification or not.
55511
55538
  */
55512
- setCamera = (position, zoom = null, notify = true) => {
55539
+ setCamera = (
55540
+ relative,
55541
+ position,
55542
+ quaternion = null,
55543
+ zoom = null,
55544
+ notify = true
55545
+ ) => {
55513
55546
  this.camera.setupCamera(
55514
- false,
55547
+ relative,
55515
55548
  new Vector3(...position),
55549
+ quaternion != null ? new Quaternion(...quaternion) : null,
55516
55550
  zoom,
55517
55551
  notify
55518
55552
  );
@@ -55569,7 +55603,6 @@ class Viewer {
55569
55603
  */
55570
55604
  reset = () => {
55571
55605
  this.controls.reset();
55572
- this.camera.lookAtTarget();
55573
55606
  this.update(true, false);
55574
55607
  };
55575
55608
 
@@ -55869,15 +55902,15 @@ class Viewer {
55869
55902
 
55870
55903
  /**
55871
55904
  * Get the current camera position.
55872
- * @returns {THREE.Vector3} camera position.
55905
+ * @returns {number[]} camera position as 3 dim array [x,y,z].
55873
55906
  **/
55874
55907
  getCameraPosition() {
55875
- return this.camera.getPosition();
55908
+ return this.camera.getPosition().toArray();
55876
55909
  }
55877
55910
 
55878
55911
  /**
55879
55912
  * Set camera position.
55880
- * @param {(Array(3) | THREE.Vector3)} position - camera position as 3 dim Array [x,y,z] or as Vector3.
55913
+ * @param {number[]} position - camera position as 3 dim Array [x,y,z].
55881
55914
  * @param {relative} [relative=false] - flag whether the position is a relative (e.g. [1,1,1] for iso) or absolute point.
55882
55915
  * @param {boolean} [notify=true] - whether to send notification or not.
55883
55916
  **/
@@ -55886,6 +55919,24 @@ class Viewer {
55886
55919
  this.update(true, false, notify);
55887
55920
  }
55888
55921
 
55922
+ /**
55923
+ * Get the current camera rotation as quaternion.
55924
+ * @returns {number[]} camera rotation as 4 dim quaternion array [x,y,z,w].
55925
+ **/
55926
+ getCameraQuaternion() {
55927
+ return this.camera.getQuaternion().toArray();
55928
+ }
55929
+
55930
+ /**
55931
+ * Set camera rotation via quaternion.
55932
+ * @param {number[]} quaternion - camera rotation as 4 dim quaternion array [x,y,z,w].
55933
+ * @param {boolean} [notify=true] - whether to send notification or not.
55934
+ **/
55935
+ setCameraQuaternion(quaternion, notify = true) {
55936
+ this.camera.setQuaternion(quaternion);
55937
+ this.update(true, false, notify);
55938
+ }
55939
+
55889
55940
  /**
55890
55941
  * Get default color of the edges.
55891
55942
  * @returns {number} edgeColor value.