Menubar.File.js 10 KB

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