Three.js - Procedurally Generated Infinite Terrain

Project_forgestruct_title
There's nothing like a good old fashioned minecraft-esque infinite procedurally generated terrain to really test what's possible in a browser. So, I decided to give it a go. Initially the basic idea was to generate map chunks on the server using Node.js. Chunks would be a group of 32x32x32 blocks. I'd send chunks to the client window using web sockets. Process the data to output geometries. Then I'd render the geometries. Easy enough. Sort of... with a little re-working.

Performance

The saying goes that "premature optimization is the root of all evil".

Read the rest of this post »

Filed under    Javascript   Terrain Generation   Three.js   WebGL  
Posted

Setup Vim for Productivity

Vim_title
Recently, I had been struggling to find a good text editor for all of my development needs. It needed to have a pleasing color scheme and font. You'd be surprised how a little eye candy can extend your productivity in front of a monitor. It also needed to be functional and productive. This includes being extremely responsive, speedy, and reliable. There was actually one hiding in front of me the entire time. I did not realize that it could be transformed into the ultimate text editor. It's Vim. Many others have discovered this before me. If only I knew sooner.

Read the rest of this post »

Filed under    productivity  
Posted

Window, Self, and Web Workers

Title
HTML5 Web workers push the ante up for what is possible in a browser. You can perform computationally heavy tasks without freezing the browser. This is done by giving the worker a script that is run in a "parallel execution environment". The script should be run asynchronously in a separate thread or process. For example, in a game you could be generating world terrain chunks in a separate thread with limited affect on the fps. Without workers you would need to do something like use setTimeout to process the chunks asynchronously. The function executed by setTimeout would need to finish in less time than the frame interval to avoid a decrease in fps. This is possible, but it increases the complexity of your code and it doesn't take advantage of multiple cpu cores. All this is great, but now that we have this new technology it may be good to be aware of how we develop javascript libraries to ensure that they can be used inside a worker.

Read the rest of this post »

Filed under    HTML5   Javascript   Web Workers  
Posted

Three.js - Animating Blender Models

Title

A few months ago I experimented with using Blender to create 3D models. First, I created a tree that you can see in previous posts. Then I had the idea to create a "troll", that could be used in a game. I followed Blender tutorials and created a humanoid model. Then I rigged the model to move naturally. Finally, I created two animations with the model. One while standing still, and one while walking. I'm proficient with web graphics and photoshop, however after finishing the model I gained a greater respect and understanding for the amount of work that must go into producing assets for a large scale game. This is partially how I came to start experimenting with procedural generation techniques. Before doing that though, I wanted to animate my model. Hopefully this post will help some people trying to animate models from Blender.

Read the rest of this post »

Filed under    Javascript   Three.js   WebGL  
Posted

Terrain Generation using Perlin Noise with Negative Coordinates

Title

Lately I have been working on another Three.js + Node.js project that involves terrain generation similar to Minecraft. In order to create interesting terrain I am using 3D Perlin noise. It's basically an algorithm that takes some coordinates, and generates natural looking noise in 3-dimensional space. It's supposed to return values in the range [-1, 1]. One way to turn this into terrain is to add the value to the height of the land, and turn everything that is less than 0 into land. However, early in the project I ran into an interesting problem that doesn't seem to be documented anywhere else on the web.

Read the rest of this post »

Filed under    Javascript   Perlin Noise   Terrain Generation   WebGL  
Posted

Node.js server and Web Sockets on Amazon EC2 with Express.js and Socket.IO

Ec2_nodeserver_title
For testing multiplayer connectivity I set up an Amazon Web Services EC2 micro instance to host a node.js app. This post will cover setting up and installing node.js (working with sudo), npm (working with sudo), a git remote repository with a hook to push changes, HAProxy as a reverse proxy, MongoDB for our database, upstart, and monit for keeping our node.js app alive. Long list, but hopefully it will be a breeze. There were a few quirks that this post will help with if you're using Socket.IO and Express.js but it will work for mostly any node.js deployment. I'm going to assume that you've already set up an AWS account, created an instance, have SSH into your instance, and that you have an existing git repository on your local machine.

Read the rest of this post »

Posted

Three.js dive - code debug

Threejs_debug_dive_title
In the first version of my previous post Three.js Game Part 3 - Animated Sprites + Backbone.js there was a slight bug. In orthographic projection objects are supposed to stay the same size no matter their distance. This was always the case in the previous post since there was only the landscape and a single sprite. However, if I added more sprites they would resize as I got further away or closer to them!

This was a bit of an annoying problem and something that I needed to solve. Playing with the parameters didn't seem to yield any results that I could tell... and { screenCoordinates: false, affectedByDistance:false } was the only thing that made the sprites look remotely normal. So I needed to take a look at the inner workings of sprites in Three.js. This was a bit difficult since I hadn't done any openGL or low-level WebGL yet, but now you get to follow along.

You can look at the full Three.js source code on github here. I'll be posting snippets as we go along.

Read the rest of this post »

Filed under    Javascript   Three.js   WebGL  
Posted

Three.js Game Part 3 - Animated Sprites + Backbone.js

Game screenshot
So far we have a very basic landscape and the ability to capture screen coordinates in an 3D world displayed isometrically. Now it'd be nice to have some interaction available. I started by just making a ball roll to whatever point I clicked. Three.js makes this easy.

However, my code had begun to get pretty messy. I decided that Backbone.js could help with my problem. Backbone.js is a lightweight javascript MVC framework that I've been using in other projects. I would highly recommend taking a look at it. If you're used to something like Rails the views in backbone.js are more like controllers. The models are models, and the views are javascript templates. Here we won't take full advantage of the power of Backbone.js since we won't be using templating or models, but it will sure help organize our code.

First thing's first

Let's create a global object for our app. We'll organize this object into Models, Views, and Controllers.

Read the rest of this post »

Filed under    Backbone.js   Javascript   Three.js   WebGL   tjsg  
Posted

Three.js Game Part 2 - Transforming Isometric Screen Coordinates to 3D Coordinates

In my previous post we started off pretty simple, just to display a landscape with a tiled texture. The next step is to create some movement. At first, I decided that my character would move by a mouse click at the desired destination. Nothing too complex, the character will move from their current position to the destination position every update. But first, we need to translate screen coordinates which are 2D to 3D world coordinates. If you don't understand orthographic projection too well this can be more difficult than it sounds.

Perspective projection

Most 3D games use perspective projection, which means that objects will become smaller as they become farther away. This is how we see things in the real world. In this case, we can imagine all the visible points in 3D space being mapped to a shape called a frustum ( http://en.wikipedia.org/wiki/Viewing_frustum ). In this case, when we transform a 3D point to the screen we multiply the point by our projection matrix and the inverse of our world matrix.

Then, we can just multiply the point by our world matrix and the inverse of our projection matrix and we get the same point back. When using screen coordinates things become a bit more complex because we throw away the z value. Screen coordinates don't have one since they are in 2D. However, we can just fill in a z value such as 0.5. Then we find the vector from the camera to our unprojected point. We look for intersecting objects to tell what the user has clicked on.

Isometric projection

The above technique is commonly used, however it won't work for an isometric projection.

Read the rest of this post »

Filed under    Javascript   Three.js   WebGL   tjsg  
Posted

jquery-tmpl.js vs Handlebars.js

Previously, I've worked on large javascript applications that make tons of manipulations to the DOM manually or store lots of HTML as a string. When these pieces of code pieces grow they become a huge pain to change and maintain. Javascript templating to the rescue! I've been working with both jquery-tmpl.js and handlebars.js and want to offer a simple comparison.

First, a little background. Jquery-tmpl is the official jQuery templating plugin as of October, 2010 and is hosted in jQuery's github. Handlebars is an extension to Mustache.js with more features. A mustache.js template is a Handlebars template. Mustache.js is a templating system that has been implemented across many languages. You can find more about these plugins at the following links.

jquery-tmpl.js - http://www.borismoore.com/2010/10/jquery-templates-is-now-official-jquery.html (and you can take a look at jQuery docs).

handlebars.js - http://handlebars.strobeapp.com/ (To download handlebars.js go to this here https://github.com/wycats/handlebars.js/downloads)

mustache.js - http://mustache.github.com/

In the examples I'll be writing my templates in a script tag with the type set to something that the browser cannot recognize (x-jquery-tmpl or x-handlebars-template). You can still write the templates as html inline to javascript, but I don't prefer this method for anything but the simplest templates for the reasons stated above. Also, Jquery-tmpl will be the first block of code, and Handlebars will be the second block of code.

Read the rest of this post »

Filed under    Handlebars.js   Javascript   Javascript templates   jquery-tmpl.js  
Posted