Menubar.File.js 7.4 KB

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