// Start the framework
W.reset(canvasElement);
// Set clear color ("#fff" by default)
W.clearColor("#rgb");
// Set camera
// Settings: position (x, y, z), angle (rx, ry, rz), fov
W.camera({x, y, z, rx, ry, rz, fov});
// Set light direction
W.light({x, y, z});
// Set ambient light's force (between 0 and 1)
W.ambient(f);
// Create a group of objects: name, position, rotation
W.group({n, x, y, z, rx, ry, rz});
// Draw a built-in 3D object
// Settings: group, name, size, position, rotation, background, texture, smooth...
let settings = {g, n, size, x, y, z, rx, ry, rz, b, t, mix, s, ns, mode};
W.plane(settings);
W.billboard(settings);
W.cube(settings);
W.pyramid(settings);
W.sphere(settings);
// Add and draw a custom 3D model (see the OBJ file loader below)
W.add("custom_model", { vertices, uv, indices });
W.custom_model(settings);
// Move/resize a group or object: name, position, rotation, animation, delay (in ms)
W.move({n, size, x, y, z, rx, ry, rz, a}, delay);
// You can also use M to set a custom transformation matrix
W.move({n, M}, delay);
// Move camera / light: settings, animation, delay
W.camera({x, y, z, rx, ry, rz, a}, delay);
W.light({x, y, z, a}, delay);
// Delete an element: name, delay
W.delete(n, delay);
Examples / Tutorial
1) Setup First, create a HTML5 canvas
<canvas id=canvasElement>
and call
W.reset(canvasElement)
.
By default, the camera is placed at [0,0,0], looking at the -Z axis, with a 30 degrees FoV.
The scene contains an ambient light and a directional light pointing to the bottom: [0, -1, 0].
You can set the camera's position, rotation and FoV using
W.camera({x, y, z, rx, ry, rz, fov})
.
You can set the directional light using
W.light({x, y, z})
and the ambient light leved using
W.ambient(n)
.
You can change the background color using
W.clearColor(c)
(where c is a hex RGB/RGBA string).
No need to start a render loop, the canvas is redrawn after each screen refresh.
2) Draw built-in 3D models The full version of the framework includes 5 built-in shapes:
W.plane
,
W.billboard
,
W.cube
,
W.pyramid
and
W.sphere
.
(Billboards are planes that always face the camera, and can be used to show sprites or particles for example.)
The shapes settings can contain the following values:
{g, n, size, w, h, d, x, y, z, rx, ry, rz, b, t, mix, s, mode}
n: name (required if you want to transform the object later)
g: parent group (if a group is set, the model will be transformed relatively to this group)
w, h, d or size (size sets w, h and d to the same value. Otherwise, w, h and d can be set individually. Default: 1)
x, y, z: position (default: 0, 0, 0)
rx, ry, rz: rotation (default: 0, 0, 0)
b: background color (a "#rgb" or "#rgba" hex value. The "#" is optional. Default: "888")
t: background texture (an IMG, CANVAS or VIDEO element present in the DOM)
mix: if both b and t are set, a mix value between 0 and 1 can be used to mix the color and texture (0: fully textured, 1: fully colored)
s: enable smooth shading (requires the full version of the framework. Default: false)
mode: drawing mode ("LINE_LOOP", "TRIANGLES", etc. Default: "TRIANGLES")
3) Draw custom 3D models Besides the built-in shapes, W can also import and display custom 3D models.
You'll need:
An array of vertices coordinates: [x, y, z, x, y, z, ...],
Optionally: an array of uv texture coordinates: [u, v, u, v, ...] (if the model is textured),
Optionally: an array of indices (if the model uses indexed buffers). W.js supports both indexed and unindexed models!
Optionally: an array of normals (if you don't want to use the built-in flat/smooth normals)
Add the new model using
W.add("custom_shape", {vertices, uv, indices})
and draw it using
W.custom_shape(settings)
.
Custom models can be drawn with the same settings as the built-in ones (group, size, position, rotation, background, shading...).
Flat shading is applied by default, smooth shading is only available in the full version of the framework.
OBJ files can be converted into W custom models using the following online tool: OBJ2JS!
All the scene's objects can be used as a group and have other objects attached to it. The camera can also be put in a group.
The camera itself can be used as a group (to create a HUD or a skybox), or put inside a group (to make a 1st or 3rd person view).
The example below shows how to attach the camera to a moving character and attach a plane (the ground) to the camera.