Menubar.File.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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( 'Any unsaved data will be lost. Are you sure?' ) ) {
  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. //
  47. options.add( new UIHorizontalRule() );
  48. // Export DRC
  49. option = new UIRow();
  50. option.setClass( 'option' );
  51. option.setTextContent( strings.getKey( 'menubar/file/export/drc' ) );
  52. option.onClick( async function () {
  53. const object = editor.selected;
  54. if ( object === null || object.isMesh === undefined ) {
  55. alert( 'No mesh selected' );
  56. return;
  57. }
  58. const { DRACOExporter } = await import( 'three/addons/exporters/DRACOExporter.js' );
  59. const exporter = new DRACOExporter();
  60. const options = {
  61. decodeSpeed: 5,
  62. encodeSpeed: 5,
  63. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  64. quantization: [ 16, 8, 8, 8, 8 ],
  65. exportUvs: true,
  66. exportNormals: true,
  67. exportColor: object.geometry.hasAttribute( 'color' )
  68. };
  69. // TODO: Change to DRACOExporter's parse( geometry, onParse )?
  70. const result = exporter.parse( object, options );
  71. saveArrayBuffer( result, 'model.drc' );
  72. } );
  73. options.add( option );
  74. // Export GLB
  75. option = new UIRow();
  76. option.setClass( 'option' );
  77. option.setTextContent( strings.getKey( 'menubar/file/export/glb' ) );
  78. option.onClick( async function () {
  79. const scene = editor.scene;
  80. const animations = getAnimations( scene );
  81. const optimizedAnimations = [];
  82. for ( const animation of animations ) {
  83. optimizedAnimations.push( animation.clone().optimize() );
  84. }
  85. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  86. const exporter = new GLTFExporter();
  87. exporter.parse( scene, function ( result ) {
  88. saveArrayBuffer( result, 'scene.glb' );
  89. }, undefined, { binary: true, animations: optimizedAnimations } );
  90. } );
  91. options.add( option );
  92. // Export GLTF
  93. option = new UIRow();
  94. option.setClass( 'option' );
  95. option.setTextContent( strings.getKey( 'menubar/file/export/gltf' ) );
  96. option.onClick( async function () {
  97. const scene = editor.scene;
  98. const animations = getAnimations( scene );
  99. const optimizedAnimations = [];
  100. for ( const animation of animations ) {
  101. optimizedAnimations.push( animation.clone().optimize() );
  102. }
  103. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  104. const exporter = new GLTFExporter();
  105. exporter.parse( scene, function ( result ) {
  106. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  107. }, undefined, { animations: optimizedAnimations } );
  108. } );
  109. options.add( option );
  110. // Export OBJ
  111. option = new UIRow();
  112. option.setClass( 'option' );
  113. option.setTextContent( strings.getKey( 'menubar/file/export/obj' ) );
  114. option.onClick( async function () {
  115. const object = editor.selected;
  116. if ( object === null ) {
  117. alert( 'No object selected.' );
  118. return;
  119. }
  120. const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
  121. const exporter = new OBJExporter();
  122. saveString( exporter.parse( object ), 'model.obj' );
  123. } );
  124. options.add( option );
  125. // Export PLY (ASCII)
  126. option = new UIRow();
  127. option.setClass( 'option' );
  128. option.setTextContent( strings.getKey( 'menubar/file/export/ply' ) );
  129. option.onClick( async function () {
  130. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  131. const exporter = new PLYExporter();
  132. exporter.parse( editor.scene, function ( result ) {
  133. saveArrayBuffer( result, 'model.ply' );
  134. } );
  135. } );
  136. options.add( option );
  137. // Export PLY (Binary)
  138. option = new UIRow();
  139. option.setClass( 'option' );
  140. option.setTextContent( strings.getKey( 'menubar/file/export/ply_binary' ) );
  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-binary.ply' );
  146. }, { binary: true } );
  147. } );
  148. options.add( option );
  149. // Export STL (ASCII)
  150. option = new UIRow();
  151. option.setClass( 'option' );
  152. option.setTextContent( strings.getKey( 'menubar/file/export/stl' ) );
  153. option.onClick( async function () {
  154. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  155. const exporter = new STLExporter();
  156. saveString( exporter.parse( editor.scene ), 'model.stl' );
  157. } );
  158. options.add( option );
  159. // Export STL (Binary)
  160. option = new UIRow();
  161. option.setClass( 'option' );
  162. option.setTextContent( strings.getKey( 'menubar/file/export/stl_binary' ) );
  163. option.onClick( async function () {
  164. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  165. const exporter = new STLExporter();
  166. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  167. } );
  168. options.add( option );
  169. // Export USDZ
  170. option = new UIRow();
  171. option.setClass( 'option' );
  172. option.setTextContent( strings.getKey( 'menubar/file/export/usdz' ) );
  173. option.onClick( async function () {
  174. const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
  175. const exporter = new USDZExporter();
  176. saveArrayBuffer( await exporter.parseAsync( editor.scene ), 'model.usdz' );
  177. } );
  178. options.add( option );
  179. //
  180. function getAnimations( scene ) {
  181. const animations = [];
  182. scene.traverse( function ( object ) {
  183. animations.push( ... object.animations );
  184. } );
  185. return animations;
  186. }
  187. return container;
  188. }
  189. export { MenubarFile };