Loader.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. import * as THREE from 'three';
  2. import { TGALoader } from 'three/addons/loaders/TGALoader.js';
  3. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  4. import { SetSceneCommand } from './commands/SetSceneCommand.js';
  5. import { LoaderUtils } from './LoaderUtils.js';
  6. import { unzipSync, strFromU8 } from 'three/addons/libs/fflate.module.js';
  7. function Loader( editor ) {
  8. const scope = this;
  9. this.texturePath = '';
  10. this.loadItemList = function ( items ) {
  11. LoaderUtils.getFilesFromItemList( items, function ( files, filesMap ) {
  12. scope.loadFiles( files, filesMap );
  13. } );
  14. };
  15. this.loadFiles = function ( files, filesMap ) {
  16. if ( files.length > 0 ) {
  17. filesMap = filesMap || LoaderUtils.createFilesMap( files );
  18. const manager = new THREE.LoadingManager();
  19. manager.setURLModifier( function ( url ) {
  20. url = url.replace( /^(\.?\/)/, '' ); // remove './'
  21. const file = filesMap[ url ];
  22. if ( file ) {
  23. console.log( 'Loading', url );
  24. return URL.createObjectURL( file );
  25. }
  26. return url;
  27. } );
  28. manager.addHandler( /\.tga$/i, new TGALoader() );
  29. for ( let i = 0; i < files.length; i ++ ) {
  30. scope.loadFile( files[ i ], manager );
  31. }
  32. }
  33. };
  34. this.loadFile = function ( file, manager ) {
  35. const filename = file.name;
  36. const extension = filename.split( '.' ).pop().toLowerCase();
  37. const reader = new FileReader();
  38. reader.addEventListener( 'progress', function ( event ) {
  39. const size = '(' + Math.floor( event.total / 1000 ).format() + ' KB)';
  40. const progress = Math.floor( ( event.loaded / event.total ) * 100 ) + '%';
  41. console.log( 'Loading', filename, size, progress );
  42. } );
  43. switch ( extension ) {
  44. case '3dm':
  45. {
  46. reader.addEventListener( 'load', async function ( event ) {
  47. const contents = event.target.result;
  48. const { Rhino3dmLoader } = await import( 'three/addons/loaders/3DMLoader.js' );
  49. const loader = new Rhino3dmLoader();
  50. loader.setLibraryPath( '../examples/jsm/libs/rhino3dm/' );
  51. loader.parse( contents, function ( object ) {
  52. object.name = filename;
  53. editor.execute( new AddObjectCommand( editor, object ) );
  54. }, function ( error ) {
  55. console.error( error )
  56. } );
  57. }, false );
  58. reader.readAsArrayBuffer( file );
  59. break;
  60. }
  61. case '3ds':
  62. {
  63. reader.addEventListener( 'load', async function ( event ) {
  64. const { TDSLoader } = await import( 'three/addons/loaders/TDSLoader.js' );
  65. const loader = new TDSLoader();
  66. const object = loader.parse( event.target.result );
  67. editor.execute( new AddObjectCommand( editor, object ) );
  68. }, false );
  69. reader.readAsArrayBuffer( file );
  70. break;
  71. }
  72. case '3mf':
  73. {
  74. reader.addEventListener( 'load', async function ( event ) {
  75. const { ThreeMFLoader } = await import( 'three/addons/loaders/3MFLoader.js' );
  76. const loader = new ThreeMFLoader();
  77. const object = loader.parse( event.target.result );
  78. editor.execute( new AddObjectCommand( editor, object ) );
  79. }, false );
  80. reader.readAsArrayBuffer( file );
  81. break;
  82. }
  83. case 'amf':
  84. {
  85. reader.addEventListener( 'load', async function ( event ) {
  86. const { AMFLoader } = await import( 'three/addons/loaders/AMFLoader.js' );
  87. const loader = new AMFLoader();
  88. const amfobject = loader.parse( event.target.result );
  89. editor.execute( new AddObjectCommand( editor, amfobject ) );
  90. }, false );
  91. reader.readAsArrayBuffer( file );
  92. break;
  93. }
  94. case 'dae':
  95. {
  96. reader.addEventListener( 'load', async function ( event ) {
  97. const contents = event.target.result;
  98. const { ColladaLoader } = await import( 'three/addons/loaders/ColladaLoader.js' );
  99. const loader = new ColladaLoader( manager );
  100. const collada = loader.parse( contents );
  101. collada.scene.name = filename;
  102. editor.execute( new AddObjectCommand( editor, collada.scene ) );
  103. }, false );
  104. reader.readAsText( file );
  105. break;
  106. }
  107. case 'drc':
  108. {
  109. reader.addEventListener( 'load', async function ( event ) {
  110. const contents = event.target.result;
  111. const { DRACOLoader } = await import( 'three/addons/loaders/DRACOLoader.js' );
  112. const loader = new DRACOLoader();
  113. loader.setDecoderPath( '../examples/jsm/libs/draco/' );
  114. loader.parse( contents, function ( geometry ) {
  115. let object;
  116. if ( geometry.index !== null ) {
  117. const material = new THREE.MeshStandardMaterial();
  118. object = new THREE.Mesh( geometry, material );
  119. object.name = filename;
  120. } else {
  121. const material = new THREE.PointsMaterial( { size: 0.01 } );
  122. material.vertexColors = geometry.hasAttribute( 'color' );
  123. object = new THREE.Points( geometry, material );
  124. object.name = filename;
  125. }
  126. loader.dispose();
  127. editor.execute( new AddObjectCommand( editor, object ) );
  128. } );
  129. }, false );
  130. reader.readAsArrayBuffer( file );
  131. break;
  132. }
  133. case 'fbx':
  134. {
  135. reader.addEventListener( 'load', async function ( event ) {
  136. const contents = event.target.result;
  137. const { FBXLoader } = await import( 'three/addons/loaders/FBXLoader.js' );
  138. const loader = new FBXLoader( manager );
  139. const object = loader.parse( contents );
  140. editor.execute( new AddObjectCommand( editor, object ) );
  141. }, false );
  142. reader.readAsArrayBuffer( file );
  143. break;
  144. }
  145. case 'glb':
  146. {
  147. reader.addEventListener( 'load', async function ( event ) {
  148. const contents = event.target.result;
  149. const { GLTFLoader } = await import( 'three/addons/loaders/GLTFLoader.js' );
  150. const { DRACOLoader } = await import( 'three/addons/loaders/DRACOLoader.js' );
  151. const { KTX2Loader } = await import( 'three/addons/loaders/KTX2Loader.js' );
  152. const { MeshoptDecoder } = await import( 'three/addons/libs/meshopt_decoder.module.js' );
  153. const dracoLoader = new DRACOLoader();
  154. dracoLoader.setDecoderPath( '../examples/jsm/libs/draco/gltf/' );
  155. const ktx2Loader = new KTX2Loader();
  156. ktx2Loader.setTranscoderPath( '../examples/jsm/libs/basis/' );
  157. editor.signals.rendererDetectKTX2Support.dispatch( ktx2Loader );
  158. const loader = new GLTFLoader();
  159. loader.setDRACOLoader( dracoLoader );
  160. loader.setKTX2Loader( ktx2Loader );
  161. loader.setMeshoptDecoder( MeshoptDecoder );
  162. loader.parse( contents, '', function ( result ) {
  163. const scene = result.scene;
  164. scene.name = filename;
  165. scene.animations.push( ...result.animations );
  166. editor.execute( new AddObjectCommand( editor, scene ) );
  167. dracoLoader.dispose();
  168. } );
  169. }, false );
  170. reader.readAsArrayBuffer( file );
  171. break;
  172. }
  173. case 'gltf':
  174. {
  175. reader.addEventListener( 'load', async function ( event ) {
  176. const contents = event.target.result;
  177. const { DRACOLoader } = await import( 'three/addons/loaders/DRACOLoader.js' );
  178. const { GLTFLoader } = await import( 'three/addons/loaders/GLTFLoader.js' );
  179. const { KTX2Loader } = await import( 'three/addons/loaders/KTX2Loader.js' );
  180. const { MeshoptDecoder } = await import( 'three/addons/libs/meshopt_decoder.module.js' );
  181. const dracoLoader = new DRACOLoader();
  182. dracoLoader.setDecoderPath( '../examples/jsm/libs/draco/gltf/' );
  183. const ktx2Loader = new KTX2Loader();
  184. ktx2Loader.setTranscoderPath( '../examples/jsm/libs/basis/' );
  185. editor.signals.rendererDetectKTX2Support.dispatch( ktx2Loader );
  186. const loader = new GLTFLoader( manager );
  187. loader.setDRACOLoader( dracoLoader );
  188. loader.setKTX2Loader( ktx2Loader );
  189. loader.setMeshoptDecoder( MeshoptDecoder );
  190. loader.parse( contents, '', function ( result ) {
  191. const scene = result.scene;
  192. scene.name = filename;
  193. scene.animations.push( ...result.animations );
  194. editor.execute( new AddObjectCommand( editor, scene ) );
  195. dracoLoader.dispose();
  196. } );
  197. }, false );
  198. reader.readAsArrayBuffer( file );
  199. break;
  200. }
  201. case 'js':
  202. case 'json':
  203. {
  204. reader.addEventListener( 'load', function ( event ) {
  205. const contents = event.target.result;
  206. // 2.0
  207. if ( contents.indexOf( 'postMessage' ) !== - 1 ) {
  208. const blob = new Blob( [ contents ], { type: 'text/javascript' } );
  209. const url = URL.createObjectURL( blob );
  210. const worker = new Worker( url );
  211. worker.onmessage = function ( event ) {
  212. event.data.metadata = { version: 2 };
  213. handleJSON( event.data );
  214. };
  215. worker.postMessage( Date.now() );
  216. return;
  217. }
  218. // >= 3.0
  219. let data;
  220. try {
  221. data = JSON.parse( contents );
  222. } catch ( error ) {
  223. alert( error );
  224. return;
  225. }
  226. handleJSON( data );
  227. }, false );
  228. reader.readAsText( file );
  229. break;
  230. }
  231. case 'ifc':
  232. {
  233. reader.addEventListener( 'load', async function ( event ) {
  234. const { IFCLoader } = await import( 'three/addons/loaders/IFCLoader.js' );
  235. var loader = new IFCLoader();
  236. loader.ifcManager.setWasmPath( 'three/addons/loaders/ifc/' );
  237. const model = await loader.parse( event.target.result );
  238. model.mesh.name = filename;
  239. editor.execute( new AddObjectCommand( editor, model.mesh ) );
  240. }, false );
  241. reader.readAsArrayBuffer( file );
  242. break;
  243. }
  244. case 'kmz':
  245. {
  246. reader.addEventListener( 'load', async function ( event ) {
  247. const { KMZLoader } = await import( 'three/addons/loaders/KMZLoader.js' );
  248. const loader = new KMZLoader();
  249. const collada = loader.parse( event.target.result );
  250. collada.scene.name = filename;
  251. editor.execute( new AddObjectCommand( editor, collada.scene ) );
  252. }, false );
  253. reader.readAsArrayBuffer( file );
  254. break;
  255. }
  256. case 'ldr':
  257. case 'mpd':
  258. {
  259. reader.addEventListener( 'load', async function ( event ) {
  260. const { LDrawLoader } = await import( 'three/addons/loaders/LDrawLoader.js' );
  261. const loader = new LDrawLoader();
  262. loader.setPath( '../../examples/models/ldraw/officialLibrary/' );
  263. loader.parse( event.target.result, function ( group ) {
  264. group.name = filename;
  265. // Convert from LDraw coordinates: rotate 180 degrees around OX
  266. group.rotation.x = Math.PI;
  267. editor.execute( new AddObjectCommand( editor, group ) );
  268. } );
  269. }, false );
  270. reader.readAsText( file );
  271. break;
  272. }
  273. case 'md2':
  274. {
  275. reader.addEventListener( 'load', async function ( event ) {
  276. const contents = event.target.result;
  277. const { MD2Loader } = await import( 'three/addons/loaders/MD2Loader.js' );
  278. const geometry = new MD2Loader().parse( contents );
  279. const material = new THREE.MeshStandardMaterial();
  280. const mesh = new THREE.Mesh( geometry, material );
  281. mesh.mixer = new THREE.AnimationMixer( mesh );
  282. mesh.name = filename;
  283. mesh.animations.push( ...geometry.animations );
  284. editor.execute( new AddObjectCommand( editor, mesh ) );
  285. }, false );
  286. reader.readAsArrayBuffer( file );
  287. break;
  288. }
  289. case 'obj':
  290. {
  291. reader.addEventListener( 'load', async function ( event ) {
  292. const contents = event.target.result;
  293. const { OBJLoader } = await import( 'three/addons/loaders/OBJLoader.js' );
  294. const object = new OBJLoader().parse( contents );
  295. object.name = filename;
  296. editor.execute( new AddObjectCommand( editor, object ) );
  297. }, false );
  298. reader.readAsText( file );
  299. break;
  300. }
  301. case 'pcd':
  302. {
  303. reader.addEventListener( 'load', async function ( event ) {
  304. const contents = event.target.result;
  305. const { PCDLoader } = await import( '../../examples/jsm/loaders/PCDLoader.js' );
  306. const points = new PCDLoader().parse( contents );
  307. points.name = filename;
  308. editor.execute( new AddObjectCommand( editor, points ) );
  309. }, false );
  310. reader.readAsArrayBuffer( file );
  311. break;
  312. }
  313. case 'ply':
  314. {
  315. reader.addEventListener( 'load', async function ( event ) {
  316. const contents = event.target.result;
  317. const { PLYLoader } = await import( 'three/addons/loaders/PLYLoader.js' );
  318. const geometry = new PLYLoader().parse( contents );
  319. let object;
  320. if ( geometry.index !== null ) {
  321. const material = new THREE.MeshStandardMaterial();
  322. object = new THREE.Mesh( geometry, material );
  323. object.name = filename;
  324. } else {
  325. const material = new THREE.PointsMaterial( { size: 0.01 } );
  326. material.vertexColors = geometry.hasAttribute( 'color' );
  327. object = new THREE.Points( geometry, material );
  328. object.name = filename;
  329. }
  330. editor.execute( new AddObjectCommand( editor, object ) );
  331. }, false );
  332. reader.readAsArrayBuffer( file );
  333. break;
  334. }
  335. case 'stl':
  336. {
  337. reader.addEventListener( 'load', async function ( event ) {
  338. const contents = event.target.result;
  339. const { STLLoader } = await import( 'three/addons/loaders/STLLoader.js' );
  340. const geometry = new STLLoader().parse( contents );
  341. const material = new THREE.MeshStandardMaterial();
  342. const mesh = new THREE.Mesh( geometry, material );
  343. mesh.name = filename;
  344. editor.execute( new AddObjectCommand( editor, mesh ) );
  345. }, false );
  346. if ( reader.readAsBinaryString !== undefined ) {
  347. reader.readAsBinaryString( file );
  348. } else {
  349. reader.readAsArrayBuffer( file );
  350. }
  351. break;
  352. }
  353. case 'svg':
  354. {
  355. reader.addEventListener( 'load', async function ( event ) {
  356. const contents = event.target.result;
  357. const { SVGLoader } = await import( 'three/addons/loaders/SVGLoader.js' );
  358. const loader = new SVGLoader();
  359. const paths = loader.parse( contents ).paths;
  360. //
  361. const group = new THREE.Group();
  362. group.scale.multiplyScalar( 0.1 );
  363. group.scale.y *= - 1;
  364. for ( let i = 0; i < paths.length; i ++ ) {
  365. const path = paths[ i ];
  366. const material = new THREE.MeshBasicMaterial( {
  367. color: path.color,
  368. depthWrite: false
  369. } );
  370. const shapes = SVGLoader.createShapes( path );
  371. for ( let j = 0; j < shapes.length; j ++ ) {
  372. const shape = shapes[ j ];
  373. const geometry = new THREE.ShapeGeometry( shape );
  374. const mesh = new THREE.Mesh( geometry, material );
  375. group.add( mesh );
  376. }
  377. }
  378. editor.execute( new AddObjectCommand( editor, group ) );
  379. }, false );
  380. reader.readAsText( file );
  381. break;
  382. }
  383. case 'usdz':
  384. {
  385. reader.addEventListener( 'load', async function ( event ) {
  386. const contents = event.target.result;
  387. const { USDZLoader } = await import( '../../examples/jsm/loaders/USDZLoader.js' );
  388. const group = new USDZLoader().parse( contents );
  389. group.name = filename;
  390. editor.execute( new AddObjectCommand( editor, group ) );
  391. }, false );
  392. reader.readAsArrayBuffer( file );
  393. break;
  394. }
  395. case 'vox':
  396. {
  397. reader.addEventListener( 'load', async function ( event ) {
  398. const contents = event.target.result;
  399. const { VOXLoader, VOXMesh } = await import( 'three/addons/loaders/VOXLoader.js' );
  400. const chunks = new VOXLoader().parse( contents );
  401. const group = new THREE.Group();
  402. group.name = filename;
  403. for ( let i = 0; i < chunks.length; i ++ ) {
  404. const chunk = chunks[ i ];
  405. const mesh = new VOXMesh( chunk );
  406. group.add( mesh );
  407. }
  408. editor.execute( new AddObjectCommand( editor, group ) );
  409. }, false );
  410. reader.readAsArrayBuffer( file );
  411. break;
  412. }
  413. case 'vtk':
  414. case 'vtp':
  415. {
  416. reader.addEventListener( 'load', async function ( event ) {
  417. const contents = event.target.result;
  418. const { VTKLoader } = await import( 'three/addons/loaders/VTKLoader.js' );
  419. const geometry = new VTKLoader().parse( contents );
  420. const material = new THREE.MeshStandardMaterial();
  421. const mesh = new THREE.Mesh( geometry, material );
  422. mesh.name = filename;
  423. editor.execute( new AddObjectCommand( editor, mesh ) );
  424. }, false );
  425. reader.readAsArrayBuffer( file );
  426. break;
  427. }
  428. case 'wrl':
  429. {
  430. reader.addEventListener( 'load', async function ( event ) {
  431. const contents = event.target.result;
  432. const { VRMLLoader } = await import( 'three/addons/loaders/VRMLLoader.js' );
  433. const result = new VRMLLoader().parse( contents );
  434. editor.execute( new SetSceneCommand( editor, result ) );
  435. }, false );
  436. reader.readAsText( file );
  437. break;
  438. }
  439. case 'xyz':
  440. {
  441. reader.addEventListener( 'load', async function ( event ) {
  442. const contents = event.target.result;
  443. const { XYZLoader } = await import( 'three/addons/loaders/XYZLoader.js' );
  444. const geometry = new XYZLoader().parse( contents );
  445. const material = new THREE.PointsMaterial();
  446. material.vertexColors = geometry.hasAttribute( 'color' );
  447. const points = new THREE.Points( geometry, material );
  448. points.name = filename;
  449. editor.execute( new AddObjectCommand( editor, points ) );
  450. }, false );
  451. reader.readAsText( file );
  452. break;
  453. }
  454. case 'zip':
  455. {
  456. reader.addEventListener( 'load', function ( event ) {
  457. handleZIP( event.target.result );
  458. }, false );
  459. reader.readAsArrayBuffer( file );
  460. break;
  461. }
  462. default:
  463. console.error( 'Unsupported file format (' + extension + ').' );
  464. break;
  465. }
  466. };
  467. function handleJSON( data ) {
  468. if ( data.metadata === undefined ) { // 2.0
  469. data.metadata = { type: 'Geometry' };
  470. }
  471. if ( data.metadata.type === undefined ) { // 3.0
  472. data.metadata.type = 'Geometry';
  473. }
  474. if ( data.metadata.formatVersion !== undefined ) {
  475. data.metadata.version = data.metadata.formatVersion;
  476. }
  477. switch ( data.metadata.type.toLowerCase() ) {
  478. case 'buffergeometry':
  479. {
  480. const loader = new THREE.BufferGeometryLoader();
  481. const result = loader.parse( data );
  482. const mesh = new THREE.Mesh( result );
  483. editor.execute( new AddObjectCommand( editor, mesh ) );
  484. break;
  485. }
  486. case 'geometry':
  487. console.error( 'Loader: "Geometry" is no longer supported.' );
  488. break;
  489. case 'object':
  490. {
  491. const loader = new THREE.ObjectLoader();
  492. loader.setResourcePath( scope.texturePath );
  493. loader.parse( data, function ( result ) {
  494. if ( result.isScene ) {
  495. editor.execute( new SetSceneCommand( editor, result ) );
  496. } else {
  497. editor.execute( new AddObjectCommand( editor, result ) );
  498. }
  499. } );
  500. break;
  501. }
  502. case 'app':
  503. editor.fromJSON( data );
  504. break;
  505. }
  506. }
  507. async function handleZIP( contents ) {
  508. const zip = unzipSync( new Uint8Array( contents ) );
  509. // Poly
  510. if ( zip[ 'model.obj' ] && zip[ 'materials.mtl' ] ) {
  511. const { MTLLoader } = await import( 'three/addons/loaders/MTLLoader.js' );
  512. const { OBJLoader } = await import( 'three/addons/loaders/OBJLoader.js' );
  513. const materials = new MTLLoader().parse( strFromU8( zip[ 'materials.mtl' ] ) );
  514. const object = new OBJLoader().setMaterials( materials ).parse( strFromU8( zip[ 'model.obj' ] ) );
  515. editor.execute( new AddObjectCommand( editor, object ) );
  516. }
  517. //
  518. for ( const path in zip ) {
  519. const file = zip[ path ];
  520. const manager = new THREE.LoadingManager();
  521. manager.setURLModifier( function ( url ) {
  522. const file = zip[ url ];
  523. if ( file ) {
  524. console.log( 'Loading', url );
  525. const blob = new Blob( [ file.buffer ], { type: 'application/octet-stream' } );
  526. return URL.createObjectURL( blob );
  527. }
  528. return url;
  529. } );
  530. const extension = path.split( '.' ).pop().toLowerCase();
  531. switch ( extension ) {
  532. case 'fbx':
  533. {
  534. const { FBXLoader } = await import( 'three/addons/loaders/FBXLoader.js' );
  535. const loader = new FBXLoader( manager );
  536. const object = loader.parse( file.buffer );
  537. editor.execute( new AddObjectCommand( editor, object ) );
  538. break;
  539. }
  540. case 'glb':
  541. {
  542. const { GLTFLoader } = await import( 'three/addons/loaders/GLTFLoader.js' );
  543. const { DRACOLoader } = await import( 'three/addons/loaders/DRACOLoader.js' );
  544. const { KTX2Loader } = await import( 'three/addons/loaders/KTX2Loader.js' );
  545. const { MeshoptDecoder } = await import( 'three/addons/libs/meshopt_decoder.module.js' );
  546. const dracoLoader = new DRACOLoader();
  547. dracoLoader.setDecoderPath( '../examples/jsm/libs/draco/gltf/' );
  548. const ktx2Loader = new KTX2Loader();
  549. ktx2Loader.setTranscoderPath( '../examples/jsm/libs/basis/' );
  550. editor.signals.rendererDetectKTX2Support.dispatch( ktx2Loader );
  551. const loader = new GLTFLoader();
  552. loader.setDRACOLoader( dracoLoader );
  553. loader.setKTX2Loader( ktx2Loader );
  554. loader.setMeshoptDecoder( MeshoptDecoder );
  555. loader.parse( file.buffer, '', function ( result ) {
  556. const scene = result.scene;
  557. scene.animations.push( ...result.animations );
  558. editor.execute( new AddObjectCommand( editor, scene ) );
  559. dracoLoader.dispose();
  560. } );
  561. break;
  562. }
  563. case 'gltf':
  564. {
  565. const { GLTFLoader } = await import( 'three/addons/loaders/GLTFLoader.js' );
  566. const { DRACOLoader } = await import( 'three/addons/loaders/DRACOLoader.js' );
  567. const { KTX2Loader } = await import( 'three/addons/loaders/KTX2Loader.js' );
  568. const { MeshoptDecoder } = await import( 'three/addons/libs/meshopt_decoder.module.js' );
  569. const dracoLoader = new DRACOLoader();
  570. dracoLoader.setDecoderPath( '../examples/jsm/libs/draco/gltf/' );
  571. const ktx2Loader = new KTX2Loader();
  572. ktx2Loader.setTranscoderPath( '../examples/jsm/libs/basis/' );
  573. editor.signals.rendererDetectKTX2Support.dispatch( ktx2Loader );
  574. const loader = new GLTFLoader();
  575. loader.setDRACOLoader( dracoLoader );
  576. loader.setKTX2Loader( ktx2Loader );
  577. loader.setMeshoptDecoder( MeshoptDecoder );
  578. loader.parse( strFromU8( file ), '', function ( result ) {
  579. const scene = result.scene;
  580. scene.animations.push( ...result.animations );
  581. editor.execute( new AddObjectCommand( editor, scene ) );
  582. dracoLoader.dispose();
  583. } );
  584. break;
  585. }
  586. }
  587. }
  588. }
  589. }
  590. export { Loader };