StandardMaterialEditor.js 2.6 KB

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