StandardMaterialEditor.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { ColorInput, SliderInput, LabelElement } from '../../libs/flow.module.js';
  2. import { BaseNode } from '../core/BaseNode.js';
  3. import { MeshStandardNodeMaterial } from '../../renderers/nodes/Nodes.js';
  4. import * as THREE from 'three';
  5. export class StandardMaterialEditor extends BaseNode {
  6. constructor() {
  7. const material = new MeshStandardNodeMaterial();
  8. super( 'Standard 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 metalness = new LabelElement( 'metalness' ).setInput( 1 );
  13. const roughness = new LabelElement( 'roughness' ).setInput( 1 );
  14. const position = new LabelElement( 'position' ).setInput( 3 );
  15. color.add( new ColorInput( material.color.getHex() ).onChange( ( input ) => {
  16. material.color.setHex( input.getValue() );
  17. } ) );
  18. opacity.add( new SliderInput( material.opacity, 0, 1 ).onChange( ( input ) => {
  19. material.opacity = input.getValue();
  20. this.updateTransparent();
  21. } ) );
  22. metalness.add( new SliderInput( material.metalness, 0, 1 ).onChange( ( input ) => {
  23. material.metalness = input.getValue();
  24. } ) );
  25. roughness.add( new SliderInput( material.roughness, 0, 1 ).onChange( ( input ) => {
  26. material.roughness = input.getValue();
  27. } ) );
  28. color.onConnect( () => this.update(), true );
  29. opacity.onConnect( () => this.update(), true );
  30. metalness.onConnect( () => this.update(), true );
  31. roughness.onConnect( () => this.update(), true );
  32. position.onConnect(() => this.update(), true );
  33. this.add( color )
  34. .add( opacity )
  35. .add( metalness )
  36. .add( roughness )
  37. .add( position );
  38. this.color = color;
  39. this.opacity = opacity;
  40. this.metalness = metalness;
  41. this.roughness = roughness;
  42. this.position = position;
  43. this.material = material;
  44. this.update();
  45. }
  46. update() {
  47. const { material, color, opacity, roughness, metalness, position } = this;
  48. color.setEnabledInputs( ! color.getLinkedObject() );
  49. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  50. roughness.setEnabledInputs( ! roughness.getLinkedObject() );
  51. metalness.setEnabledInputs( ! metalness.getLinkedObject() );
  52. material.colorNode = color.getLinkedObject();
  53. material.opacityNode = opacity.getLinkedObject() || null;
  54. material.metalnessNode = metalness.getLinkedObject();
  55. material.roughnessNode = roughness.getLinkedObject();
  56. material.positionNode = position.getLinkedObject() || null;
  57. material.dispose();
  58. this.updateTransparent();
  59. // TODO: Fix on NodeMaterial System
  60. material.customProgramCacheKey = () => {
  61. return THREE.MathUtils.generateUUID();
  62. };
  63. }
  64. updateTransparent() {
  65. const { material, opacity } = this;
  66. material.transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  67. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  68. }
  69. }