user-interfacewebglthree.jshud

Drawing UI elements directly to the WebGL area with Three.js


In Three.js, is it possible to draw directly to the WebGL area (for a heads-up display or UI elements, for example) the way you could with a regular HTML5 canvas element?

If so, how can you get the context and what drawing commands are available?

If not, is there another way to accomplish this, through other Three.js or WebGL-specific drawing commands that would cooperate with Three.js?

My backup plan is to use HTML divs as overlays, but I think there should be a better solution.

Thanks!


Solution

  • You can't draw directly to the WebGL canvas in the same way you do with with regular canvas. However, there are other methods, e.g.

    Using these usually means setting up a an orthographic camera.

    However, all this is quite a bit of work and e.g. drawing text with real geometry can be expensive. If you can make do with HTML divs with CSS styling, you should use them as it's very quick to set up. Also, drawing over the WebGL canvas, perhaps using transparency, should be a strong hint to the browser to GPU accelerate its div drawing if it doesn't already accelerate everything.

    Also remember that you can achieve quite much with CSS3, e.g. rounded corners, alpha transparency, even 3d perspective transformations as demonstrated by Anton's link in the question's comment.