BasicMaterialEditor.js 2.0 KB

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