OrbitControl - restrict panning movement

后端 未结 2 657
暗喜
暗喜 2021-01-14 04:38

Is there any way to restrict the panning movement of a camera in scene?

Tried altering the pan method in orbitControls but I\'m not really satisfied wit

相关标签:
2条回答
  • 2021-01-14 05:13

    I have encountered the same problem. The solution is not to touch the pan() function but to check the limits in the update() function. Locate the line 162:

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

    Do your limit calculations right after this line:

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

    This will clamp the target x-position. It works quite smoothly.

    0 讨论(0)
  • 2021-01-14 05:18

    I have the exact same problem and thanks to David's solution, which gives me a lot of inspiration. I've some add up to David's answer:

    If we only set target X, when keep panning to that limit, I have some unwanted rotation effect. This is because OrbitControls is working with 2 things: the target and the camera. To solve that, we need to set both target and the camera.

    scope.target.setX(0); 
    camera.position.setX(0);
    

    In this way, we guarantee the camera is always on the top of the object, hence no unwanted rotation happens.

    If we want to keep the current rotation angle, we need to do some math. For example in my case, I only enabled the polar rotation:

    let polarAngle = scope.getPolarAngle(); 
    scope.target.set(0, camera.position.y + camera.position.z * Math.tan(polarAngle), 0);
    camera.position.setX(0);
    

    The idea is to set both target and camera position, but don't try to change the rotation angle. If there is rotation, do some math to calculate the target position first.

    0 讨论(0)
提交回复
热议问题