简体   繁体   English

相机单击鼠标即可平滑放大到对象位置

[英]Camera Smoothly Zoom Into Object Position On Mouse Click

So I have a scene with multiple boxes and a PerspectiveCamera. 因此,我有一个包含多个框和一个PerspectiveCamera的场景。

I'd like to achieve this effect whenever I click on a specific box. 我想在每次单击特定框时都达到这种效果。

  • The camera will smoothly transition from its current position to the box's position 摄像机将从当前位置平稳过渡到盒子的位置
  • The box is centered in the camera's viewport 该框位于相机视口的中心
  • The camera will smoothly zoom in and focus on the box 相机将平稳放大并对焦在盒子上

This effect was inspired by 100,000 Stars . 这种效果的灵感来自100,000星 Whenever a user would click on a star, the camera would zoom into the star and display it in the center. 每当用户单击星标时,相机就会放大该星标并将其显示在中央。 I'm trying to replicate that effect. 我正在尝试复制这种效果。

I'm currently able to grab the box's position and look at it. 目前,我可以抓住盒子的位置并查看它。 But I want to do more than that and I'm unsure how to proceed. 但是我想做的还不止于此,我不确定该如何进行。

I think what you need is an animation, there are many animation libraries like anime.js and tween.js . 我认为您需要的是动画,有很多动画库,例如anime.jstween.js As you have grabbed the position after translating, you can make an animation to smooth your translation. 翻译后您已抓住位置,可以制作动画来平滑翻译。 Here is a snippet with tween.js: 这是tween.js的片段:

var tween2 = new TWEEN.Tween(camera.position)
                .to({
                    x : target.position.x,
                    y : target.position.y,
                    z : target.position.z
                } , 1000)
                .easing(TWEEN.Easing.Linear.None)
                .start();

If you want to locate the box on the center of your camera. 如果要在相机中央找到该框。 we also need to change the camera rotation. 我们还需要更改相机的旋转角度。 Here is a way to compute the target rotation by using matrix. 这是一种使用矩阵计算目标旋转的方法。

    var rotation_matrix = new THREE.Matrix4();
    rotation_matrix.lookAt(target_position,target_box.position,camera.up);
    var target_rotation = new THREE.Euler(0,0,0,"XYZ");
    target_rotation.setFromRotationMatrix(rotation_matrix);
    //now, the target_rotation would be the rotation after translating.

Then, you can use the same way to make an animation to change the rotation. 然后,您可以使用相同的方法制作动画来更改旋转。

BTW, It seems in 100000 stars they switch camera in the end. 顺便说一句,似乎在100000星中他们最终切换了相机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM