StandardMaterialEditor.js 3.3 KB

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