Menubar.File.js 10 KB

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