Menubar.File.js 10 KB

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