Menubar.Edit.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // Clone
  15. var option = new UI.Panel();
  16. option.setClass( 'option' );
  17. option.setTextContent( 'Clone' );
  18. option.onClick( function () {
  19. var object = editor.selected;
  20. if ( object.parent === undefined ) return; // avoid cloning the camera or scene
  21. object = object.clone();
  22. editor.addObject( object );
  23. editor.select( object );
  24. } );
  25. options.add( option );
  26. // Delete
  27. var option = new UI.Panel();
  28. option.setClass( 'option' );
  29. option.setTextContent( 'Delete' );
  30. option.onClick( function () {
  31. var object = editor.selected;
  32. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  33. var parent = object.parent;
  34. editor.removeObject( object );
  35. editor.select( parent );
  36. } );
  37. options.add( option );
  38. // Minify shaders
  39. var option = new UI.Panel();
  40. option.setClass( 'option' );
  41. option.setTextContent( 'Minify Shaders' );
  42. option.onClick( function() {
  43. var root = editor.selected || editor.scene;
  44. var errors = [];
  45. var nMaterialsChanged = 0;
  46. var path = [];
  47. function getPath ( object ) {
  48. path.length = 0;
  49. var parent = object.parent;
  50. if ( parent !== undefined ) getPath( parent );
  51. path.push( object.name || object.uuid );
  52. return path;
  53. }
  54. root.traverse( function ( object ) {
  55. var material = object.material;
  56. if ( material instanceof THREE.ShaderMaterial ) {
  57. try {
  58. var shader = glslprep.minifyGlsl( [
  59. material.vertexShader, material.fragmentShader ] );
  60. material.vertexShader = shader[ 0 ];
  61. material.fragmentShader = shader[ 1 ];
  62. ++nMaterialsChanged;
  63. } catch ( e ) {
  64. var path = getPath( object ).join( "/" );
  65. if ( e instanceof glslprep.SyntaxError )
  66. errors.push( path + ":" +
  67. e.line + ":" + e.column + ": " + e.message );
  68. else {
  69. errors.push( path +
  70. ": Unexpected error (see console for details)." );
  71. console.error( e.stack || e );
  72. }
  73. }
  74. }
  75. } );
  76. window.alert( nMaterialsChanged +
  77. " material(s) were changed.\n" + errors.join( "\n" ) );
  78. } );
  79. options.add( option );
  80. return container;
  81. };