CTMLoader.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * OpenCTM LICENSE:
  11. *
  12. * Copyright (c) 2009-2010 Marcus Geelnard
  13. *
  14. * This software is provided 'as-is', without any express or implied
  15. * warranty. In no event will the authors be held liable for any damages
  16. * arising from the use of this software.
  17. *
  18. * Permission is granted to anyone to use this software for any purpose,
  19. * including commercial applications, and to alter it and redistribute it
  20. * freely, subject to the following restrictions:
  21. *
  22. * 1. The origin of this software must not be misrepresented; you must not
  23. * claim that you wrote the original software. If you use this software
  24. * in a product, an acknowledgment in the product documentation would be
  25. * appreciated but is not required.
  26. *
  27. * 2. Altered source versions must be plainly marked as such, and must not
  28. * be misrepresented as being the original software.
  29. *
  30. * 3. This notice may not be removed or altered from any source
  31. * distribution.
  32. *
  33. */
  34. THREE.CTMLoader = function () {
  35. };
  36. THREE.CTMLoader.prototype.constructor = THREE.CTMLoader;
  37. // Load multiple CTM parts defined in JSON
  38. THREE.CTMLoader.prototype.loadParts = function ( url, callback, parameters ) {
  39. parameters = parameters || {};
  40. var scope = this;
  41. var xhr = new XMLHttpRequest();
  42. var basePath = parameters.basePath ? parameters.basePath : THREE.LoaderUtils.extractUrlBase( url );
  43. xhr.onreadystatechange = function () {
  44. if ( xhr.readyState === 4 ) {
  45. if ( xhr.status === 200 || xhr.status === 0 ) {
  46. var jsonObject = JSON.parse( xhr.responseText );
  47. var materials = [], geometries = [], counter = 0;
  48. function callbackFinal( geometry ) {
  49. counter += 1;
  50. geometries.push( geometry );
  51. if ( counter === jsonObject.offsets.length ) {
  52. callback( geometries, materials );
  53. }
  54. }
  55. // init materials
  56. for ( var i = 0; i < jsonObject.materials.length; i ++ ) {
  57. materials[ i ] = THREE.Loader.prototype.createMaterial( jsonObject.materials[ i ], basePath );
  58. }
  59. // load joined CTM file
  60. var partUrl = basePath + jsonObject.data;
  61. var parametersPart = { useWorker: parameters.useWorker, worker: parameters.worker, offsets: jsonObject.offsets };
  62. scope.load( partUrl, callbackFinal, parametersPart );
  63. }
  64. }
  65. };
  66. xhr.open( "GET", url, true );
  67. xhr.setRequestHeader( "Content-Type", "text/plain" );
  68. xhr.send( null );
  69. };
  70. // Load CTMLoader compressed models
  71. // - parameters
  72. // - url (required)
  73. // - callback (required)
  74. THREE.CTMLoader.prototype.load = function ( url, callback, parameters ) {
  75. parameters = parameters || {};
  76. var scope = this;
  77. var offsets = parameters.offsets !== undefined ? parameters.offsets : [ 0 ];
  78. var xhr = new XMLHttpRequest(),
  79. callbackProgress = null;
  80. var length = 0;
  81. xhr.onreadystatechange = function () {
  82. if ( xhr.readyState === 4 ) {
  83. if ( xhr.status === 200 || xhr.status === 0 ) {
  84. var binaryData = new Uint8Array( xhr.response );
  85. var s = Date.now();
  86. if ( parameters.useWorker ) {
  87. var worker = parameters.worker || new Worker( 'js/loaders/ctm/CTMWorker.js' );
  88. worker.onmessage = function ( event ) {
  89. var files = event.data;
  90. for ( var i = 0; i < files.length; i ++ ) {
  91. var ctmFile = files[ i ];
  92. var e1 = Date.now();
  93. // console.log( "CTM data parse time [worker]: " + (e1-s) + " ms" );
  94. scope.createModel( ctmFile, callback );
  95. var e = Date.now();
  96. console.log( "model load time [worker]: " + ( e - e1 ) + " ms, total: " + ( e - s ) );
  97. }
  98. };
  99. worker.postMessage( { "data": binaryData, "offsets": offsets }, [ binaryData.buffer ] );
  100. } else {
  101. for ( var i = 0; i < offsets.length; i ++ ) {
  102. var stream = new CTM.Stream( binaryData );
  103. stream.offset = offsets[ i ];
  104. var ctmFile = new CTM.File( stream );
  105. scope.createModel( ctmFile, callback );
  106. }
  107. //var e = Date.now();
  108. //console.log( "CTM data parse time [inline]: " + (e-s) + " ms" );
  109. }
  110. } else {
  111. console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
  112. }
  113. } else if ( xhr.readyState === 3 ) {
  114. if ( callbackProgress ) {
  115. if ( length === 0 ) {
  116. length = xhr.getResponseHeader( "Content-Length" );
  117. }
  118. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  119. }
  120. } else if ( xhr.readyState === 2 ) {
  121. length = xhr.getResponseHeader( "Content-Length" );
  122. }
  123. };
  124. xhr.open( "GET", url, true );
  125. xhr.responseType = "arraybuffer";
  126. xhr.send( null );
  127. };
  128. THREE.CTMLoader.prototype.createModel = function ( file, callback ) {
  129. var Model = function () {
  130. THREE.BufferGeometry.call( this );
  131. this.materials = [];
  132. var indices = file.body.indices;
  133. var positions = file.body.vertices;
  134. var normals = file.body.normals;
  135. var uvs, colors;
  136. var uvMaps = file.body.uvMaps;
  137. if ( uvMaps !== undefined && uvMaps.length > 0 ) {
  138. uvs = uvMaps[ 0 ].uv;
  139. }
  140. var attrMaps = file.body.attrMaps;
  141. if ( attrMaps !== undefined && attrMaps.length > 0 && attrMaps[ 0 ].name === 'Color' ) {
  142. colors = attrMaps[ 0 ].attr;
  143. }
  144. this.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  145. this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  146. if ( normals !== undefined ) {
  147. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  148. }
  149. if ( uvs !== undefined ) {
  150. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  151. }
  152. if ( colors !== undefined ) {
  153. this.addAttribute( 'color', new THREE.BufferAttribute( colors, 4 ) );
  154. }
  155. };
  156. Model.prototype = Object.create( THREE.BufferGeometry.prototype );
  157. Model.prototype.constructor = Model;
  158. var geometry = new Model();
  159. // compute vertex normals if not present in the CTM model
  160. if ( geometry.attributes.normal === undefined ) {
  161. geometry.computeVertexNormals();
  162. }
  163. callback( geometry );
  164. };