Menubar.File.js 9.4 KB

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