我有一个使用 Three.js...canvas 构建项目,您可以在其中拖动对象 并玩弄相机 View ......有一个著名的例子 - “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 );
}
}
有人知道问题出在哪里吗?
最佳答案
在这一行中:
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;
关于javascript - iPhone 上的 Three.js 项目 - 事件问题(选择&拖动对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8307535/