简体   繁体   English

三.js - 射线相交和球体位置

[英]three.js - ray intersection and sphere position

I've a scene with tube geometry and ray intersection is working fine.我有一个带有管几何形状的场景,光线相交工作正常。 Upon ray intersection I am showing a small red color sphere and a tooltip next to the cursor.在光线相交时,我会在光标旁边显示一个小的红色球体和一个工具提示。 Please find the image without header:请找到没有标题的图像:

没有标题的图像

Now if I add a header with div element, the ray intersection is working but the issue is there is a distance between red sphere, tooltip and a mouse cursor.现在,如果我添加一个带有 div 元素的标题,光线交叉点正在工作,但问题是红色球体、工具提示和鼠标光标之间存在距离。 Please find the image with header:请找到带有标题的图像:

带有标题的图像

Please find below the code of header, tool-tip div, sphere and collision detection function:请在下面找到标题、工具提示div、球体和碰撞检测功能的代码:

Header:标题:

<div style="margin-top:10px;margin-left:3%;height:100px;width:70%">
  <label style="color:#b0bb02;font-size:14pt">three.js</label>
  <label style="color:#f5f7f9;font-size:14pt;font-weight:bold">Tube Geometry</label><br><br>               
</div>

Tool-tip div:工具提示div:

textDiv = document.createElement( 'div' );          
            textDiv.style.position = 'absolute';
            textDiv.style.top = '50px';
            textDiv.style.margin = '8px';
            textDiv.style.border = '1px solid blue';
            textDiv.style.zIndex = '100';
            textDiv.style.background = '#ffffff';
            textDiv.style.display = 'block';
            container.appendChild( textDiv );

Sphere geometry:球体几何:

dot = new THREE.Mesh ( new THREE.SphereGeometry( 1, 12, 1 ), new THREE.MeshBasicMaterial( { color: 0xff0000 } ) );

Collision detection:碰撞检测:

var intersects;
        
        function detectCollision(event){
            
            var vector = new THREE.Vector3(( event.clientX / window.innerWidth ) * 2 - 1,- ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
            
            /*var mouse_x =   ((event.pageX-event.target.offsetParent.offsetLeft) / renderer.domElement.width) * 2 - 1;
            var mouse_y = - ((event.pageY-event.target.offsetParent.offsetTop) / renderer.domElement.height) * 2 + 1;
            var vector = new THREE.Vector3(mouse_x, mouse_y, 0.5);*/
                
                projector.unprojectVector( vector, camera );
                var ray = new THREE.Raycaster( camera.position, vector.subSelf( camera.position ).normalize() );

                intersects = ray.intersectObjects( objects );                                   
                
                var pnt=0; var clickedMD = 0; var clickedDegree; var clickedTVD;
                
                if ( intersects.length > 0 ) {                  
                                                                            
                    dot.position.copy( intersects[0].point );
                    scene.add( dot );
                    
                    var fi = intersects[0].faceIndex;
                    pnt = Math.round(fi/11);                    
                    
                    clickedMD = pathObjColl[pnt].md;
                    clickedTVD = Math.round(pathObjColl[pnt].point.z);
                    clickedDegree = degrees[Math.round(fi%11)]; 
                    
                    // tooltip
                    var elem = document.getElementsByTagName("canvas");
                    var canvas = elem[0];
                    var x = event.pageX - canvas.offsetLeft ;
                    var y = event.pageY - canvas.offsetTop  ;
                    //console.log("X: "+x+" Y: "+y);
                    textDiv.style.top = y+"px";
                    textDiv.style.left = x+"px";                
                    textDiv.innerHTML = "&nbsp;Degree: "+clickedDegree+"<br/>&nbsp;MD: "+clickedMD+" ft<br/>&nbsp;TVD: "+clickedTVD+" ft";
                    if(textDiv.style.display == 'none'){
                        textDiv.style.display = 'block';    
                    }
                }
                
                else if(intersects.length == 0){
                    var textDivVis = textDiv.style.display;
                    console.log(textDivVis);
                    if(textDivVis == 'block'){
                        textDiv.style.display = 'none'; 
                    }
                    
                }
            
        }

demo on jsfiddle jsfiddle 上的演示

Why there is a distance between the mouse cursor, sphere geometry and a too-tip if I add header ?如果我添加标题,为什么鼠标光标、球体几何形状和太尖之间有距离?

Is the textDiv positioned absolutely? textDiv 是绝对定位的吗? Maybe the header just throws off the layout of the page, and the tooltip.. Try this:也许标题只是抛出了页面的布局和工具提示..试试这个:

textDiv.style.position = "absolute";

EDIT:编辑:

Well, in fact it's the header that needs to be absolutely positioned.. Otherwise it will move the canvas and your mouse position in HTML does not match mouse position in the webgl canvas.好吧,实际上它是需要绝对定位的标题。否则它会移动画布,并且您在 HTML 中的鼠标位置与 webgl 画布中的鼠标位置不匹配。

Alternatively, if you don't want the header to be on top of canvas, you can take your container position into account when calculating mouse position.或者,如果您不希望标题位于画布顶部,则可以在计算鼠标位置时考虑容器位置。 For the Vector:对于向量:

       var vector = new THREE.Vector3( 
                ( (event.pageX - container.offsetLeft) / window.innerWidth ) * 2 - 1, 
                - ( (event.pageY - container.offsetTop) / window.innerHeight ) * 2 + 1, 
                0.5 );

For the tooltip:对于工具提示:

                textDiv.style.top = (container.offsetTop + y)+"px";
                textDiv.style.left = (container.offsetLeft + x)+"px";       

Updated jsFiddle: http://jsfiddle.net/tje8y/更新 jsFiddle: http : //jsfiddle.net/tje8y/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM