Menubar.File.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Menubar.File = function ( signals ) {
  2. var container = new UI.Panel();
  3. container.setClass( 'menu' );
  4. var title = new UI.Panel();
  5. title.setTextContent( 'File' ).setColor( '#666' );
  6. title.setMargin( '0px' );
  7. title.setPadding( '8px' );
  8. container.add( title );
  9. //
  10. var selectedObject;
  11. var scene;
  12. var options = new UI.Panel();
  13. options.setClass( 'options' );
  14. container.add( options );
  15. /*
  16. // open
  17. var option = new UI.Panel();
  18. option.setClass( 'option' );
  19. option.setTextContent( 'Open' );
  20. option.onClick( function () { alert( 'Open' ) } );
  21. options.add( option );
  22. */
  23. // reset
  24. var option = new UI.Panel();
  25. option.setClass( 'option' );
  26. option.setTextContent( 'Reset' );
  27. option.onClick( function () {
  28. if ( confirm( 'Are you sure?' ) ) {
  29. if ( localStorage.threejsEditor !== undefined ) {
  30. delete localStorage.threejsEditor;
  31. }
  32. location.reload();
  33. }
  34. } );
  35. options.add( option );
  36. // export geometry
  37. var option = new UI.Panel();
  38. option.setClass( 'option' );
  39. option.setTextContent( 'Export Geometry' );
  40. option.onClick( function () {
  41. exportGeometry( THREE.GeometryExporter );
  42. } );
  43. options.add( option );
  44. /*
  45. // export scene
  46. var option = new UI.Panel();
  47. option.setClass( 'option' );
  48. option.setTextContent( 'Export Scene' );
  49. option.onClick( function () {
  50. exportScene( THREE.SceneExporter );
  51. } );
  52. options.add( option );
  53. */
  54. // export scene 2
  55. var option = new UI.Panel();
  56. option.setClass( 'option' );
  57. option.setTextContent( 'Export Scene 2' );
  58. option.onClick( function () {
  59. exportScene( THREE.SceneExporter2 );
  60. } );
  61. options.add( option );
  62. // export OBJ
  63. var option = new UI.Panel();
  64. option.setClass( 'option' );
  65. option.setTextContent( 'Export OBJ' );
  66. option.onClick( function () {
  67. exportGeometry( THREE.OBJExporter );
  68. } );
  69. options.add( option );
  70. var exportGeometry = function ( exporterClass ) {
  71. if ( selectedObject.geometry === undefined ) {
  72. alert( "Selected object doesn't have any geometry" );
  73. return;
  74. }
  75. var exporter = new exporterClass();
  76. var output;
  77. if ( exporter instanceof THREE.GeometryExporter ) {
  78. output = JSON.stringify( exporter.parse( selectedObject.geometry ), null, '\t' );
  79. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  80. } else {
  81. output = exporter.parse( selectedObject.geometry );
  82. }
  83. var blob = new Blob( [ output ], { type: 'text/plain' } );
  84. var objectURL = URL.createObjectURL( blob );
  85. window.open( objectURL, '_blank' );
  86. window.focus();
  87. };
  88. var exportScene = function ( exporterClass ) {
  89. var exporter = new exporterClass();
  90. var output = JSON.stringify( exporter.parse( scene ), null, '\t' );
  91. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  92. var blob = new Blob( [ output ], { type: 'text/plain' } );
  93. var objectURL = URL.createObjectURL( blob );
  94. window.open( objectURL, '_blank' );
  95. window.focus();
  96. };
  97. // signals
  98. signals.objectSelected.add( function ( object ) {
  99. selectedObject = object;
  100. } );
  101. signals.sceneChanged.add( function ( object ) {
  102. scene = object;
  103. } );
  104. return container;
  105. }