gl_PrimitiveID in threejs?

Is it possible to use the built-in parameter “gl_PrimitiveID” in fragment shader?

AI generated answer with shader code that won’t work with Three, complete garbage. (NB: The offending message has been removed now)

1 Like

I don’t think gl_PrimitiveID exists in WebGL. You could instead store an ID in a vertex attribute.

1 Like

Another option if you’re not using an index buffer on the geometry:
(from: Emulating gl_PrimitiveID in OpenGL ES 3.0? - Graphics and GPU Programming - GameDev.net )
“If you don’t use an index buffer, gl_PrimitiveID can be derived in the vertex shader as (gl_VertexID / 3) (for triangles) in the vertex shader and sent to the pixel shader.”

Thank you for your reply. A vertex attribute storing primitive ID does work for non_indexed geometry.