简体   繁体   English

Orbit Controls 跟随鼠标不点击three.js

[英]Orbit Controls follow the mouse without clicking three.js

Does anyone have a code to force orbit controls in three.js to move the scene on mousemove instead of click + mousemove I've tried using the answer in this thread: How to recreate the Three.js OrbitControl movement on mouse move?有没有人有代码可以强制three.js 中的轨道控件在mousemove 上移动场景而不是click + mousemove 我试过使用这个线程中的答案: 如何在鼠标移动时重新创建Three.js OrbitControl 移动? but sadly it throws an error "Maximum call stack size exceeded error" and I just get a black canvas with nothing in it...但遗憾的是它抛出一个错误“最大调用堆栈大小超出错误”,我只得到一个黑色画布,里面什么都没有......

I also tried changing我也试过改变

scope.domElement.addEventListener( 'mousedown', onMouseDown, false );

to

scope.domElement.addEventListener( 'mousemove', onMouseDown, false );

In the OrbitControls.js file but that seems to freeze when moving and stops every now and again...在 OrbitControls.js 文件中,但它似乎在移动和时不时停止时冻结......

Has anyone managed to solve this?有没有人设法解决这个问题? Thanks in advance!提前致谢!

How about this;这个怎么样; https://jsfiddle.net/EthanHermsey/e3b501cw/51/ https://jsfiddle.net/EthanHermsey/e3b501cw/51/

document.addEventListener('mousemove', function(e){
    let scale = -0.01;
    orbit.rotateY( e.movementX * scale );
    orbit.rotateX( e.movementY * scale ); 
    orbit.rotation.z = 0; //this is important to keep the camera level..
})

//the camera rotation pivot
orbit = new THREE.Object3D();
orbit.rotation.order = "YXZ"; //this is important to keep level, so Z should be the last axis to rotate in order...
orbit.position.copy( mesh.position );
scene.add(orbit );

//offset the camera and add it to the pivot
//you could adapt the code so that you can 'zoom' by changing the z value in camera.position in a mousewheel event..
let cameraDistance = 1;
camera.position.z = cameraDistance;
orbit.add( camera );

Use an Object3D that acts as a pivot for the camera.使用一个 Object3D 作为相机的枢轴。 It rotates on mouse movement.它随着鼠标移动而旋转。

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

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