3D Publishing via Web with Three.js

0
823

Some time ago I addressed the issue of publishing and distributing 3D content since often a large part of the generated 3D documentation rarely sees the light of day due to the uncommon nature of 3D content visualization tools, the size of many documents, and the relative unfamiliarity with certain web publishing strategies.
Although in recent years proprietary platforms such as Sketchfab or p3dj.in have become an interesting solution, the costs their use entails in the long term may not be especially sustainable for certain projects.

As an alternative, we have the well-known 3D PDF or X3DOM for the web, based on the X3D format, which presents itself as a strong candidate for standardization in web publishing.

The problem with publishing in X3DOM is, for now, the dependence on tools like Instant Player’s AOPT to perform conversions from X3D documents to HTML5.

Currently, perhaps one of the few mature alternatives to X3DOM could be the Three.js project, which as its name indicates is based on Javascript and also leverages the webGL capabilities of our browsers to render 3D models through the Web.

Perhaps one of the most interesting possibilities of Three.js, compared to the use of X3DOM, is the direct use of file formats such as Wavefront (.obj) or Stanford Triangle Format (.ply), which are extremely common file formats, so no prior preparation of the model is necessary, except for the obvious and necessary simplification of the model in order to reduce the size of our documents sufficiently for web loading without boring the user with eternal waiting times.

Three.js is an open-source project released by Ricardo Cabello in 2010, coincidentally the same year X3DOM emerged. Currently, this project has strong development and has been scaled with a good number of libraries that provide interesting functionalities.

We can get an idea of the tremendous possibilities of this library through the website of the University of Michigan’s Museum of Paleontology (http://umorf.ummp.lsa.umich.edu/wp/).

Introduction to the use of Three.js

We can easily get started with this library thanks to the numerous examples we can download from its website (http://mrdoob.github.io/three.js/examples/). The following code we are going to see is taken from the example of loading documents in Wavefront format (obj Loader).

This example uses four libraries:

  • Three.js, which is the main library responsible for generating the render of our model via webGL
  • TrackballControls.js, which as its name indicates allows us to rotate the model (camera control)
  • OBJLoader.js, responsible for loading models in Wavefront format (.obj)
  • Detector.js, whose mission is to verify if our internet browser supports webGL

The complete source code is as follows:

 <script src="js/three.min.js"></script>
  <script src="js/TrackballControls.js"></script>
  <script src="js/OBJLoader.js"></script>
  <script src="js/Detector.js"></script>
 <script>
 var container, stats;
  var camera, scene, renderer;
  var mouseX = 0, mouseY = 0;
  var windowHalfX = window.innerWidth / 2;
  var windowHalfY = window.innerHeight / 2;
 init();
  animate();
 function init() {
 container = document.createElement( 'div' );
  document.body.appendChild( container );
  camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  camera.position.z = 100;
  controls = new THREE.TrackballControls( camera );
  controls.rotateSpeed = 5.0;
  controls.zoomSpeed = 5;
  controls.panSpeed = 2;
  controls.noZoom = false;
  controls.noPan = false;
  controls.staticMoving = true;
  controls.dynamicDampingFactor = 0.3;
 // scene
  scene = new THREE.Scene();
 // ambient light
  var ambient = new THREE.AmbientLight( 0xffffff );
  scene.add( ambient );
 // Load texture
  var texture = new THREE.Texture();
  var loader = new THREE.ImageLoader();
  loader.addEventListener( 'load', function ( event ) {
  texture.image = event.content;
  texture.needsUpdate = true;
  texture.magFilter = THREE.NearestFilter;
  texture.minFilter = THREE.NearestMipMapLinearFilter;
  } );
  loader.load( 'diosa_textura_low.png' );
 // Load model
 var loader = new THREE.OBJLoader();
  loader.addEventListener( 'load', function ( event ) {
 var object = event.content;
  object.traverse( function ( child ) {
  if ( child instanceof THREE.Mesh ) {
  child.material.map = texture;
  }
  } );
  object.scale = new THREE.Vector3(25,25,25 );
  scene.add( object );
  });
  loader.load( 'diosa.0.mesh_low_low.textura.obj' );
 // Render with webGL
  renderer = new THREE.WebGLRenderer();
  renderer.setSize( window.innerWidth, window.innerHeight );
  container.appendChild( renderer.domElement );
  document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  window.addEventListener( 'resize', onWindowResize, false );
 }
 function onWindowResize() {
  windowHalfX = window.innerWidth / 2;
  windowHalfY = window.innerHeight / 2;
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize( window.innerWidth, window.innerHeight );
  }
 function onDocumentMouseMove( event ) {
  mouseX = ( event.clientX - windowHalfX ) / 2;
  mouseY = ( event.clientY - windowHalfY ) / 2;
  }
 function animate() {
  requestAnimationFrame( animate );
  render();
  }
 function render() {
  controls.update();
  camera.lookAt( scene.position );
  renderer.render( scene, camera );
  }
 </script>
  <div>
  <canvas width="800" height="800" style="width: 800px; height: 800px;"></canvas>
  </div>

From this code, it is interesting to highlight the loading of our texture in:

loader.load( 'diosa_textura_low.png' );

And finally the loading of the Wavefront file

loader.load( 'diosa.0.mesh_low_low.textura.obj' );

It should be noted that OBJLoader can also load .mtl documents for material references.

loader.load( 'diosa.0.mesh_low_low.textura.obj', 'diosa.0.mesh_low_low.textura.mtl')

Below you can see a small model of 24,000 polygons, with a total weight of 1.4Mb and a jpeg texture of 500Kb published with Three.js: The Queen of The Night