StandardMaterialEditor.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { ColorInput, SliderInput, LabelElement } from '../../libs/flow.module.js';
  2. import { BaseNode } from '../core/BaseNode.js';
  3. import { MeshStandardNodeMaterial } from 'three/nodes';
  4. export class StandardMaterialEditor extends BaseNode {
  5. constructor() {
  6. const material = new MeshStandardNodeMaterial();
  7. super( 'Standard Material', 1, material );
  8. this.setWidth( 300 );
  9. const color = new LabelElement( 'color' ).setInput( 3 );
  10. const opacity = new LabelElement( 'opacity' ).setInput( 1 );
  11. const metalness = new LabelElement( 'metalness' ).setInput( 1 );
  12. const roughness = new LabelElement( 'roughness' ).setInput( 1 );
  13. const emissive = new LabelElement( 'emissive' ).setInput( 3 );
  14. const normal = new LabelElement( 'normal' ).setInput( 3 );
  15. const position = new LabelElement( 'position' ).setInput( 3 );
  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.material = material;
  51. this.update();
  52. }
  53. update() {
  54. const { material, color, opacity, emissive, roughness, metalness, normal, position } = this;
  55. color.setEnabledInputs( ! color.getLinkedObject() );
  56. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  57. roughness.setEnabledInputs( ! roughness.getLinkedObject() );
  58. metalness.setEnabledInputs( ! metalness.getLinkedObject() );
  59. material.colorNode = color.getLinkedObject();
  60. material.opacityNode = opacity.getLinkedObject();
  61. material.metalnessNode = metalness.getLinkedObject();
  62. material.roughnessNode = roughness.getLinkedObject();
  63. material.emissiveNode = emissive.getLinkedObject();
  64. material.normalNode = normal.getLinkedObject();
  65. material.positionNode = position.getLinkedObject();
  66. material.dispose();
  67. this.updateTransparent();
  68. }
  69. updateTransparent() {
  70. const { material, opacity } = this;
  71. const transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  72. const needsUpdate = transparent !== material.transparent;
  73. material.transparent = transparent;
  74. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  75. if ( needsUpdate === true ) material.dispose();
  76. }
  77. }