Menubar.File.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. }, { binary: true } );
  160. } );
  161. options.add( option );
  162. // Export GLTF
  163. var option = new UIRow();
  164. option.setClass( 'option' );
  165. option.setTextContent( strings.getKey( 'menubar/file/export/gltf' ) );
  166. option.onClick( function () {
  167. var exporter = new GLTFExporter();
  168. exporter.parse( editor.scene, function ( result ) {
  169. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  170. } );
  171. } );
  172. options.add( option );
  173. // Export OBJ
  174. var option = new UIRow();
  175. option.setClass( 'option' );
  176. option.setTextContent( strings.getKey( 'menubar/file/export/obj' ) );
  177. option.onClick( function () {
  178. var object = editor.selected;
  179. if ( object === null ) {
  180. alert( 'No object selected.' );
  181. return;
  182. }
  183. var exporter = new OBJExporter();
  184. saveString( exporter.parse( object ), 'model.obj' );
  185. } );
  186. options.add( option );
  187. // Export PLY (ASCII)
  188. var option = new UIRow();
  189. option.setClass( 'option' );
  190. option.setTextContent( strings.getKey( 'menubar/file/export/ply' ) );
  191. option.onClick( function () {
  192. var exporter = new PLYExporter();
  193. exporter.parse( editor.scene, function ( result ) {
  194. saveArrayBuffer( result, 'model.ply' );
  195. } );
  196. } );
  197. options.add( option );
  198. // Export PLY (Binary)
  199. var option = new UIRow();
  200. option.setClass( 'option' );
  201. option.setTextContent( strings.getKey( 'menubar/file/export/ply_binary' ) );
  202. option.onClick( function () {
  203. var exporter = new PLYExporter();
  204. exporter.parse( editor.scene, function ( result ) {
  205. saveArrayBuffer( result, 'model-binary.ply' );
  206. }, { binary: true } );
  207. } );
  208. options.add( option );
  209. // Export STL (ASCII)
  210. var option = new UIRow();
  211. option.setClass( 'option' );
  212. option.setTextContent( strings.getKey( 'menubar/file/export/stl' ) );
  213. option.onClick( function () {
  214. var exporter = new STLExporter();
  215. saveString( exporter.parse( editor.scene ), 'model.stl' );
  216. } );
  217. options.add( option );
  218. // Export STL (Binary)
  219. var option = new UIRow();
  220. option.setClass( 'option' );
  221. option.setTextContent( strings.getKey( 'menubar/file/export/stl_binary' ) );
  222. option.onClick( function () {
  223. var exporter = new STLExporter();
  224. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  225. } );
  226. options.add( option );
  227. //
  228. options.add( new UIHorizontalRule() );
  229. // Publish
  230. var option = new UIRow();
  231. option.setClass( 'option' );
  232. option.setTextContent( strings.getKey( 'menubar/file/publish' ) );
  233. option.onClick( function () {
  234. var zip = new JSZip();
  235. //
  236. var output = editor.toJSON();
  237. output.metadata.type = 'App';
  238. delete output.history;
  239. output = JSON.stringify( output, parseNumber, '\t' );
  240. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  241. zip.file( 'app.json', output );
  242. //
  243. var title = config.getKey( 'project/title' );
  244. var manager = new THREE.LoadingManager( function () {
  245. save( zip.generate( { type: 'blob' } ), ( title !== '' ? title : 'untitled' ) + '.zip' );
  246. } );
  247. var loader = new THREE.FileLoader( manager );
  248. loader.load( 'js/libs/app/index.html', function ( content ) {
  249. content = content.replace( '<!-- title -->', title );
  250. var includes = [];
  251. content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );
  252. var editButton = '';
  253. if ( config.getKey( 'project/editable' ) ) {
  254. editButton = [
  255. '',
  256. ' var button = document.createElement( \'a\' );',
  257. ' button.href = \'https://threejs.org/editor/#file=\' + location.href.split( \'/\' ).slice( 0, - 1 ).join( \'/\' ) + \'/app.json\';',
  258. ' button.style.cssText = \'position: absolute; bottom: 20px; right: 20px; padding: 10px 16px; color: #fff; border: 1px solid #fff; border-radius: 20px; text-decoration: none;\';',
  259. ' button.target = \'_blank\';',
  260. ' button.textContent = \'EDIT\';',
  261. ' document.body.appendChild( button );',
  262. ''
  263. ].join( '\n' );
  264. }
  265. content = content.replace( '\n\t\t\t/* edit button */\n', editButton );
  266. zip.file( 'index.html', content );
  267. } );
  268. loader.load( 'js/libs/app.js', function ( content ) {
  269. zip.file( 'js/app.js', content );
  270. } );
  271. loader.load( '../build/three.module.js', function ( content ) {
  272. zip.file( 'js/three.module.js', content );
  273. } );
  274. loader.load( '../examples/jsm/webxr/VRButton.js', function ( content ) {
  275. zip.file( 'js/VRButton.js', content );
  276. } );
  277. loader.load( '../examples/js/vr/HelioWebXRPolyfill.js', function ( content ) {
  278. zip.file( 'js/HelioWebXRPolyfill.js', content );
  279. } );
  280. } );
  281. options.add( option );
  282. //
  283. var link = document.createElement( 'a' );
  284. function save( blob, filename ) {
  285. link.href = URL.createObjectURL( blob );
  286. link.download = filename || 'data.json';
  287. link.dispatchEvent( new MouseEvent( 'click' ) );
  288. // URL.revokeObjectURL( url ); breaks Firefox...
  289. }
  290. function saveArrayBuffer( buffer, filename ) {
  291. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  292. }
  293. function saveString( text, filename ) {
  294. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  295. }
  296. return container;
  297. };
  298. export { MenubarFile };