HTML Canvas Element real 3D Demo
This is an extremely old version, for a newer build with better performance and new features check here.
OK, first for all of you impatient guys, the controls:
- Arrow Keys: Turn/Walk
- PageUp/PageDown: Fly
Now a bit of background info:
This is a demo program for the Canvas element, which is right now being formulated as a standard over at WHATWG.
For that purpose, the Canvas element provides an API that allows you to draw vector shapes and bitmaps.
The target for Canvas is to not only provide a 2D API, but also one for 3D based on OpenGLes. Sadly, this API hasn't yet arrived, and it looks like it will take some time until browsers support it, but the 2D API is here and it's working just fine in anything but InternetExplorer, and even for that Google provides wrapper code free of charge (note however that this wrapper code isn't being used here, so you'll need either Firefox 2+ or Opera 9+, Safari hasn't been tested). So I decided to write a little 3D Renderer myself. It's not complex, it's not finished, but what the hell: it works. While this isn't the first attempt to do this (Canvascape comes to mind), it is as far as I know the first one to try real 3D instead of Doom-style pseudo 3D. In fact, this demo loads a standard Alias Wavefront OBJ file, and builds the model from the triangles it finds in there.
So what does this demo do? Well, basically this:
- Load a model
- Set up controls to modify a view object
- Find the nearest triangle below the player
- Move the player down to that triangle
- Rotate the model
- Clip it against Z=0 so you only render what's in front of you
- Split partially visible triangles against Z=0
- Project all vertices, so that they get smaller the farther away they are
- Draw them, with the distance dictating the color
So what do I want to do with this? Well, eventually it should becomes a little racing game. Remember ReVolt?
Something like that and from the performance I get so far, it seems possible.
Which brings me to the needed optimizations:
I've got absolutely no idea why, but it seems like all Canvas implementations do pixel based clipping against the drawing window, instead of shape-based clipping... essentially that means that drawing a 1000x1000 pixel shape, it takes 100 times as long as drawing a 100x100 pixel shape, even when the Canvas is only 20x20 pixels in size.
Then we need a real colission detection for a meaningfull game and support for sprites. There will probably be a 1000 other things to do, but that's what I have in mind now.
Oh, and forget about textures... it's just not possible at descent speed.
Notes. The code is copyrighted Hans Schmucker and licensed und the GPL v2.5