Sidebar.Material.ColorProperty.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 ).setClass( 'Label' ) );
  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 materialSlot = null;
  17. let material = null;
  18. function onChange() {
  19. if ( material[ property ].getHex() !== color.getHexValue() ) {
  20. editor.execute( new SetMaterialColorCommand( editor, object, property, color.getHexValue(), materialSlot ) );
  21. }
  22. if ( intensity !== undefined ) {
  23. if ( material[ `${ property }Intensity` ] !== intensity.getValue() ) {
  24. editor.execute( new SetMaterialValueCommand( editor, object, `${ property }Intensity`, intensity.getValue(), materialSlot ) );
  25. }
  26. }
  27. }
  28. function update( currentObject, currentMaterialSlot = 0 ) {
  29. object = currentObject;
  30. materialSlot = currentMaterialSlot;
  31. if ( object === null ) return;
  32. if ( object.material === undefined ) return;
  33. material = editor.getObjectMaterial( object, materialSlot );
  34. if ( property in material ) {
  35. color.setHexValue( material[ property ].getHexString() );
  36. if ( intensity !== undefined ) {
  37. intensity.setValue( material[ `${ property }Intensity` ] );
  38. }
  39. container.setDisplay( '' );
  40. } else {
  41. container.setDisplay( 'none' );
  42. }
  43. }
  44. //
  45. signals.objectSelected.add( update );
  46. signals.materialChanged.add( update );
  47. return container;
  48. }
  49. export { SidebarMaterialColorProperty };