Menubar.File.js 9.4 KB

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