CTMLoader.js 5.1 KB

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