2
0

CTMLoader.js 6.0 KB

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