Menubar.File.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. // Save
  68. option = new UIRow()
  69. .addClass( 'option' )
  70. .setTextContent( strings.getKey( 'menubar/file/save' ) )
  71. .onClick( function () {
  72. const json = editor.toJSON();
  73. const blob = new Blob( [ JSON.stringify( json ) ], { type: 'application/json' } );
  74. editor.utils.save( blob, 'project.json' );
  75. } );
  76. options.add( option );
  77. // Open
  78. const openProjectForm = document.createElement( 'form' );
  79. openProjectForm.style.display = 'none';
  80. document.body.appendChild( openProjectForm );
  81. const openProjectInput = document.createElement( 'input' );
  82. openProjectInput.multiple = false;
  83. openProjectInput.type = 'file';
  84. openProjectInput.accept = '.json';
  85. openProjectInput.addEventListener( 'change', async function () {
  86. const file = openProjectInput.files[ 0 ];
  87. if ( file === undefined ) return;
  88. try {
  89. const json = JSON.parse( await file.text() );
  90. async function onEditorCleared() {
  91. await editor.fromJSON( json );
  92. editor.signals.editorCleared.remove( onEditorCleared );
  93. }
  94. editor.signals.editorCleared.add( onEditorCleared );
  95. editor.clear();
  96. } catch ( e ) {
  97. alert( strings.getKey( 'prompt/file/failedToOpenProject' ) );
  98. console.error( e );
  99. } finally {
  100. form.reset();
  101. }
  102. } );
  103. openProjectForm.appendChild( openProjectInput );
  104. option = new UIRow()
  105. .addClass( 'option' )
  106. .setTextContent( strings.getKey( 'menubar/file/open' ) )
  107. .onClick( function () {
  108. if ( confirm( strings.getKey( 'prompt/file/open' ) ) ) {
  109. openProjectInput.click();
  110. }
  111. } );
  112. options.add( option );
  113. //
  114. options.add( new UIHorizontalRule() );
  115. // Import
  116. const form = document.createElement( 'form' );
  117. form.style.display = 'none';
  118. document.body.appendChild( form );
  119. const fileInput = document.createElement( 'input' );
  120. fileInput.multiple = true;
  121. fileInput.type = 'file';
  122. fileInput.accept = Loader.getSupportedFileFormats().map( format => '.' + format ).join( ', ' );
  123. fileInput.addEventListener( 'change', function () {
  124. editor.loader.loadFiles( fileInput.files );
  125. form.reset();
  126. } );
  127. form.appendChild( fileInput );
  128. option = new UIRow();
  129. option.setClass( 'option' );
  130. option.setTextContent( strings.getKey( 'menubar/file/import' ) );
  131. option.onClick( function () {
  132. fileInput.click();
  133. } );
  134. options.add( option );
  135. // Export
  136. const fileExportSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/file/export' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  137. fileExportSubmenuTitle.onMouseOver( function () {
  138. const { top, right } = this.dom.getBoundingClientRect();
  139. const { paddingTop } = getComputedStyle( this.dom );
  140. fileExportSubmenu.setLeft( right + 'px' );
  141. fileExportSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  142. fileExportSubmenu.setDisplay( 'block' );
  143. } );
  144. fileExportSubmenuTitle.onMouseOut( function () {
  145. fileExportSubmenu.setDisplay( 'none' );
  146. } );
  147. options.add( fileExportSubmenuTitle );
  148. const fileExportSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  149. fileExportSubmenuTitle.add( fileExportSubmenu );
  150. // Export DRC
  151. option = new UIRow();
  152. option.setClass( 'option' );
  153. option.setTextContent( 'DRC' );
  154. option.onClick( async function () {
  155. const object = editor.selected;
  156. if ( object === null || object.isMesh === undefined ) {
  157. alert( strings.getKey( 'prompt/file/export/noMeshSelected' ) );
  158. return;
  159. }
  160. const { DRACOExporter } = await import( 'three/addons/exporters/DRACOExporter.js' );
  161. const exporter = new DRACOExporter();
  162. const options = {
  163. decodeSpeed: 5,
  164. encodeSpeed: 5,
  165. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  166. quantization: [ 16, 8, 8, 8, 8 ],
  167. exportUvs: true,
  168. exportNormals: true,
  169. exportColor: object.geometry.hasAttribute( 'color' )
  170. };
  171. // TODO: Change to DRACOExporter's parse( geometry, onParse )?
  172. const result = exporter.parse( object, options );
  173. saveArrayBuffer( result, 'model.drc' );
  174. } );
  175. fileExportSubmenu.add( option );
  176. // Export GLB
  177. option = new UIRow();
  178. option.setClass( 'option' );
  179. option.setTextContent( 'GLB' );
  180. option.onClick( async function () {
  181. const scene = editor.scene;
  182. const animations = getAnimations( scene );
  183. const optimizedAnimations = [];
  184. for ( const animation of animations ) {
  185. optimizedAnimations.push( animation.clone().optimize() );
  186. }
  187. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  188. const exporter = new GLTFExporter();
  189. exporter.parse( scene, function ( result ) {
  190. saveArrayBuffer( result, 'scene.glb' );
  191. }, undefined, { binary: true, animations: optimizedAnimations } );
  192. } );
  193. fileExportSubmenu.add( option );
  194. // Export GLTF
  195. option = new UIRow();
  196. option.setClass( 'option' );
  197. option.setTextContent( 'GLTF' );
  198. option.onClick( async function () {
  199. const scene = editor.scene;
  200. const animations = getAnimations( scene );
  201. const optimizedAnimations = [];
  202. for ( const animation of animations ) {
  203. optimizedAnimations.push( animation.clone().optimize() );
  204. }
  205. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  206. const exporter = new GLTFExporter();
  207. exporter.parse( scene, function ( result ) {
  208. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  209. }, undefined, { animations: optimizedAnimations } );
  210. } );
  211. fileExportSubmenu.add( option );
  212. // Export OBJ
  213. option = new UIRow();
  214. option.setClass( 'option' );
  215. option.setTextContent( 'OBJ' );
  216. option.onClick( async function () {
  217. const object = editor.selected;
  218. if ( object === null ) {
  219. alert( strings.getKey( 'prompt/file/export/noObjectSelected' ) );
  220. return;
  221. }
  222. const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
  223. const exporter = new OBJExporter();
  224. saveString( exporter.parse( object ), 'model.obj' );
  225. } );
  226. fileExportSubmenu.add( option );
  227. // Export PLY (ASCII)
  228. option = new UIRow();
  229. option.setClass( 'option' );
  230. option.setTextContent( 'PLY' );
  231. option.onClick( async function () {
  232. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  233. const exporter = new PLYExporter();
  234. exporter.parse( editor.scene, function ( result ) {
  235. saveArrayBuffer( result, 'model.ply' );
  236. } );
  237. } );
  238. fileExportSubmenu.add( option );
  239. // Export PLY (BINARY)
  240. option = new UIRow();
  241. option.setClass( 'option' );
  242. option.setTextContent( 'PLY (BINARY)' );
  243. option.onClick( async function () {
  244. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  245. const exporter = new PLYExporter();
  246. exporter.parse( editor.scene, function ( result ) {
  247. saveArrayBuffer( result, 'model-binary.ply' );
  248. }, { binary: true } );
  249. } );
  250. fileExportSubmenu.add( option );
  251. // Export STL (ASCII)
  252. option = new UIRow();
  253. option.setClass( 'option' );
  254. option.setTextContent( 'STL' );
  255. option.onClick( async function () {
  256. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  257. const exporter = new STLExporter();
  258. saveString( exporter.parse( editor.scene ), 'model.stl' );
  259. } );
  260. fileExportSubmenu.add( option );
  261. // Export STL (BINARY)
  262. option = new UIRow();
  263. option.setClass( 'option' );
  264. option.setTextContent( 'STL (BINARY)' );
  265. option.onClick( async function () {
  266. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  267. const exporter = new STLExporter();
  268. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  269. } );
  270. fileExportSubmenu.add( option );
  271. // Export USDZ
  272. option = new UIRow();
  273. option.setClass( 'option' );
  274. option.setTextContent( 'USDZ' );
  275. option.onClick( async function () {
  276. const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
  277. const exporter = new USDZExporter();
  278. saveArrayBuffer( await exporter.parseAsync( editor.scene ), 'model.usdz' );
  279. } );
  280. fileExportSubmenu.add( option );
  281. //
  282. function getAnimations( scene ) {
  283. const animations = [];
  284. scene.traverse( function ( object ) {
  285. animations.push( ... object.animations );
  286. } );
  287. return animations;
  288. }
  289. return container;
  290. }
  291. export { MenubarFile };