Menubar.Edit.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { Box3, Vector3 } from 'three';
  2. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  3. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  4. import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
  5. import { SetPositionCommand } from './commands/SetPositionCommand.js';
  6. import { clone } from '../../examples/jsm/utils/SkeletonUtils.js';
  7. function MenubarEdit( editor ) {
  8. const strings = editor.strings;
  9. const container = new UIPanel();
  10. container.setClass( 'menu' );
  11. const title = new UIPanel();
  12. title.setClass( 'title' );
  13. title.setTextContent( strings.getKey( 'menubar/edit' ) );
  14. container.add( title );
  15. const options = new UIPanel();
  16. options.setClass( 'options' );
  17. container.add( options );
  18. // Undo
  19. const undo = new UIRow();
  20. undo.setClass( 'option' );
  21. undo.setTextContent( strings.getKey( 'menubar/edit/undo' ) );
  22. undo.onClick( function () {
  23. editor.undo();
  24. } );
  25. options.add( undo );
  26. // Redo
  27. const redo = new UIRow();
  28. redo.setClass( 'option' );
  29. redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
  30. redo.onClick( function () {
  31. editor.redo();
  32. } );
  33. options.add( redo );
  34. // Clear History
  35. let option = new UIRow();
  36. option.setClass( 'option' );
  37. option.setTextContent( strings.getKey( 'menubar/edit/clear_history' ) );
  38. option.onClick( function () {
  39. if ( confirm( 'The Undo/Redo History will be cleared. Are you sure?' ) ) {
  40. editor.history.clear();
  41. }
  42. } );
  43. options.add( option );
  44. editor.signals.historyChanged.add( function () {
  45. const history = editor.history;
  46. undo.setClass( 'option' );
  47. redo.setClass( 'option' );
  48. if ( history.undos.length == 0 ) {
  49. undo.setClass( 'inactive' );
  50. }
  51. if ( history.redos.length == 0 ) {
  52. redo.setClass( 'inactive' );
  53. }
  54. } );
  55. // ---
  56. options.add( new UIHorizontalRule() );
  57. // Center
  58. option = new UIRow();
  59. option.setClass( 'option' );
  60. option.setTextContent( strings.getKey( 'menubar/edit/center' ) );
  61. option.onClick( function () {
  62. const object = editor.selected;
  63. if ( object === null || object.parent === null ) return; // avoid centering the camera or scene
  64. const aabb = new Box3().setFromObject( object );
  65. const center = aabb.getCenter( new Vector3() );
  66. const newPosition = new Vector3();
  67. newPosition.x = object.position.x + ( object.position.x - center.x );
  68. newPosition.y = object.position.y + ( object.position.y - center.y );
  69. newPosition.z = object.position.z + ( object.position.z - center.z );
  70. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  71. } );
  72. options.add( option );
  73. // Clone
  74. option = new UIRow();
  75. option.setClass( 'option' );
  76. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  77. option.onClick( function () {
  78. let object = editor.selected;
  79. if ( object === null || object.parent === null ) return; // avoid cloning the camera or scene
  80. object = clone( object );
  81. editor.execute( new AddObjectCommand( editor, object ) );
  82. } );
  83. options.add( option );
  84. // Delete
  85. option = new UIRow();
  86. option.setClass( 'option' );
  87. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  88. option.onClick( function () {
  89. const object = editor.selected;
  90. if ( object !== null && object.parent !== null ) {
  91. editor.execute( new RemoveObjectCommand( editor, object ) );
  92. }
  93. } );
  94. options.add( option );
  95. //
  96. options.add( new UIHorizontalRule() );
  97. // Set textures to sRGB. See #15903
  98. option = new UIRow();
  99. option.setClass( 'option' );
  100. option.setTextContent( strings.getKey( 'menubar/edit/fixcolormaps' ) );
  101. option.onClick( function () {
  102. editor.scene.traverse( fixColorMap );
  103. } );
  104. options.add( option );
  105. const colorMaps = [ 'map', 'envMap', 'emissiveMap' ];
  106. function fixColorMap( obj ) {
  107. const material = obj.material;
  108. if ( material !== undefined ) {
  109. if ( Array.isArray( material ) === true ) {
  110. for ( let i = 0; i < material.length; i ++ ) {
  111. fixMaterial( material[ i ] );
  112. }
  113. } else {
  114. fixMaterial( material );
  115. }
  116. editor.signals.sceneGraphChanged.dispatch();
  117. }
  118. }
  119. function fixMaterial( material ) {
  120. let needsUpdate = material.needsUpdate;
  121. for ( let i = 0; i < colorMaps.length; i ++ ) {
  122. const map = material[ colorMaps[ i ] ];
  123. if ( map ) {
  124. map.colorSpace = THREE.SRGBColorSpace;
  125. needsUpdate = true;
  126. }
  127. }
  128. material.needsUpdate = needsUpdate;
  129. }
  130. return container;
  131. }
  132. export { MenubarEdit };