Menubar.File.js 11 KB

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