Menubar.File.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.File = function ( editor ) {
  5. var container = new UI.Panel();
  6. container.setClass( 'menu' );
  7. var title = new UI.Panel();
  8. title.setClass( 'title' );
  9. title.setTextContent( 'File' );
  10. container.add( title );
  11. var options = new UI.Panel();
  12. options.setClass( 'options' );
  13. container.add( options );
  14. // New
  15. var option = new UI.Panel();
  16. option.setClass( 'option' );
  17. option.setTextContent( 'New' );
  18. option.onClick( function () {
  19. if ( confirm( 'Are you sure?' ) ) {
  20. editor.config.setKey(
  21. 'camera/position', [ 500, 250, 500 ],
  22. 'camera/target', [ 0, 0, 0 ]
  23. );
  24. editor.storage.clear( function () {
  25. location.href = location.pathname;
  26. } );
  27. }
  28. } );
  29. options.add( option );
  30. //
  31. options.add( new UI.HorizontalRule() );
  32. // Import
  33. var fileInput = document.createElement( 'input' );
  34. fileInput.type = 'file';
  35. fileInput.addEventListener( 'change', function ( event ) {
  36. editor.loader.loadFile( fileInput.files[ 0 ] );
  37. } );
  38. var option = new UI.Panel();
  39. option.setClass( 'option' );
  40. option.setTextContent( 'Import' );
  41. option.onClick( function () {
  42. fileInput.click();
  43. } );
  44. options.add( option );
  45. //
  46. options.add( new UI.HorizontalRule() );
  47. // Export Geometry
  48. var option = new UI.Panel();
  49. option.setClass( 'option' );
  50. option.setTextContent( 'Export Geometry' );
  51. option.onClick( function () {
  52. var object = editor.selected;
  53. if ( object === null ) {
  54. alert( 'No object selected.' );
  55. return;
  56. }
  57. var geometry = object.geometry;
  58. if ( geometry === undefined ) {
  59. alert( 'The selected object doesn\'t have geometry.' );
  60. return;
  61. }
  62. var output = geometry.toJSON();
  63. output = JSON.stringify( output, null, '\t' );
  64. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  65. exportString( output );
  66. } );
  67. options.add( option );
  68. // Export Object
  69. var option = new UI.Panel();
  70. option.setClass( 'option' );
  71. option.setTextContent( 'Export Object' );
  72. option.onClick( function () {
  73. var object = editor.selected;
  74. if ( object === null ) {
  75. alert( 'No object selected' );
  76. return;
  77. }
  78. var output = object.toJSON();
  79. output = JSON.stringify( output, null, '\t' );
  80. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  81. exportString( output );
  82. } );
  83. options.add( option );
  84. // Export Scene
  85. var option = new UI.Panel();
  86. option.setClass( 'option' );
  87. option.setTextContent( 'Export Scene' );
  88. option.onClick( function () {
  89. var output = editor.scene.toJSON();
  90. output = JSON.stringify( output, null, '\t' );
  91. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  92. exportString( output );
  93. } );
  94. options.add( option );
  95. // Export OBJ
  96. var option = new UI.Panel();
  97. option.setClass( 'option' );
  98. option.setTextContent( 'Export OBJ' );
  99. option.onClick( function () {
  100. var object = editor.selected;
  101. if ( object === null ) {
  102. alert( 'No object selected.' );
  103. return;
  104. }
  105. var exporter = new THREE.OBJExporter();
  106. exportString( exporter.parse( object ) );
  107. } );
  108. options.add( option );
  109. // Export STL
  110. var option = new UI.Panel();
  111. option.setClass( 'option' );
  112. option.setTextContent( 'Export STL' );
  113. option.onClick( function () {
  114. var exporter = new THREE.STLExporter();
  115. exportString( exporter.parse( editor.scene ) );
  116. } );
  117. options.add( option );
  118. //
  119. options.add( new UI.HorizontalRule() );
  120. // Publish
  121. var option = new UI.Panel();
  122. option.setClass( 'option' );
  123. option.setTextContent( 'Publish' );
  124. option.onClick( function () {
  125. var camera = editor.camera;
  126. var zip = new JSZip();
  127. zip.file( 'index.html', [
  128. '<!DOCTYPE html>',
  129. '<html lang="en">',
  130. ' <head>',
  131. ' <title>three.js</title>',
  132. ' <meta charset="utf-8">',
  133. ' <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">',
  134. ' <style>',
  135. ' body {',
  136. ' margin: 0px;',
  137. ' overflow: hidden;',
  138. ' }',
  139. ' </style>',
  140. ' </head>',
  141. ' <body ontouchstart="">',
  142. ' <script src="js/three.min.js"></script>',
  143. ' <script src="js/OrbitControls.js"></script>',
  144. ' <script>',
  145. '',
  146. ' var camera, controls, scene, renderer;',
  147. '',
  148. ' var loader = new THREE.ObjectLoader();',
  149. ' loader.load( \'scene.json\', function ( object ) {',
  150. '',
  151. ' scene = object;',
  152. '',
  153. ' camera = new THREE.PerspectiveCamera( ' + camera.fov + ', 1, ' + camera.near + ', ' + camera.far + ' );',
  154. ' camera.position.set( ' + camera.position.x + ', ' + camera.position.y + ', ' + camera.position.z + ' );',
  155. ' camera.rotation.set( ' + camera.rotation.x + ', ' + camera.rotation.y + ', ' + camera.rotation.z + ' );',
  156. '',
  157. ' controls = new THREE.OrbitControls( camera );',
  158. ' controls.addEventListener( \'change\', render );',
  159. '',
  160. ' renderer = new THREE.WebGLRenderer();',
  161. ' renderer.setSize( window.innerWidth, window.innerHeight );',
  162. ' document.body.appendChild( renderer.domElement );',
  163. '',
  164. ' camera.aspect = window.innerWidth / window.innerHeight;',
  165. ' camera.updateProjectionMatrix();',
  166. '',
  167. ' animate();',
  168. ' render();',
  169. '',
  170. ' } );',
  171. '',
  172. ' var render = function () {',
  173. '',
  174. ' renderer.render( scene, camera );',
  175. '',
  176. ' };',
  177. '',
  178. ' var animate = function () {',
  179. '',
  180. ' requestAnimationFrame( animate );',
  181. ' controls.update();',
  182. '',
  183. ' };',
  184. '',
  185. ' </script>',
  186. ' </body>',
  187. '</html>'
  188. ].join( '\n' ) );
  189. //
  190. var output = editor.scene.toJSON();
  191. output = JSON.stringify( output, null, '\t' );
  192. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  193. zip.file( 'scene.json', output );
  194. //
  195. var manager = new THREE.LoadingManager( function () {
  196. location.href = 'data:application/zip;base64,' + zip.generate();
  197. } );
  198. var loader = new THREE.XHRLoader( manager );
  199. loader.load( '../build/three.min.js', function ( content ) {
  200. zip.file( 'js/three.min.js', content );
  201. } );
  202. loader.load( '../examples/js/controls/OrbitControls.js', function ( content ) {
  203. zip.file( 'js/OrbitControls.js', content );
  204. } );
  205. } );
  206. options.add( option );
  207. /*
  208. // Test
  209. var option = new UI.Panel();
  210. option.setClass( 'option' );
  211. option.setTextContent( 'Test' );
  212. option.onClick( function () {
  213. var text = new UI.Text( 'blah' );
  214. editor.showDialog( text );
  215. } );
  216. options.add( option );
  217. */
  218. //
  219. var exportString = function ( output ) {
  220. var blob = new Blob( [ output ], { type: 'text/plain' } );
  221. var objectURL = URL.createObjectURL( blob );
  222. window.open( objectURL, '_blank' );
  223. window.focus();
  224. };
  225. return container;
  226. };