Menubar.File.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  2. import { Loader } from './Loader.js';
  3. function MenubarFile( editor ) {
  4. const strings = editor.strings;
  5. const saveArrayBuffer = editor.utils.saveArrayBuffer;
  6. const saveString = editor.utils.saveString;
  7. const container = new UIPanel();
  8. container.setClass( 'menu' );
  9. const title = new UIPanel();
  10. title.setClass( 'title' );
  11. title.setTextContent( strings.getKey( 'menubar/file' ) );
  12. container.add( title );
  13. const options = new UIPanel();
  14. options.setClass( 'options' );
  15. container.add( options );
  16. // New Project
  17. const newProjectSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/file/newProject' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  18. newProjectSubmenuTitle.onMouseOver( function () {
  19. const { top, right } = this.dom.getBoundingClientRect();
  20. const { paddingTop } = getComputedStyle( this.dom );
  21. newProjectSubmenu.setLeft( right + 'px' );
  22. newProjectSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  23. newProjectSubmenu.setDisplay( 'block' );
  24. } );
  25. newProjectSubmenuTitle.onMouseOut( function () {
  26. newProjectSubmenu.setDisplay( 'none' );
  27. } );
  28. options.add( newProjectSubmenuTitle );
  29. const newProjectSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  30. newProjectSubmenuTitle.add( newProjectSubmenu );
  31. // New Project / Empty
  32. let option = new UIRow().setTextContent( strings.getKey( 'menubar/file/newProject/empty' ) ).setClass( 'option' );
  33. option.onClick( function () {
  34. if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
  35. editor.clear();
  36. }
  37. } );
  38. newProjectSubmenu.add( option );
  39. //
  40. newProjectSubmenu.add( new UIHorizontalRule() );
  41. // New Project / ...
  42. const examples = [
  43. { title: 'menubar/file/newProject/Arkanoid', file: 'arkanoid.app.json' },
  44. { title: 'menubar/file/newProject/Camera', file: 'camera.app.json' },
  45. { title: 'menubar/file/newProject/Particles', file: 'particles.app.json' },
  46. { title: 'menubar/file/newProject/Pong', file: 'pong.app.json' },
  47. { title: 'menubar/file/newProject/Shaders', file: 'shaders.app.json' }
  48. ];
  49. const loader = new THREE.FileLoader();
  50. for ( let i = 0; i < examples.length; i ++ ) {
  51. ( function ( i ) {
  52. const example = examples[ i ];
  53. const option = new UIRow();
  54. option.setClass( 'option' );
  55. option.setTextContent( strings.getKey( example.title ) );
  56. option.onClick( function () {
  57. if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
  58. loader.load( 'examples/' + example.file, function ( text ) {
  59. editor.clear();
  60. editor.fromJSON( JSON.parse( text ) );
  61. } );
  62. }
  63. } );
  64. newProjectSubmenu.add( option );
  65. } )( i );
  66. }
  67. options.add( new UIHorizontalRule() );
  68. // Import
  69. const form = document.createElement( 'form' );
  70. form.style.display = 'none';
  71. document.body.appendChild( form );
  72. const fileInput = document.createElement( 'input' );
  73. fileInput.multiple = true;
  74. fileInput.type = 'file';
  75. fileInput.accept = Loader.getSupportedFileFormats().map( format => '.' + format ).join( ', ' );
  76. fileInput.addEventListener( 'change', function () {
  77. editor.loader.loadFiles( fileInput.files );
  78. form.reset();
  79. } );
  80. form.appendChild( fileInput );
  81. option = new UIRow();
  82. option.setClass( 'option' );
  83. option.setTextContent( strings.getKey( 'menubar/file/import' ) );
  84. option.onClick( function () {
  85. fileInput.click();
  86. } );
  87. options.add( option );
  88. // Export
  89. const fileExportSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/file/export' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  90. fileExportSubmenuTitle.onMouseOver( function () {
  91. const { top, right } = this.dom.getBoundingClientRect();
  92. const { paddingTop } = getComputedStyle( this.dom );
  93. fileExportSubmenu.setLeft( right + 'px' );
  94. fileExportSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  95. fileExportSubmenu.setDisplay( 'block' );
  96. } );
  97. fileExportSubmenuTitle.onMouseOut( function () {
  98. fileExportSubmenu.setDisplay( 'none' );
  99. } );
  100. options.add( fileExportSubmenuTitle );
  101. const fileExportSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  102. fileExportSubmenuTitle.add( fileExportSubmenu );
  103. // Export DRC
  104. option = new UIRow();
  105. option.setClass( 'option' );
  106. option.setTextContent( 'DRC' );
  107. option.onClick( async function () {
  108. const object = editor.selected;
  109. if ( object === null || object.isMesh === undefined ) {
  110. alert( strings.getKey( 'prompt/file/export/noMeshSelected' ) );
  111. return;
  112. }
  113. const { DRACOExporter } = await import( 'three/addons/exporters/DRACOExporter.js' );
  114. const exporter = new DRACOExporter();
  115. const options = {
  116. decodeSpeed: 5,
  117. encodeSpeed: 5,
  118. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  119. quantization: [ 16, 8, 8, 8, 8 ],
  120. exportUvs: true,
  121. exportNormals: true,
  122. exportColor: object.geometry.hasAttribute( 'color' )
  123. };
  124. // TODO: Change to DRACOExporter's parse( geometry, onParse )?
  125. const result = exporter.parse( object, options );
  126. saveArrayBuffer( result, 'model.drc' );
  127. } );
  128. fileExportSubmenu.add( option );
  129. // Export GLB
  130. option = new UIRow();
  131. option.setClass( 'option' );
  132. option.setTextContent( 'GLB' );
  133. option.onClick( async function () {
  134. const scene = editor.scene;
  135. const animations = getAnimations( scene );
  136. const optimizedAnimations = [];
  137. for ( const animation of animations ) {
  138. optimizedAnimations.push( animation.clone().optimize() );
  139. }
  140. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  141. const exporter = new GLTFExporter();
  142. exporter.parse( scene, function ( result ) {
  143. saveArrayBuffer( result, 'scene.glb' );
  144. }, undefined, { binary: true, animations: optimizedAnimations } );
  145. } );
  146. fileExportSubmenu.add( option );
  147. // Export GLTF
  148. option = new UIRow();
  149. option.setClass( 'option' );
  150. option.setTextContent( 'GLTF' );
  151. option.onClick( async function () {
  152. const scene = editor.scene;
  153. const animations = getAnimations( scene );
  154. const optimizedAnimations = [];
  155. for ( const animation of animations ) {
  156. optimizedAnimations.push( animation.clone().optimize() );
  157. }
  158. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  159. const exporter = new GLTFExporter();
  160. exporter.parse( scene, function ( result ) {
  161. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  162. }, undefined, { animations: optimizedAnimations } );
  163. } );
  164. fileExportSubmenu.add( option );
  165. // Export OBJ
  166. option = new UIRow();
  167. option.setClass( 'option' );
  168. option.setTextContent( 'OBJ' );
  169. option.onClick( async function () {
  170. const object = editor.selected;
  171. if ( object === null ) {
  172. alert( strings.getKey( 'prompt/file/export/noObjectSelected' ) );
  173. return;
  174. }
  175. const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
  176. const exporter = new OBJExporter();
  177. saveString( exporter.parse( object ), 'model.obj' );
  178. } );
  179. fileExportSubmenu.add( option );
  180. // Export PLY (ASCII)
  181. option = new UIRow();
  182. option.setClass( 'option' );
  183. option.setTextContent( 'PLY' );
  184. option.onClick( async function () {
  185. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  186. const exporter = new PLYExporter();
  187. exporter.parse( editor.scene, function ( result ) {
  188. saveArrayBuffer( result, 'model.ply' );
  189. } );
  190. } );
  191. fileExportSubmenu.add( option );
  192. // Export PLY (BINARY)
  193. option = new UIRow();
  194. option.setClass( 'option' );
  195. option.setTextContent( 'PLY (BINARY)' );
  196. option.onClick( async function () {
  197. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  198. const exporter = new PLYExporter();
  199. exporter.parse( editor.scene, function ( result ) {
  200. saveArrayBuffer( result, 'model-binary.ply' );
  201. }, { binary: true } );
  202. } );
  203. fileExportSubmenu.add( option );
  204. // Export STL (ASCII)
  205. option = new UIRow();
  206. option.setClass( 'option' );
  207. option.setTextContent( 'STL' );
  208. option.onClick( async function () {
  209. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  210. const exporter = new STLExporter();
  211. saveString( exporter.parse( editor.scene ), 'model.stl' );
  212. } );
  213. fileExportSubmenu.add( option );
  214. // Export STL (BINARY)
  215. option = new UIRow();
  216. option.setClass( 'option' );
  217. option.setTextContent( 'STL (BINARY)' );
  218. option.onClick( async function () {
  219. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  220. const exporter = new STLExporter();
  221. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  222. } );
  223. fileExportSubmenu.add( option );
  224. // Export USDZ
  225. option = new UIRow();
  226. option.setClass( 'option' );
  227. option.setTextContent( 'USDZ' );
  228. option.onClick( async function () {
  229. const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
  230. const exporter = new USDZExporter();
  231. saveArrayBuffer( await exporter.parseAsync( editor.scene ), 'model.usdz' );
  232. } );
  233. fileExportSubmenu.add( option );
  234. //
  235. function getAnimations( scene ) {
  236. const animations = [];
  237. scene.traverse( function ( object ) {
  238. animations.push( ... object.animations );
  239. } );
  240. return animations;
  241. }
  242. return container;
  243. }
  244. export { MenubarFile };