Menubar.File.js 10 KB

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