I began building upon Martin Schuhfuß's response to create this answer:
Creating with DOM:
If you're interested in "raycasting," you can find additional information in the documentation and also check out the examples.
Raycasting allows you to determine which object is currently under the mouse cursor. With this knowledge, you can populate data into a tooltip and position it based on the mouse's location.
Another approach could be to use something similar to this method to calculate the screen position of the 3D object and place the DOM tooltip there using screen space coordinates.
Although creating tooltips with DOM is straightforward, the drawback is that you won't benefit from WebGL rendering features like shine, shadow, bump maps, etc., for your tooltips.
Creating with WebGL is more complex:
You can utilize a technique like this one to determine the screen position of the 3D object and position the tooltip accordingly in screen space coordinates.
Next, you'll need to figure out how to align content relative to your Three.js camera so that item size and position are in screen space. For assistance with this, refer to these discussions on the Three.js forum:
Once you've mastered drawing in screen space within Three.js, you can apply the methods outlined in the earlier Making it with DOM
section to determine where to draw the WebGL tooltips in screen space.
There are multiple ways to draw WebGL tooltips:
- Include elements in the same scene, adjusting the
renderOrder
of objects to ensure UI components always appear on top. Refer to three.js: how to control rendering order for guidance.
- Render elements onto a quad (PlaneGeometry) as a texture (requires understanding of render targets), then employ techniques like
renderOrder
to guarantee the quad is rendered above everything else.
- An alternative is to render the quad atop the scene through a post-processing effect, ensuring it appears on top without affecting the original scene's render orders. This necessitates learning about post processing, which you can find here.
- Render screen-space WebGL content to a second Three.js Scene on a secondary canvas, then position the second canvas over the first using DOM techniques. This method prevents UI elements from disrupting the rendering order of the initial scene but may be resource-intensive.
The fastest approach might be the first option, although it is the most challenging. Conversely, the last option is likely the easiest but slowest; however, speed might not be critical depending on your specific requirements.
This list doesn't cover all possibilities. There are other techniques available too! Please feel free to enhance this explanation with additional methods or details.
If you have more questions, don't hesitate to ask. I can further expand on the answer.<