Browse Source

Added possibility of using workers for CTM decompression.

To be used with care, for the moment there is no mechanism to prevent shooting yourself in the foot with spawning too many workers.
alteredq 13 years ago
parent
commit
4a82869c1e
3 changed files with 63 additions and 17 deletions
  1. 32 14
      examples/js/ctm/CTMLoader.js
  2. 8 0
      examples/js/ctm/CTMWorker.js
  3. 23 3
      examples/webgl_loader_ctm.html

+ 32 - 14
examples/js/ctm/CTMLoader.js

@@ -21,7 +21,7 @@ THREE.CTMLoader.prototype.constructor = THREE.CTMLoader;
 //		- url (required)
 //		- callback (required)
 
-THREE.CTMLoader.prototype.load = function( url, callback ) {
+THREE.CTMLoader.prototype.load = function( url, callback, useWorker ) {
 
 	var xhr = new XMLHttpRequest(),
 		callbackProgress = null;
@@ -34,11 +34,39 @@ THREE.CTMLoader.prototype.load = function( url, callback ) {
 
 			if ( xhr.status == 200 || xhr.status == 0 ) {
 
-				THREE.CTMLoader.prototype.createModel( xhr.responseText, callback );
+				var binaryData = xhr.responseText;
+
+				var s = Date.now();
+
+				if ( useWorker ) {
+
+					var worker = new Worker( "js/ctm/CTMWorker.js" );
+
+					worker.onmessage = function( event ) {
+
+						var ctmFile = event.data;
+						THREE.CTMLoader.prototype.createModel( ctmFile, callback );
+
+						var e = Date.now();
+						console.log( "CTM data parse time [worker]: " + (e-s) + " ms" );
+
+					};
+
+					worker.postMessage( binaryData );
+
+				} else {
+
+					var ctmFile = new CTM.File( new CTM.Stream( binaryData ) );
+					THREE.CTMLoader.prototype.createModel( ctmFile, callback );
+
+					var e = Date.now();
+					console.log( "CTM data parse time [inline]: " + (e-s) + " ms" );
+
+				}
 
 			} else {
 
-				alert( "Couldn't load [" + url + "] [" + xhr.status + "]" );
+				console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
 
 			}
 
@@ -71,22 +99,16 @@ THREE.CTMLoader.prototype.load = function( url, callback ) {
 };
 
 
-THREE.CTMLoader.prototype.createModel = function ( data, callback ) {
+THREE.CTMLoader.prototype.createModel = function ( file, callback ) {
 
 	var Model = function ( texture_path ) {
 
-		var s = (new Date).getTime();
-
 		var scope = this;
 
 		scope.materials = [];
 
 		THREE.Geometry.call( this );
 
-		var file = new CTM.File( new CTM.Stream( data ) );
-
-		console.log( file );
-
 		var normals = [],
 			uvs = [],
 			colors = [];
@@ -112,10 +134,6 @@ THREE.CTMLoader.prototype.createModel = function ( data, callback ) {
 		this.computeFaceNormals();
 		//this.computeTangents();
 
-		var e = (new Date).getTime();
-
-		console.log( "CTM data parse time: " + (e-s) + " ms" );
-
 		function init_vertices( buffer ) {
 
 			var x, y, z, i, il = buffer.length;

+ 8 - 0
examples/js/ctm/CTMWorker.js

@@ -0,0 +1,8 @@
+importScripts( "lzma.js", "ctm.js" );
+
+self.onmessage = function( event ) {
+
+  self.postMessage( new CTM.File( new CTM.Stream( event.data ) ) );
+  self.close();
+
+}

+ 23 - 3
examples/webgl_loader_ctm.html

@@ -126,14 +126,32 @@
 
 				// LOADER
 
+				var c = 0, s = Date.now();
+
+				function checkTime() {
+
+					c ++;
+
+					if ( c === 3 ) {
+
+						var e = Date.now();
+						console.log( "Total parse time: " + (e-s) + " ms" );
+
+					}
+
+				}
+
+				var useWorker = true;
+
 				var loader = new THREE.CTMLoader();
 
 				loader.load( "ctm/ben.ctm",   function( geometry ) {
 
 					var material = new THREE.MeshLambertMaterial( { color: 0xffaa00, map: THREE.ImageUtils.loadTexture( "textures/ash_uvgrid01.jpg" ), envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
 					callbackModel( geometry, 450, material, 0, -200, 0, 0, 0 );
+					checkTime();
 
-				} );
+				}, useWorker );
 
 				loader.load( "ctm/WaltHead.ctm",  function( geometry ) {
 
@@ -141,15 +159,17 @@
 
 					var material = new THREE.MeshLambertMaterial( { color: 0xffffff, envMaps: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
 					callbackModel( geometry, 5, material, -200, 0, 0, 0, 0 );
+					checkTime();
 
-				});
+				}, useWorker );
 
 				loader.load( "ctm/LeePerry.ctm",  function( geometry ) {
 
 					var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x444444, shininess: 30, map: THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" ), envMaps: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
 					callbackModel( geometry, 1300, material, 200, 50, 0, 0, 0 );
+					checkTime();
 
-				});
+				}, useWorker );
 
 			}