Menubar.File.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 DRC
  50. option = new UIRow();
  51. option.setClass( 'option' );
  52. option.setTextContent( strings.getKey( 'menubar/file/export/drc' ) );
  53. option.onClick( async function () {
  54. const object = editor.selected;
  55. if ( object === null || object.isMesh === undefined ) {
  56. alert( 'No mesh selected' );
  57. return;
  58. }
  59. const { DRACOExporter } = await import( 'three/addons/exporters/DRACOExporter.js' );
  60. const exporter = new DRACOExporter();
  61. const options = {
  62. decodeSpeed: 5,
  63. encodeSpeed: 5,
  64. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  65. quantization: [ 16, 8, 8, 8, 8 ],
  66. exportUvs: true,
  67. exportNormals: true,
  68. exportColor: object.geometry.hasAttribute( 'color' )
  69. };
  70. // TODO: Change to DRACOExporter's parse( geometry, onParse )?
  71. const result = exporter.parse( object, options );
  72. saveArrayBuffer( result, 'model.drc' );
  73. } );
  74. options.add( option );
  75. // Export GLB
  76. option = new UIRow();
  77. option.setClass( 'option' );
  78. option.setTextContent( strings.getKey( 'menubar/file/export/glb' ) );
  79. option.onClick( async function () {
  80. const scene = editor.scene;
  81. const animations = getAnimations( scene );
  82. const optimizedAnimations = [];
  83. for ( const animation of animations ) {
  84. optimizedAnimations.push( animation.clone().optimize() );
  85. }
  86. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  87. const exporter = new GLTFExporter();
  88. exporter.parse( scene, function ( result ) {
  89. saveArrayBuffer( result, 'scene.glb' );
  90. }, undefined, { binary: true, animations: optimizedAnimations } );
  91. } );
  92. options.add( option );
  93. // Export GLTF
  94. option = new UIRow();
  95. option.setClass( 'option' );
  96. option.setTextContent( strings.getKey( 'menubar/file/export/gltf' ) );
  97. option.onClick( async function () {
  98. const scene = editor.scene;
  99. const animations = getAnimations( scene );
  100. const optimizedAnimations = [];
  101. for ( const animation of animations ) {
  102. optimizedAnimations.push( animation.clone().optimize() );
  103. }
  104. const { GLTFExporter } = await import( 'three/addons/exporters/GLTFExporter.js' );
  105. const exporter = new GLTFExporter();
  106. exporter.parse( scene, function ( result ) {
  107. saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
  108. }, undefined, { animations: optimizedAnimations } );
  109. } );
  110. options.add( option );
  111. // Export OBJ
  112. option = new UIRow();
  113. option.setClass( 'option' );
  114. option.setTextContent( strings.getKey( 'menubar/file/export/obj' ) );
  115. option.onClick( async function () {
  116. const object = editor.selected;
  117. if ( object === null ) {
  118. alert( 'No object selected.' );
  119. return;
  120. }
  121. const { OBJExporter } = await import( 'three/addons/exporters/OBJExporter.js' );
  122. const exporter = new OBJExporter();
  123. saveString( exporter.parse( object ), 'model.obj' );
  124. } );
  125. options.add( option );
  126. // Export PLY (ASCII)
  127. option = new UIRow();
  128. option.setClass( 'option' );
  129. option.setTextContent( strings.getKey( 'menubar/file/export/ply' ) );
  130. option.onClick( async function () {
  131. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  132. const exporter = new PLYExporter();
  133. exporter.parse( editor.scene, function ( result ) {
  134. saveArrayBuffer( result, 'model.ply' );
  135. } );
  136. } );
  137. options.add( option );
  138. // Export PLY (Binary)
  139. option = new UIRow();
  140. option.setClass( 'option' );
  141. option.setTextContent( strings.getKey( 'menubar/file/export/ply_binary' ) );
  142. option.onClick( async function () {
  143. const { PLYExporter } = await import( 'three/addons/exporters/PLYExporter.js' );
  144. const exporter = new PLYExporter();
  145. exporter.parse( editor.scene, function ( result ) {
  146. saveArrayBuffer( result, 'model-binary.ply' );
  147. }, { binary: true } );
  148. } );
  149. options.add( option );
  150. // Export STL (ASCII)
  151. option = new UIRow();
  152. option.setClass( 'option' );
  153. option.setTextContent( strings.getKey( 'menubar/file/export/stl' ) );
  154. option.onClick( async function () {
  155. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  156. const exporter = new STLExporter();
  157. saveString( exporter.parse( editor.scene ), 'model.stl' );
  158. } );
  159. options.add( option );
  160. // Export STL (Binary)
  161. option = new UIRow();
  162. option.setClass( 'option' );
  163. option.setTextContent( strings.getKey( 'menubar/file/export/stl_binary' ) );
  164. option.onClick( async function () {
  165. const { STLExporter } = await import( 'three/addons/exporters/STLExporter.js' );
  166. const exporter = new STLExporter();
  167. saveArrayBuffer( exporter.parse( editor.scene, { binary: true } ), 'model-binary.stl' );
  168. } );
  169. options.add( option );
  170. // Export USDZ
  171. option = new UIRow();
  172. option.setClass( 'option' );
  173. option.setTextContent( strings.getKey( 'menubar/file/export/usdz' ) );
  174. option.onClick( async function () {
  175. const { USDZExporter } = await import( 'three/addons/exporters/USDZExporter.js' );
  176. const exporter = new USDZExporter();
  177. saveArrayBuffer( await exporter.parse( editor.scene ), 'model.usdz' );
  178. } );
  179. options.add( option );
  180. //
  181. options.add( new UIHorizontalRule() );
  182. // Publish
  183. option = new UIRow();
  184. option.setClass( 'option' );
  185. option.setTextContent( strings.getKey( 'menubar/file/publish' ) );
  186. option.onClick( function () {
  187. const toZip = {};
  188. //
  189. let output = editor.toJSON();
  190. output.metadata.type = 'App';
  191. delete output.history;
  192. output = JSON.stringify( output, null, '\t' );
  193. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  194. toZip[ 'app.json' ] = strToU8( output );
  195. //
  196. const title = config.getKey( 'project/title' );
  197. const manager = new THREE.LoadingManager( function () {
  198. const zipped = zipSync( toZip, { level: 9 } );
  199. const blob = new Blob( [ zipped.buffer ], { type: 'application/zip' } );
  200. save( blob, ( title !== '' ? title : 'untitled' ) + '.zip' );
  201. } );
  202. const loader = new THREE.FileLoader( manager );
  203. loader.load( 'js/libs/app/index.html', function ( content ) {
  204. content = content.replace( '<!-- title -->', title );
  205. const includes = [];
  206. content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );
  207. let editButton = '';
  208. if ( config.getKey( 'project/editable' ) ) {
  209. editButton = [
  210. ' let button = document.createElement( \'a\' );',
  211. ' button.href = \'https://threejs.org/editor/#file=\' + location.href.split( \'/\' ).slice( 0, - 1 ).join( \'/\' ) + \'/app.json\';',
  212. ' button.style.cssText = \'position: absolute; bottom: 20px; right: 20px; padding: 10px 16px; color: #fff; border: 1px solid #fff; border-radius: 20px; text-decoration: none;\';',
  213. ' button.target = \'_blank\';',
  214. ' button.textContent = \'EDIT\';',
  215. ' document.body.appendChild( button );',
  216. ].join( '\n' );
  217. }
  218. content = content.replace( '\t\t\t/* edit button */', editButton );
  219. toZip[ 'index.html' ] = strToU8( content );
  220. } );
  221. loader.load( 'js/libs/app.js', function ( content ) {
  222. toZip[ 'js/app.js' ] = strToU8( content );
  223. } );
  224. loader.load( '../build/three.module.js', function ( content ) {
  225. toZip[ 'js/three.module.js' ] = strToU8( content );
  226. } );
  227. loader.load( '../examples/jsm/webxr/VRButton.js', function ( content ) {
  228. toZip[ 'js/VRButton.js' ] = strToU8( content );
  229. } );
  230. } );
  231. options.add( option );
  232. //
  233. const link = document.createElement( 'a' );
  234. function save( blob, filename ) {
  235. if ( link.href ) {
  236. URL.revokeObjectURL( link.href );
  237. }
  238. link.href = URL.createObjectURL( blob );
  239. link.download = filename || 'data.json';
  240. link.dispatchEvent( new MouseEvent( 'click' ) );
  241. }
  242. function saveArrayBuffer( buffer, filename ) {
  243. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  244. }
  245. function saveString( text, filename ) {
  246. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  247. }
  248. function getAnimations( scene ) {
  249. const animations = [];
  250. scene.traverse( function ( object ) {
  251. animations.push( ... object.animations );
  252. } );
  253. return animations;
  254. }
  255. return container;
  256. }
  257. export { MenubarFile };