"WaveJS is not only a JavaScript library..."

It is the easiest tool to create 3D experiences in HTML5 Canvas
Html5 logo

WAVE

JS


WaveJS is not an only 3D JavaScript library


WaveJS is an open-source library that helps you in the development of multiplatform apps in 3D. WaveJS is developed using JavaScript and its source code is available in GitHub.


Using WaveJS will allow you to forget about the complexity of working with 3D objects projected on a HTML5 2D canvas. WaveJS was created to develop applications in the main browsers (Internet Explorer, Google Chrome, Firefox and Safari) in different operative systems, and in mobile devices or tablets (iPad, Windows 8, iPhone, Android, …). Some of the characteristics that make WaveJS a valuable tool are the possibilities to:


- Create simple and grouped models
- Handle basic and spherical camera systems
- Calculate shadows and lights
- Apply ray casting
- Perform 3D transformations
- Handle models (and their groups)
- Handle vertices


Explore the features and learn more about what you can do and how you can use WaveJS.

Html5 logo

Get started with WaveJS


WaveJS is fully available on GitHub under the MIT license. To start, you just have to download and include the js file, "wave.js", in your solution.

Then, you will just have to create your scene in a canvas
var canvas = document.getElementById("canvas");
var scene = new WaveEngineJS.Scene(canvas);

At the end of this page, you will find several examples and tutorials explaining how to use WaveJS.


Case Study: Project Prometheus


Project Prometheus is the site of the new prequel to the famous Ridley Scott's Alien saga. This website contains HTML5-based games which present the challenge of handling 3D objects, obstacles, holographic cubes that you can move and camera rotations. The main difficulty is that all games should be able to be displayed and to work in the main browsers and devices without using plugins.

To achieve this goal, we developed a reduced version of Weekend Game Studio multi-platform game engine Wave. This library has proven its excellent performance in the various games developed in only a couple of weeks. Look at the final result in www.projectprometheus.com.

Html5 logo
DOCUMENTATION

Examples

Check the following 3D scene examples to see how well WaveJS performs in your browser:


Add a model to the scene

The first thing we need to start using WaveJS is to create a canvas element in our HTML. From this canvas, we will create our scene with WaveEngineJS.Scene on which we can add models simply using the method addModel. These models are js objects created using the constructor WaveJS.Model();.

For the 3D models design, we created a command line tool that allows reading models in the obj format (a common format used in 3D design tools) and writing a js adapted for WaveJS.

var canvas = document.getElementById("canvas");
var scene = new WaveEngineJS.Scene(canvas);
scene.addModel(mymodel);

Now we can apply a transformation (rotation, displacement or scaling) on the model o simply change the color of its edges.

mymodel.changeRotation(0, rotY, 0);
mymodel.changePosition(0, 0, 0); mymodel.changeScale(7, 7, 7);
mymodel.changeColor([255, 0, 0, 1], [255, 0, 0, 1]);



Camera movement

To add a camera WaveEngineJS.Camera to scene, we simply use the method addCamera of Scene. Once added, we can modify its position using the camera direction vector as a parameter.

var camera = new WaveEngineJS.Camera(canvas.width, canvas.height);
scene.addCamera(camera);
camera.changeCameraPosition([0,150, 150]);



Apply shadows and lights

We can add shadows and illumination to our model. To do so, we need to specify that the model supports shadows (hasShadow) and illumination (hasIllumination).

mymodel.hasShadow = true;
mymodel.hasIllumination = true;



Animation

To animate the model, we will use the following function: requestAnimationFrame, in each execution of this function, we will change the rotation of our model. For screen display, we will use the Draw function that will render the scene in 3 different ways (Wireframe, Solid and Points).

window.requestAnimationFrame(animate);
var rotY = 0;
function animate() {
    scene.models[0].changeRotation(0, rotY, 0);
    Draw();
    rotY -= 0.1;
    window.requestAnimationFrame(animate);
}
var drawType = scene.SOLID;

function Draw() {
   switch (drawType) {
       case scene.WIRE:
       scene.render(scene.WIRE);
       break;
    case scene.SOLID:
       scene.render(scene.SOLID);
       break;
    case scene.POINTS:
       scene.render(scene.POINTS);
       break;
    }
}



Ray casting

To determine which of the scene models we clicked on, we will use the scene function getGroupModelInMap to which we can enter the values x and y obtained in clicking the screen. This function uses ray casting, which extends a ray from the camera to check for collisions, and in case of collision it will return the closest model to the camera.

groupModel = scene.getGroupModelInMap([x, y]);

For more details about how to use ray casting with WaveJS, you can check out the following example: Example 1: Sphere with shadows.



Culling. Visibility algorithms

For those who do not know, there are several culling techniques (elimination of non-visible polygons). BackfaceCulling hides polygons that don´t face the camera, for example the rear faces of cubes you are drawing. Another process called FrustumCulling (no implemented in WaveJS) consists in not rendering non-visible polygons that is to say polygons that are outside the Frusum (camera field of view).

In WaveJS, we can activate the backfaceCulling of the models to only render elements that are visible from the camera:

cubeW1.backfaceCulling = true;