three.js rotate object on mouse down and move

前端 未结 1 1448
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 04:31

I\'m trying to get a good mouse movement in my scene so I can rotate around the object.

I have two problems, I can figure out how to limit the movement so that it n

相关标签:
1条回答
  • 2021-01-03 05:01

    You can rotate you scene with this code,

    To ensure to not rotate under 0, simule rotation of a vector (0,0,1) and check if y of vector is negative

    var mouseDown = false,
            mouseX = 0,
            mouseY = 0;
    
        function onMouseMove(evt) {
            if (!mouseDown) {
                return;
            }
    
            evt.preventDefault();
    
            var deltaX = evt.clientX - mouseX,
                deltaY = evt.clientY - mouseY;
            mouseX = evt.clientX;
            mouseY = evt.clientY;
            rotateScene(deltaX, deltaY);
        }
    
        function onMouseDown(evt) {
            evt.preventDefault();
    
            mouseDown = true;
            mouseX = evt.clientX;
            mouseY = evt.clientY;
        }
    
        function onMouseUp(evt) {
            evt.preventDefault();
    
            mouseDown = false;
        }
    
        function addMouseHandler(canvas) {
        canvas.addEventListener('mousemove', function (e) {
            onMouseMove(e);
        }, false);
        canvas.addEventListener('mousedown', function (e) {
            onMouseDown(e);
        }, false);
        canvas.addEventListener('mouseup', function (e) {
            onMouseUp(e);
        }, false);
    }
    
        function rotateScene(deltaX, deltaY) {
        root.rotation.y += deltaX / 100;
        root.rotation.x += deltaY / 100;
    }
    
    0 讨论(0)
提交回复
热议问题