Javascript鼠标事件不适用于三个.JS场景元素

Javascript鼠标事件不适用于三个.JS场景元素,javascript,events,three.js,mouseevent,mouse,Javascript,Events,Three.js,Mouseevent,Mouse,好吧,我只是有点困惑(而且可能太累了,我自己都不想现在就做这个……)。我试图让我的three.js应用程序在鼠标位于场景中特定类型的实体上时,为不同的鼠标事件执行不同的功能。类型为“mousemove”的事件工作正常 console.log('INTERSECTED.isgraphielement:',INTERSECTED.isgraphielement,'MouseEvent:',mouse.type) 语句注册我正在侦听的所有鼠标事件,包括“mousemove”、“click”、“dblc

好吧,我只是有点困惑(而且可能太累了,我自己都不想现在就做这个……)。我试图让我的three.js应用程序在鼠标位于场景中特定类型的实体上时,为不同的鼠标事件执行不同的功能。类型为“mousemove”的事件工作正常

console.log('INTERSECTED.isgraphielement:',INTERSECTED.isgraphielement,'MouseEvent:',mouse.type)

语句注册我正在侦听的所有鼠标事件,包括“mousemove”、“click”、“dblclick”、“wheel”和“oncontextmenu”。它还检测
相交的.isGraphElement
对象

但是,当运行
transformGraphElement()
时,在
transformGraphElement
函数中只注册“mousemove”事件。即使是注释掉的代码测试行也不会运行,该测试行应该控制台输出“gotthedblclick!”。这里似乎没有检测到我正在侦听的其他鼠标事件

我在
transformGraphElement
函数中尝试过的东西:

  • 将dblclick事件替换为我正在侦听的其他类型之一。没有骰子
  • 注释掉用于处理mousemove事件的行。没有骰子
  • 测试查看my
    obj.referent.transformOnDblClick()函数中是否存在错误。我只知道它根本没有被调用(当我将它与“mousemove”事件关联时,它运行得非常完美)
  • 为我的“if”语句尝试不同的有效语法。没有骰子
  • 正在尝试对
    mouseEventHandler()
    函数进行重构。没有骰子
这是我的相关代码:

function render() { mouseEventHandler( transformGraphElement, unTransformGraphElement ); requestAnimationFrame( render ); renderer.render(scene, entities.cameras.perspCamera ); } function mouseEventHandler( fn, revFn ){ // update the picking ray with the camera and mouse position ray.setFromCamera( mouse, entities.cameras.perspCamera ); // calculate objects intersecting the picking ray var intersects = ray.intersectObjects( scene.children ); if ( intersects && intersects[0] && intersects[0].object ){ if ( intersects[ 0 ].object != INTERSECTED ){ // if there's an intersected object if ( INTERSECTED ) { // and if a previous INTERSECTED object exists: revFn( INTERSECTED, mouse ); // restore the previous intersected object to its non-intersected state. } INTERSECTED = intersects[ 0 ].object; // set the currently intersected object to INTERSECTED fn( INTERSECTED, mouse ); // transform the currentlY INTERSECTED object. } console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', mouse.type ); } } function transformGraphElement( obj, mouse ){ // Check if INTERSECTED is a Graph Element, and if so, invoke it's transform function. if ( mouse.type === "mousemove" && obj.isGraphElement ) { obj.referent.transformOnMouseOver(); } //if ( mouse.type === 'dblclick' ) { console.log('Got the dblclick Inside!') } if ( mouse.type === 'dblclick' && obj.isGraphElement ) { obj.referent.transformOnDblClick(); ) } } function unTransformGraphElement( obj, mouse ){ // Check if INTERSECTED is a Graph Element, and if so, revert it to it's pre-mouseEvent state. if ( mouse.type === "mousemove" && obj.isGraphElement ) { obj.referent.transformOnMouseOut(); } if ( mouse.type === 'dblclick' ) { console.log('Got the dblclick Out!') } } 函数render(){ mouseEventHandler(transformgraphielement、untransformgraphielement); 请求动画帧(渲染); renderer.render(场景,entities.cameras.perspCamera); } 函数mouseEventHandler(fn,revFn){ //使用相机和鼠标位置更新拾取光线 光线.setFromCamera(鼠标,entities.cameras.perspCamera); //计算与拾取光线相交的对象 var intersects=光线.intersectObjects(scene.children); if(相交和相交[0]&&intersects[0]。对象){ 如果(相交[0]。对象!=相交){//如果存在相交对象 如果(相交){//并且如果存在先前相交的对象: revFn(相交,鼠标);//将上一个相交对象恢复为其非相交状态。 } 相交=相交[0]。对象;//将当前相交的对象设置为相交 fn(相交,鼠标);//变换当前相交的对象。 } log('INTERSECTED.isGraphElement:',INTERSECTED.isGraphElement,'MouseEvent:',mouse.type); } } 函数转换图形元素(obj,鼠标){ //检查INTERSECTED是否是图形元素,如果是,则调用它的transform函数。 if(mouse.type==“mousemove”&&obj.isGraphElement){obj.referent.transformOnMouseOver();} //if(mouse.type==='dblclick'){console.log('Got thedblclick Inside!')} if(mouse.type==='dblclick'&&obj.isGraphElement){obj.referent.transformOnDblClick();)} } 函数未转换图形元素(obj,鼠标){ //检查INTERSECTED是否为图形元素,如果是,则将其还原为鼠标事件前的状态。 if(mouse.type==“mousemove”&&obj.isGraphElement){obj.referent.transformOnMouseOut();} if(mouse.type==='dblclick'){console.log('Got the dblclick Out!')} } 我想知道这是否是我遇到的某种默认行为或重写,但是event.preventDefault()行不应该处理它吗?(下面的代码在上面的代码之前运行):

var-ray=new-THREE.Raycaster(); var mouse=new THREE.Vector2(); 变量相交;//距离摄影机最近的对象 鼠标上的函数(事件){ event.preventDefault(); //在标准化设备坐标中计算鼠标位置 //两个组件的-1到+1 mouse.x=(event.clientX/window.innerWidth)*2-1; mouse.y=-(event.clientY/window.innerHeight)*2+1; mouse.type=(event.type); } 函数listenFor(){ document.addEventListener('click',onMouse,false); document.addEventListener('mousemove',onMouse,false); document.addEventListener('mousedown',onMouse,false); document.addEventListener('dblclick',onMouse,false) 文件。添加的监听器('wheel',onMouse,false); document.addEventListener('contextmenu',onMouse,false); } listen();
Console.log(mouse.type)
onMouse()函数内部注册我正在监听的所有鼠标事件

我已经用头撞了三个小时了。我希望是因为心情不好而错过的一些简单的愚蠢的事情。欢迎提供所有帮助,如果有任何对提供有用答案很重要的缺失代码,请告诉我。。。我不这么认为,但再一次,心情不好


谢谢你的帮助

第二天早上,我起床解决了这个问题。问题在于代码中的逻辑:

if ( intersects[ 0 ].object != INTERSECTED ){ // if there's an intersected object if ( INTERSECTED ) { // and if a previous INTERSECTED object exists: revFn( INTERSECTED, mouse ); // restore the previous intersected object to its non-intersected state. } INTERSECTED = intersects[ 0 ].object; // set the currently intersected object to INTERSECTED fn( INTERSECTED, mouse ); // transform the currentlY INTERSECTED object. } console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', mouse.type ); }
mouseEventHandler()
的逻辑仍然存在一些问题,但核心问题已经得到解决。值得一提的一些其他重构:

  • 无需添加
    事件。在
    鼠标中键入
  • 我将
    mouseEventHandler()
    调用从
    render()
    移动到
    onMouse()
    if ( intersects[ 0 ].object != INTERSECTED ){   // if there's an intersected object
    
                    if ( INTERSECTED ) {                        // and if a previous INTERSECTED object exists:
                        revFn( INTERSECTED, mouse );                    // restore the previous intersected object to its non-intersected state.                
                    }                       
    
                    INTERSECTED = intersects[ 0 ].object;       // set the currently intersected object to INTERSECTED
                    fn( INTERSECTED, mouse );                           // transform the currentlY INTERSECTED object.
    
                    }   
    
                    console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', mouse.type );
                }
    
    var ray = new THREE.Raycaster();
    var mouse = new THREE.Vector2();
    var INTERSECTED;  // Object closest to the camera
    
    function onMouse( event ) {
    
        event.preventDefault();
    
        // calculate mouse position in normalized device coordinates
        // (-1 to +1) for both components
    
        mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
    
        mouseEventHandler( event /*, transformGraphElement, unTransformGraphElement */ );
    
    }
    
    function listenFor(){
        document.addEventListener( 'click', onMouse, false );
        document.addEventListener( 'mousemove', onMouse, false );
        document.addEventListener( 'mousedown', onMouse, false );
        document.addEventListener( 'dblclick', onMouse, false )
        document.addEventListener( 'wheel', onMouse, false );
        document.addEventListener( 'contextmenu', onMouse, false );
    }
    
    listenFor();
    
    /* ... */
    
    function render() {
    
        requestAnimationFrame( render );
        renderer.render(scene, entities.cameras.perspCamera );
    }
    
    function mouseEventHandler( event /* , fn, revFn */ ){
    
        // update the picking ray with the camera and mouse position
        ray.setFromCamera( mouse, entities.cameras.perspCamera );
    
        // calculate objects intersecting the picking ray
        var intersects = ray.intersectObjects( scene.children );
    
        // if there's at least one intersected object...
        if ( intersects && intersects[0] && intersects[0].object ){
    
            // Check if the event is a mouse move, INTERSECTED exists and we're sitting on the same INTERSECTED object as the last time this function ran...        
            if ( event.type === 'mousemove' ){
                // Check if the current top-level intersected object is the previous INTERSECTED        
                if ( intersects[ 0 ].object != INTERSECTED ){
                    // ... if there is a previous INTERSECTED
                    if ( INTERSECTED ) {    
                        // restore the previous INTERSECTED to it's previous state.
                        unTransformGraphElementOnMouseOut( INTERSECTED, event );                                    
                    }                       
                    // set the currently intersected object to INTERSECTED  
                    INTERSECTED = intersects[ 0 ].object;       
                    // and transform it accordingly.
                    transformGraphElementOnMouseOver( INTERSECTED, event );                         
                    }   
            }
    
            // Check if the mouse event is a doubble click 
            if ( event.type === 'dblclick' ){
                // If the currently intersected object is INTERSECTED
                if ( intersects[ 0 ].object === INTERSECTED ){
                    // select it.               
                    transformGraphElementOnSelect( INTERSECTED, event );                            
                }
                // If the currently intersected object is not INTERSECTED
                if ( intersects[ 0 ].object !== INTERSECTED ){
                    // If there is a previous INTERSECTED
                    if ( INTERSECTED )
                        // restore it to its unselected state.
                        unTransformGraphElementOnUnselect( INTERSECTED, event );                                
                }
            }       
    
        INTERSECTED && console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', event.type );           
        }
    }
    
    function transformGraphElementOnMouseOver( obj, event ){
        if ( obj.isGraphElement ) { obj.referent.transformOnMouseOver(); }  
    }
    
    function unTransformGraphElementOnMouseOut( obj, event ){
        if ( obj.isGraphElement ) { obj.referent.transformOnMouseOut(); }
    }
    
    function transformGraphElementOnSelect( obj, event ){
        if ( obj.isGraphElement ) { obj.referent.transformOnDblClick(); }   
    }
    
    function unTransformGraphElementOnUnselect( obj, event ){
        if ( obj.isGraphElement ) { obj.referent.unTransformOnDblClick(); } 
    }