请问要如何通过编程来改变 Forge Viewer 相机位置

您好,我想知道有没有办法通过写代码的方式来改变 Forge Viewer 相机的位置,这是有办法做到的吗?

DIEA
浏览 597回答 1
1回答

慕姐8265434

可以的,在 Forge Viewer 里头有很多方法可以做到这点,这边我会以 AutoCam.goToView() 来示范,以下样例假设相机的新位置是 ( x1, y1, z1 ):// 获取当前相机信息const currentView = viewer.autocam.getCurrentView();cosnt eye = viewer.navigation.getEyeVector();const eyeDir = viewVec.normalize();const distance = eye.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//!<<< 相机与焦点的距离const newPosition = new THREE.Vector3( x1, y1, z1 );&nbsp; &nbsp;//!<<< 相机的新位置const target = eye.add( newPosition );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//!<<< 计算新焦点位置// 产生新相机信息const newView = {&nbsp; &nbsp; position: newPosition.clone(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//!<<< 相机的新位置&nbsp; &nbsp; up: currentView.up.clone(),&nbsp; &nbsp; center: target.clone(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //!<<< 相机的新焦点&nbsp; &nbsp; pivot: target.clone(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//!<<< 相机的新环绕(Orbit)中心&nbsp; &nbsp; fov: currentView.fov,&nbsp; &nbsp; worldUp: currentView.worldUp.clone(),&nbsp; &nbsp; isOrtho: (currentView.isOrtho === false)};// 将信息更新到相机上viewer.autocam.goToView( newView );以上希望对你帮助~
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript