2
0

CTMLoader.js 6.0 KB

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