PointsMaterialEditor.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { ColorInput, ToggleInput, SliderInput, LabelElement } from '../../libs/flow.module.js';
  2. import { BaseNode } from '../core/BaseNode.js';
  3. import { PointsNodeMaterial } from 'three/nodes';
  4. import * as THREE from 'three';
  5. export class PointsMaterialEditor extends BaseNode {
  6. constructor() {
  7. const material = new PointsNodeMaterial();
  8. super( 'Points 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 size = new LabelElement( 'size' ).setInput( 1 );
  13. const position = new LabelElement( 'position' ).setInput( 3 );
  14. const sizeAttenuation = new LabelElement( 'Size Attenuation' );
  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. sizeAttenuation.add( new ToggleInput( material.sizeAttenuation ).onClick( ( input ) => {
  23. material.sizeAttenuation = input.getValue();
  24. material.dispose();
  25. } ) );
  26. color.onConnect( () => this.update(), true );
  27. opacity.onConnect( () => this.update(), true );
  28. size.onConnect( () => this.update(), true );
  29. position.onConnect( () => this.update(), true );
  30. this.add( color )
  31. .add( opacity )
  32. .add( size )
  33. .add( position )
  34. .add( sizeAttenuation );
  35. this.color = color;
  36. this.opacity = opacity;
  37. this.size = size;
  38. this.position = position;
  39. this.sizeAttenuation = sizeAttenuation;
  40. this.material = material;
  41. this.update();
  42. }
  43. update() {
  44. const { material, color, opacity, size, position } = this;
  45. color.setEnabledInputs( ! color.getLinkedObject() );
  46. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  47. material.colorNode = color.getLinkedObject();
  48. material.opacityNode = opacity.getLinkedObject() || null;
  49. material.sizeNode = size.getLinkedObject() || null;
  50. material.positionNode = position.getLinkedObject() || null;
  51. material.dispose();
  52. this.updateTransparent();
  53. // TODO: Fix on NodeMaterial System
  54. material.customProgramCacheKey = () => {
  55. return THREE.MathUtils.generateUUID();
  56. };
  57. }
  58. updateTransparent() {
  59. const { material, opacity } = this;
  60. material.transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  61. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  62. }
  63. }