鍍金池/ 問答/云計算  網(wǎng)絡(luò)安全  HTML/ 請問要如何通過編程來改變 Forge Viewer 相機(jī)位置

請問要如何通過編程來改變 Forge Viewer 相機(jī)位置

您好,我想知道有沒有辦法通過寫代碼的方式來改變 Forge Viewer 相機(jī)的位置,這是有辦法做到的嗎?

回答
編輯回答
失魂人

可以的,在 Forge Viewer 里頭有很多方法可以做到這點,這邊我會以 AutoCam.goToView() 來示范,以下樣例假設(shè)相機(jī)的新位置是 ( x1, y1, z1 ):

// 獲取當(dāng)前相機(jī)信息
const currentView = viewer.autocam.getCurrentView();
cosnt eye = viewer.navigation.getEyeVector();
const eyeDir = viewVec.normalize();
const distance = eye.length();                         //!<<< 相機(jī)與焦點的距離

const newPosition = new THREE.Vector3( x1, y1, z1 );   //!<<< 相機(jī)的新位置
const target = eye.add( newPosition );                 //!<<< 計算新焦點位置

// 產(chǎn)生新相機(jī)信息
const newView = {
    position: newPosition.clone(),                     //!<<< 相機(jī)的新位置
    up: currentView.up.clone(),
    center: target.clone(),                            //!<<< 相機(jī)的新焦點
    pivot: target.clone(),                             //!<<< 相機(jī)的新環(huán)繞(Orbit)中心
    fov: currentView.fov,
    worldUp: currentView.worldUp.clone(),
    isOrtho: (currentView.isOrtho === false)
};

// 將信息更新到相機(jī)上
viewer.autocam.goToView( newView );

以上希望對你幫助~

2018年1月30日 15:58