Problem Description
Hi! In our WebGL application, we are drawing many (even hundreds of thousands) shapes and we want to discover which shape is currently under the mouse. I'm looking for a way to do it in an efficient manner.
Details
The shapes are defined with Signed Distance Functions. Each shape is drawn by applying a predefined sdf fragment shader to a square polygon (2 triangles). Each shape is assigned with a unique ID (uint) on the Rust side (we're using WASM here). The idea is to render the scene twice (in WebGL 1.0) or once to multiple render targets (in WebGL 2.0), where one of the targets would be the ID encoded as a color. Then we can use readPixels to query the color and get the ID of the shape under the mouse. Unfortunately, every solution that we try has some downsides.
Requirements
- We need to encode 2 ints per shape (one to tell us what shape it was, like if it was a button, or maybe a slider), and second to tell us which instance of object it was (e.g. 5th slider).
 - We will have a lot of shapes (and instances) on the stage, so for each int we would need at least 24-bit, preferably 32-bit precision.
 
What he have tried so far
- Rendering ID information to 
RGBA32UItexture type. In this solution we are using 32 bits per channel, so we can use 2 channels to represent our IDs. Unfortunately, blending applies only in RGBA mode and only if the color buffer has a fixed-point or floating-point format. We need some form of blending because when drawing shapes, like circles, some parts need to be transparent. In the case of ID color output our alpha is always 0 or 1. - Rendering ID information to 
RGBAtexture and convertinguinttofloatin GLSL by using intBitsToFloat and then backfloattouintin Rust. Unfortunately, this is available in GLSL 330 and we are limited to GLSL 300 in WebGL. - Rendering ID information to 
RGB32UItexture and usediscardfor some pixels. This would work but it can cause performance problems and we would rather not like to use it. - Converting ID information on Rust side to 
float, using it instead ofuint, and rendering it toRGBAtexture, and converting it back touinton Rust side. The problem with this solution is that it is pretty complex, we cannot use all of 32-bits (we need to be extra careful about possible NAN-encoding) and we feel there should be a better way to do it.