Menubar.File.js 9.2 KB

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