Menubar.File.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  2. function MenubarFile( editor ) {
  3. const strings = editor.strings;
  4. const saveArrayBuffer = editor.utils.saveArrayBuffer;
  5. const saveString = editor.utils.saveString;
  6. const container = new UIPanel();
  7. container.setClass( 'menu' );
  8. const title = new UIPanel();
  9. title.setClass( 'title' );
  10. title.setTextContent( strings.getKey( 'menubar/file' ) );
  11. container.add( title );
  12. const options = new UIPanel();
  13. options.setClass( 'options' );
  14. container.add( options );
  15. // New
  16. let option = new UIRow();
  17. option.setClass( 'option' );
  18. option.setTextContent( strings.getKey( 'menubar/file/new' ) );
  19. option.onClick( function () {
  20. if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
  21. editor.clear();
  22. }
  23. } );
  24. options.add( option );
  25. // Import
  26. const form = document.createElement( 'form' );
  27. form.style.display = 'none';
  28. document.body.appendChild( form );
  29. const fileInput = document.createElement( 'input' );
  30. fileInput.multiple = true;
  31. fileInput.type = 'file';
  32. fileInput.accept = '.3dm, .3ds, .3mf, .amf, .dae, .drc, .fbx, .glb, .gltf, .js, .json, .kmz, .ldr, .md2, .mpd, .obj, .pcd, .ply, .stl, .svg, .usdz, .vox, .vtk, .vtp, .wrl, .xyz, .zip';
  33. fileInput.addEventListener( 'change', function () {
  34. editor.loader.loadFiles( fileInput.files );
  35. form.reset();
  36. } );
  37. form.appendChild( fileInput );
  38. option = new UIRow();
  39. option.setClass( 'option' );
  40. option.setTextContent( strings.getKey( 'menubar/file/import' ) );
  41. option.onClick( function () {
  42. fileInput.click();
  43. } );
  44. options.add( option );
  45. // Export
  46. const fileExportSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/file/export' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  47. fileExportSubmenuTitle.onMouseOver( function () {
  48. const { top, right } = this.dom.getBoundingClientRect();
  49. const { paddingTop } = getComputedStyle( this.dom );
  50. fileExportSubmenu.setLeft( right + 'px' );
  51. fileExportSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  52. fileExportSubmenu.setDisplay( 'block' );
  53. } );
  54. fileExportSubmenuTitle.onMouseOut( function () {
  55. fileExportSubmenu.setDisplay( 'none' );
  56. } );
  57. options.add( fileExportSubmenuTitle );
  58. const fileExportSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  59. fileExportSubmenuTitle.add( fileExportSubmenu );
  60. // Export DRC
  61. option = new UIRow();
  62. option.setClass( 'option' );
  63. option.setTextContent( 'DRC' );
  64. option.onClick( async function () {
  65. const object = editor.selected;
  66. if ( object === null || object.isMesh === undefined ) {
  67. alert( strings.getKey( 'prompt/file/export/noMeshSelected' ) );
  68. return;
  69. }
  70. const { DRACOExporter } = await import( 'three/addons/exporters/DRACOExporter.js' );
  71. const exporter = new DRACOExporter();
  72. const options = {
  73. decodeSpeed: 5,
  74. encodeSpeed: 5,
  75. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  76. quantization: [ 16, 8, 8, 8, 8 ],
  77. exportUvs: true,
  78. exportNormals: true,
  79. exportColor: object.geometry.hasAttribute( 'color' )
  80. };
  81. // TODO: Change to DRACOExporter's parse( geometry, onParse )?
  82. const result = exporter.parse( object, options );
  83. saveArrayBuffer( result, 'model.drc' );
  84. } );
  85. fileExportSubmenu.add( option );
  86. // Export GLB
  87. option = new UIRow();
  88. option.setClass( 'option' );
  89. option.setTextContent( 'GLB' );
  90. option.onClick( async function () {
  91. const scene = editor.scene;
  92. const animations = getAnimations( scene );
  93. const optimizedAnimations = [];
  94. for ( const animation of animations ) {
  95. optimizedAnimations.push( animation.clone().optimize() );
  96. }
  97. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  98. const exporter = new GLTFExporter();
  99. exporter.parse( scene, function ( result ) {
  100. saveArrayBuffer( result, 'scene.glb' );
  101. }, undefined, { binary: true, animations: optimizedAnimations } );
  102. } );
  103. fileExportSubmenu.add( option );
  104. // Export GLTF
  105. option = new UIRow();
  106. option.setClass( 'option' );
  107. option.setTextContent( 'GLTF' );
  108. option.onClick( async function () {
  109. const scene = editor.scene;
  110. const animations = getAnimations( scene );
  111. const optimizedAnimations = [];
  112. for ( const animation of animations ) {
  113. optimizedAnimations.push( animation.clone().optimize() );
  114. }
  115. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  116. const exporter = new GLTFExporter();
  117. exporter.parse( scene, function ( result ) {
  118. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  119. }, undefined, { animations: optimizedAnimations } );
  120. } );
  121. fileExportSubmenu.add( option );
  122. // Export OBJ
  123. option = new UIRow();
  124. option.setClass( 'option' );
  125. option.setTextContent( 'OBJ' );
  126. option.onClick( async function () {
  127. const object = editor.selected;
  128. if ( object === null ) {
  129. alert( strings.getKey( 'prompt/file/export/noObjectSelected' ) );
  130. return;
  131. }
  132. const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
  133. const exporter = new OBJExporter();
  134. saveString( exporter.parse( object ), 'model.obj' );
  135. } );
  136. fileExportSubmenu.add( option );
  137. // Export PLY (ASCII)
  138. option = new UIRow();
  139. option.setClass( 'option' );
  140. option.setTextContent( 'PLY' );
  141. option.onClick( async function () {
  142. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  143. const exporter = new PLYExporter();
  144. exporter.parse( editor.scene, function ( result ) {
  145. saveArrayBuffer( result, 'model.ply' );
  146. } );
  147. } );
  148. fileExportSubmenu.add( option );
  149. // Export PLY (BINARY)
  150. option = new UIRow();
  151. option.setClass( 'option' );
  152. option.setTextContent( 'PLY (BINARY)' );
  153. option.onClick( async function () {
  154. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  155. const exporter = new PLYExporter();
  156. exporter.parse( editor.scene, function ( result ) {
  157. saveArrayBuffer( result, 'model-binary.ply' );
  158. }, { binary: true } );
  159. } );
  160. fileExportSubmenu.add( option );
  161. // Export STL (ASCII)
  162. option = new UIRow();
  163. option.setClass( 'option' );
  164. option.setTextContent( 'STL' );
  165. option.onClick( async function () {
  166. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  167. const exporter = new STLExporter();
  168. saveString( exporter.parse( editor.scene ), 'model.stl' );
  169. } );
  170. fileExportSubmenu.add( option );
  171. // Export STL (BINARY)
  172. option = new UIRow();
  173. option.setClass( 'option' );
  174. option.setTextContent( 'STL (BINARY)' );
  175. option.onClick( async function () {
  176. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  177. const exporter = new STLExporter();
  178. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  179. } );
  180. fileExportSubmenu.add( option );
  181. // Export USDZ
  182. option = new UIRow();
  183. option.setClass( 'option' );
  184. option.setTextContent( 'USDZ' );
  185. option.onClick( async function () {
  186. const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
  187. const exporter = new USDZExporter();
  188. saveArrayBuffer( await exporter.parseAsync( editor.scene ), 'model.usdz' );
  189. } );
  190. fileExportSubmenu.add( option );
  191. //
  192. function getAnimations( scene ) {
  193. const animations = [];
  194. scene.traverse( function ( object ) {
  195. animations.push( ... object.animations );
  196. } );
  197. return animations;
  198. }
  199. return container;
  200. }
  201. export { MenubarFile };