BasicMaterialEditor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { ColorInput, SliderInput, LabelElement } from '../../libs/flow.module.js';
  2. import { BaseNode } from '../core/BaseNode.js';
  3. import { MeshBasicNodeMaterial } from 'three/nodes';
  4. export class BasicMaterialEditor extends BaseNode {
  5. constructor() {
  6. const material = new MeshBasicNodeMaterial();
  7. super( 'Basic Material', 1, material );
  8. this.setWidth( 300 );
  9. const color = new LabelElement( 'color' ).setInput( 3 );
  10. const opacity = new LabelElement( 'opacity' ).setInput( 1 );
  11. const position = new LabelElement( 'position' ).setInput( 3 );
  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.material = material;
  29. this.update();
  30. }
  31. update() {
  32. const { material, color, opacity, position } = this;
  33. color.setEnabledInputs( ! color.getLinkedObject() );
  34. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  35. material.colorNode = color.getLinkedObject();
  36. material.opacityNode = opacity.getLinkedObject() || null;
  37. material.positionNode = position.getLinkedObject() || null;
  38. material.dispose();
  39. this.updateTransparent();
  40. }
  41. updateTransparent() {
  42. const { material, opacity } = this;
  43. const transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  44. const needsUpdate = transparent !== material.transparent;
  45. material.transparent = transparent;
  46. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  47. if ( needsUpdate === true ) material.dispose();
  48. }
  49. }