Menubar.Edit.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.Edit = 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( 'Edit' );
  10. container.add( title );
  11. var options = new UI.Panel();
  12. options.setClass( 'options' );
  13. container.add( options );
  14. // Undo
  15. var option = new UI.Panel();
  16. option.setClass( 'option' );
  17. option.setTextContent( 'Undo' );
  18. option.onClick( function () {
  19. editor.history.undo();
  20. } );
  21. options.add( option );
  22. // Redo
  23. var option = new UI.Panel();
  24. option.setClass( 'option' );
  25. option.setTextContent( 'Redo' );
  26. option.onClick( function () {
  27. editor.history.redo();
  28. } );
  29. options.add( option );
  30. // ---
  31. options.add( new UI.HorizontalRule() );
  32. // Clone
  33. var option = new UI.Panel();
  34. option.setClass( 'option' );
  35. option.setTextContent( 'Clone' );
  36. option.onClick( function () {
  37. var object = editor.selected;
  38. if ( object.parent === null ) return; // avoid cloning the camera or scene
  39. object = object.clone();
  40. editor.addObject( object );
  41. editor.select( object );
  42. } );
  43. options.add( option );
  44. // Delete
  45. var option = new UI.Panel();
  46. option.setClass( 'option' );
  47. option.setTextContent( 'Delete' );
  48. option.onClick( function () {
  49. var object = editor.selected;
  50. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  51. var parent = object.parent;
  52. editor.removeObject( object );
  53. editor.select( parent );
  54. } );
  55. options.add( option );
  56. // Minify shaders
  57. var option = new UI.Panel();
  58. option.setClass( 'option' );
  59. option.setTextContent( 'Minify Shaders' );
  60. option.onClick( function() {
  61. var root = editor.selected || editor.scene;
  62. var errors = [];
  63. var nMaterialsChanged = 0;
  64. var path = [];
  65. function getPath ( object ) {
  66. path.length = 0;
  67. var parent = object.parent;
  68. if ( parent !== undefined ) getPath( parent );
  69. path.push( object.name || object.uuid );
  70. return path;
  71. }
  72. root.traverse( function ( object ) {
  73. var material = object.material;
  74. if ( material instanceof THREE.ShaderMaterial ) {
  75. try {
  76. var shader = glslprep.minifyGlsl( [
  77. material.vertexShader, material.fragmentShader ] );
  78. material.vertexShader = shader[ 0 ];
  79. material.fragmentShader = shader[ 1 ];
  80. ++nMaterialsChanged;
  81. } catch ( e ) {
  82. var path = getPath( object ).join( "/" );
  83. if ( e instanceof glslprep.SyntaxError )
  84. errors.push( path + ":" +
  85. e.line + ":" + e.column + ": " + e.message );
  86. else {
  87. errors.push( path +
  88. ": Unexpected error (see console for details)." );
  89. console.error( e.stack || e );
  90. }
  91. }
  92. }
  93. } );
  94. window.alert( nMaterialsChanged +
  95. " material(s) were changed.\n" + errors.join( "\n" ) );
  96. } );
  97. options.add( option );
  98. return container;
  99. };