Raycaster only works on the first triangle of the BufferGeometry
Description of the problem
I have textured quads built using BufferGeometry with 4 vertices. It is in fact rendered using rasterMesh.drawMode = THREE.TriangleFanDrawMode.
Three.js version
- [x] r79
- [ ] r81
- [ ] Dev
Browser
- [x] All of them
- [ ] Chrome
- [ ] Firefox
- [ ] Internet Explorer
OS
- [x] All of them
- [ ] Windows
- [ ] Linux
- [ ] Android
- [ ] IOS
Hardware Requirements (graphics card, VR Device, ...)
Confirmed. When switching the quad geometry to 6 verts the raycast works properly.
Hmm... It appears Mesh.raycast() implicitly assumes drawMode is TrianglesDrawMode.
Indeed, for indexed and non-indexed geometry https://github.com/mrdoob/three.js/blob/dcfc3a3846cad01157e5de05adf9e4525270f439/src/objects/Mesh.js#L275-L281
Is there a fix coming for this or a work around? I am using CylinderBufferGeometry and BoxBufferGeometry and can only get an intersection on one triangle of the shapes.
@glockjt This thread is about supporting alternate draw modes, and is unrelated to your problem if you are using the geometries you say you are.
The book WebGL Insights does not recommend to use TRIANGLE_FAN:
Avoid use of gl.TRIANGLE_FAN, as it may be emulated in software.
That makes we wonder if we should not just removed Mesh.drawMode and always work with TRIANGLES. I've seen very few use cases so far where Mesh.drawMode is actually changed by users.
That makes we wonder if we should not just removed Mesh.drawMode and always work with TRIANGLES.
The glTF spec requires support for all draw modes.
If the renderer is not going to support all draw modes directly, I assume it will need to convert the geometry to triangles draw mode automatically...
@donmccurdy How common is the usage of the primitive modes TRIANGLE_STRIP and TRIANGLE_FAN in glTF? What do you think about converting them to TRIANGLES with helper methods?
The glTF spec requires support for all draw modes.
I think we should aim to support models that were authored with other draw modes, but that's not (necessarily) to say we couldn't convert them and render with TRIANGLES, if the visual result is the same.
How common is the usage of the primitive modes TRIANGLE_STRIP and TRIANGLE_FAN in glTF?
I don't know, unfortunately. No idea how you would create one using a DCC tool, anyway. Threads like this make me think it's not a particularly common optimization ([Unity's] MeshTopology.TriangleStrip was removed for reasons unknown ), but I don't have numbers here.
What do you think about converting them to TRIANGLES with helper methods?
Did you have in mind that the conversion would be done by GLTFLoader, or by the renderer? I wouldn't want it to be in the renderer, I don't think – users should be able to assume that if they've made a valid BufferGeometry, it is GPU-ready.
Another option would be to begin warning in the raycaster, provide users methods to convert, and see if there are users for whom that doesn't work well enough.
Another option would be to begin warning in the raycaster, provide users methods to convert, and see if there are users for whom that doesn't work well enough.
That's sounds like a good idea to me!
Perhaps relevant, from https://github.com/zeux/meshoptimizer#triangle-strip-conversion —
On most hardware, indexed triangle lists are the most efficient way to drive the GPU. However, in some cases triangle strips might prove beneficial:
- On some older GPUs, triangle strips may be a bit more efficient to render
- On extremely memory constrained systems, index buffers for triangle strips could save a bit of memory
I don't find that entirely compelling, but definitely helpful as the only resource I've found on it.
Okay, we have now a helper function which can convert the draw modes of geometries to gl.TRIANGLES. Hence, I suggest to close the issue and refer to BufferGeometryUtils.toTrianglesDrawMode() from now on.
We should consider in a new issue to completely remove drawMode from Mesh and always use gl.TRIANGLES. Loaders like GLTFLoader would have to perform a draw mode conversion via BufferGeometryUtils.toTrianglesDrawMode() if necessary. AFAIK, it's the only loader that actually sets Mesh.drawMode.
We should consider in a new issue to completely remove drawMode from Mesh and always use gl.TRIANGLES.
I think that is reasonable.
I think that sounds good to me too.
As far as I can remember, it was @fernandojsg that added that functionality for A-Painter but we were not aware of all the ramifications.
Some more thoughts about this topic:
In context of lines, we have Line, LineLoop and LineSegments which implicitly encode the draw mode and other primitive specific information (e.g line distances).
In Mesh, we just have a drawMode property instead. So the current design was never consistent. It would be if there were something like TriangleMesh, TriangleFanMesh and TriangleStripMesh. But I don't think it's beneficial to have such classes.
Mesh.drawMode has been removed. Please check the migration guide for R112 to see the recommended migration task.
https://github.com/mrdoob/three.js/wiki/Migration-Guide#r111--r112
@Mugen87 I have a situation where I have to generate heightmapped tiles pretty efficiently, and save memory with indices and triangleStrips ( https://www.learnopengles.com/tag/index-buffer-object/ ). I do my raycasting somewhere else and would kinda like to see this feature come back.
What feature?
Triangle Strip draw mode
*maybe im in the wrong place, but i upgraded from 110 and now i get an error which points to the migration guide ( super helpful btw ), which said that from now on you have to use triangles because triangle strips arent supported
The support for triangle strip and fan was very limited in the past and improving this situation would have been caused many enhancements in the core (quickly leading to over-engineering). Read #18041 for more information about this topic. I think it's unlikely to bring triangle strip and fan back to the core.