Menubar.File.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 object
  55. var option = new UI.Panel();
  56. option.setClass( 'option' );
  57. option.setTextContent( 'Export Object' );
  58. option.onClick( function () {
  59. exportObject( THREE.ObjectExporter );
  60. } );
  61. options.add( option );
  62. // export scene
  63. var option = new UI.Panel();
  64. option.setClass( 'option' );
  65. option.setTextContent( 'Export Scene' );
  66. option.onClick( function () {
  67. exportScene( THREE.ObjectExporter );
  68. } );
  69. options.add( option );
  70. // export OBJ
  71. var option = new UI.Panel();
  72. option.setClass( 'option' );
  73. option.setTextContent( 'Export OBJ' );
  74. option.onClick( function () {
  75. exportGeometry( THREE.OBJExporter );
  76. } );
  77. options.add( option );
  78. var exportGeometry = function ( exporterClass ) {
  79. if ( selectedObject.geometry === undefined ) {
  80. alert( "Selected object doesn't have any geometry" );
  81. return;
  82. }
  83. var exporter = new exporterClass();
  84. var output;
  85. if ( exporter instanceof THREE.GeometryExporter ) {
  86. output = JSON.stringify( exporter.parse( selectedObject.geometry ), null, '\t' );
  87. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  88. } else {
  89. output = exporter.parse( selectedObject.geometry );
  90. }
  91. var blob = new Blob( [ output ], { type: 'text/plain' } );
  92. var objectURL = URL.createObjectURL( blob );
  93. window.open( objectURL, '_blank' );
  94. window.focus();
  95. };
  96. var exportObject = function ( exporterClass ) {
  97. var exporter = new exporterClass();
  98. var output = JSON.stringify( exporter.parse( selectedObject ), null, '\t' );
  99. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  100. var blob = new Blob( [ output ], { type: 'text/plain' } );
  101. var objectURL = URL.createObjectURL( blob );
  102. window.open( objectURL, '_blank' );
  103. window.focus();
  104. };
  105. var exportScene = function ( exporterClass ) {
  106. var exporter = new exporterClass();
  107. var output = JSON.stringify( exporter.parse( scene ), null, '\t' );
  108. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  109. var blob = new Blob( [ output ], { type: 'text/plain' } );
  110. var objectURL = URL.createObjectURL( blob );
  111. window.open( objectURL, '_blank' );
  112. window.focus();
  113. };
  114. // signals
  115. signals.objectSelected.add( function ( object ) {
  116. selectedObject = object;
  117. } );
  118. signals.sceneChanged.add( function ( object ) {
  119. scene = object;
  120. } );
  121. return container;
  122. }
粤ICP备19079148号