Three.js可拖动的形状

时间:2014-10-22 23:41:47

标签: javascript three.js

我正在研究draggable cube

的代码

但是我无法理解在这部分代码中创建平面和所选对象之间偏移的原因:

function onDocumentMouseDown( event ) {

            event.preventDefault();

            var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
            projector.unprojectVector( vector, camera );

            var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

            var intersects = raycaster.intersectObjects( objects );

            if ( intersects.length > 0 ) {

                controls.enabled = false;

                SELECTED = intersects[ 0 ].object;

                var intersects = raycaster.intersectObject( plane );
                offset.copy( intersects[ 0 ].point ).sub( plane.position );// OFFSET

                container.style.cursor = 'move';

            }

        }

1 个答案:

答案 0 :(得分:1)

移动时的物体坐标计算在平面上鼠标的点击坐标,因此存在移位。可以不做偏移:

function onDocumentMouseMove( event ) {

    ...if ( SELECTED ) {

       var intersects = raycaster.intersectObject( plane );
       SELECTED.position.copy( intersects[ 0 ].point );

   }
}

尝试,你会感到有所不同。您也可以根据此example制作。查看控制台中的消息。

<script src="js/controls/EventsControls.js"></script>

EventsControls = new EventsControls( camera, renderer.domElement );

EventsControls1.attachEvent( 'dragAndDrop', function () {

    this.container.style.cursor = 'move';
    this.focused.position.y = this.previous.y;

});

var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh ); 

EventsControls.attach( mesh );