StandardMaterialEditor.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { ColorInput, SliderInput, LabelElement } from 'flow';
  2. import { MaterialEditor } from './MaterialEditor.js';
  3. import { MeshStandardNodeMaterial } from 'three/nodes';
  4. export class StandardMaterialEditor extends MaterialEditor {
  5. constructor() {
  6. const material = new MeshStandardNodeMaterial();
  7. super( 'Standard Material', material );
  8. const color = new LabelElement( 'color' ).setInput( 3 );
  9. const opacity = new LabelElement( 'opacity' ).setInput( 1 );
  10. const metalness = new LabelElement( 'metalness' ).setInput( 1 );
  11. const roughness = new LabelElement( 'roughness' ).setInput( 1 );
  12. const emissive = new LabelElement( 'emissive' ).setInput( 3 );
  13. const normal = new LabelElement( 'normal' ).setInput( 3 );
  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. emissive.onConnect( () => this.update(), true );
  33. normal.onConnect( () => this.update(), true );
  34. position.onConnect( () => this.update(), true );
  35. this.add( color )
  36. .add( opacity )
  37. .add( metalness )
  38. .add( roughness )
  39. .add( emissive )
  40. .add( normal )
  41. .add( position );
  42. this.color = color;
  43. this.opacity = opacity;
  44. this.metalness = metalness;
  45. this.roughness = roughness;
  46. this.emissive = emissive;
  47. this.normal = normal;
  48. this.position = position;
  49. this.update();
  50. }
  51. update() {
  52. const { material, color, opacity, emissive, roughness, metalness, normal, position } = this;
  53. color.setEnabledInputs( ! color.getLinkedObject() );
  54. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  55. roughness.setEnabledInputs( ! roughness.getLinkedObject() );
  56. metalness.setEnabledInputs( ! metalness.getLinkedObject() );
  57. material.colorNode = color.getLinkedObject();
  58. material.opacityNode = opacity.getLinkedObject();
  59. material.metalnessNode = metalness.getLinkedObject();
  60. material.roughnessNode = roughness.getLinkedObject();
  61. material.emissiveNode = emissive.getLinkedObject();
  62. material.normalNode = normal.getLinkedObject();
  63. material.positionNode = position.getLinkedObject();
  64. material.dispose();
  65. this.updateTransparent();
  66. }
  67. updateTransparent() {
  68. const { material, opacity } = this;
  69. const transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  70. const needsUpdate = transparent !== material.transparent;
  71. material.transparent = transparent;
  72. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  73. if ( needsUpdate === true ) material.dispose();
  74. }
  75. }