iPhone 上的 Three.js 项目 - 事件问题(选择&拖动对象)

发布于 2024-12-18 18:29:29 字数 1094 浏览 0 评论 0原文

我有一个使用 Three.js...canvas 构建项目,您可以在其中拖动对象 并玩弄相机视图......有一个著名的例子 - “Draggable Cubes”, 我的项目非常相似。

在我的项目中,有 3 个主要事件: mouseup /mousedown/ mousemove...

一切都很好......但现在我'我尝试在 iPhone 上运行此代码,更改我的事件 与 touchstart / touchmove / touchend...

移动对象功能似乎工作正常,但是当我尝试通过以下方式选择对象时点击他, 它总是被选择的同一对象......而不是我指向的对象......

我想问题出在这个函数上:

function onDocumentMouseDown( event ) {

            event.preventDefault();

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

            var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

            var intersects = ray.intersectObjects( objects );

            if ( intersects.length > 0 ) {

                SELECTED = intersects[ 0 ].object;

                var intersects = ray.intersectObject( plane );
                offset.copy( intersects[ 0 ].point ).subSelf( plane.position );

            }
}

有人知道问题是什么吗?

I have building project with three.js... canvas where you can drag object
and play with the camera view as well... there is a famous example- "Draggable Cubes",
well my project is pretty similar.

On my project there are 3 main events: mouseup /mousedown/ mousemove...

Well everything was ok....but now I'm trying to run this code on iPhone,changing my events
with touchstart / touchmove / touchend...

The moving object function seems to work fine, but when I'm trying to select the object by clicking him,
it's always the same object that been selected... and not the one I'm pointing on....

I guess the problem is with this function:

function onDocumentMouseDown( event ) {

            event.preventDefault();

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

            var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

            var intersects = ray.intersectObjects( objects );

            if ( intersects.length > 0 ) {

                SELECTED = intersects[ 0 ].object;

                var intersects = ray.intersectObject( plane );
                offset.copy( intersects[ 0 ].point ).subSelf( plane.position );

            }
}

Is anybody have an idea what is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-12-25 18:29:29

在这一行中:

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

您使用鼠标 Vector2 对象,但没有初始化它。

像这样的东西应该有效:

mouse.x = +(event.targetTouches[0].pageX / window.innerwidth) * 2 +-1;

mouse.y = -(event.targetTouches[0].pageY / window.innerHeight) * 2 + 1;

in this line :

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

You use the mouse Vector2 object but you don't initialize it.

Something like this should work :

mouse.x = +(event.targetTouches[0].pageX / window.innerwidth) * 2 +-1;

mouse.y = -(event.targetTouches[0].pageY / window.innerHeight) * 2 + 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文