Menubar.File.js 8.5 KB

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