简体   繁体   English

Three.js定制几何raycaster捕获错误的对象

[英]Three.js custom geometry raycaster catches wrong object

I need to use my own gemetry since the default cube does not look like it should in wireframe mode (now it is made of triangles instead of squares). 我需要使用自己的几何图形,因为默认的立方体在线框模式下看起来不像现在(现在它是由三角形而不是正方形组成)。

So I made my own geometry and it looks ok, but the raycaster does not work as good with my own objects as it does with the built-in cubes. 因此,我制作了自己的几何图形,看起来还不错,但光线投射器在处理自己的对象时不如内置多维数据集那样好用。

var cube = new THREE.Line( getCube( 5,5, 5), new THREE.LineDashedMaterial( { color: 0x000000,dashSize: 1, gapSize: 0.1, linewidth: 2 } ),THREE.LinePieces );

where getCube() returns getCube()返回的位置

var geometry = new THREE.Geometry()

See example: http://jsfiddle.net/QHjSM/12/ 参见示例: http : //jsfiddle.net/QHjSM/12/

6 colour filled box on the top are the defalt THREE.CubeGeometry boxes, and selecting them with raycaster works perfect, 6 wireframe are my custom geometry. 顶部的6个彩色填充框是默认的THREE.CubeGeometry框,并使用raycaster进行选择非常完美,6线框是我自定义的几何。

Issues: If you try to click outside the box, but pretty close to it it will catch the box, and if you click inside the box (in the middle of it) it will not catch it neither. 问题:如果您尝试在框的外部单击,但非常接近它,它将抓住该框;如果您在框的内部(在其中的中间)单击,也不会捕捉到该框。

But the most annoying thing is that if you click inside one box, but close to another one sometimes it catches not the wrong one. 但是,最令人讨厌的是,如果您在一个框内单击,但又靠近另一个框,有时会捕获错误的框。

I'm not sure can it be done better, tried all the geometry.compute... methods, but still no effect. 我不确定是否可以做得更好,尝试了所有geometry.compute ...方法,但仍然没有效果。

Good day, your custom cubes are not in fact cubes. 美好的一天,您的自定义多维数据集实际上不是多维数据集。 They are just a stack of lines with no cooresponding faces. 它们只是一排没有对应面孔的线。 Your selection is not returning as expected due to the fact that your "cubes" indeed have gapping holes right threw them. 由于您的“多维数据集”确实确实有空缺,因此您的选择未如预期那样返回。 What you can do is in your getCube function after you've built the vertices you can then build all your faces in a similar way. 可以做的就是在建立顶点之后在getCube函数中,然后可以用类似的方式来构建所有面。

Have a look at this example: Issue with custom geometry and face normal 看一下这个例子: 自定义几何和面法线问题

Generally you'll need to carefully pattern out every 3 set of vertices so that when you build the faces there in a clock-wise direction so that the normals will be computerd correctly. 通常,您需要仔细地对每3组顶点进行构图,以便在按顺时针方向在此处构建面时可以正确地计算法线。 Here's a basic example of adding a face. 这是添加面孔的基本示例。

geometry.faces.push(new THREE.Face3(1,2,3));

BUT! 但! Note that this will result in the same aforementioned diagonal lines through your wireframe. 请注意,这将导致通过线框的上述对角线相同。 So, for your use case why not simply use both the basic cube mesh with picking and remove the wireframe then overlay the line drawn boxes as your custom wireframe. 因此,对于您的用例,为什么不简单地同时使用基本立方体网格和拾取并移除线框,然后将绘制的线框覆盖为您的自定义线框。 Best of both worlds. 两全其美。

FYI, you probably already know but Face4 is gone, so you'll need to use Face3 and some sort of custom wireframe to do this anyway. 仅供参考,您可能已经知道,但是Face4已经消失了,因此无论如何您都需要使用Face3和某种自定义线框。

Edit: I did a little test to your fiddle, noticed something strange. 编辑:我对您的小提琴做了一些测试,发现有些奇怪。 Using the CanvasRender, even with the wireframe off the default cube you still see the diagonal lines! 使用CanvasRender,即使线框没有默认的立方体,您仍然可以看到对角线! I try WebGLRenderer and it's fine. 我尝试使用WebGLRenderer,这很好。 I'll have to look into that one further. 我将不得不进一步研究。

CanvasRenderer http://jsfiddle.net/QHjSM/13/ CanvasRenderer http://jsfiddle.net/QHjSM/13/

WebGLRenderer http://jsfiddle.net/QHjSM/14/ WebGLRenderer http://jsfiddle.net/QHjSM/14/

Me again, hmm it appears those ghosted face lines are visible in all the CanvasRenderer examples that use a MeshBasicMaterial on the Three.js site. 再说一次,在所有在Three.js网站上使用MeshBasicMaterial的CanvasRenderer示例中,那些鬼脸线都是可见的。 The only thing I was able to do was simply reduce the opacity of the cube mesh material to 0.1 to lessen the effect. 我唯一能做的就是简单地将立方体网格材料的不透明度降低到0.1,以减小效果。 I suppose the only other method is to switch to the WebGLRenderer but I look forward to being wrong on this :) Here's the last test 我想唯一的其他方法是切换到WebGLRenderer,但我期待在此方面出错:)这是最后一个测试

http://jsfiddle.net/QHjSM/16/ http://jsfiddle.net/QHjSM/16/

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

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