Menubar.File.js 7.1 KB

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