Loader.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. switch ( extension ) {
  12. case 'amf':
  13. var reader = new FileReader();
  14. reader.addEventListener( 'load', function ( event ) {
  15. var loader = new THREE.AMFLoader();
  16. var amfobject = loader.parse( event.target.result );
  17. editor.addObject( amfobject );
  18. editor.select( amfobject );
  19. }, false );
  20. reader.readAsArrayBuffer( file );
  21. break;
  22. case 'awd':
  23. var reader = new FileReader();
  24. reader.addEventListener( 'load', function ( event ) {
  25. var loader = new THREE.AWDLoader();
  26. var scene = loader.parse( event.target.result );
  27. editor.setScene( scene );
  28. }, false );
  29. reader.readAsArrayBuffer( file );
  30. break;
  31. case 'babylon':
  32. var reader = new FileReader();
  33. reader.addEventListener( 'load', function ( event ) {
  34. var contents = event.target.result;
  35. var json = JSON.parse( contents );
  36. var loader = new THREE.BabylonLoader();
  37. var scene = loader.parse( json );
  38. editor.setScene( scene );
  39. }, false );
  40. reader.readAsText( file );
  41. break;
  42. case 'babylonmeshdata':
  43. var reader = new FileReader();
  44. reader.addEventListener( 'load', function ( event ) {
  45. var contents = event.target.result;
  46. var json = JSON.parse( contents );
  47. var loader = new THREE.BabylonLoader();
  48. var geometry = loader.parseGeometry( json );
  49. var material = new THREE.MeshPhongMaterial();
  50. var mesh = new THREE.Mesh( geometry, material );
  51. mesh.name = filename;
  52. editor.addObject( mesh );
  53. editor.select( mesh );
  54. }, false );
  55. reader.readAsText( file );
  56. break;
  57. case 'ctm':
  58. var reader = new FileReader();
  59. reader.addEventListener( 'load', function ( event ) {
  60. var data = new Uint8Array( event.target.result );
  61. var stream = new CTM.Stream( data );
  62. stream.offset = 0;
  63. var loader = new THREE.CTMLoader();
  64. loader.createModel( new CTM.File( stream ), function( geometry ) {
  65. geometry.sourceType = "ctm";
  66. geometry.sourceFile = file.name;
  67. var material = new THREE.MeshPhongMaterial();
  68. var mesh = new THREE.Mesh( geometry, material );
  69. mesh.name = filename;
  70. editor.addObject( mesh );
  71. editor.select( mesh );
  72. } );
  73. }, false );
  74. reader.readAsArrayBuffer( file );
  75. break;
  76. case 'dae':
  77. var reader = new FileReader();
  78. reader.addEventListener( 'load', function ( event ) {
  79. var contents = event.target.result;
  80. var parser = new DOMParser();
  81. var xml = parser.parseFromString( contents, 'text/xml' );
  82. var loader = new THREE.ColladaLoader();
  83. var collada = loader.parse( xml );
  84. collada.scene.name = filename;
  85. editor.addObject( collada.scene );
  86. editor.select( collada.scene );
  87. }, false );
  88. reader.readAsText( file );
  89. break;
  90. case 'js':
  91. case 'json':
  92. case '3geo':
  93. case '3mat':
  94. case '3obj':
  95. case '3scn':
  96. var reader = new FileReader();
  97. reader.addEventListener( 'load', function ( event ) {
  98. var contents = event.target.result;
  99. // 2.0
  100. if ( contents.indexOf( 'postMessage' ) !== -1 ) {
  101. var blob = new Blob( [ contents ], { type: 'text/javascript' } );
  102. var url = URL.createObjectURL( blob );
  103. var worker = new Worker( url );
  104. worker.onmessage = function ( event ) {
  105. event.data.metadata = { version: 2 };
  106. handleJSON( event.data, file, filename );
  107. };
  108. worker.postMessage( Date.now() );
  109. return;
  110. }
  111. // >= 3.0
  112. var data;
  113. try {
  114. data = JSON.parse( contents );
  115. } catch ( error ) {
  116. alert( error );
  117. return;
  118. }
  119. handleJSON( data, file, filename );
  120. }, false );
  121. reader.readAsText( file );
  122. break;
  123. case 'md2':
  124. var reader = new FileReader();
  125. reader.addEventListener( 'load', function ( event ) {
  126. var contents = event.target.result;
  127. var geometry = new THREE.MD2Loader().parse( contents );
  128. var material = new THREE.MeshPhongMaterial( {
  129. morphTargets: true,
  130. morphNormals: true
  131. } );
  132. var mesh = new THREE.Mesh( geometry, material );
  133. mesh.mixer = new THREE.AnimationMixer( mesh )
  134. mesh.name = filename;
  135. editor.addObject( mesh );
  136. editor.select( mesh );
  137. }, false );
  138. reader.readAsArrayBuffer( file );
  139. break;
  140. case 'obj':
  141. var reader = new FileReader();
  142. reader.addEventListener( 'load', function ( event ) {
  143. var contents = event.target.result;
  144. var object = new THREE.OBJLoader().parse( contents );
  145. object.name = filename;
  146. editor.addObject( object );
  147. editor.select( object );
  148. }, false );
  149. reader.readAsText( file );
  150. break;
  151. case 'ply':
  152. var reader = new FileReader();
  153. reader.addEventListener( 'load', function ( event ) {
  154. var contents = event.target.result;
  155. var geometry = new THREE.PLYLoader().parse( contents );
  156. geometry.sourceType = "ply";
  157. geometry.sourceFile = file.name;
  158. var material = new THREE.MeshPhongMaterial();
  159. var mesh = new THREE.Mesh( geometry, material );
  160. mesh.name = filename;
  161. editor.addObject( mesh );
  162. editor.select( mesh );
  163. }, false );
  164. reader.readAsText( file );
  165. break;
  166. case 'stl':
  167. var reader = new FileReader();
  168. reader.addEventListener( 'load', function ( event ) {
  169. var contents = event.target.result;
  170. var geometry = new THREE.STLLoader().parse( contents );
  171. geometry.sourceType = "stl";
  172. geometry.sourceFile = file.name;
  173. var material = new THREE.MeshPhongMaterial();
  174. var mesh = new THREE.Mesh( geometry, material );
  175. mesh.name = filename;
  176. editor.addObject( mesh );
  177. editor.select( mesh );
  178. }, false );
  179. if ( reader.readAsBinaryString !== undefined ) {
  180. reader.readAsBinaryString( file );
  181. } else {
  182. reader.readAsArrayBuffer( file );
  183. }
  184. break;
  185. /*
  186. case 'utf8':
  187. var reader = new FileReader();
  188. reader.addEventListener( 'load', function ( event ) {
  189. var contents = event.target.result;
  190. var geometry = new THREE.UTF8Loader().parse( contents );
  191. var material = new THREE.MeshLambertMaterial();
  192. var mesh = new THREE.Mesh( geometry, material );
  193. editor.addObject( mesh );
  194. editor.select( mesh );
  195. }, false );
  196. reader.readAsBinaryString( file );
  197. break;
  198. */
  199. case 'vtk':
  200. var reader = new FileReader();
  201. reader.addEventListener( 'load', function ( event ) {
  202. var contents = event.target.result;
  203. var geometry = new THREE.VTKLoader().parse( contents );
  204. geometry.sourceType = "vtk";
  205. geometry.sourceFile = file.name;
  206. var material = new THREE.MeshPhongMaterial();
  207. var mesh = new THREE.Mesh( geometry, material );
  208. mesh.name = filename;
  209. editor.addObject( mesh );
  210. editor.select( mesh );
  211. }, false );
  212. reader.readAsText( file );
  213. break;
  214. case 'wrl':
  215. var reader = new FileReader();
  216. reader.addEventListener( 'load', function ( event ) {
  217. var contents = event.target.result;
  218. var result = new THREE.VRMLLoader().parse( contents );
  219. editor.setScene( result );
  220. }, false );
  221. reader.readAsText( file );
  222. break;
  223. default:
  224. alert( 'Unsupported file format (' + extension + ').' );
  225. break;
  226. }
  227. }
  228. var handleJSON = function ( data, file, filename ) {
  229. if ( data.metadata === undefined ) { // 2.0
  230. data.metadata = { type: 'Geometry' };
  231. }
  232. if ( data.metadata.type === undefined ) { // 3.0
  233. data.metadata.type = 'Geometry';
  234. }
  235. if ( data.metadata.version === undefined ) {
  236. data.metadata.version = data.metadata.formatVersion;
  237. }
  238. if ( data.metadata.type === 'BufferGeometry' ) {
  239. var loader = new THREE.BufferGeometryLoader();
  240. var result = loader.parse( data );
  241. var mesh = new THREE.Mesh( result );
  242. editor.addObject( mesh );
  243. editor.select( mesh );
  244. } else if ( data.metadata.type.toLowerCase() === 'geometry' ) {
  245. var loader = new THREE.JSONLoader();
  246. loader.setTexturePath( scope.texturePath );
  247. var result = loader.parse( data );
  248. var geometry = result.geometry;
  249. var material;
  250. if ( result.materials !== undefined ) {
  251. if ( result.materials.length > 1 ) {
  252. material = new THREE.MeshFaceMaterial( result.materials );
  253. } else {
  254. material = result.materials[ 0 ];
  255. }
  256. } else {
  257. material = new THREE.MeshPhongMaterial();
  258. }
  259. geometry.sourceType = "ascii";
  260. geometry.sourceFile = file.name;
  261. var mesh;
  262. if ( geometry.animation && geometry.animation.hierarchy ) {
  263. mesh = new THREE.SkinnedMesh( geometry, material );
  264. } else {
  265. mesh = new THREE.Mesh( geometry, material );
  266. }
  267. mesh.name = filename;
  268. editor.addObject( mesh );
  269. editor.select( mesh );
  270. } else if ( data.metadata.type.toLowerCase() === 'object' ) {
  271. var loader = new THREE.ObjectLoader();
  272. loader.setTexturePath( scope.texturePath );
  273. var result = loader.parse( data );
  274. if ( result instanceof THREE.Scene ) {
  275. editor.setScene( result );
  276. } else {
  277. editor.addObject( result );
  278. editor.select( result );
  279. }
  280. } else if ( data.metadata.type.toLowerCase() === 'scene' ) {
  281. // DEPRECATED
  282. var loader = new THREE.SceneLoader();
  283. loader.parse( data, function ( result ) {
  284. editor.setScene( result.scene );
  285. }, '' );
  286. }
  287. };
  288. }