NeHe Lesson 13 – Using Sprites for Text Labels in three.js

Test Sprites in three.js

Text Sprites Demo in three.js

 

Introduction

This post is a bit of a hybrid as it makes use of Canvas2D to render the text and labels, then composites them into the scene as three.js sprites.  The original NeHe lesson 13 was about bitmapped fonts and how to use them, but that’s so last century.  Canvas2D provides support for true vector fonts so why would one use bitmap fonts?

On the other hand, being able to place labels where you want in a 3D scene is a handy feature.  Moreover, the use of three.js sprites has another useful aspect as three.js sprites are implemented such that they are effectively in 2D space in the plane of the screen, so they are always facing the user, no matter how the scene is oriented.

There are four main parts to the demo:

  • Setting up the Canvas2D that is the basis for the sprite
  • Rendering the text and label on the canvas
  • Loading the contents of the canvas as a texture
  • Positioning the resulting sprite in the scene

Setting up the Canvas

This is standard HTLM5 – nothing tricky here.  Two tips:

  • Make the canvas big since only the label and the text will actually be rendered as the texture since the rest of the canvas is transparent as far as three.js is concerned
  • Ensure that the canvas size is a power of two, e.g. 2048×2048.  If you don’t three.js will change it for you and send a warning to the console

Rendering the Text Sprites

I won’t go into the details how HOW this is done.  Take a look at the lesson on the Geo-F/X website here for all the details (and of course the sources are on github).  The key is the call to makeTextSprite:

var txSprite1 = makeTextSprite( "Bottom-Right", -1, 1, 0.25, { fontsize: 72, fontface: 
      "Georgia", borderColor: {r:0, g:0, b:255, a:1.0}, borderThickness:4, 
      fillColor: {r:255, g:255, b:255, a:1.0}, radius:0, vAlign:"bottom", hAlign:"right" } );

Yeah, the function’s signature is unwieldy.  I have a to-do item to convert it to use some form of class/object but it works as-is.  Notice that you can set the vertical and horizontal alignment of the text with respect to the 3D point where the sprite will be rendered.  You can set the color of both the text and the label, round-corners or not, opacity, etc. etc.

And that’s pretty much it!  Fairly simple but rather handy.  At some point I will do the refactoring mentioned above and add it to the GFXScene class.

You can find the lesson at Geo-F/X here. As always, the sources are on github here. Feel free to contact me at rkwright@geofx.com or comment on this article directly.

Graphics explorations: three.js, WebGL and Digital Publishing

Bear on a bike

The author is in his natural habitat

 

Intro

This is the first blog from my new site (host). The intent is to provide some outlines of the wacky graphics explorations (WebGL, three.js) I have been trying out. During the day I manage a large open-source project (readium.org) and do some consulting in digital publishing. Not much scope for playing with graphics. At the same time, in the distant past I was a university professor, specializing in hydrology, permafrost and GIS. Don’t do much of that anymore either… 🙂 So I spend my spare time playing with scientific visualization of some of my experiments (when I am not in my woodshop). My website is here, my github account here, and my LinkedIn profile here.

Intent

The intent of the blog is to provide some color and explanations of the details, motivations and experience in some of the graphics and digital publishing explorations I have been doing. On my website Geo-F/X I’ll post the result of my explorations on my website. Here in the blog I’ll look at and discuss how and why they came to be.

Three.js and NeHe

I’ll start out the blogs with a recent project of mine, implementing the legacy NeHe demos with three.js and WebGL. I did a lot of work in OpenGL at one point, back in the early days of OpenGL (when little worked correctly) and the NeHe demos were nice cool intros. I was busy writing PostScript interpreters (for Eicon and QMS) so OpenGL was just a hobby. More recently, after a number of excursions (GIS, SVG, working at Adobe then eBooks and digital publishing, I decided to look into WebGL as the browser support was getting pretty good.

I came across three.js and it looked cool, so I decided to amuse myself by implementing the NeHe demos in three.js. Turned out to be more work than I expected, but the code is all done and most of the “tutorials” are as well. Hope to complete them very soon. In the meantime, I am going to start blogging about the first couple of dozen which are all done. You can see the results here and the sources are on Github here.

Feel free to contact me at rkwright@geofx.com or comment on this article directly.