Loader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Loader = function ( editor ) {
  5. var scope = this;
  6. this.texturePath = '';
  7. this.loadFiles = function ( files ) {
  8. if ( files.length > 0 ) {
  9. var filesMap = createFileMap( files );
  10. var manager = new THREE.LoadingManager();
  11. manager.setURLModifier( function ( url ) {
  12. var file = filesMap[ url ];
  13. if ( file ) {
  14. console.log( 'Loading', url );
  15. return URL.createObjectURL( file );
  16. }
  17. return url;
  18. } );
  19. for ( var i = 0; i < files.length; i ++ ) {
  20. scope.loadFile( files[ i ], manager );
  21. }
  22. }
  23. };
  24. this.loadFile = function ( file, manager ) {
  25. var filename = file.name;
  26. var extension = filename.split( '.' ).pop().toLowerCase();
  27. var reader = new FileReader();
  28. reader.addEventListener( 'progress', function ( event ) {
  29. var size = '(' + Math.floor( event.total / 1000 ).format() + ' KB)';
  30. var progress = Math.floor( ( event.loaded / event.total ) * 100 ) + '%';
  31. console.log( 'Loading', filename, size, progress );
  32. } );
  33. switch ( extension ) {
  34. case '3ds':
  35. reader.addEventListener( 'load', function ( event ) {
  36. var loader = new THREE.TDSLoader();
  37. var object = loader.parse( event.target.result );
  38. editor.execute( new AddObjectCommand( editor, object ) );
  39. }, false );
  40. reader.readAsArrayBuffer( file );
  41. break;
  42. case 'amf':
  43. reader.addEventListener( 'load', function ( event ) {
  44. var loader = new THREE.AMFLoader();
  45. var amfobject = loader.parse( event.target.result );
  46. editor.execute( new AddObjectCommand( editor, amfobject ) );
  47. }, false );
  48. reader.readAsArrayBuffer( file );
  49. break;
  50. case 'dae':
  51. reader.addEventListener( 'load', function ( event ) {
  52. var contents = event.target.result;
  53. var loader = new THREE.ColladaLoader( manager );
  54. var collada = loader.parse( contents );
  55. collada.scene.name = filename;
  56. editor.addAnimation( collada.scene, collada.animations );
  57. editor.execute( new AddObjectCommand( editor, collada.scene ) );
  58. }, false );
  59. reader.readAsText( file );
  60. break;
  61. case 'drc':
  62. reader.addEventListener( 'load', function ( event ) {
  63. var contents = event.target.result;
  64. var loader = new THREE.DRACOLoader();
  65. loader.setDecoderPath( '../examples/js/libs/draco/' );
  66. loader.decodeDracoFile( contents, function ( geometry ) {
  67. var material = new THREE.MeshStandardMaterial();
  68. var mesh = new THREE.Mesh( geometry, material );
  69. mesh.name = filename;
  70. editor.execute( new AddObjectCommand( editor, mesh ) );
  71. } );
  72. }, false );
  73. reader.readAsArrayBuffer( file );
  74. break;
  75. case 'fbx':
  76. reader.addEventListener( 'load', function ( event ) {
  77. var contents = event.target.result;
  78. var loader = new THREE.FBXLoader( manager );
  79. var object = loader.parse( contents );
  80. editor.addAnimation( object, object.animations );
  81. editor.execute( new AddObjectCommand( editor, object ) );
  82. }, false );
  83. reader.readAsArrayBuffer( file );
  84. break;
  85. case 'glb':
  86. reader.addEventListener( 'load', function ( event ) {
  87. var contents = event.target.result;
  88. var dracoLoader = new THREE.DRACOLoader();
  89. dracoLoader.setDecoderPath( '../examples/js/libs/draco/gltf/' );
  90. var loader = new THREE.GLTFLoader();
  91. loader.setDRACOLoader( dracoLoader );
  92. loader.parse( contents, '', function ( result ) {
  93. var scene = result.scene;
  94. scene.name = filename;
  95. editor.addAnimation( scene, result.animations );
  96. editor.execute( new AddObjectCommand( editor, scene ) );
  97. } );
  98. }, false );
  99. reader.readAsArrayBuffer( file );
  100. break;
  101. case 'gltf':
  102. reader.addEventListener( 'load', function ( event ) {
  103. var contents = event.target.result;
  104. var loader;
  105. if ( isGLTF1( contents ) ) {
  106. alert( 'Import of glTF asset not possible. Only versions >= 2.0 are supported. Please try to upgrade the file to glTF 2.0 using glTF-Pipeline.' );
  107. } else {
  108. loader = new THREE.GLTFLoader( manager );
  109. }
  110. loader.parse( contents, '', function ( result ) {
  111. var scene = result.scene;
  112. scene.name = filename;
  113. editor.addAnimation( scene, result.animations );
  114. editor.execute( new AddObjectCommand( editor, scene ) );
  115. } );
  116. }, false );
  117. reader.readAsArrayBuffer( file );
  118. break;
  119. case 'js':
  120. case 'json':
  121. case '3geo':
  122. case '3mat':
  123. case '3obj':
  124. case '3scn':
  125. reader.addEventListener( 'load', function ( event ) {
  126. var contents = event.target.result;
  127. // 2.0
  128. if ( contents.indexOf( 'postMessage' ) !== - 1 ) {
  129. var blob = new Blob( [ contents ], { type: 'text/javascript' } );
  130. var url = URL.createObjectURL( blob );
  131. var worker = new Worker( url );
  132. worker.onmessage = function ( event ) {
  133. event.data.metadata = { version: 2 };
  134. handleJSON( event.data, file, filename );
  135. };
  136. worker.postMessage( Date.now() );
  137. return;
  138. }
  139. // >= 3.0
  140. var data;
  141. try {
  142. data = JSON.parse( contents );
  143. } catch ( error ) {
  144. alert( error );
  145. return;
  146. }
  147. handleJSON( data, file, filename );
  148. }, false );
  149. reader.readAsText( file );
  150. break;
  151. case 'kmz':
  152. reader.addEventListener( 'load', function ( event ) {
  153. var loader = new THREE.KMZLoader();
  154. var collada = loader.parse( event.target.result );
  155. collada.scene.name = filename;
  156. editor.execute( new AddObjectCommand( editor, collada.scene ) );
  157. }, false );
  158. reader.readAsArrayBuffer( file );
  159. break;
  160. case 'md2':
  161. reader.addEventListener( 'load', function ( event ) {
  162. var contents = event.target.result;
  163. var geometry = new THREE.MD2Loader().parse( contents );
  164. var material = new THREE.MeshStandardMaterial( {
  165. morphTargets: true,
  166. morphNormals: true
  167. } );
  168. var mesh = new THREE.Mesh( geometry, material );
  169. mesh.mixer = new THREE.AnimationMixer( mesh );
  170. mesh.name = filename;
  171. editor.addAnimation( mesh, geometry.animations );
  172. editor.execute( new AddObjectCommand( editor, mesh ) );
  173. }, false );
  174. reader.readAsArrayBuffer( file );
  175. break;
  176. case 'obj':
  177. reader.addEventListener( 'load', function ( event ) {
  178. var contents = event.target.result;
  179. var object = new THREE.OBJLoader().parse( contents );
  180. object.name = filename;
  181. editor.execute( new AddObjectCommand( editor, object ) );
  182. }, false );
  183. reader.readAsText( file );
  184. break;
  185. case 'ply':
  186. reader.addEventListener( 'load', function ( event ) {
  187. var contents = event.target.result;
  188. var geometry = new THREE.PLYLoader().parse( contents );
  189. geometry.sourceType = "ply";
  190. geometry.sourceFile = file.name;
  191. var material = new THREE.MeshStandardMaterial();
  192. var mesh = new THREE.Mesh( geometry, material );
  193. mesh.name = filename;
  194. editor.execute( new AddObjectCommand( editor, mesh ) );
  195. }, false );
  196. reader.readAsArrayBuffer( file );
  197. break;
  198. case 'stl':
  199. reader.addEventListener( 'load', function ( event ) {
  200. var contents = event.target.result;
  201. var geometry = new THREE.STLLoader().parse( contents );
  202. geometry.sourceType = "stl";
  203. geometry.sourceFile = file.name;
  204. var material = new THREE.MeshStandardMaterial();
  205. var mesh = new THREE.Mesh( geometry, material );
  206. mesh.name = filename;
  207. editor.execute( new AddObjectCommand( editor, mesh ) );
  208. }, false );
  209. if ( reader.readAsBinaryString !== undefined ) {
  210. reader.readAsBinaryString( file );
  211. } else {
  212. reader.readAsArrayBuffer( file );
  213. }
  214. break;
  215. case 'svg':
  216. reader.addEventListener( 'load', function ( event ) {
  217. var contents = event.target.result;
  218. var loader = new THREE.SVGLoader();
  219. var paths = loader.parse( contents ).paths;
  220. //
  221. var group = new THREE.Group();
  222. group.scale.multiplyScalar( 0.1 );
  223. group.scale.y *= - 1;
  224. for ( var i = 0; i < paths.length; i ++ ) {
  225. var path = paths[ i ];
  226. var material = new THREE.MeshBasicMaterial( {
  227. color: path.color,
  228. depthWrite: false
  229. } );
  230. var shapes = path.toShapes( true );
  231. for ( var j = 0; j < shapes.length; j ++ ) {
  232. var shape = shapes[ j ];
  233. var geometry = new THREE.ShapeBufferGeometry( shape );
  234. var mesh = new THREE.Mesh( geometry, material );
  235. group.add( mesh );
  236. }
  237. }
  238. editor.execute( new AddObjectCommand( editor, group ) );
  239. }, false );
  240. reader.readAsText( file );
  241. break;
  242. case 'vtk':
  243. reader.addEventListener( 'load', function ( event ) {
  244. var contents = event.target.result;
  245. var geometry = new THREE.VTKLoader().parse( contents );
  246. geometry.sourceType = "vtk";
  247. geometry.sourceFile = file.name;
  248. var material = new THREE.MeshStandardMaterial();
  249. var mesh = new THREE.Mesh( geometry, material );
  250. mesh.name = filename;
  251. editor.execute( new AddObjectCommand( editor, mesh ) );
  252. }, false );
  253. reader.readAsText( file );
  254. break;
  255. case 'wrl':
  256. reader.addEventListener( 'load', function ( event ) {
  257. var contents = event.target.result;
  258. var result = new THREE.VRMLLoader().parse( contents );
  259. editor.execute( new SetSceneCommand( editor, result ) );
  260. }, false );
  261. reader.readAsText( file );
  262. break;
  263. case 'zip':
  264. reader.addEventListener( 'load', function ( event ) {
  265. handleZIP( event.target.result );
  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( editor, mesh ) );
  290. break;
  291. case 'geometry':
  292. console.error( 'Loader: "Geometry" is no longer supported.' );
  293. break;
  294. case 'object':
  295. var loader = new THREE.ObjectLoader();
  296. loader.setResourcePath( scope.texturePath );
  297. var result = loader.parse( data );
  298. if ( result.isScene ) {
  299. editor.execute( new SetSceneCommand( editor, result ) );
  300. } else {
  301. editor.execute( new AddObjectCommand( editor, result ) );
  302. }
  303. break;
  304. case 'app':
  305. editor.fromJSON( data );
  306. break;
  307. }
  308. }
  309. function createFileMap( files ) {
  310. var map = {};
  311. for ( var i = 0; i < files.length; i ++ ) {
  312. var file = files[ i ];
  313. map[ file.name ] = file;
  314. }
  315. return map;
  316. }
  317. function handleZIP( contents ) {
  318. var zip = new JSZip( contents );
  319. // Poly
  320. if ( zip.files[ 'model.obj' ] && zip.files[ 'materials.mtl' ] ) {
  321. var materials = new THREE.MTLLoader().parse( zip.file( 'materials.mtl' ).asText() );
  322. var object = new THREE.OBJLoader().setMaterials( materials ).parse( zip.file( 'model.obj' ).asText() );
  323. editor.execute( new AddObjectCommand( editor, object ) );
  324. }
  325. //
  326. zip.filter( function ( path, file ) {
  327. var manager = new THREE.LoadingManager();
  328. manager.setURLModifier( function ( url ) {
  329. var file = zip.files[ url ];
  330. if ( file ) {
  331. console.log( 'Loading', url );
  332. var blob = new Blob( [ file.asArrayBuffer() ], { type: 'application/octet-stream' } );
  333. return URL.createObjectURL( blob );
  334. }
  335. return url;
  336. } );
  337. var extension = file.name.split( '.' ).pop().toLowerCase();
  338. switch ( extension ) {
  339. case 'fbx':
  340. var loader = new THREE.FBXLoader( manager );
  341. var object = loader.parse( file.asArrayBuffer() );
  342. editor.execute( new AddObjectCommand( editor, object ) );
  343. break;
  344. case 'glb':
  345. var loader = new THREE.GLTFLoader();
  346. loader.parse( file.asArrayBuffer(), '', function ( result ) {
  347. var scene = result.scene;
  348. editor.addAnimation( scene, result.animations );
  349. editor.execute( new AddObjectCommand( editor, scene ) );
  350. } );
  351. break;
  352. case 'gltf':
  353. var loader = new THREE.GLTFLoader( manager );
  354. loader.parse( file.asText(), '', function ( result ) {
  355. var scene = result.scene;
  356. editor.addAnimation( scene, result.animations );
  357. editor.execute( new AddObjectCommand( editor, scene ) );
  358. } );
  359. break;
  360. }
  361. } );
  362. }
  363. function isGLTF1( contents ) {
  364. var resultContent;
  365. if ( typeof contents === 'string' ) {
  366. // contents is a JSON string
  367. resultContent = contents;
  368. } else {
  369. var magic = THREE.LoaderUtils.decodeText( new Uint8Array( contents, 0, 4 ) );
  370. if ( magic === 'glTF' ) {
  371. // contents is a .glb file; extract the version
  372. var version = new DataView( contents ).getUint32( 4, true );
  373. return version < 2;
  374. } else {
  375. // contents is a .gltf file
  376. resultContent = THREE.LoaderUtils.decodeText( new Uint8Array( contents ) );
  377. }
  378. }
  379. var json = JSON.parse( resultContent );
  380. return ( json.asset != undefined && json.asset.version[ 0 ] < 2 );
  381. }
  382. };