Sidebar.Material.MapProperty.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import * as THREE from '../../build/three.module.js';
  2. import { UICheckbox, UINumber, UIRow, UIText } from './libs/ui.js';
  3. import { UITexture } from './libs/ui.three.js';
  4. import { SetMaterialMapCommand } from './commands/SetMaterialMapCommand.js';
  5. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  6. import { SetMaterialVectorCommand } from './commands/SetMaterialVectorCommand.js';
  7. function SidebarMaterialMapProperty( editor, property, name ) {
  8. const signals = editor.signals;
  9. const container = new UIRow();
  10. container.add( new UIText( name ).setWidth( '90px' ) );
  11. const enabled = new UICheckbox( false ).setMarginRight( '8px' ).onChange( onChange );
  12. container.add( enabled );
  13. const map = new UITexture().onChange( onMapChange );
  14. container.add( map );
  15. const mapType = property.replace( 'Map', '' );
  16. let intensity;
  17. if ( property === 'aoMap' ) {
  18. intensity = new UINumber().setWidth( '30px' ).onChange( onIntensityChange );
  19. container.add( intensity );
  20. }
  21. let scale;
  22. if ( property === 'bumpMap' || property === 'displacementMap' ) {
  23. scale = new UINumber().setWidth( '30px' ).onChange( onScaleChange );
  24. container.add( scale );
  25. }
  26. let scaleX, scaleY;
  27. if ( property === 'normalMap' || property === 'clearcoatNormalMap' ) {
  28. scaleX = new UINumber().setWidth( '30px' ).onChange( onScaleXYChange );
  29. container.add( scaleX );
  30. scaleY = new UINumber().setWidth( '30px' ).onChange( onScaleXYChange );
  31. container.add( scaleY );
  32. }
  33. let object = null;
  34. let material = null;
  35. function onChange() {
  36. const newMap = enabled.getValue() ? map.getValue() : null;
  37. if ( material[ property ] !== newMap ) {
  38. if ( newMap !== null ) {
  39. const geometry = object.geometry;
  40. if ( geometry.hasAttribute( 'uv' ) === false ) console.warn( 'Geometry doesn\'t have uvs:', geometry );
  41. if ( property === 'envMap' ) newMap.mapping = THREE.EquirectangularReflectionMapping;
  42. }
  43. editor.execute( new SetMaterialMapCommand( editor, object, property, newMap, 0 /* TODO: currentMaterialSlot */ ) );
  44. }
  45. }
  46. function onMapChange( texture ) {
  47. if ( texture !== null ) {
  48. if ( texture.isDataTexture !== true && texture.encoding !== THREE.sRGBEncoding ) {
  49. texture.encoding = THREE.sRGBEncoding;
  50. material.needsUpdate = true;
  51. }
  52. }
  53. enabled.setDisabled( false );
  54. onChange();
  55. }
  56. function onIntensityChange() {
  57. if ( material[ `${ property }Intensity` ] !== intensity.getValue() ) {
  58. editor.execute( new SetMaterialValueCommand( editor, object, `${ property }Intensity`, intensity.getValue(), 0 /* TODO: currentMaterialSlot */ ) );
  59. }
  60. }
  61. function onScaleChange() {
  62. if ( material[ `${ mapType }Scale` ] !== scale.getValue() ) {
  63. editor.execute( new SetMaterialValueCommand( editor, object, `${ mapType }Scale`, scale.getValue(), 0 /* TODO: currentMaterialSlot */ ) );
  64. }
  65. }
  66. function onScaleXYChange() {
  67. const value = [ scaleX.getValue(), scaleY.getValue() ];
  68. if ( material[ `${ mapType }Scale` ].x !== value[ 0 ] || material[ `${ mapType }Scale` ].y !== value[ 1 ] ) {
  69. editor.execute( new SetMaterialVectorCommand( editor, object, `${ mapType }Scale`, value, 0 /* TODOL currentMaterialSlot */ ) );
  70. }
  71. }
  72. function update() {
  73. if ( object === null ) return;
  74. if ( object.material === undefined ) return;
  75. material = object.material;
  76. if ( property in material ) {
  77. if ( material[ property ] !== null ) {
  78. map.setValue( material[ property ] );
  79. }
  80. enabled.setValue( material[ property ] !== null );
  81. enabled.setDisabled( map.getValue() === null );
  82. if ( intensity !== undefined ) {
  83. intensity.setValue( material[ `${ property }Intensity` ] );
  84. }
  85. if ( scale !== undefined ) {
  86. scale.setValue( material[ `${ mapType }Scale` ] );
  87. }
  88. if ( scaleX !== undefined ) {
  89. scaleX.setValue( material[ `${ mapType }Scale` ].x );
  90. scaleY.setValue( material[ `${ mapType }Scale` ].y );
  91. }
  92. container.setDisplay( '' );
  93. } else {
  94. container.setDisplay( 'none' );
  95. }
  96. }
  97. //
  98. signals.objectSelected.add( function ( selected ) {
  99. object = selected;
  100. map.setValue( null );
  101. update();
  102. } );
  103. signals.materialChanged.add( update );
  104. return container;
  105. }
  106. export { SidebarMaterialMapProperty };