Menubar.File.js 12 KB

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