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

Dev picture Dev · Oct 1, 2012 · Viewed 22.9k times · Source

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!

Answer

Tapio picture Tapio · Oct 1, 2012

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.

  • Draw to a hidden 2D canvas as usual and transfer that to WebGL by using it as a texture to a quad
  • Draw images using texture mapped quads (e.g. frames of your health box)
  • Draw paths (and shapes) by putting their vertices to a VBO and draw that with the appropriate polygon type
  • Draw text by using a bitmap font (basically textured quads) or real geometry (three.js has examples and helpers for this)

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.