3D Publishing via Web with JSC3D

0
461

I recently came across JSC3D, another interesting library for publishing 3D models, which after reviewing X3DOM and Three.js, I believe could be the definitive library for publishing 3D content via the web.

Similarly to Three.js, this open source library natively supports OBJ (Wavefront) models as well as STL (STereo Lithography) and Autodesk 3Ds.

Like the other libraries, it exploits the WebGL capabilities of web browsers, with the exception that it includes a library to compensate for the lack of WebGL support in IE < 9, as well as interesting support for multitouch screens which, together with the good results of its rendering and especially the simplicity of its code, make it an interesting option when choosing a library for developing 3D viewers.

Its code is really simple:

  <canvas id="cv" width="640" height="400" ></canvas>
  <script type="text/javascript" src="jsc3d/jsc3d.js"></script>
  <script type="text/javascript" src="jsc3d/jsc3d.touch.js"></script>
  <script type="text/javascript" src="jsc3d/jsc3d.webgl.js"></script>

As we can see, we start with the now usual HTML5 Canvas object and then load basic libraries such as jsc3d.js and jsc3d.touch.js for multitouch support and jsc3d.webgl.js for WebGL compatibility.

First, we define the Canvas element:

var canvas = document.getElementById('cav');

Then we instantiate the Viewer object:

viewer.setParameter('SceneUrl', 'diosa.0.mesh.textura.obj')

Next, we load our model:

viewer.setParameter('SceneUrl', 'diosa.0.mesh.textura.obj')

If we are using a .mtl document to describe our texture, we must use the same file name as the one used in our obj for automatic loading.

Subsequently, we redefine the initial position of our object and background color.

  viewer.setParameter('InitRotationX', 0);
  viewer.setParameter('InitRotationY', 0);
  viewer.setParameter('InitRotationZ', 0);
  viewer.setParameter('BackgroundColor1', '#000000');
  viewer.setParameter('BackgroundColor2', '#000000');

An interesting feature is the possibility to choose the shading mode of our model with

viewer.setParameter('RenderMode', 'texturesmooth');

The RenderMode options are: point, wireframe, flat, smooth, texture, textureflat, and texturesmooth.

An interesting and optional method of JSC3D is:

viewer.setParameter('MipMapping', 'on');

With this method activated, automatic generation of mipmaps on our texture file is allowed in order to reduce resolution without especially sacrificing appearance.

Another really practical aspect is the possibility to display a progress bar, something quite significant with 3D models to inform the user about loading times, since these are usually not particularly lightweight:

viewer.setParameter('ProgressBar', 'on');

Finally, this library, as mentioned at the beginning, includes its own WebGL library which improves the rendering in some way, as well as providing compatibility.

Finally, with:

viewer.init();
viewer.update();

The model rendering is started.

As we see, in barely 14 lines of code it is possible to generate a simple viewer for 3D models. As always, you can see an example of this code on my dummy model “The Queen Of the Night“