Menubar.Edit.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  5. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  6. import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
  7. import { MultiCmdsCommand } from './commands/MultiCmdsCommand.js';
  8. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  9. var MenubarEdit = function ( editor ) {
  10. var strings = editor.strings;
  11. var container = new UIPanel();
  12. container.setClass( 'menu' );
  13. var title = new UIPanel();
  14. title.setClass( 'title' );
  15. title.setTextContent( strings.getKey( 'menubar/edit' ) );
  16. container.add( title );
  17. var options = new UIPanel();
  18. options.setClass( 'options' );
  19. container.add( options );
  20. // Undo
  21. var undo = new UIRow();
  22. undo.setClass( 'option' );
  23. undo.setTextContent( strings.getKey( 'menubar/edit/undo' ) );
  24. undo.onClick( function () {
  25. editor.undo();
  26. } );
  27. options.add( undo );
  28. // Redo
  29. var redo = new UIRow();
  30. redo.setClass( 'option' );
  31. redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
  32. redo.onClick( function () {
  33. editor.redo();
  34. } );
  35. options.add( redo );
  36. // Clear History
  37. var option = new UIRow();
  38. option.setClass( 'option' );
  39. option.setTextContent( strings.getKey( 'menubar/edit/clear_history' ) );
  40. option.onClick( function () {
  41. if ( confirm( 'The Undo/Redo History will be cleared. Are you sure?' ) ) {
  42. editor.history.clear();
  43. }
  44. } );
  45. options.add( option );
  46. editor.signals.historyChanged.add( function () {
  47. var history = editor.history;
  48. undo.setClass( 'option' );
  49. redo.setClass( 'option' );
  50. if ( history.undos.length == 0 ) {
  51. undo.setClass( 'inactive' );
  52. }
  53. if ( history.redos.length == 0 ) {
  54. redo.setClass( 'inactive' );
  55. }
  56. } );
  57. // ---
  58. options.add( new UIHorizontalRule() );
  59. // Clone
  60. var option = new UIRow();
  61. option.setClass( 'option' );
  62. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  63. option.onClick( function () {
  64. var object = editor.selected;
  65. if ( object.parent === null ) return; // avoid cloning the camera or scene
  66. object = object.clone();
  67. editor.execute( new AddObjectCommand( editor, object ) );
  68. } );
  69. options.add( option );
  70. // Delete
  71. var option = new UIRow();
  72. option.setClass( 'option' );
  73. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  74. option.onClick( function () {
  75. var object = editor.selected;
  76. var parent = object.parent;
  77. if ( parent === undefined ) return; // avoid deleting the camera or scene
  78. editor.execute( new RemoveObjectCommand( editor, object ) );
  79. } );
  80. options.add( option );
  81. // Minify shaders
  82. var option = new UIRow();
  83. option.setClass( 'option' );
  84. option.setTextContent( strings.getKey( 'menubar/edit/minify_shaders' ) );
  85. option.onClick( function () {
  86. var root = editor.selected || editor.scene;
  87. var errors = [];
  88. var nMaterialsChanged = 0;
  89. var path = [];
  90. function getPath( object ) {
  91. path.length = 0;
  92. var parent = object.parent;
  93. if ( parent !== undefined ) getPath( parent );
  94. path.push( object.name || object.uuid );
  95. return path;
  96. }
  97. var cmds = [];
  98. root.traverse( function ( object ) {
  99. var material = object.material;
  100. if ( material.isShaderMaterial ) {
  101. try {
  102. var shader = glslprep.minifyGlsl( [
  103. material.vertexShader, material.fragmentShader ] );
  104. cmds.push( new SetMaterialValueCommand( editor, object, 'vertexShader', shader[ 0 ] ) );
  105. cmds.push( new SetMaterialValueCommand( editor, object, 'fragmentShader', shader[ 1 ] ) );
  106. ++ nMaterialsChanged;
  107. } catch ( e ) {
  108. var path = getPath( object ).join( "/" );
  109. if ( e instanceof glslprep.SyntaxError )
  110. errors.push( path + ":" +
  111. e.line + ":" + e.column + ": " + e.message );
  112. else {
  113. errors.push( path +
  114. ": Unexpected error (see console for details)." );
  115. console.error( e.stack || e );
  116. }
  117. }
  118. }
  119. } );
  120. if ( nMaterialsChanged > 0 ) {
  121. editor.execute( new MultiCmdsCommand( editor, cmds ), 'Minify Shaders' );
  122. }
  123. window.alert( nMaterialsChanged +
  124. " material(s) were changed.\n" + errors.join( "\n" ) );
  125. } );
  126. options.add( option );
  127. options.add( new UIHorizontalRule() );
  128. // Set textures to sRGB. See #15903
  129. var option = new UIRow();
  130. option.setClass( 'option' );
  131. option.setTextContent( strings.getKey( 'menubar/edit/fixcolormaps' ) );
  132. option.onClick( function () {
  133. editor.scene.traverse( fixColorMap );
  134. } );
  135. options.add( option );
  136. var colorMaps = [ 'map', 'envMap', 'emissiveMap' ];
  137. function fixColorMap( obj ) {
  138. var material = obj.material;
  139. if ( material !== undefined ) {
  140. if ( Array.isArray( material ) === true ) {
  141. for ( var i = 0; i < material.length; i ++ ) {
  142. fixMaterial( material[ i ] );
  143. }
  144. } else {
  145. fixMaterial( material );
  146. }
  147. editor.signals.sceneGraphChanged.dispatch();
  148. }
  149. }
  150. function fixMaterial( material ) {
  151. var needsUpdate = material.needsUpdate;
  152. for ( var i = 0; i < colorMaps.length; i ++ ) {
  153. var map = material[ colorMaps[ i ] ];
  154. if ( map ) {
  155. map.encoding = THREE.sRGBEncoding;
  156. needsUpdate = true;
  157. }
  158. }
  159. material.needsUpdate = needsUpdate;
  160. }
  161. return container;
  162. };
  163. export { MenubarEdit };