On my last post I added a little animation to mark the occasion. I though I do something different so I decided to try WebGL and after meticulous googling1 I decided to try out Three.js, which turned out quite alright as getting it to work was a breeze and the only issues I had was with the animation itself and never with any of the technical bits.
WebGL itself seems pretty well supported, at least in all modern browsers -
including mobile - and because things get drawn on a
embedding stuff with the rest of your web app / site is a mater of CSS’ing your
way around it.
Before going into a short how-to, you can get the whole source code for on the Github Rosetta P67 Webgl page, if you do fix my math - I’m sure it’s easy :) - open a pull-request and happy days. The code samples are all CoffeeScript and run in the context of a bigger class, but hopefully they are informative enough on their own or as guidelines when reviewing the actual code.
For reference, all the examples will be based on the this code
The Basics of Three.js
So, in order for us to have something to look at we need to set up a few things, namely:
- A Scene - this will hold our objects, our lights and our camera.
- A Camera - this is our point of view, we’ll point it to the objects we want to see and control field of view, aspect ratio, etc.
- The objects and materials themselves - in our case, one comet with texture and a Rosetta spacecraft with a simple color.
- A renderer - this is where Three.js does the magic of rendering something we can look at based on all the geometry and lights and such
You can see where all these elements are initialized here, which generally boils down to:
Setting up the basics
This sets up the basics for our project, we have our Scene, Camera, Light and Renderer . We pulled the camera a bit backwards on out x-axis and lifted it up just a tad to get some perspective and we setup the scene with an ambient white light so all objects are equally lit. At this point we don’t see anything given that we haven’t created any objects yet.
Setting up objects
The comet is loaded with a JPG texture and Rosetta has a single color, in this example it’s merely black.
Loading the objects and applying the textures is pretty much boilerplate code, the only adjustments we do to each object are related to size and position. Scale wise I absolutely eyeballed it - with impressive scientific disregard - while in terms of position both objects are positioned at the origin by default, I merely more Rosetta to its relative position in relation to the comet to help with the animation code.
This is where we take care of positioning the objects in the scene and rendering each frame. We use a
Three.Clock object to keep track of elapsed time and use that as a variable to calculate the position of the objects.
The loop itself runs using the
requestAnimationFrame browser method, which gives it some control, namely it ensures that animations only run if you actually have the tab visible, so not to waste CPU cycles.
Each frame then simply calculates the position of each object and in the case of Rosetta, adds a very non-scientificly-accurate rotation for effect.
At the end of it we ask Three to render the scene and wait for the browser to call the
animationLoop method again.
Calculating the positions
The comet position is pretty straightforward, as it merely rotates around the y axis, so with time as the variable we do
The Rosetta spacecraft does a more complex movement, as it rotates around the comet as this moves through space. At this stage I’m happy to report I gained much more insight over trigonometry and vector operations than I ever had with only theoretical knowledge2, but I’m sure I’m still overcomplicating and possibly fumbling the whole of it, except that it generally looks good enough, inaccurate as I think it is. Anyway, this is what we do
I calculate the circular “orbit” movement of Rosetta, then rotate the point to match the angle of the comet and then simply add the comet’s current position to calculate the final point in space.
If you look at the console, there are a few settings that you can toggle, namely showing the world axis, a grid and drawing the movement of both objects over time. Nothing too mind-blowing there, you should pick it up super easily when checking the code itself, but just wanted to mentioning it here, so you know what to look - or how to interpret what’s going on in the actual code :)
This is a very simple yet specific intro based on a project I did. I hope it’s clear enough, if not hit me on twitter or Github itself. Three.js made things super easy, as you can see by the size of the boilerplate code - close to none - which is great as it means you can focus on the important things of your project.
I also like the way I can easily mingle this with the rest of the page however I want, which meant that I could use this animation as the header for the blog post, but it was also super easy to have it play on the listing page with a different size.