Menubar.File.js 7.8 KB

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