Get the Camera position and restore it in Forge Viewer for a virtual visit

自作多情 提交于 2021-01-07 02:29:11

问题


I'm trying to put the camera correctly on dbId defines.

For this, I run in the javascript console this command after put the view like I want :

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

But if I make a test and try to load the result directly, I have an error :

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

Error : camera.up is undefined

In other words, how can I save the camera position manually and restore it?

EDIT

I try to do this with 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 ]);

But it zoom to much IN the object and beyond...

EDIT 2

I've found a solution with restoreState() and getState() but it moving to fast and these methods do not seem to be queued.

I would like to make a virtual visit of my building...

Edit 3

I've try to use your function. So I've migrate it to use it in a Javascript file :

    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()
    })
  }

But When I try to use it, it drive me in a big zoom, not in front of my building for example. I've miss something but I do not see what.

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:


This blog can probably help you: 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)
  }


来源:https://stackoverflow.com/questions/50775511/get-the-camera-position-and-restore-it-in-forge-viewer-for-a-virtual-visit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!