Menubar.File.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 GLTF
  115. var option = new UI.Row();
  116. option.setClass( 'option' );
  117. option.setTextContent( 'Export GLTF' );
  118. option.onClick( function () {
  119. var exporter = new THREE.GLTFExporter();
  120. exporter.parse( editor.scene, function ( result ) {
  121. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  122. } );
  123. } );
  124. options.add( option );
  125. // Export OBJ
  126. var option = new UI.Row();
  127. option.setClass( 'option' );
  128. option.setTextContent( 'Export OBJ' );
  129. option.onClick( function () {
  130. var object = editor.selected;
  131. if ( object === null ) {
  132. alert( 'No object selected.' );
  133. return;
  134. }
  135. var exporter = new THREE.OBJExporter();
  136. saveString( exporter.parse( object ), 'model.obj' );
  137. } );
  138. options.add( option );
  139. // Export STL
  140. var option = new UI.Row();
  141. option.setClass( 'option' );
  142. option.setTextContent( 'Export STL' );
  143. option.onClick( function () {
  144. var exporter = new THREE.STLExporter();
  145. saveString( exporter.parse( editor.scene ), 'model.stl' );
  146. } );
  147. options.add( option );
  148. //
  149. options.add( new UI.HorizontalRule() );
  150. // Publish
  151. var option = new UI.Row();
  152. option.setClass( 'option' );
  153. option.setTextContent( 'Publish' );
  154. option.onClick( function () {
  155. var zip = new JSZip();
  156. //
  157. var output = editor.toJSON();
  158. output.metadata.type = 'App';
  159. delete output.history;
  160. var vr = output.project.vr;
  161. output = JSON.stringify( output, parseNumber, '\t' );
  162. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  163. zip.file( 'app.json', output );
  164. //
  165. var title = config.getKey( 'project/title' );
  166. var manager = new THREE.LoadingManager( function () {
  167. save( zip.generate( { type: 'blob' } ), ( title !== '' ? title : 'untitled' ) + '.zip' );
  168. } );
  169. var loader = new THREE.FileLoader( manager );
  170. loader.load( 'js/libs/app/index.html', function ( content ) {
  171. content = content.replace( '<!-- title -->', title );
  172. var includes = [];
  173. if ( vr ) {
  174. includes.push( '<script src="js/WebVR.js"></script>' );
  175. }
  176. content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );
  177. var editButton = '';
  178. if ( config.getKey( 'project/editable' ) ) {
  179. editButton = [
  180. '',
  181. ' var button = document.createElement( \'a\' );',
  182. ' button.href = \'https://threejs.org/editor/#file=\' + location.href.split( \'/\' ).slice( 0, - 1 ).join( \'/\' ) + \'/app.json\';',
  183. ' button.style.cssText = \'position: absolute; bottom: 20px; right: 20px; padding: 12px 14px; color: #fff; border: 1px solid #fff; border-radius: 4px; text-decoration: none;\';',
  184. ' button.target = \'_blank\';',
  185. ' button.textContent = \'EDIT\';',
  186. ' document.body.appendChild( button );',
  187. ''
  188. ].join( '\n' );
  189. }
  190. content = content.replace( '\n\t\t\t/* edit button */\n', editButton );
  191. zip.file( 'index.html', content );
  192. } );
  193. loader.load( 'js/libs/app.js', function ( content ) {
  194. zip.file( 'js/app.js', content );
  195. } );
  196. loader.load( '../build/three.min.js', function ( content ) {
  197. zip.file( 'js/three.min.js', content );
  198. } );
  199. if ( vr ) {
  200. loader.load( '../examples/js/vr/WebVR.js', function ( content ) {
  201. zip.file( 'js/WebVR.js', content );
  202. } );
  203. }
  204. } );
  205. options.add( option );
  206. //
  207. var link = document.createElement( 'a' );
  208. link.style.display = 'none';
  209. document.body.appendChild( link ); // Firefox workaround, see #6594
  210. function save( blob, filename ) {
  211. link.href = URL.createObjectURL( blob );
  212. link.download = filename || 'data.json';
  213. link.click();
  214. // URL.revokeObjectURL( url ); breaks Firefox...
  215. }
  216. function saveString( text, filename ) {
  217. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  218. }
  219. return container;
  220. };