CTMLoader.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Loader for CTM encoded models generated by OpenCTM tools:
  3. * http://openctm.sourceforge.net/
  4. *
  5. * Uses js-openctm library by Juan Mellado
  6. * http://code.google.com/p/js-openctm/
  7. *
  8. * @author alteredq / http://alteredqualia.com/
  9. */
  10. THREE.CTMLoader = function () {
  11. THREE.Loader.call( this );
  12. // Deprecated
  13. Object.defineProperties( this, {
  14. statusDomElement: {
  15. get: function () {
  16. if ( this._statusDomElement === undefined ) {
  17. this._statusDomElement = document.createElement( 'div' );
  18. }
  19. console.warn( 'THREE.BinaryLoader: .statusDomElement has been removed.' );
  20. return this._statusDomElement;
  21. }
  22. },
  23. } );
  24. };
  25. THREE.CTMLoader.prototype = Object.create( THREE.Loader.prototype );
  26. THREE.CTMLoader.prototype.constructor = THREE.CTMLoader;
  27. // Load multiple CTM parts defined in JSON
  28. THREE.CTMLoader.prototype.loadParts = function( url, callback, parameters ) {
  29. parameters = parameters || {};
  30. var scope = this;
  31. var xhr = new XMLHttpRequest();
  32. var basePath = parameters.basePath ? parameters.basePath : this.extractUrlBase( url );
  33. xhr.onreadystatechange = function() {
  34. if ( xhr.readyState === 4 ) {
  35. if ( xhr.status === 200 || xhr.status === 0 ) {
  36. var jsonObject = JSON.parse( xhr.responseText );
  37. var materials = [], geometries = [], counter = 0;
  38. function callbackFinal( geometry ) {
  39. counter += 1;
  40. geometries.push( geometry );
  41. if ( counter === jsonObject.offsets.length ) {
  42. callback( geometries, materials );
  43. }
  44. }
  45. // init materials
  46. for ( var i = 0; i < jsonObject.materials.length; i ++ ) {
  47. materials[ i ] = scope.createMaterial( jsonObject.materials[ i ], basePath );
  48. }
  49. // load joined CTM file
  50. var partUrl = basePath + jsonObject.data;
  51. var parametersPart = { useWorker: parameters.useWorker, offsets: jsonObject.offsets };
  52. scope.load( partUrl, callbackFinal, parametersPart );
  53. }
  54. }
  55. };
  56. xhr.open( "GET", url, true );
  57. xhr.setRequestHeader( "Content-Type", "text/plain" );
  58. xhr.send( null );
  59. };
  60. // Load CTMLoader compressed models
  61. // - parameters
  62. // - url (required)
  63. // - callback (required)
  64. THREE.CTMLoader.prototype.load = function( url, callback, parameters ) {
  65. parameters = parameters || {};
  66. var scope = this;
  67. var offsets = parameters.offsets !== undefined ? parameters.offsets : [ 0 ];
  68. var xhr = new XMLHttpRequest(),
  69. callbackProgress = null;
  70. var length = 0;
  71. xhr.onreadystatechange = function() {
  72. if ( xhr.readyState === 4 ) {
  73. if ( xhr.status === 200 || xhr.status === 0 ) {
  74. var binaryData = new Uint8Array(xhr.response);
  75. var s = Date.now();
  76. if ( parameters.useWorker ) {
  77. var worker = parameters.worker || new Worker( "js/loaders/ctm/CTMWorker.js" );
  78. worker.onmessage = function( event ) {
  79. var files = event.data;
  80. for ( var i = 0; i < files.length; i ++ ) {
  81. var ctmFile = files[ i ];
  82. var e1 = Date.now();
  83. // console.log( "CTM data parse time [worker]: " + (e1-s) + " ms" );
  84. scope.createModel( ctmFile, callback );
  85. var e = Date.now();
  86. console.log( "model load time [worker]: " + (e - e1) + " ms, total: " + (e - s));
  87. }
  88. };
  89. worker.postMessage( { "data": binaryData, "offsets": offsets } );
  90. } else {
  91. for ( var i = 0; i < offsets.length; i ++ ) {
  92. var stream = new CTM.Stream( binaryData );
  93. stream.offset = offsets[ i ];
  94. var ctmFile = new CTM.File( stream );
  95. scope.createModel( ctmFile, callback );
  96. }
  97. //var e = Date.now();
  98. //console.log( "CTM data parse time [inline]: " + (e-s) + " ms" );
  99. }
  100. } else {
  101. console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
  102. }
  103. } else if ( xhr.readyState === 3 ) {
  104. if ( callbackProgress ) {
  105. if ( length === 0 ) {
  106. length = xhr.getResponseHeader( "Content-Length" );
  107. }
  108. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  109. }
  110. } else if ( xhr.readyState === 2 ) {
  111. length = xhr.getResponseHeader( "Content-Length" );
  112. }
  113. };
  114. xhr.open( "GET", url, true );
  115. xhr.responseType = "arraybuffer";
  116. xhr.send( null );
  117. };
  118. THREE.CTMLoader.prototype.createModel = function ( file, callback ) {
  119. var Model = function () {
  120. THREE.BufferGeometry.call( this );
  121. this.materials = [];
  122. var indices = file.body.indices,
  123. positions = file.body.vertices,
  124. normals = file.body.normals;
  125. var uvs, colors;
  126. var uvMaps = file.body.uvMaps;
  127. if ( uvMaps !== undefined && uvMaps.length > 0 ) {
  128. uvs = uvMaps[ 0 ].uv;
  129. }
  130. var attrMaps = file.body.attrMaps;
  131. if ( attrMaps !== undefined && attrMaps.length > 0 && attrMaps[ 0 ].name === 'Color' ) {
  132. colors = attrMaps[ 0 ].attr;
  133. }
  134. this.addIndex( new THREE.BufferAttribute( indices, 1 ) );
  135. this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  136. if ( normals !== undefined ) {
  137. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  138. }
  139. if ( uvs !== undefined ) {
  140. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  141. }
  142. if ( colors !== undefined ) {
  143. this.addAttribute( 'color', new THREE.BufferAttribute( colors, 4 ) );
  144. }
  145. };
  146. Model.prototype = Object.create( THREE.BufferGeometry.prototype );
  147. Model.prototype.constructor = Model;
  148. var geometry = new Model();
  149. // compute vertex normals if not present in the CTM model
  150. if ( geometry.attributes.normal === undefined ) {
  151. geometry.computeVertexNormals();
  152. }
  153. callback( geometry );
  154. };