简体   繁体   中英

Rotate camera around model in Autodesk Forge

I have created an extension that rotates the camera around a Revit model that was uploaded in a bucket made, but how do I make the camera constantly rotate around the model after the button in the toolbar has been clicked on. In addition, how do I click the same button again to stop the rotation? The code below is what I used to rotate the camera once the button is clicked on.

var _this = this;
var _viewer = _this.viewer;
var turnTableToolbarButton = new Autodesk.Viewing.UI.Button('turnTableButton');
turnTableToolbarButton.onClick = function (e) { 

    const nav = _viewer.navigation;
    const up = nav.getCameraUpVector();
    var pos = nav.getPosition();
    const axis = new THREE.Vector3(0, 0, 1);
    const speed = 10.0 * Math.PI / 180;

    const matrix = new THREE.Matrix4().makeRotationAxis(axis, speed * 0.1);

    pos.applyMatrix4(matrix);
    up.applyMatrix4(matrix);

    nav.setView(pos, new THREE.Vector3(0, 0, 0));
    nav.setCameraUpVector(up);
    var viewState = _viewer.getState();
    _viewer.restoreState(viewState);
};

I think what you would need here is use of requestAnimation, with your button stopping or starting the request for the new frame:

  let started = false;

  let rotateCamera = () => {
      if (started) {
          requestAnimationFrame(rotateCamera);
      }

      const nav = viewer.navigation;
      const up = nav.getCameraUpVector();
      const axis = new THREE.Vector3(0, 0, 1);
      const speed = 10.0 * Math.PI / 180;
      const matrix = new THREE.Matrix4().makeRotationAxis(axis, speed * 0.1);

      let pos = nav.getPosition();
      pos.applyMatrix4(matrix);
      up.applyMatrix4(matrix);
      nav.setView(pos, new THREE.Vector3(0, 0, 0));
      nav.setCameraUpVector(up);
      var viewState = viewer.getState();
      // viewer.restoreState(viewState);

  };

  turnTableToolbarButton.onClick = function (e) {
      started = !started;
      if (started) rotateCamera()
  };

Check my repo of extensions for full code based on your snippet and a live illustration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM