What's a technique for snapping the mouse pointer to vertices in WebGL or OpenGL? All I need is the vertex position, no other info.

Ideally, I'd like to do this without needing to keep positions and indices arrays in memory outside of the GPU. BTW, I have already a surface picking technique that uses GPU-resident geometry, which works by sampling the depth buffer at the mouse coordinates and combining that with the unprojected mouse coordinates.

One vague idea: use a vertex colors buffer, that fills triangles with fragments that somehow each encode the position of their nearest vertex position. The fragments could have the absolute position of their nearest vertex, or a vector with length, pointing towards their vertex position. Picking a color on the triangles would provide the nearast vertex position. Though I can't imagine a way to set up that interpolation on WebGL or OpenGL ES.

Any tips appreciated!

  • \$\begingroup\$ how many vertices are we talking about here on average? \$\endgroup\$ May 11 '20 at 12:12
  • \$\begingroup\$ Rendering the kind of "nearest vertex" texture you describe, then reading that back to the CPU for manipulating the pointer, is likely to be substantially more expensive than just navigating the vertices CPU-side - unless you have a very small display and very very very many vertices, without any kind of acceleration structure to help you narrow the candidate pool. \$\endgroup\$
    – DMGregory
    May 11 '20 at 14:43
  • \$\begingroup\$ @htmlcoderexe a lot - hundreds of thousands to millions \$\endgroup\$
    – xeolabs
    May 12 '20 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.