iPhone 上的 Three.js 项目 - 事件问题(选择和拖动对象) [英] Three.js project on iPhone - events issue (select&drag object )

查看:53
本文介绍了iPhone 上的 Three.js 项目 - 事件问题(选择和拖动对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用three.js构建项目...画布,您可以在其中拖动对象并使用相机视图......有一个着名的例子 - Draggable Cubes",好吧,我的项目非常相似.

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.

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

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

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

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....

我猜这个函数有问题:

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?

推荐答案

在这一行:

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

您使用鼠标 Vector2 对象,但没有对其进行初始化.

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;

这篇关于iPhone 上的 Three.js 项目 - 事件问题(选择和拖动对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆