Zoom to object in ThreeJS

前端 未结 6 1438
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 09:08

Where can I change the zoom direction in three.js? I would like to zoom in the direction of the mouse cursor but I don\'t get where you can change the zoom target.

6条回答
  •  温柔的废话
    2020-12-10 09:37

    I am completely new to Three.js but it's.... wonderful. I am not even a good developer. I am practicing. I was facing the problem of zooming to the mouse location and I think I improved the code a little bit. Here it is.

    // zooming to the mouse position
    window.addEventListener('mousewheel', function (e) { mousewheel(e); }, false); 
    function mousewheel(event) {
        orbitControl.enableZoom = false;
        event.preventDefault();
    
        // the following are constants depending on the scale of the scene
        // they need be adjusted according to your model scale  
        var factor = 5; 
        // factor determines how fast the user can zoom-in/out  
        var minTargetToCameraDistanceAllowed = 15; 
        // this is the minimum radius the camera can orbit around a target. 
    
        // calculate the mouse distance from the center of the window
        var mX = (event.clientX / window.innerWidth) * 2 - 1;
        var mY = -(event.clientY / window.innerHeight) * 2 + 1;
        var vector = new THREE.Vector3(mX, mY, 0.5);
    
        vector.unproject(camera);
        vector.sub(camera.position);
    
        if (event.deltaY < 0) { 
            // zoom-in -> the camera is approaching the scene
            // with OrbitControls the target is always in front of the camera (in the center of the screen)
            // So when the user zoom-in, the target distance from the camera decrease.
            // This is achieved because the camera position changes, not the target.
            camera.position.addVectors(camera.position, vector.setLength(factor));
        } else { 
            // zoom-out -> the camera is moving away from the scene -> the target distance increase
            camera.position.subVectors(camera.position, vector.setLength(factor));
        }
        // Now camera.position is changed but not the control target. As a result: 
        //  - the distance from the camera to the target is changed, and this is ok.
        //  - the target is no more in the center of the screen and needs to be repositioned. 
        // The new target will be in front of the camera (in the direction of the camera.getWorldDirection() )
        // at a suitable distance (no less than the value of minTargetToCameraDistanceAllowed constant).
        // Thus, the target is pushed a little further if the user approaches too much the target.
        var targetToCameraDistance = Math.max(minTargetToCameraDistanceAllowed, 
                                                orbitControl.target.distanceTo(camera.position));
        var newTarget = camera.getWorldDirection().setLength( targetToCameraDistance ).add(camera.position);
        orbitControl.target = newTarget;
    
        camera.updateProjectionMatrix();
    
    }
    

    Another improvement could be to set the targetToCameraDistance to the distance of an object hit by the mouse when the user starts orbiting.
    If the mouse hit an object, and the distance > minTargetToCameraDistanceAllowed, then the new target is calculated and set.
    ... but I still don't know how to do this.

提交回复
热议问题