JSModeler Tutorial
1. First steps

To use JSModeler we need to include the following files in our HTML documents.

<script type="text/javascript" src="three.min.js"></script>
<script type="text/javascript" src="jsmodeler.js"></script>

2. Initialize viewer

JSModeler contains a built-in viewer to show models. The viewer needs the THREE.js framework, and we can initialize it for an existing canvas in the onload event of the page. If we have a canvas with an id "example", we can use the following code to initialize the viewer.

var viewerSettings = {
	cameraEyePosition : [-2.0, -1.5, 1.0],
	cameraCenterPosition : [0.0, 0.0, 0.0],
	cameraUpVector : [0.0, 0.0, 1.0]
};

var viewer = new JSM.ThreeViewer ();
viewer.Start ('example', viewerSettings);
viewer.Draw ();

3. Create the first model

The below example shows how to create a Body object and add some vertices and polygons manually.

var body = new JSM.Body ();

body.AddVertex (new JSM.BodyVertex (new JSM.Coord (0.0, 0.0, 0.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (1.0, 0.0, 0.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (1.0, 0.0, 1.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (0.0, 0.0, 1.0)));

body.AddPolygon (new JSM.BodyPolygon ([0, 1, 2, 3]));
The above code adds 4 vertices to a body, and then adds a polygon from these vertices. If we would like to use this body in a THREE.js based viewer, we can convert it with the below function.
var meshes = JSM.ConvertBodyToThreeMeshes (body);
The function returns an array of THREE.Mesh objects, which we can use in our viewer.
Live Demo
To show the body in the built-in viewer, we can use the following code.
for (var i = 0; i < meshes.length; i++) {
	viewer.AddMesh (meshes[i]);
}		

4. Using materials

The above body converted with the default material color. In JSModeler we can define materials, and use them in the Body. For example, create a body with two polygons.

var body = new JSM.Body ();

body.AddVertex (new JSM.BodyVertex (new JSM.Coord (0.0, 0.0, 0.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (1.0, 0.0, 0.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (1.0, 0.0, 1.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (0.0, 0.0, 1.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (0.0, 1.0, 0.0)));
body.AddVertex (new JSM.BodyVertex (new JSM.Coord (0.0, 1.0, 1.0)));

body.AddPolygon (new JSM.BodyPolygon ([0, 1, 2, 3]));
body.AddPolygon (new JSM.BodyPolygon ([0, 3, 5, 4]));

var meshes = JSM.ConvertBodyToThreeMeshes (body);
Live Demo
This model generated with the default material. If we would like to use uniqe materials, we need to define a Material object, and add some Materials. If we have materials, we need to set the material of polygons.
var materials = new JSM.Materials ();
materials.AddMaterial (new JSM.Material ({ambient : 0xcc0000, diffuse : 0xcc0000}));
materials.AddMaterial (new JSM.Material ({ambient : 0x0000cc, diffuse : 0x0000cc}));

body.GetPolygon (0).SetMaterialIndex (0);
body.GetPolygon (1).SetMaterialIndex (1);
The above example creates two materials. In the material definition it sets the ambient and diffuse colors of the material. If we have materials, we need to modify the conversion.
var meshes = JSM.ConvertBodyToThreeMeshes (body, materials);
Live Demo

5. Using smooth surfaces

If we would like to use smooth surfaces, we need to set curve groups. The body does not set curve groups by default, so during the conversion every polygon has its own normal vector. For example, create a body with two polygons next to each other.

Live Demo
This model generated without curve groups, so it seems a hard edge between the two polygons. If we would like to make this surface smooth, we need to set the polygons curve group to the same value.
body.GetPolygon (0).SetCurveGroup (0);
body.GetPolygon (1).SetCurveGroup (0);
Live Demo

6. Using generator functions

JSModeler contains fenerator functions, which can generate complex models from a few parameters. All of the generator functions return a Body with the default material. For example, generate a cuboid with unit edge length. Most of the generators generate the body in the origo.

var body = new JSM.GenerateCuboid (1.0, 1.0, 1.0);
Live Demo

7. Using transformations

If we would like to transform the created body, we need to create a transformation object, and apply it to the body. For example, create two cuboid, and translate one of them.

var body1 = new JSM.GenerateCuboid (1.0, 1.0, 1.0);
var body2 = new JSM.GenerateCuboid (1.0, 1.0, 1.0);

var transformation = JSM.TranslationTransformation (new JSM.Coord (1.5, 0.0, 0.0));
body2.Transform (transformation);
Live Demo
We can combine transformations. For example, translate and then rotate one of the bodies.
var body1 = new JSM.GenerateCuboid (1.0, 1.0, 1.0);
var body2 = new JSM.GenerateCuboid (1.0, 1.0, 1.0);

var addition = JSM.TranslationTransformation (new JSM.Coord (1.5, 0.0, 0.0));
var rotation = JSM.RotationXTransformation (45.0 * JSM.DegRad);

var transformation = new JSM.Transformation ();
transformation.Append (addition);
transformation.Append (rotation);

body2.Transform (transformation);
Live Demo