javascript Three.js 3D缩放到鼠标位置

v8wbuo2f  于 2023-06-04  发布在  Java
关注(0)|答案(3)|浏览(253)

我还有个问题。我用Three.js + orbitControls.js做了一个项目,用于处理相机。有一个场景,上面有一个简单的立方体,还有相机。我有一个来自orbitControls.js的摄像头,当我使用鼠标滚轮时,它会向屏幕中心放大/缩小(默认行为)。我需要修改,以便将放大/缩小到3D对象上的鼠标位置,就像在3D编辑器中一样,如3D Max,Blender。
我理解逻辑应该是这样的:从你开始缩放的地方取鼠标的位置,实际上你想放大的地方,存储那个值,放大并得到新的鼠标位置,找到第一个鼠标位置和当前位置之间的偏移量,并在放大过程中将相机移动到那个偏移量。但是,它不工作ok。我想主要是我的相机的坐标问题,以及我想放大的位置。
下面是我的代码:

window.onload = function() {

        var renderer = new THREE.WebGLRenderer();
        var width = 640;
        var height = 480;
        renderer.setSize( width, height );
        var div = document.getElementById('canvas')
        div.appendChild( renderer.domElement );

        var scene = new THREE.Scene();

            camera = new THREE.PerspectiveCamera(
            70,
            width / height,
            0.1,
            10000
        );
        camera.position.set( 0, 10, 10 );
        camera.lookAt( scene.position );

        control = new THREE.OrbitControls( camera );

        var geometry = new THREE.CubeGeometry( 5, 5, 5 );
        var material = new THREE.MeshLambertMaterial( { color: 0xFF0000 } );
         mesh = new THREE.Mesh( geometry, material );
        scene.add( mesh );

        var light = new THREE.PointLight( 0xFFFF00 );
        light.position.set( 10, 0, 10 );
        scene.add( light );

        var dt = 30;
        var renderFunc = function() {
          update(dt);
          renderer.render(scene,camera);
        };

        setInterval(renderFunc,dt);

        //MY ZOOMING CODE.
        var mouseVector = new THREE.Vector3();
        var projector = new THREE.Projector();  
        var startMousePos = new THREE.Vector3();
        var intersectedPoint = new THREE.Vector3();
        var intersectedOffset = new THREE.Vector3();

//FIND INTERSECTED POSITION ON THE CUBE         
        canvas.onmousemove = function (event)
        {
            mouseVector.x = ( event.clientX / 640 ) * 2 - 1;
            mouseVector.y = - ( event.clientY / 480 ) * 2 + 1;
            startMousePos.x = mouseVector.x;
            startMousePos.y = mouseVector.y;

            var vector = new THREE.Vector3( mouseVector.x, mouseVector.y, 1 );
            projector.unprojectVector( vector, camera );
            var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

            var intersects = ray.intersectObjects( scene.children );

            if ( intersects.length > 0 )
            {
                 intersectedPoint = intersects[0].point;
            }
        }
//HERE I THINK I MUST TRANSLATE MY CAMERA.
        canvas.onmousewheel = function(event)
        {
            if(event.wheelDelta > 0)
            {
                camera.position.x -= intersectedPoint.x;
                camera.position.y -= intersectedPoint.y;
                camera.position.z -= intersectedPoint.z;
            }
            else
            {
                camera.position.x += intersectedPoint.x;
                camera.position.y += intersectedPoint.y;
                camera.position.z += intersectedPoint.z;

            }
        }
    };

以前有人这么做过吗?你能告诉我你是怎么解决的吗?或者给予我一个建议如何处理?
谢谢!

cwxwcias

cwxwcias1#

获得相交点后,可以将其应用为orbitControl的目标,如下所示:

if ( intersects.length > 0 )
        {
             intersectedPoint = intersects[0].point;
        }

 control.target = intersectedPoint ;

然后,当你将放大它将朝向你选择的点。

yk9xbfzb

yk9xbfzb2#

试试这个,很管用

mousewheel = function (event) {
var factor = 15;
var mX = (event.clientX / jQuery(container).width()) * 2 - 1;
var mY = -(event.clientY / jQuery(container).height()) * 2 + 1;
var vector = new THREE.Vector3(mX, mY, 0.1);
vector.unproject(camera);
vector.sub(camera.position);
if (event.deltaY < 0) {
   camera.position.addVectors(camera.position, vector.setLength(factor));
   controls.target.addVectors(controls.target, vector.setLength(factor));
} else {
   camera.position.subVectors(camera.position, vector.setLength(factor));
   controls.target.subVectors(controls.target, vector.setLength(factor));
}
};
efzxgjgh

efzxgjgh3#

根据这个Pull Request,这个功能将被集成到version r154中的three.js中-计划于2023年6月28日发布。
这还应该使用following question中提到的正交摄影机处理缩放

相关问题