three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Raycaster only works on the first triangle of the BufferGeometry

Open unphased opened this issue 10 years ago • 40 comments

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, ...)

unphased avatar Oct 16 '16 20:10 unphased

Confirmed. When switching the quad geometry to 6 verts the raycast works properly.

unphased avatar Oct 16 '16 20:10 unphased

Hmm... It appears Mesh.raycast() implicitly assumes drawMode is TrianglesDrawMode.

WestLangley avatar Oct 17 '16 03:10 WestLangley

Indeed, for indexed and non-indexed geometry https://github.com/mrdoob/three.js/blob/dcfc3a3846cad01157e5de05adf9e4525270f439/src/objects/Mesh.js#L275-L281

tsbehlman avatar Aug 04 '17 15:08 tsbehlman

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 avatar Sep 20 '17 00:09 glockjt

@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.

WestLangley avatar Sep 20 '17 16:09 WestLangley

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.

Mugen87 avatar Oct 12 '19 09:10 Mugen87

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...

WestLangley avatar Oct 13 '19 00:10 WestLangley

@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?

Mugen87 avatar Oct 13 '19 10:10 Mugen87

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.

donmccurdy avatar Oct 13 '19 17:10 donmccurdy

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!

Mugen87 avatar Oct 23 '19 09:10 Mugen87

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.

donmccurdy avatar Oct 24 '19 21:10 donmccurdy

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.

Mugen87 avatar Nov 30 '19 10:11 Mugen87

We should consider in a new issue to completely remove drawMode from Mesh and always use gl.TRIANGLES.

I think that is reasonable.

WestLangley avatar Nov 30 '19 16:11 WestLangley

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.

mrdoob avatar Nov 30 '19 19:11 mrdoob

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.

Mugen87 avatar Dec 01 '19 11:12 Mugen87

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 avatar Dec 03 '19 22:12 Mugen87

@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.

Dmarcoux111 avatar Jun 11 '20 02:06 Dmarcoux111

What feature?

mrdoob avatar Jun 11 '20 04:06 mrdoob

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

Dmarcoux111 avatar Jun 11 '20 04:06 Dmarcoux111

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.

Mugen87 avatar Jun 11 '20 09:06 Mugen87