Sidebar.Material.ColorProperty.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { UIColor, UINumber, UIRow, UIText } from './libs/ui.js';
  2. import { SetMaterialColorCommand } from './commands/SetMaterialColorCommand.js';
  3. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  4. function SidebarMaterialColorProperty( editor, property, name ) {
  5. const signals = editor.signals;
  6. const container = new UIRow();
  7. container.add( new UIText( name ).setWidth( '90px' ) );
  8. const color = new UIColor().onInput( onChange );
  9. container.add( color );
  10. let intensity;
  11. if ( property === 'emissive' ) {
  12. intensity = new UINumber( 1 ).setWidth( '30px' ).setRange( 0, Infinity ).onChange( onChange );
  13. container.add( intensity );
  14. }
  15. let object = null;
  16. let material = null;
  17. function onChange() {
  18. if ( material[ property ].getHex() !== color.getHexValue() ) {
  19. editor.execute( new SetMaterialColorCommand( editor, object, property, color.getHexValue(), 0 /* TODO: currentMaterialSlot */ ) );
  20. }
  21. if ( intensity !== undefined ) {
  22. if ( material[ `${ property }Intensity` ] !== intensity.getValue() ) {
  23. editor.execute( new SetMaterialValueCommand( editor, object, `${ property }Intensity`, intensity.getValue(), /* TODO: currentMaterialSlot*/ 0 ) );
  24. }
  25. }
  26. }
  27. function update() {
  28. if ( object === null ) return;
  29. if ( object.material === undefined ) return;
  30. material = object.material;
  31. if ( property in material ) {
  32. color.setHexValue( material[ property ].getHexString() );
  33. if ( intensity !== undefined ) {
  34. intensity.setValue( material[ `${ property }Intensity` ] );
  35. }
  36. container.setDisplay( '' );
  37. } else {
  38. container.setDisplay( 'none' );
  39. }
  40. }
  41. //
  42. signals.objectSelected.add( function ( selected ) {
  43. object = selected;
  44. update();
  45. } );
  46. signals.materialChanged.add( update );
  47. return container;
  48. }
  49. export { SidebarMaterialColorProperty };