Menubar.File.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  2. import { FileLoader, PropertyBinding } from 'three';
  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 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. if ( needsUniqueNames( scene ) ) { // see #25179
  182. if ( confirm( strings.getKey( 'prompt/file/export/duplicateNames' ) ) === false ) return;
  183. ensureUniqueNames( scene );
  184. }
  185. const animations = getAnimations( scene );
  186. const optimizedAnimations = [];
  187. for ( const animation of animations ) {
  188. optimizedAnimations.push( animation.clone().optimize() );
  189. }
  190. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  191. const exporter = new GLTFExporter();
  192. exporter.parse( scene, function ( result ) {
  193. saveArrayBuffer( result, 'scene.glb' );
  194. }, undefined, { binary: true, animations: optimizedAnimations } );
  195. } );
  196. fileExportSubmenu.add( option );
  197. // Export GLTF
  198. option = new UIRow();
  199. option.setClass( 'option' );
  200. option.setTextContent( 'GLTF' );
  201. option.onClick( async function () {
  202. const scene = editor.scene;
  203. if ( needsUniqueNames( scene ) ) { // see #25179
  204. if ( confirm( strings.getKey( 'prompt/file/export/duplicateNames' ) ) === false ) return;
  205. ensureUniqueNames( scene );
  206. }
  207. const animations = getAnimations( scene );
  208. const optimizedAnimations = [];
  209. for ( const animation of animations ) {
  210. optimizedAnimations.push( animation.clone().optimize() );
  211. }
  212. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  213. const exporter = new GLTFExporter();
  214. exporter.parse( scene, function ( result ) {
  215. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  216. }, undefined, { animations: optimizedAnimations } );
  217. } );
  218. fileExportSubmenu.add( option );
  219. // Export OBJ
  220. option = new UIRow();
  221. option.setClass( 'option' );
  222. option.setTextContent( 'OBJ' );
  223. option.onClick( async function () {
  224. const object = editor.selected;
  225. if ( object === null ) {
  226. alert( strings.getKey( 'prompt/file/export/noObjectSelected' ) );
  227. return;
  228. }
  229. const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
  230. const exporter = new OBJExporter();
  231. saveString( exporter.parse( object ), 'model.obj' );
  232. } );
  233. fileExportSubmenu.add( option );
  234. // Export PLY (ASCII)
  235. option = new UIRow();
  236. option.setClass( 'option' );
  237. option.setTextContent( 'PLY' );
  238. option.onClick( async function () {
  239. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  240. const exporter = new PLYExporter();
  241. exporter.parse( editor.scene, function ( result ) {
  242. saveArrayBuffer( result, 'model.ply' );
  243. } );
  244. } );
  245. fileExportSubmenu.add( option );
  246. // Export PLY (BINARY)
  247. option = new UIRow();
  248. option.setClass( 'option' );
  249. option.setTextContent( 'PLY (BINARY)' );
  250. option.onClick( async function () {
  251. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  252. const exporter = new PLYExporter();
  253. exporter.parse( editor.scene, function ( result ) {
  254. saveArrayBuffer( result, 'model-binary.ply' );
  255. }, { binary: true } );
  256. } );
  257. fileExportSubmenu.add( option );
  258. // Export STL (ASCII)
  259. option = new UIRow();
  260. option.setClass( 'option' );
  261. option.setTextContent( 'STL' );
  262. option.onClick( async function () {
  263. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  264. const exporter = new STLExporter();
  265. saveString( exporter.parse( editor.scene ), 'model.stl' );
  266. } );
  267. fileExportSubmenu.add( option );
  268. // Export STL (BINARY)
  269. option = new UIRow();
  270. option.setClass( 'option' );
  271. option.setTextContent( 'STL (BINARY)' );
  272. option.onClick( async function () {
  273. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  274. const exporter = new STLExporter();
  275. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  276. } );
  277. fileExportSubmenu.add( option );
  278. // Export USDZ
  279. option = new UIRow();
  280. option.setClass( 'option' );
  281. option.setTextContent( 'USDZ' );
  282. option.onClick( async function () {
  283. const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
  284. const exporter = new USDZExporter();
  285. saveArrayBuffer( await exporter.parseAsync( editor.scene ), 'model.usdz' );
  286. } );
  287. fileExportSubmenu.add( option );
  288. //
  289. function getAnimations( scene ) {
  290. const animations = [];
  291. scene.traverse( function ( object ) {
  292. animations.push( ... object.animations );
  293. } );
  294. return animations;
  295. }
  296. function needsUniqueNames( scene ) {
  297. const usedNames = new Set();
  298. let duplicate = false;
  299. let animated = false;
  300. scene.traverse( function ( object ) {
  301. if ( object.animations.length > 0 ) animated = true;
  302. if ( object.name === '' ) return;
  303. if ( usedNames.has( object.name ) ) duplicate = true;
  304. usedNames.add( object.name );
  305. } );
  306. return duplicate && animated;
  307. }
  308. // Gives every object a unique name and keeps the animation tracks that
  309. // reference them by name in sync. The renamed scene mirrors the result of a
  310. // glTF round-trip, where the loader makes all names unique, too.
  311. function ensureUniqueNames( scene ) {
  312. // Resolve each track's target object up front, scoped to the object that
  313. // owns the clip. This disambiguates colliding names before they change.
  314. const trackBindings = [];
  315. scene.traverse( function ( owner ) {
  316. for ( const clip of owner.animations ) {
  317. for ( const track of clip.tracks ) {
  318. const nodeName = PropertyBinding.parseTrackName( track.name ).nodeName;
  319. const target = PropertyBinding.findNode( owner, nodeName );
  320. // References by UUID stay valid, so only track name-based ones.
  321. if ( target !== null && target.name === nodeName ) {
  322. trackBindings.push( { track, target, nodeName } );
  323. }
  324. }
  325. }
  326. } );
  327. // Assign a unique name to every named object.
  328. let changed = false;
  329. const usedNames = new Set();
  330. scene.traverse( function ( object ) {
  331. if ( object.name === '' ) return;
  332. if ( usedNames.has( object.name ) ) {
  333. let suffix = 1, name;
  334. do {
  335. name = object.name + '_' + ( suffix ++ );
  336. } while ( usedNames.has( name ) );
  337. object.name = name;
  338. changed = true;
  339. }
  340. usedNames.add( object.name );
  341. } );
  342. if ( changed === false ) return;
  343. // Point the affected tracks at their renamed targets.
  344. for ( const { track, target, nodeName } of trackBindings ) {
  345. if ( target.name !== nodeName ) {
  346. track.name = target.name + track.name.slice( nodeName.length );
  347. }
  348. }
  349. editor.signals.sceneGraphChanged.dispatch();
  350. }
  351. return container;
  352. }
  353. export { MenubarFile };
粤ICP备19079148号