Loader.js 9.7 KB

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