How do I put limits on OrbitControl?

前端 未结 3 1131
攒了一身酷
攒了一身酷 2020-12-24 06:24

Is there a way to put limits on the OrbitControls.js? Imagine I\'m creating something above the ground, I wouldn\'t like the camera to go below the ground, know

3条回答
  •  -上瘾入骨i
    2020-12-24 06:43

    Just in case someone needs a a more robust answer with ground altitude and camera target adjustment:

    You find the angle relative to the controls target and the ground position of the camera (regardless of altitude) and assign the maxPolarAngle. Adjust for your up axis, mine was Y. Inside the controls change event:

    var centerPosition = controls.target.clone();
    centerPosition.y = 0;
    var groundPosition = camera.position.clone();
    groundPosition.y = 0;
    var d = (centerPosition.distanceTo(groundPosition));
    
    var origin = new THREE.Vector2(controls.target.y,0);
    var remote = new THREE.Vector2(0,d); // replace 0 with raycasted ground altitude
    var angleRadians = Math.atan2(remote.y - origin.y, remote.x - origin.x);
    controls.maxPolarAngle = angleRadians;
    

提交回复
热议问题