Menubar.File.js 11 KB

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