Menubar.File.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.File = function ( editor ) {
  5. var NUMBER_PRECISION = 6;
  6. function parseNumber( key, value ) {
  7. return typeof value === 'number' ? parseFloat( value.toFixed( NUMBER_PRECISION ) ) : value;
  8. }
  9. //
  10. var config = editor.config;
  11. var container = new UI.Panel();
  12. container.setClass( 'menu' );
  13. var title = new UI.Panel();
  14. title.setClass( 'title' );
  15. title.setTextContent( 'File' );
  16. container.add( title );
  17. var options = new UI.Panel();
  18. options.setClass( 'options' );
  19. container.add( options );
  20. // New
  21. var option = new UI.Row();
  22. option.setClass( 'option' );
  23. option.setTextContent( '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 UI.HorizontalRule() );
  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.type = 'file';
  38. fileInput.addEventListener( 'change', function ( event ) {
  39. editor.loader.loadFile( fileInput.files[ 0 ] );
  40. form.reset();
  41. } );
  42. form.appendChild( fileInput );
  43. var option = new UI.Row();
  44. option.setClass( 'option' );
  45. option.setTextContent( 'Import' );
  46. option.onClick( function () {
  47. fileInput.click();
  48. } );
  49. options.add( option );
  50. //
  51. options.add( new UI.HorizontalRule() );
  52. // Export Geometry
  53. var option = new UI.Row();
  54. option.setClass( 'option' );
  55. option.setTextContent( 'Export Geometry' );
  56. option.onClick( function () {
  57. var object = editor.selected;
  58. if ( object === null ) {
  59. alert( 'No object selected.' );
  60. return;
  61. }
  62. var geometry = object.geometry;
  63. if ( geometry === undefined ) {
  64. alert( 'The selected object doesn\'t have geometry.' );
  65. return;
  66. }
  67. var output = geometry.toJSON();
  68. try {
  69. output = JSON.stringify( output, parseNumber, '\t' );
  70. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  71. } catch ( e ) {
  72. output = JSON.stringify( output );
  73. }
  74. saveString( output, 'geometry.json' );
  75. } );
  76. options.add( option );
  77. // Export Object
  78. var option = new UI.Row();
  79. option.setClass( 'option' );
  80. option.setTextContent( 'Export Object' );
  81. option.onClick( function () {
  82. var object = editor.selected;
  83. if ( object === null ) {
  84. alert( 'No object selected' );
  85. return;
  86. }
  87. var output = object.toJSON();
  88. try {
  89. output = JSON.stringify( output, parseNumber, '\t' );
  90. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  91. } catch ( e ) {
  92. output = JSON.stringify( output );
  93. }
  94. saveString( output, 'model.json' );
  95. } );
  96. options.add( option );
  97. // Export Scene
  98. var option = new UI.Row();
  99. option.setClass( 'option' );
  100. option.setTextContent( 'Export Scene' );
  101. option.onClick( function () {
  102. var output = editor.scene.toJSON();
  103. try {
  104. output = JSON.stringify( output, parseNumber, '\t' );
  105. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  106. } catch ( e ) {
  107. output = JSON.stringify( output );
  108. }
  109. saveString( output, 'scene.json' );
  110. } );
  111. options.add( option );
  112. //
  113. options.add( new UI.HorizontalRule() );
  114. // Export GLB
  115. var option = new UI.Row();
  116. option.setClass( 'option' );
  117. option.setTextContent( 'Export GLB' );
  118. option.onClick( function () {
  119. var exporter = new THREE.GLTFExporter();
  120. exporter.parse( editor.scene, function ( result ) {
  121. saveArrayBuffer( result, 'scene.glb' );
  122. }, { binary: true } );
  123. } );
  124. options.add( option );
  125. // Export GLTF
  126. var option = new UI.Row();
  127. option.setClass( 'option' );
  128. option.setTextContent( 'Export GLTF' );
  129. option.onClick( function () {
  130. var exporter = new THREE.GLTFExporter();
  131. exporter.parse( editor.scene, function ( result ) {
  132. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  133. } );
  134. } );
  135. options.add( option );
  136. // Export OBJ
  137. var option = new UI.Row();
  138. option.setClass( 'option' );
  139. option.setTextContent( 'Export OBJ' );
  140. option.onClick( function () {
  141. var object = editor.selected;
  142. if ( object === null ) {
  143. alert( 'No object selected.' );
  144. return;
  145. }
  146. var exporter = new THREE.OBJExporter();
  147. saveString( exporter.parse( object ), 'model.obj' );
  148. } );
  149. options.add( option );
  150. // Export STL
  151. var option = new UI.Row();
  152. option.setClass( 'option' );
  153. option.setTextContent( 'Export STL' );
  154. option.onClick( function () {
  155. var exporter = new THREE.STLExporter();
  156. saveString( exporter.parse( editor.scene ), 'model.stl' );
  157. } );
  158. options.add( option );
  159. //
  160. options.add( new UI.HorizontalRule() );
  161. // Publish
  162. var option = new UI.Row();
  163. option.setClass( 'option' );
  164. option.setTextContent( 'Publish' );
  165. option.onClick( function () {
  166. var zip = new JSZip();
  167. //
  168. var output = editor.toJSON();
  169. output.metadata.type = 'App';
  170. delete output.history;
  171. var vr = output.project.vr;
  172. output = JSON.stringify( output, parseNumber, '\t' );
  173. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  174. zip.file( 'app.json', output );
  175. //
  176. var title = config.getKey( 'project/title' );
  177. var manager = new THREE.LoadingManager( function () {
  178. save( zip.generate( { type: 'blob' } ), ( title !== '' ? title : 'untitled' ) + '.zip' );
  179. } );
  180. var loader = new THREE.FileLoader( manager );
  181. loader.load( 'js/libs/app/index.html', function ( content ) {
  182. content = content.replace( '<!-- title -->', title );
  183. var includes = [];
  184. if ( vr ) {
  185. includes.push( '<script src="js/WebVR.js"></script>' );
  186. }
  187. content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );
  188. var editButton = '';
  189. if ( config.getKey( 'project/editable' ) ) {
  190. editButton = [
  191. '',
  192. ' var button = document.createElement( \'a\' );',
  193. ' button.href = \'https://threejs.org/editor/#file=\' + location.href.split( \'/\' ).slice( 0, - 1 ).join( \'/\' ) + \'/app.json\';',
  194. ' button.style.cssText = \'position: absolute; bottom: 20px; right: 20px; padding: 12px 14px; color: #fff; border: 1px solid #fff; border-radius: 4px; text-decoration: none;\';',
  195. ' button.target = \'_blank\';',
  196. ' button.textContent = \'EDIT\';',
  197. ' document.body.appendChild( button );',
  198. ''
  199. ].join( '\n' );
  200. }
  201. content = content.replace( '\n\t\t\t/* edit button */\n', editButton );
  202. zip.file( 'index.html', content );
  203. } );
  204. loader.load( 'js/libs/app.js', function ( content ) {
  205. zip.file( 'js/app.js', content );
  206. } );
  207. loader.load( '../build/three.min.js', function ( content ) {
  208. zip.file( 'js/three.min.js', content );
  209. } );
  210. if ( vr ) {
  211. loader.load( '../examples/js/vr/WebVR.js', function ( content ) {
  212. zip.file( 'js/WebVR.js', content );
  213. } );
  214. }
  215. } );
  216. options.add( option );
  217. /*
  218. // Publish (Dropbox)
  219. var option = new UI.Row();
  220. option.setClass( 'option' );
  221. option.setTextContent( 'Publish (Dropbox)' );
  222. option.onClick( function () {
  223. var parameters = {
  224. files: [
  225. { 'url': 'data:text/plain;base64,' + window.btoa( "Hello, World" ), 'filename': 'app/test.txt' }
  226. ]
  227. };
  228. Dropbox.save( parameters );
  229. } );
  230. options.add( option );
  231. */
  232. //
  233. var link = document.createElement( 'a' );
  234. link.style.display = 'none';
  235. document.body.appendChild( link ); // Firefox workaround, see #6594
  236. function save( blob, filename ) {
  237. link.href = URL.createObjectURL( blob );
  238. link.download = filename || 'data.json';
  239. link.click();
  240. // URL.revokeObjectURL( url ); breaks Firefox...
  241. }
  242. function saveArrayBuffer( buffer, filename ) {
  243. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  244. }
  245. function saveString( text, filename ) {
  246. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  247. }
  248. return container;
  249. };