Webgl 雷不';t到达JSON
我试图用鼠标点击事件捕捉JSON对象。我使用光线来识别对象,但由于某些原因,对象并不总是被识别的。我怀疑这与我移动相机有关,因为当我在对象附近单击时,我被识别 你能帮我找出如何根据相机的移动正确设置光线吗 代码如下: 这是鼠标按下事件的一部分*Webgl 雷不';t到达JSON,webgl,three.js,Webgl,Three.js,我试图用鼠标点击事件捕捉JSON对象。我使用光线来识别对象,但由于某些原因,对象并不总是被识别的。我怀疑这与我移动相机有关,因为当我在对象附近单击时,我被识别 你能帮我找出如何根据相机的移动正确设置光线吗 代码如下: 这是鼠标按下事件的一部分* document.addEventListener(“mousemove”,onDocumentMouseMove,false); 文件。添加的列表(“mouseup”,onDocumentMouseUp,false); 文件。添加了监听器(“mouse
document.addEventListener(“mousemove”,onDocumentMouseMove,false);
文件。添加的列表(“mouseup”,onDocumentMouseUp,false);
文件。添加了监听器(“mouseout”,onDocumentMouseOut,false);
mouseXOnMouseDown=event.clientX-windowHalfX;
TargetRotationNoMouseDown=targetRotation;
var射线,交点;
_vector.set((event.clientX/window.innerWidth)*2-1,-(event.clientY/window.innerHeight)*2+1,0);
投影仪.未投影向量(_向量,摄像机);
ray=新的三个.ray(camera.position,_vector.subSelf(camera.position).normalize());
相交=光线。相交对象(家具);
如果(交点长度>0){
选定的_块=交点[0]。对象;
_向量集(0,0,0);
选定的块。设置角度因子(_向量);
选定的块。设置角度速度(_向量);
所选的_block.setLinearFactor(_vector);
选定的块。setLinearVelocity(_向量);
鼠标位置。复制(交点[0]。点);
block\u offset.sub(选定的块位置、鼠标位置);
相交平面.position.y=鼠标位置.y;
}
}
这是相机移动的一部分*
camera.position.x=(Math.cos(计时器)*10);
camera.position.z=(数学sin(计时器)*10);
摄像机。注视(场景。位置);
Hmmm,如果看不到程序实际运行情况的演示,很难说问题出在哪里。我建议看一下我今天一直在做的演示。我处理我的相机、控制器和光线。我也在使用JSON
首先,您可以查看我的演示:了解它在做什么,您的描述听起来有什么相似之处。如果你能理解我的代码,你应该能够修改它。
--如果您希望直接链接到源代码: 我还有另一个例子,你可能会发现我使用光线和鼠标碰撞来很有用
--源代码: 最后,我将在第一个演示中发布我的鼠标事件的细节,以及我如何使用轨迹球相机处理它,希望其中的一些内容将引导您找到解决方案:
/** Event fired when the mouse button is pressed down */
function onDocumentMouseDown(event) {
event.preventDefault();
/** Calculate mouse position and project vector through camera and mouse3D */
mouse3D.x = mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse3D.y = mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
mouse3D.z = 0.5;
projector.unprojectVector(mouse3D, camera);
var ray = new THREE.Ray(camera.position, mouse3D.subSelf(camera.position).normalize());
var intersects = ray.intersectObject(maskMesh);
if (intersects.length > 0) {
SELECTED = intersects[0].object;
var intersects = ray.intersectObject(plane);
offset.copy(intersects[0].point).subSelf(plane.position);
killControls = true;
}
else if (controls.enabled == false)
controls.enabled = true;
}
/** This event handler is only fired after the mouse down event and
before the mouse up event and only when the mouse moves */
function onDocumentMouseMove(event) {
event.preventDefault();
/** Calculate mouse position and project through camera and mouse3D */
mouse3D.x = mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse3D.y = mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
mouse3D.z = 0.5;
projector.unprojectVector(mouse3D, camera);
var ray = new THREE.Ray(camera.position, mouse3D.subSelf(camera.position).normalize());
if (SELECTED) {
var intersects = ray.intersectObject(plane);
SELECTED.position.copy(intersects[0].point.subSelf(offset));
killControls = true;
return;
}
var intersects = ray.intersectObject(maskMesh);
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
INTERSECTED = intersects[0].object;
INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
plane.position.copy(INTERSECTED.position);
}
}
else {
INTERSECTED = null;
}
}
/** Removes event listeners when the mouse button is let go */
function onDocumentMouseUp(event) {
event.preventDefault();
if (INTERSECTED) {
plane.position.copy(INTERSECTED.position);
SELECTED = null;
killControls = false;
}
}
/** Removes event listeners if the mouse runs off the renderer */
function onDocumentMouseOut(event) {
event.preventDefault();
if (INTERSECTED) {
plane.position.copy(INTERSECTED.position);
SELECTED = null;
}
}
为了获得我第一次演示中想要的效果,我必须将其添加到动画循环中,以便使用killControls标志根据鼠标碰撞有选择地打开和关闭轨迹球相机控件:
if (!killControls) controls.update(delta);
else controls.enabled = false;