OrbitControl - 限制平移运动

Ale*_*xus 3 javascript html5 three.js

有没有办法限制场景中摄像机的平移运动?

尝试pan在orbitControls中改变方法,但我对结果并不满意,我希望有更方便/更合适的方法来做到这一点.

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 );

}
Run Code Online (Sandbox Code Playgroud)

小智 7

我遇到了同样的问题.解决方案不是触摸pan()函数,而是检查update()函数中的限制.找到162行:

// move target to panned location
scope.target.add( panOffset );
Run Code Online (Sandbox Code Playgroud)

在此行之后立即进行限制计算:

if (scope.target.x > 1000)
    scope.target.setX(1000);
if (scope.target.x < 0)
    scope.target.setX (0);
...
Run Code Online (Sandbox Code Playgroud)

这将钳制目标x位置.它工作得很顺利.