Menubar.File.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.File = function ( editor ) {
  5. var container = new UI.Panel();
  6. container.setClass( 'menu' );
  7. var title = new UI.Panel();
  8. title.setClass( 'title' );
  9. title.setTextContent( 'File' );
  10. container.add( title );
  11. var options = new UI.Panel();
  12. options.setClass( 'options' );
  13. container.add( options );
  14. // New
  15. var option = new UI.Panel();
  16. option.setClass( 'option' );
  17. option.setTextContent( 'New' );
  18. option.onClick( function () {
  19. if ( confirm( 'Any unsaved data will be lost. Are you sure?' ) ) {
  20. editor.clear();
  21. }
  22. } );
  23. options.add( option );
  24. //
  25. options.add( new UI.HorizontalRule() );
  26. // Import
  27. var fileInput = document.createElement( 'input' );
  28. fileInput.type = 'file';
  29. fileInput.addEventListener( 'change', function ( event ) {
  30. editor.loader.loadFile( fileInput.files[ 0 ] );
  31. } );
  32. var option = new UI.Panel();
  33. option.setClass( 'option' );
  34. option.setTextContent( 'Import' );
  35. option.onClick( function () {
  36. fileInput.click();
  37. } );
  38. options.add( option );
  39. //
  40. options.add( new UI.HorizontalRule() );
  41. // Export Geometry
  42. var option = new UI.Panel();
  43. option.setClass( 'option' );
  44. option.setTextContent( 'Export Geometry' );
  45. option.onClick( function () {
  46. var object = editor.selected;
  47. if ( object === null ) {
  48. alert( 'No object selected.' );
  49. return;
  50. }
  51. var geometry = object.geometry;
  52. if ( geometry === undefined ) {
  53. alert( 'The selected object doesn\'t have geometry.' );
  54. return;
  55. }
  56. var output = geometry.toJSON();
  57. try {
  58. output = JSON.stringify( output, null, '\t' );
  59. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  60. } catch ( e ) {
  61. output = JSON.stringify( output );
  62. }
  63. exportString( output, 'geometry.json' );
  64. } );
  65. options.add( option );
  66. // Export Object
  67. var option = new UI.Panel();
  68. option.setClass( 'option' );
  69. option.setTextContent( 'Export Object' );
  70. option.onClick( function () {
  71. var object = editor.selected;
  72. if ( object === null ) {
  73. alert( 'No object selected' );
  74. return;
  75. }
  76. var output = object.toJSON();
  77. try {
  78. output = JSON.stringify( output, null, '\t' );
  79. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  80. } catch ( e ) {
  81. output = JSON.stringify( output );
  82. }
  83. exportString( output, 'model.json' );
  84. } );
  85. options.add( option );
  86. // Export Scene
  87. var option = new UI.Panel();
  88. option.setClass( 'option' );
  89. option.setTextContent( 'Export Scene' );
  90. option.onClick( function () {
  91. var output = editor.scene.toJSON();
  92. try {
  93. output = JSON.stringify( output, null, '\t' );
  94. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  95. } catch ( e ) {
  96. output = JSON.stringify( output );
  97. }
  98. exportString( output, 'scene.json' );
  99. } );
  100. options.add( option );
  101. // Export OBJ
  102. var option = new UI.Panel();
  103. option.setClass( 'option' );
  104. option.setTextContent( 'Export OBJ' );
  105. option.onClick( function () {
  106. var object = editor.selected;
  107. if ( object === null ) {
  108. alert( 'No object selected.' );
  109. return;
  110. }
  111. var exporter = new THREE.OBJExporter();
  112. exportString( exporter.parse( object ), 'model.obj' );
  113. } );
  114. options.add( option );
  115. // Export STL
  116. var option = new UI.Panel();
  117. option.setClass( 'option' );
  118. option.setTextContent( 'Export STL' );
  119. option.onClick( function () {
  120. var exporter = new THREE.STLExporter();
  121. exportString( exporter.parse( editor.scene ), 'model.stl' );
  122. } );
  123. options.add( option );
  124. //
  125. options.add( new UI.HorizontalRule() );
  126. // Publish
  127. var option = new UI.Panel();
  128. option.setClass( 'option' );
  129. option.setTextContent( 'Publish' );
  130. option.onClick( function () {
  131. var camera = editor.camera;
  132. var zip = new JSZip();
  133. zip.file( 'index.html', [
  134. '<!DOCTYPE html>',
  135. '<html lang="en">',
  136. ' <head>',
  137. ' <title>three.js</title>',
  138. ' <meta charset="utf-8">',
  139. ' <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">',
  140. ' <style>',
  141. ' body {',
  142. ' margin: 0px;',
  143. ' overflow: hidden;',
  144. ' }',
  145. ' </style>',
  146. ' </head>',
  147. ' <body ontouchstart="">',
  148. ' <script src="js/three.min.js"></script>',
  149. ' <script src="js/app.js"></script>',
  150. ' <script>',
  151. '',
  152. ' var loader = new THREE.XHRLoader();',
  153. ' loader.load( \'app.json\', function ( text ) {',
  154. '',
  155. ' var player = new APP.Player();',
  156. ' player.load( JSON.parse( text ) );',
  157. ' player.setSize( window.innerWidth, window.innerHeight );',
  158. ' player.play();',
  159. '',
  160. ' document.body.appendChild( player.dom );',
  161. '',
  162. ' window.addEventListener( \'resize\', function () {',
  163. ' player.setSize( window.innerWidth, window.innerHeight );',
  164. ' } );',
  165. '',
  166. ' } );',
  167. '',
  168. ' </script>',
  169. ' </body>',
  170. '</html>'
  171. ].join( '\n' ) );
  172. //
  173. var output = editor.toJSON();
  174. output.metadata.type = 'App';
  175. delete output.history;
  176. output = JSON.stringify( output, null, '\t' );
  177. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  178. zip.file( 'app.json', output );
  179. //
  180. var manager = new THREE.LoadingManager( function () {
  181. location.href = 'data:application/zip;base64,' + zip.generate();
  182. } );
  183. var loader = new THREE.XHRLoader( manager );
  184. loader.load( 'js/libs/app.js', function ( content ) {
  185. zip.file( 'js/app.js', content );
  186. } );
  187. loader.load( '../build/three.min.js', function ( content ) {
  188. zip.file( 'js/three.min.js', content );
  189. } );
  190. } );
  191. options.add( option );
  192. //
  193. var link = document.createElement( 'a' );
  194. link.style.display = 'none';
  195. document.body.appendChild( link ); // Firefox workaround, see #6594
  196. var exportString = function ( output, filename ) {
  197. var blob = new Blob( [ output ], { type: 'text/plain' } );
  198. var objectURL = URL.createObjectURL( blob );
  199. link.href = objectURL;
  200. link.download = filename || 'data.json';
  201. link.target = '_blank';
  202. var event = document.createEvent( 'MouseEvents' );
  203. event.initMouseEvent(
  204. 'click', true, false, window, 0, 0, 0, 0, 0,
  205. false, false, false, false, 0, null
  206. );
  207. link.dispatchEvent( event );
  208. };
  209. return container;
  210. };