获取摄像头位置并在Forge Viewer中将其恢复以进行虚拟访问

时间:2018-06-09 14:52:15

标签: autodesk-forge

我试图将相机正确放在dbId定义上。

为此,我按照我想要的视图运行javascript控制台这个命令:

JSON.stringify(v.navigation.getCamera());

但如果我进行测试并尝试直接加载结果,则会出错:

v.navigation.setCamera(JSON.parse("{\"metadata\":{\"version\":4.3,\"type\":\"Object\",\"generator\":\"Ob...."))

错误: camera.up is undefined

换句话说,如何手动保存相机位置并将其恢复?

修改

我尝试使用setViewFromArray

执行此操作

viewerApp.myCurrentViewer.setViewFromArray([ 454.76857106060265, 96.01886808305997, 212.6431659314611, 287.11932000223214, 167.19053946002487, 97.17925996096139, -0.49285695792051964, 0.20923119682030047, 0.8445793777416518, 2.7467811158798283, 45.00000125223908, 1, 1 ]);

但它可以放大对象以及更远......

编辑2

我找到了restoreState()getState()的解决方案,但它移动到了快速,这些方法似乎没有排队。

我想虚拟参观我的建筑......

编辑3

我尝试使用你的功能。所以我已将其迁移到Javascript文件中使用它:

    var animate = false;

    function tweenCameraTo(viewer, state) {

        var targetTweenEasing = {
            id: TWEEN.Easing.Linear.None,
            name: 'Linear' 
          };
        var posTweenEasing = {
            id: TWEEN.Easing.Linear.None,
            name: 'Linear'
          };
        var  upTweenEasing =  {
            id: TWEEN.Easing.Linear.None,
            name: 'Linear'
        };

        const targetEnd = new THREE.Vector3(
          state.viewport.target[0],
          state.viewport.target[1],
          state.viewport.target[2])

        const posEnd = new THREE.Vector3(
          state.viewport.eye[0],
          state.viewport.eye[1],
          state.viewport.eye[2])

        const upEnd = new THREE.Vector3(
          state.viewport.up[0],
          state.viewport.up[1],
          state.viewport.up[2])

        const nav = viewer.navigation

        const target = new THREE.Vector3().copy(
          nav.getTarget())

        const pos = new THREE.Vector3().copy(
          nav.getPosition())

        const up = new THREE.Vector3().copy(
          nav.getCameraUpVector())

        //nav.setView (posEnd, targetEnd);

        //nav.setCameraUpVector(upEnd);


        var targetTween = createTween({
          easing: targetTweenEasing.id,
          onUpdate: (v) => {
            nav.setTarget(v)
          },
          duration: 25000, //targetTweenDuration,
          object: target,
          to: targetEnd
        }).then((r) => {console.log("targetTween");});

        var posTween = createTween({
          easing: posTweenEasing.id,
          onUpdate: (v) => {
            nav.setPosition(v)
          },
          duration: 25000,//posTweenDuration,
          object: pos,
          to: posEnd
        }).then((r) => {console.log("posTween");});

        var upTween = createTween({
          easing: upTweenEasing.id,
          onUpdate: (v) => {
            nav.setCameraUpVector(v)
          },
          duration: 25000, //upTweenDuration,
          object: up,
          to: upEnd
        }).then((r) => {console.log("upTween");});

        Promise.all([
          targetTween,
          posTween,
          upTween]).then(() => {
          console.log("Fin animation");
            animate = false;
        })

        runAnimation(true);
  }

 // var animId = null;

  var runAnimation = function runAnimation (start) {        
        if(start || animate){
          requestAnimationFrame(runAnimation);
          TWEEN.update()
        }
  }


  function createTween (params) {

    return new Promise((resolve) => {
        console.log("params.to", params.to);
      new TWEEN.Tween(params.object)
        .to(params.to, params.duration)
        .onComplete(() => {resolve();})
        .onUpdate(params.onUpdate)
        .easing(params.easing)
        .start()
    })
  }

但是当我尝试使用它时,它会让我大变焦,而不是在我的建筑物前面。我错过了一些东西,但我看不出来。

tweenCameraTo(viewer, {"viewport":{"name":"","eye":[888.5217895507812,-257.4985656738281,576.9136962890625],"target":[262.7552795410156,81.58747863769531,73.64283752441406],"up":[0,0,1],"worldUpVector":[0,0,1],"pivotPoint":[262.7552795410156,81.58747863769531,73.64283752441406],"distanceToOrbit":871.6906720725796,"aspectRatio":2.7507163323782233,"projection":"perspective","isOrthographic":false,"fieldOfView":45.00000125223908}}); 

tweenCameraTo(viewer, {"viewport":{"name":"","eye":[243.36675374870242,423.8180714045694,167.78380714288494],"target":[303.9841786300087,-347.23884414908446,-234.26269334678466],"up":[0.03614822612815841,-0.4598073869962326,0.8872826340076113],"worldUpVector":[0,0,1],"pivotPoint":[262.7552795410156,81.58747863769531,73.64283752441406],"distanceToOrbit":347.4897746012467,"aspectRatio":2.7507163323782233,"projection":"perspective","isOrthographic":false,"fieldOfView":45.00000125223908}}); 

1 个答案:

答案 0 :(得分:1)

此博客可能会对您有所帮助:Smooth Camera Transitions in the Forge Viewer

/////////////////////////////////////////////////////////
// Smooth camera transition from current state to
// target state using Tween.js
//
/////////////////////////////////////////////////////////
tweenCameraTo (state) {

    // tween parameters, specific to my app but easy
    // to adapt ...
    const {

      targetTweenDuration,
      posTweenDuration,
      upTweenDuration,

      targetTweenEasing,
      posTweenEasing,
      upTweenEasing

    } = this.react.getState()

    const targetEnd = new THREE.Vector3(
      state.viewport.target[0],
      state.viewport.target[1],
      state.viewport.target[2])

    const posEnd = new THREE.Vector3(
      state.viewport.eye[0],
      state.viewport.eye[1],
      state.viewport.eye[2])

    const upEnd = new THREE.Vector3(
      state.viewport.up[0],
      state.viewport.up[1],
      state.viewport.up[2])

    const nav = this.navigation

    const target = new THREE.Vector3().copy(
      nav.getTarget())

    const pos = new THREE.Vector3().copy(
      nav.getPosition())

    const up = new THREE.Vector3().copy(
      nav.getCameraUpVector())


    const targetTween = this.createTween({
      easing: targetTweenEasing.id,
      onUpdate: (v) => {
        nav.setTarget(v)
      },
      duration: targetTweenDuration,
      object: target,
      to: targetEnd
    })

    const posTween = this.createTween({
      easing: posTweenEasing.id,
      onUpdate: (v) => {
        nav.setPosition(v)
      },
      duration: posTweenDuration,
      object: pos,
      to: posEnd
    })

    const upTween = this.createTween({
      easing: upTweenEasing.id,
      onUpdate: (v) => {
        nav.setCameraUpVector(v)
      },
      duration: upTweenDuration,
      object: up,
      to: upEnd
    })

    Promise.all([
      targetTween,
      posTween,
      upTween]).then(() => {

      this.animate = false
    })

    this.runAnimation(true)
  }