StandardMaterialEditor.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { ObjectNode, LabelElement } from '../../libs/flow.module.js';
  2. import { MeshStandardNodeMaterial, ColorNode, FloatNode } from '../../renderers/nodes/Nodes.js';
  3. import * as THREE from 'three';
  4. const NULL_COLOR = new ColorNode();
  5. const NULL_FLOAT = new FloatNode();
  6. export class StandardMaterialEditor extends ObjectNode {
  7. constructor() {
  8. const material = new MeshStandardNodeMaterial();
  9. super( 'Standard Material', 0, material );
  10. this.title.setStyle( 'blue' );
  11. this.setWidth( 300 );
  12. const color = new LabelElement( 'color' ).setInput( 3 );
  13. const opacity = new LabelElement( 'opacity' ).setInput( 1 );
  14. const metalness = new LabelElement( 'metalness' ).setInput( 1 );
  15. const roughness = new LabelElement( 'roughness' ).setInput( 1 );
  16. color.onConnect( () => this.update(), true );
  17. opacity.onConnect( () => this.update(), true );
  18. metalness.onConnect( () => this.update(), true );
  19. roughness.onConnect( () => this.update(), true );
  20. this.add( color )
  21. .add( opacity )
  22. .add( metalness )
  23. .add( roughness );
  24. this.color = color;
  25. this.opacity = opacity;
  26. this.metalness = metalness;
  27. this.roughness = roughness;
  28. this.material = material;
  29. this.update();
  30. }
  31. update() {
  32. const { material, color, opacity, roughness, metalness } = this;
  33. material.colorNode = color.linkedExtra || NULL_COLOR;
  34. material.opacityNode = opacity.linkedExtra || null;
  35. material.transparent = opacity.linkedExtra ? true : false;
  36. material.metalnessNode = metalness.linkedExtra || NULL_FLOAT;
  37. material.roughnessNode = roughness.linkedExtra || NULL_FLOAT;
  38. material.dispose();
  39. // TODO: Fix on NodeMaterial System
  40. material.customProgramCacheKey = () => {
  41. return THREE.MathUtils.generateUUID();
  42. };
  43. }
  44. }