StandardMaterialEditor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { ColorInput, SliderInput, LabelElement } from 'flow';
  2. import { MaterialEditor } from './MaterialEditor.js';
  3. import { MeshStandardNodeMaterial } from 'three/nodes';
  4. import { setInputAestheticsFromType } from '../DataTypeLib.js';
  5. export class StandardMaterialEditor extends MaterialEditor {
  6. constructor() {
  7. const material = new MeshStandardNodeMaterial();
  8. super( 'Standard Material', material );
  9. const color = setInputAestheticsFromType( new LabelElement( 'color' ), 'Color' );
  10. const opacity = setInputAestheticsFromType( new LabelElement( 'opacity' ), 'Number' );
  11. const metalness = setInputAestheticsFromType( new LabelElement( 'metalness' ), 'Number' );
  12. const roughness = setInputAestheticsFromType( new LabelElement( 'roughness' ), 'Number' );
  13. const emissive = setInputAestheticsFromType( new LabelElement( 'emissive' ), 'Color' );
  14. const normal = setInputAestheticsFromType( new LabelElement( 'normal' ), 'Vector3' );
  15. const position = setInputAestheticsFromType( new LabelElement( 'position' ), 'Vector3' );
  16. color.add( new ColorInput( material.color.getHex() ).onChange( ( input ) => {
  17. material.color.setHex( input.getValue() );
  18. } ) );
  19. opacity.add( new SliderInput( material.opacity, 0, 1 ).onChange( ( input ) => {
  20. material.opacity = input.getValue();
  21. this.updateTransparent();
  22. } ) );
  23. metalness.add( new SliderInput( material.metalness, 0, 1 ).onChange( ( input ) => {
  24. material.metalness = input.getValue();
  25. } ) );
  26. roughness.add( new SliderInput( material.roughness, 0, 1 ).onChange( ( input ) => {
  27. material.roughness = input.getValue();
  28. } ) );
  29. color.onConnect( () => this.update(), true );
  30. opacity.onConnect( () => this.update(), true );
  31. metalness.onConnect( () => this.update(), true );
  32. roughness.onConnect( () => this.update(), true );
  33. emissive.onConnect( () => this.update(), true );
  34. normal.onConnect( () => this.update(), true );
  35. position.onConnect( () => this.update(), true );
  36. this.add( color )
  37. .add( opacity )
  38. .add( metalness )
  39. .add( roughness )
  40. .add( emissive )
  41. .add( normal )
  42. .add( position );
  43. this.color = color;
  44. this.opacity = opacity;
  45. this.metalness = metalness;
  46. this.roughness = roughness;
  47. this.emissive = emissive;
  48. this.normal = normal;
  49. this.position = position;
  50. this.update();
  51. }
  52. update() {
  53. const { material, color, opacity, emissive, roughness, metalness, normal, position } = this;
  54. color.setEnabledInputs( ! color.getLinkedObject() );
  55. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  56. roughness.setEnabledInputs( ! roughness.getLinkedObject() );
  57. metalness.setEnabledInputs( ! metalness.getLinkedObject() );
  58. material.colorNode = color.getLinkedObject();
  59. material.opacityNode = opacity.getLinkedObject();
  60. material.metalnessNode = metalness.getLinkedObject();
  61. material.roughnessNode = roughness.getLinkedObject();
  62. material.emissiveNode = emissive.getLinkedObject();
  63. material.normalNode = normal.getLinkedObject();
  64. material.positionNode = position.getLinkedObject();
  65. material.dispose();
  66. this.updateTransparent();
  67. }
  68. updateTransparent() {
  69. const { material, opacity } = this;
  70. const transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  71. const needsUpdate = transparent !== material.transparent;
  72. material.transparent = transparent;
  73. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  74. if ( needsUpdate === true ) material.dispose();
  75. }
  76. }