Javascript 通过GUI防止光线投射拾取的三个js
我需要通过单击对象和弹出的非模态对话框来拾取对象,以显示有关对象的信息。它可以正常工作,但是如果用户单击任何GUI项目,包括对话框,并且后面有一个对象,则会选择三个js对象,从而更改弹出窗口中的信息 以下是片段Javascript 通过GUI防止光线投射拾取的三个js,javascript,jquery,three.js,dat.gui,Javascript,Jquery,Three.js,Dat.gui,我需要通过单击对象和弹出的非模态对话框来拾取对象,以显示有关对象的信息。它可以正常工作,但是如果用户单击任何GUI项目,包括对话框,并且后面有一个对象,则会选择三个js对象,从而更改弹出窗口中的信息 以下是片段 document.addEventListener( 'mousedown', onDocumentMouseDown, false ); jQuery对话框生成器: function createDialog(title, text, top) { return $("&
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
jQuery对话框生成器:
function createDialog(title, text, top) {
return $("<div class='dialog' title='" + title + "'><p id='bodyText'> " + text + "</p></div>")
.dialog({ position: { my: ("right" + 0 + "top+" + top), at: "right top" },
close : setDialogNull, });
}
其他GUI元素是使用dat.GUI创建的
当搜索这个问题时,大多数结果都是关于Unity的,作为一名经验丰富的开发人员,我真的不知道如何适应它们。如果没有更多的代码来了解您的设置和事件,就很难知道解决方案是什么。我会给保存three.js的元素一个特定ID,在onDocumentMouseDown中,检查它是否是活动元素。根据您的设置,您可能需要检查document.activeElement.parentElement.id或其他内容
function onDocumentMouseDown( e ) {
if(document.activeElement.id != "myThreeJSElementID"){
//some other element has been clicked so let's return with nothing:
return;
}
e.preventDefault();
console.log(dialog);
var mouseVector = new THREE.Vector3();
mouseVector.x = 2 * (e.clientX / window.innerWidth ) - 1;
mouseVector.y = 1 - 2 * ( e.clientY / window.innerHeight );
var raycaster = new THREE.Raycaster();
raycaster.setFromCamera( mouseVector, camera );
var intersects = raycaster.intersectObjects(objectCollection );
if (intersects.length > 0) {
var selectedObject = intersects[0];
console.log("Intersected object", intersects[0]);
selectedObject.object.material = (selectedObject.object.material.wireframe) ? selectedBoxMaterial : unselectedBoxMaterial;
var text = intersects[0].distance;
var title = "Shelf info";
if (dialog == false) {
createDialog(title, text, offset);
offset = offset - 50;
console.log(offset);
dialog = true;
console.log(dialog);
}
if (dialog == true) {
{jQuery("#bodyText").text(text);
}
}
}
更理想的情况是,您应该为three.js容器设置一个特定事件,而不是使用全局文档事件,但是现在应该可以使用异常。许多代码示例使用document.addEventListener是出于简单性,而不是最佳实践
function onDocumentMouseDown( e ) {
if(document.activeElement.id != "myThreeJSElementID"){
//some other element has been clicked so let's return with nothing:
return;
}
e.preventDefault();
console.log(dialog);
var mouseVector = new THREE.Vector3();
mouseVector.x = 2 * (e.clientX / window.innerWidth ) - 1;
mouseVector.y = 1 - 2 * ( e.clientY / window.innerHeight );
var raycaster = new THREE.Raycaster();
raycaster.setFromCamera( mouseVector, camera );
var intersects = raycaster.intersectObjects(objectCollection );
if (intersects.length > 0) {
var selectedObject = intersects[0];
console.log("Intersected object", intersects[0]);
selectedObject.object.material = (selectedObject.object.material.wireframe) ? selectedBoxMaterial : unselectedBoxMaterial;
var text = intersects[0].distance;
var title = "Shelf info";
if (dialog == false) {
createDialog(title, text, offset);
offset = offset - 50;
console.log(offset);
dialog = true;
console.log(dialog);
}
if (dialog == true) {
{jQuery("#bodyText").text(text);
}
}
}