首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在three.js中用DragControls旋转一条直线的端点?

如何在three.js中用DragControls旋转一条直线的端点?
EN

Stack Overflow用户
提问于 2018-06-14 05:38:32
回答 1查看 150关注 0票数 0

我已经修改了DragControls.js,添加了一个约束,使其只在y轴上移动直线。

我使用的代码行如下:

代码语言:javascript
复制
material = new THREE.LineBasicMaterial( { color: 0xFF0000 } );
geometry.vertices.push(new THREE.Vector3( 0, 0, 0) );
geometry.vertices.push(new THREE.Vector3( 10, 0, 0) );
scene.add( redline );
dragControls = new THREE.DragControls( objects, camera, renderer.domElement );

这是我对Dragcontrols.js的修改,我在THREE.Dragcontrols函数中添加了约束。

代码语言:javascript
复制
this.constrains = function(xyz) {

    if (xyz === undefined)
        xyz = 'xyz';

    moveX = moveY = moveZ = false;

    if (xyz.indexOf('x') > -1) {
        moveX = true;
    }

    if (xyz.indexOf('y') > -1) {
        moveY = true;
    }

    if (xyz.indexOf('z') > -1) {
        moveZ = true;
    }

    return this;

};

这就是我在onDocumentMouseMove(事件)中应用它的方式:

代码语言:javascript
复制
if ( _selected && scope.enabled ) {
        if (event.altKey === true) {
            rotationDrag = true;
        }

//TODO: somewhere here should be a rotationDrag check and if it's true 
than rotate the line instead of moving it
        if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {

            /**
             *  Constrain feature added
             */

            _intersection.sub( _offset );
            //_selected.position.copy( _intersection.sub( _offset ) );

            if (!rotationDrag) {
                if (moveX) _selected.position.x = _intersection.x;
                if (moveY) _selected.position.y = _intersection.y;
                if (moveZ) _selected.position.z = _intersection.z;
            } else {
                debugger;
            }
        }

        scope.dispatchEvent( { type: 'drag', object: _selected } );

        return;

    }

调试器在哪里我想实现的是,如果按下altKey并移动鼠标,线的左端在y轴上移动,而右端在X和Y坐标上移动。

基本上,这条线围绕其端点旋转,就像时钟的指针一样。

你知道怎么做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2018-06-16 20:07:48

答案很简单,只需要弄清楚,如何计算角度和方向。

代码语言:javascript
复制
         if ( _raycaster.ray.intersectPlane( _plane, _intersection ) ) {

            /**
             *  Constrain feature added
             */

            _intersection.sub( _offset );
            //_selected.position.copy( _intersection.sub( _offset ) );
            if (!rotationDrag) {
                if (moveX) _selected.position.x = _intersection.x;
                if (moveY) _selected.position.y = _intersection.y;
                if (moveZ) _selected.position.z = _intersection.z;
            } else {
                _selected.rotateZ();
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50846654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档