BasicMaterialEditor.js 1.9 KB

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