Menubar.File.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. import * as THREE from '../../build/three.module.js';
  2. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  3. function MenubarFile( editor ) {
  4. function parseNumber( key, value ) {
  5. var precision = config.getKey( 'exportPrecision' );
  6. return typeof value === 'number' ? parseFloat( value.toFixed( precision ) ) : value;
  7. }
  8. //
  9. var config = editor.config;
  10. var strings = editor.strings;
  11. var container = new UIPanel();
  12. container.setClass( 'menu' );
  13. var title = new UIPanel();
  14. title.setClass( 'title' );
  15. title.setTextContent( strings.getKey( 'menubar/file' ) );
  16. container.add( title );
  17. var options = new UIPanel();
  18. options.setClass( 'options' );
  19. container.add( options );
  20. // New
  21. var option = new UIRow();
  22. option.setClass( 'option' );
  23. option.setTextContent( strings.getKey( 'menubar/file/new' ) );
  24. option.onClick( function () {
  25. if ( confirm( 'Any unsaved data will be lost. Are you sure?' ) ) {
  26. editor.clear();
  27. }
  28. } );
  29. options.add( option );
  30. //
  31. options.add( new UIHorizontalRule() );
  32. // Import
  33. var form = document.createElement( 'form' );
  34. form.style.display = 'none';
  35. document.body.appendChild( form );
  36. var fileInput = document.createElement( 'input' );
  37. fileInput.multiple = true;
  38. fileInput.type = 'file';
  39. fileInput.addEventListener( 'change', function () {
  40. editor.loader.loadFiles( fileInput.files );
  41. form.reset();
  42. } );
  43. form.appendChild( fileInput );
  44. var option = new UIRow();
  45. option.setClass( 'option' );
  46. option.setTextContent( strings.getKey( 'menubar/file/import' ) );
  47. option.onClick( function () {
  48. fileInput.click();
  49. } );
  50. options.add( option );
  51. //
  52. options.add( new UIHorizontalRule() );
  53. // Export Geometry
  54. var option = new UIRow();
  55. option.setClass( 'option' );
  56. option.setTextContent( strings.getKey( 'menubar/file/export/geometry' ) );
  57. option.onClick( function () {
  58. var object = editor.selected;
  59. if ( object === null ) {
  60. alert( 'No object selected.' );
  61. return;
  62. }
  63. var geometry = object.geometry;
  64. if ( geometry === undefined ) {
  65. alert( 'The selected object doesn\'t have geometry.' );
  66. return;
  67. }
  68. var output = geometry.toJSON();
  69. try {
  70. output = JSON.stringify( output, parseNumber, '\t' );
  71. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  72. } catch ( e ) {
  73. output = JSON.stringify( output );
  74. }
  75. saveString( output, 'geometry.json' );
  76. } );
  77. options.add( option );
  78. // Export Object
  79. var option = new UIRow();
  80. option.setClass( 'option' );
  81. option.setTextContent( strings.getKey( 'menubar/file/export/object' ) );
  82. option.onClick( function () {
  83. var object = editor.selected;
  84. if ( object === null ) {
  85. alert( 'No object selected' );
  86. return;
  87. }
  88. var output = object.toJSON();
  89. try {
  90. output = JSON.stringify( output, parseNumber, '\t' );
  91. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  92. } catch ( e ) {
  93. output = JSON.stringify( output );
  94. }
  95. saveString( output, 'model.json' );
  96. } );
  97. options.add( option );
  98. // Export Scene
  99. var option = new UIRow();
  100. option.setClass( 'option' );
  101. option.setTextContent( strings.getKey( 'menubar/file/export/scene' ) );
  102. option.onClick( function () {
  103. var output = editor.scene.toJSON();
  104. try {
  105. output = JSON.stringify( output, parseNumber, '\t' );
  106. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  107. } catch ( e ) {
  108. output = JSON.stringify( output );
  109. }
  110. saveString( output, 'scene.json' );
  111. } );
  112. options.add( option );
  113. //
  114. options.add( new UIHorizontalRule() );
  115. // Export DAE
  116. var option = new UIRow();
  117. option.setClass( 'option' );
  118. option.setTextContent( strings.getKey( 'menubar/file/export/dae' ) );
  119. option.onClick( async function () {
  120. var { ColladaExporter } = await import( '../../examples/jsm/exporters/ColladaExporter.js' );
  121. var exporter = new ColladaExporter();
  122. exporter.parse( editor.scene, function ( result ) {
  123. saveString( result.data, 'scene.dae' );
  124. } );
  125. } );
  126. options.add( option );
  127. // Export DRC
  128. var option = new UIRow();
  129. option.setClass( 'option' );
  130. option.setTextContent( strings.getKey( 'menubar/file/export/drc' ) );
  131. option.onClick( async function () {
  132. var object = editor.selected;
  133. if ( object === null || object.isMesh === undefined ) {
  134. alert( 'No mesh selected' );
  135. return;
  136. }
  137. var { DRACOExporter } = await import( '../../examples/jsm/exporters/DRACOExporter.js' );
  138. var exporter = new DRACOExporter();
  139. // TODO: Change to DRACOExporter's parse( geometry, onParse )?
  140. var result = exporter.parse( object );
  141. saveArrayBuffer( result, 'model.drc' );
  142. } );
  143. options.add( option );
  144. // Export GLB
  145. var option = new UIRow();
  146. option.setClass( 'option' );
  147. option.setTextContent( strings.getKey( 'menubar/file/export/glb' ) );
  148. option.onClick( async function () {
  149. var scene = editor.scene;
  150. var animations = getAnimations( scene );
  151. var { GLTFExporter } = await import( '../../examples/jsm/exporters/GLTFExporter.js' );
  152. var exporter = new GLTFExporter();
  153. exporter.parse( scene, function ( result ) {
  154. saveArrayBuffer( result, 'scene.glb' );
  155. }, { binary: true, animations: animations } );
  156. } );
  157. options.add( option );
  158. // Export GLTF
  159. var option = new UIRow();
  160. option.setClass( 'option' );
  161. option.setTextContent( strings.getKey( 'menubar/file/export/gltf' ) );
  162. option.onClick( async function () {
  163. var scene = editor.scene;
  164. var animations = getAnimations( scene );
  165. var { GLTFExporter } = await import( '../../examples/jsm/exporters/GLTFExporter.js' );
  166. var exporter = new GLTFExporter();
  167. exporter.parse( scene, function ( result ) {
  168. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  169. }, { animations: animations } );
  170. } );
  171. options.add( option );
  172. // Export OBJ
  173. var option = new UIRow();
  174. option.setClass( 'option' );
  175. option.setTextContent( strings.getKey( 'menubar/file/export/obj' ) );
  176. option.onClick( async function () {
  177. var object = editor.selected;
  178. if ( object === null ) {
  179. alert( 'No object selected.' );
  180. return;
  181. }
  182. var { OBJExporter } = await import( '../../examples/jsm/exporters/OBJExporter.js' );
  183. var exporter = new OBJExporter();
  184. saveString( exporter.parse( object ), 'model.obj' );
  185. } );
  186. options.add( option );
  187. // Export PLY (ASCII)
  188. var option = new UIRow();
  189. option.setClass( 'option' );
  190. option.setTextContent( strings.getKey( 'menubar/file/export/ply' ) );
  191. option.onClick( async function () {
  192. var { PLYExporter } = await import( '../../examples/jsm/exporters/PLYExporter.js' );
  193. var exporter = new PLYExporter();
  194. exporter.parse( editor.scene, function ( result ) {
  195. saveArrayBuffer( result, 'model.ply' );
  196. } );
  197. } );
  198. options.add( option );
  199. // Export PLY (Binary)
  200. var option = new UIRow();
  201. option.setClass( 'option' );
  202. option.setTextContent( strings.getKey( 'menubar/file/export/ply_binary' ) );
  203. option.onClick( async function () {
  204. var { PLYExporter } = await import( '../../examples/jsm/exporters/PLYExporter.js' );
  205. var exporter = new PLYExporter();
  206. exporter.parse( editor.scene, function ( result ) {
  207. saveArrayBuffer( result, 'model-binary.ply' );
  208. }, { binary: true } );
  209. } );
  210. options.add( option );
  211. // Export STL (ASCII)
  212. var option = new UIRow();
  213. option.setClass( 'option' );
  214. option.setTextContent( strings.getKey( 'menubar/file/export/stl' ) );
  215. option.onClick( async function () {
  216. var { STLExporter } = await import( '../../examples/jsm/exporters/STLExporter.js' );
  217. var exporter = new STLExporter();
  218. saveString( exporter.parse( editor.scene ), 'model.stl' );
  219. } );
  220. options.add( option );
  221. // Export STL (Binary)
  222. var option = new UIRow();
  223. option.setClass( 'option' );
  224. option.setTextContent( strings.getKey( 'menubar/file/export/stl_binary' ) );
  225. option.onClick( async function () {
  226. var { STLExporter } = await import( '../../examples/jsm/exporters/STLExporter.js' );
  227. var exporter = new STLExporter();
  228. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  229. } );
  230. options.add( option );
  231. //
  232. options.add( new UIHorizontalRule() );
  233. // Publish
  234. var option = new UIRow();
  235. option.setClass( 'option' );
  236. option.setTextContent( strings.getKey( 'menubar/file/publish' ) );
  237. option.onClick( function () {
  238. var zip = new JSZip(); // eslint-disable-line no-undef
  239. //
  240. var output = editor.toJSON();
  241. output.metadata.type = 'App';
  242. delete output.history;
  243. output = JSON.stringify( output, parseNumber, '\t' );
  244. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  245. zip.file( 'app.json', output );
  246. //
  247. var title = config.getKey( 'project/title' );
  248. var manager = new THREE.LoadingManager( function () {
  249. save( zip.generate( { type: 'blob' } ), ( title !== '' ? title : 'untitled' ) + '.zip' );
  250. } );
  251. var loader = new THREE.FileLoader( manager );
  252. loader.load( 'js/libs/app/index.html', function ( content ) {
  253. content = content.replace( '<!-- title -->', title );
  254. var includes = [];
  255. content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );
  256. var editButton = '';
  257. if ( config.getKey( 'project/editable' ) ) {
  258. editButton = [
  259. '',
  260. ' var button = document.createElement( \'a\' );',
  261. ' button.href = \'https://threejs.org/editor/#file=\' + location.href.split( \'/\' ).slice( 0, - 1 ).join( \'/\' ) + \'/app.json\';',
  262. ' button.style.cssText = \'position: absolute; bottom: 20px; right: 20px; padding: 10px 16px; color: #fff; border: 1px solid #fff; border-radius: 20px; text-decoration: none;\';',
  263. ' button.target = \'_blank\';',
  264. ' button.textContent = \'EDIT\';',
  265. ' document.body.appendChild( button );',
  266. ''
  267. ].join( '\n' );
  268. }
  269. content = content.replace( '\n\t\t\t/* edit button */\n', editButton );
  270. zip.file( 'index.html', content );
  271. } );
  272. loader.load( 'js/libs/app.js', function ( content ) {
  273. zip.file( 'js/app.js', content );
  274. } );
  275. loader.load( '../build/three.module.js', function ( content ) {
  276. zip.file( 'js/three.module.js', content );
  277. } );
  278. loader.load( '../examples/jsm/webxr/VRButton.js', function ( content ) {
  279. zip.file( 'js/VRButton.js', content );
  280. } );
  281. } );
  282. options.add( option );
  283. //
  284. var link = document.createElement( 'a' );
  285. function save( blob, filename ) {
  286. link.href = URL.createObjectURL( blob );
  287. link.download = filename || 'data.json';
  288. link.dispatchEvent( new MouseEvent( 'click' ) );
  289. // URL.revokeObjectURL( url ); breaks Firefox...
  290. }
  291. function saveArrayBuffer( buffer, filename ) {
  292. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  293. }
  294. function saveString( text, filename ) {
  295. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  296. }
  297. function getAnimations( scene ) {
  298. var animations = [];
  299. scene.traverse( function ( object ) {
  300. animations.push( ... object.animations );
  301. } );
  302. return animations;
  303. }
  304. return container;
  305. }
  306. export { MenubarFile };
粤ICP备19079148号