2016-07-26 360 views
2

是否有任何方法來限制場景中攝像機的平移運動?OrbitControl - 限制平移運動

試圖改變在orbitControls的pan方法,但我不是很滿意的結果,我希望有這樣做更方便/有道是..

if (scope.object instanceof THREE.PerspectiveCamera) { 
    // perspective 
    var position = scope.object.position; 

    var offset = position.clone().sub(scope.target); 
    var targetDistance = offset.length(); 

    // half of the fov is center to top of screen 
    targetDistance *= Math.tan((scope.object.fov/2) * Math.PI/180.0); 

    // we actually don't use screenWidth, since perspective camera is fixed to screen height 
    var dist_l = (2 * deltaX * targetDistance/screenHeight); 
    var dist_u = (2 * deltaY * targetDistance/screenHeight); 

    /////// X,Y limit calculation ////// 
    var limit = 100; 
    if((position.x - dist_l) <= -limit){ 
     dist_l = -0.1; 
    }else if((position.x - dist_l) >= limit){ 
     dist_l = 0.1; 
    } 
    if((position.z - dist_u) <= -limit){ 
     dist_u = -0.1; 
    }else if((position.z - dist_u) >= (limit*2.5)){ 
     dist_u = 0.1; 
    } 
    /////// X,Y limit calculation ////// 

    scope.panLeft(dist_l); 
    scope.panUp(dist_u); 

} else if (scope.object instanceof THREE.OrthographicCamera) { 

    // orthographic 
    scope.panLeft(deltaX * (scope.object.right - scope.object.left)/screenWidth); 
    scope.panUp(deltaY * (scope.object.top - scope.object.bottom)/screenHeight); 

} 

回答

5

我也遇到了同樣的問題。解決方案不是觸摸pan()函數,而是檢查update()函數中的限制。找到行162:

// move target to panned location 
scope.target.add(panOffset); 

做你的極限計算這一行之後:

if (scope.target.x > 1000) 
    scope.target.setX(1000); 
if (scope.target.x < 0) 
    scope.target.setX (0); 
... 

這將夾住目標x位置。它工作得很順利。

+0

這正是我想要的,謝謝! :) – Alexus

+0

而不是手動檢查和設置'scope.target'的每個屬性,你可以像這樣['clamp()'](http://threejs.org/docs/#Reference/Math/Vector3.clamp) :'scope.target.clamp(new THREE.Vector3(minX,minY,minZ),new THREE.Vector3(maxX,maxY,maxZ))''。 –