Menubar.File.js 10 KB

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