1
0

BasicMaterialEditor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { ColorInput, SliderInput, LabelElement } from 'flow';
  2. import { MaterialEditor } from './MaterialEditor.js';
  3. import { MeshBasicNodeMaterial } from 'three/tsl';
  4. import { setInputAestheticsFromType } from '../DataTypeLib.js';
  5. export class BasicMaterialEditor extends MaterialEditor {
  6. constructor() {
  7. const material = new MeshBasicNodeMaterial();
  8. super( 'Basic Material', material );
  9. const color = setInputAestheticsFromType( new LabelElement( 'color' ), 'Color' );
  10. const opacity = setInputAestheticsFromType( new LabelElement( 'opacity' ), 'Number' );
  11. const position = setInputAestheticsFromType( new LabelElement( 'position' ), 'Vector3' );
  12. color.add( new ColorInput( material.color.getHex() ).onChange( ( input ) => {
  13. material.color.setHex( input.getValue() );
  14. } ) );
  15. opacity.add( new SliderInput( material.opacity, 0, 1 ).onChange( ( input ) => {
  16. material.opacity = input.getValue();
  17. this.updateTransparent();
  18. } ) );
  19. color.onConnect( () => this.update(), true );
  20. opacity.onConnect( () => this.update(), true );
  21. position.onConnect( () => this.update(), true );
  22. this.add( color )
  23. .add( opacity )
  24. .add( position );
  25. this.color = color;
  26. this.opacity = opacity;
  27. this.position = position;
  28. this.update();
  29. }
  30. update() {
  31. const { material, color, opacity, position } = this;
  32. color.setEnabledInputs( ! color.getLinkedObject() );
  33. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  34. material.colorNode = color.getLinkedObject();
  35. material.opacityNode = opacity.getLinkedObject() || null;
  36. material.positionNode = position.getLinkedObject() || null;
  37. material.dispose();
  38. this.updateTransparent();
  39. }
  40. updateTransparent() {
  41. const { material, opacity } = this;
  42. const transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  43. const needsUpdate = transparent !== material.transparent;
  44. material.transparent = transparent;
  45. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  46. if ( needsUpdate === true ) material.dispose();
  47. }
  48. }