Menubar.File.js 7.2 KB

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