StandardMaterialEditor.js 1.8 KB

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