Loader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Loader = function ( editor ) {
  5. var scope = this;
  6. var signals = editor.signals;
  7. this.texturePath = '';
  8. this.loadFile = function ( file ) {
  9. var filename = file.name;
  10. var extension = filename.split( '.' ).pop().toLowerCase();
  11. var reader = new FileReader();
  12. reader.addEventListener( 'progress', function ( event ) {
  13. var size = '(' + Math.floor( event.total / 1000 ).format() + ' KB)';
  14. var progress = Math.floor( ( event.loaded / event.total ) * 100 ) + '%';
  15. console.log( 'Loading', filename, size, progress );
  16. } );
  17. switch ( extension ) {
  18. case '3ds':
  19. reader.addEventListener( 'load', function ( event ) {
  20. var loader = new THREE.TDSLoader();
  21. var object = loader.parse( event.target.result );
  22. editor.execute( new AddObjectCommand( object ) );
  23. }, false );
  24. reader.readAsArrayBuffer( file );
  25. break;
  26. case 'amf':
  27. reader.addEventListener( 'load', function ( event ) {
  28. var loader = new THREE.AMFLoader();
  29. var amfobject = loader.parse( event.target.result );
  30. editor.execute( new AddObjectCommand( amfobject ) );
  31. }, false );
  32. reader.readAsArrayBuffer( file );
  33. break;
  34. case 'awd':
  35. reader.addEventListener( 'load', function ( event ) {
  36. var loader = new THREE.AWDLoader();
  37. var scene = loader.parse( event.target.result );
  38. editor.execute( new SetSceneCommand( scene ) );
  39. }, false );
  40. reader.readAsArrayBuffer( file );
  41. break;
  42. case 'babylon':
  43. reader.addEventListener( 'load', function ( event ) {
  44. var contents = event.target.result;
  45. var json = JSON.parse( contents );
  46. var loader = new THREE.BabylonLoader();
  47. var scene = loader.parse( json );
  48. editor.execute( new SetSceneCommand( scene ) );
  49. }, false );
  50. reader.readAsText( file );
  51. break;
  52. case 'babylonmeshdata':
  53. reader.addEventListener( 'load', function ( event ) {
  54. var contents = event.target.result;
  55. var json = JSON.parse( contents );
  56. var loader = new THREE.BabylonLoader();
  57. var geometry = loader.parseGeometry( json );
  58. var material = new THREE.MeshStandardMaterial();
  59. var mesh = new THREE.Mesh( geometry, material );
  60. mesh.name = filename;
  61. editor.execute( new AddObjectCommand( mesh ) );
  62. }, false );
  63. reader.readAsText( file );
  64. break;
  65. case 'ctm':
  66. reader.addEventListener( 'load', function ( event ) {
  67. var data = new Uint8Array( event.target.result );
  68. var stream = new CTM.Stream( data );
  69. stream.offset = 0;
  70. var loader = new THREE.CTMLoader();
  71. loader.createModel( new CTM.File( stream ), function( geometry ) {
  72. geometry.sourceType = "ctm";
  73. geometry.sourceFile = file.name;
  74. var material = new THREE.MeshStandardMaterial();
  75. var mesh = new THREE.Mesh( geometry, material );
  76. mesh.name = filename;
  77. editor.execute( new AddObjectCommand( mesh ) );
  78. } );
  79. }, false );
  80. reader.readAsArrayBuffer( file );
  81. break;
  82. case 'dae':
  83. reader.addEventListener( 'load', function ( event ) {
  84. var contents = event.target.result;
  85. var loader = new THREE.ColladaLoader();
  86. var collada = loader.parse( contents );
  87. collada.scene.name = filename;
  88. editor.execute( new AddObjectCommand( collada.scene ) );
  89. }, false );
  90. reader.readAsText( file );
  91. break;
  92. case 'fbx':
  93. reader.addEventListener( 'load', function ( event ) {
  94. var contents = event.target.result;
  95. var loader = new THREE.FBXLoader();
  96. var object = loader.parse( contents );
  97. editor.execute( new AddObjectCommand( object ) );
  98. }, false );
  99. reader.readAsArrayBuffer( file );
  100. break;
  101. case 'glb':
  102. case 'gltf':
  103. reader.addEventListener( 'load', function ( event ) {
  104. var contents = event.target.result;
  105. var loader;
  106. if ( isGltf1( contents ) ) {
  107. loader = new THREE.LegacyGLTFLoader();
  108. } else {
  109. loader = new THREE.GLTFLoader();
  110. }
  111. loader.parse( contents, '', function ( result ) {
  112. result.scene.name = filename;
  113. editor.execute( new AddObjectCommand( result.scene ) );
  114. } );
  115. }, false );
  116. reader.readAsArrayBuffer( file );
  117. break;
  118. case 'js':
  119. case 'json':
  120. case '3geo':
  121. case '3mat':
  122. case '3obj':
  123. case '3scn':
  124. reader.addEventListener( 'load', function ( event ) {
  125. var contents = event.target.result;
  126. // 2.0
  127. if ( contents.indexOf( 'postMessage' ) !== - 1 ) {
  128. var blob = new Blob( [ contents ], { type: 'text/javascript' } );
  129. var url = URL.createObjectURL( blob );
  130. var worker = new Worker( url );
  131. worker.onmessage = function ( event ) {
  132. event.data.metadata = { version: 2 };
  133. handleJSON( event.data, file, filename );
  134. };
  135. worker.postMessage( Date.now() );
  136. return;
  137. }
  138. // >= 3.0
  139. var data;
  140. try {
  141. data = JSON.parse( contents );
  142. } catch ( error ) {
  143. alert( error );
  144. return;
  145. }
  146. handleJSON( data, file, filename );
  147. }, false );
  148. reader.readAsText( file );
  149. break;
  150. case 'kmz':
  151. reader.addEventListener( 'load', function ( event ) {
  152. var loader = new THREE.KMZLoader();
  153. var collada = loader.parse( event.target.result );
  154. collada.scene.name = filename;
  155. editor.execute( new AddObjectCommand( collada.scene ) );
  156. }, false );
  157. reader.readAsArrayBuffer( file );
  158. break;
  159. case 'md2':
  160. reader.addEventListener( 'load', function ( event ) {
  161. var contents = event.target.result;
  162. var geometry = new THREE.MD2Loader().parse( contents );
  163. var material = new THREE.MeshStandardMaterial( {
  164. morphTargets: true,
  165. morphNormals: true
  166. } );
  167. var mesh = new THREE.Mesh( geometry, material );
  168. mesh.mixer = new THREE.AnimationMixer( mesh );
  169. mesh.name = filename;
  170. editor.execute( new AddObjectCommand( mesh ) );
  171. }, false );
  172. reader.readAsArrayBuffer( file );
  173. break;
  174. case 'obj':
  175. reader.addEventListener( 'load', function ( event ) {
  176. var contents = event.target.result;
  177. var object = new THREE.OBJLoader().parse( contents );
  178. object.name = filename;
  179. editor.execute( new AddObjectCommand( object ) );
  180. }, false );
  181. reader.readAsText( file );
  182. break;
  183. case 'playcanvas':
  184. reader.addEventListener( 'load', function ( event ) {
  185. var contents = event.target.result;
  186. var json = JSON.parse( contents );
  187. var loader = new THREE.PlayCanvasLoader();
  188. var object = loader.parse( json );
  189. editor.execute( new AddObjectCommand( object ) );
  190. }, false );
  191. reader.readAsText( file );
  192. break;
  193. case 'ply':
  194. reader.addEventListener( 'load', function ( event ) {
  195. var contents = event.target.result;
  196. var geometry = new THREE.PLYLoader().parse( contents );
  197. geometry.sourceType = "ply";
  198. geometry.sourceFile = file.name;
  199. var material = new THREE.MeshStandardMaterial();
  200. var mesh = new THREE.Mesh( geometry, material );
  201. mesh.name = filename;
  202. editor.execute( new AddObjectCommand( mesh ) );
  203. }, false );
  204. reader.readAsArrayBuffer( file );
  205. break;
  206. case 'stl':
  207. reader.addEventListener( 'load', function ( event ) {
  208. var contents = event.target.result;
  209. var geometry = new THREE.STLLoader().parse( contents );
  210. geometry.sourceType = "stl";
  211. geometry.sourceFile = file.name;
  212. var material = new THREE.MeshStandardMaterial();
  213. var mesh = new THREE.Mesh( geometry, material );
  214. mesh.name = filename;
  215. editor.execute( new AddObjectCommand( mesh ) );
  216. }, false );
  217. if ( reader.readAsBinaryString !== undefined ) {
  218. reader.readAsBinaryString( file );
  219. } else {
  220. reader.readAsArrayBuffer( file );
  221. }
  222. break;
  223. /*
  224. case 'utf8':
  225. reader.addEventListener( 'load', function ( event ) {
  226. var contents = event.target.result;
  227. var geometry = new THREE.UTF8Loader().parse( contents );
  228. var material = new THREE.MeshLambertMaterial();
  229. var mesh = new THREE.Mesh( geometry, material );
  230. editor.execute( new AddObjectCommand( mesh ) );
  231. }, false );
  232. reader.readAsBinaryString( file );
  233. break;
  234. */
  235. case 'vtk':
  236. reader.addEventListener( 'load', function ( event ) {
  237. var contents = event.target.result;
  238. var geometry = new THREE.VTKLoader().parse( contents );
  239. geometry.sourceType = "vtk";
  240. geometry.sourceFile = file.name;
  241. var material = new THREE.MeshStandardMaterial();
  242. var mesh = new THREE.Mesh( geometry, material );
  243. mesh.name = filename;
  244. editor.execute( new AddObjectCommand( mesh ) );
  245. }, false );
  246. reader.readAsText( file );
  247. break;
  248. case 'wrl':
  249. reader.addEventListener( 'load', function ( event ) {
  250. var contents = event.target.result;
  251. var result = new THREE.VRMLLoader().parse( contents );
  252. editor.execute( new SetSceneCommand( result ) );
  253. }, false );
  254. reader.readAsText( file );
  255. break;
  256. case 'zip':
  257. reader.addEventListener( 'load', function ( event ) {
  258. var contents = event.target.result;
  259. var zip = new JSZip( contents );
  260. // BLOCKS
  261. if ( zip.files[ 'model.obj' ] && zip.files[ 'materials.mtl' ] ) {
  262. var materials = new THREE.MTLLoader().parse( zip.file( 'materials.mtl' ).asText() );
  263. var object = new THREE.OBJLoader().setMaterials( materials ).parse( zip.file( 'model.obj' ).asText() );
  264. editor.execute( new AddObjectCommand( object ) );
  265. }
  266. }, false );
  267. reader.readAsBinaryString( file );
  268. break;
  269. default:
  270. alert( 'Unsupported file format (' + extension + ').' );
  271. break;
  272. }
  273. };
  274. function handleJSON( data, file, filename ) {
  275. if ( data.metadata === undefined ) { // 2.0
  276. data.metadata = { type: 'Geometry' };
  277. }
  278. if ( data.metadata.type === undefined ) { // 3.0
  279. data.metadata.type = 'Geometry';
  280. }
  281. if ( data.metadata.formatVersion !== undefined ) {
  282. data.metadata.version = data.metadata.formatVersion;
  283. }
  284. switch ( data.metadata.type.toLowerCase() ) {
  285. case 'buffergeometry':
  286. var loader = new THREE.BufferGeometryLoader();
  287. var result = loader.parse( data );
  288. var mesh = new THREE.Mesh( result );
  289. editor.execute( new AddObjectCommand( mesh ) );
  290. break;
  291. case 'geometry':
  292. var loader = new THREE.JSONLoader();
  293. loader.setTexturePath( scope.texturePath );
  294. var result = loader.parse( data );
  295. var geometry = result.geometry;
  296. var material;
  297. if ( result.materials !== undefined ) {
  298. if ( result.materials.length > 1 ) {
  299. material = new THREE.MultiMaterial( result.materials );
  300. } else {
  301. material = result.materials[ 0 ];
  302. }
  303. } else {
  304. material = new THREE.MeshStandardMaterial();
  305. }
  306. geometry.sourceType = "ascii";
  307. geometry.sourceFile = file.name;
  308. var mesh;
  309. if ( geometry.animation && geometry.animation.hierarchy ) {
  310. mesh = new THREE.SkinnedMesh( geometry, material );
  311. } else {
  312. mesh = new THREE.Mesh( geometry, material );
  313. }
  314. mesh.name = filename;
  315. editor.execute( new AddObjectCommand( mesh ) );
  316. break;
  317. case 'object':
  318. var loader = new THREE.ObjectLoader();
  319. loader.setTexturePath( scope.texturePath );
  320. var result = loader.parse( data );
  321. if ( result instanceof THREE.Scene ) {
  322. editor.execute( new SetSceneCommand( result ) );
  323. } else {
  324. editor.execute( new AddObjectCommand( result ) );
  325. }
  326. break;
  327. case 'app':
  328. editor.fromJSON( data );
  329. break;
  330. }
  331. }
  332. function isGltf1( contents ) {
  333. var resultContent;
  334. if ( typeof contents === 'string' ) {
  335. // contents is a JSON string
  336. resultContent = contents;
  337. } else {
  338. var magic = THREE.LoaderUtils.decodeText( new Uint8Array( contents, 0, 4 ) );
  339. if ( magic === 'glTF' ) {
  340. // contents is a .glb file; extract the version
  341. var u32bytes = new Uint8Array( contents, 4, 4 );
  342. var versionNumber = new Uint32Array(u32bytes)[0];
  343. return ( versionNumber < 2 );
  344. } else {
  345. // contents is a .gltf file
  346. resultContent = THREE.LoaderUtils.decodeText( new Uint8Array( contents ) );
  347. }
  348. }
  349. var json = JSON.parse( resultContent );
  350. return ( json.asset != undefined && json.asset.version[ 0 ] < 2 );
  351. }
  352. };