CTMLoader.js 5.1 KB

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