CTMLoader.js 5.1 KB

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