PointsMaterialEditor.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { ColorInput, ToggleInput, SliderInput, LabelElement } from 'flow';
  2. import { MaterialEditor } from './MaterialEditor.js';
  3. import { PointsNodeMaterial } from 'three/tsl';
  4. import * as THREE from 'three';
  5. import { setInputAestheticsFromType } from '../DataTypeLib.js';
  6. export class PointsMaterialEditor extends MaterialEditor {
  7. constructor() {
  8. const material = new PointsNodeMaterial();
  9. super( 'Points Material', material );
  10. const color = setInputAestheticsFromType( new LabelElement( 'color' ), 'Color' );
  11. const opacity = setInputAestheticsFromType( new LabelElement( 'opacity' ), 'Number' );
  12. const size = setInputAestheticsFromType( new LabelElement( 'size' ), 'Number' );
  13. const position = setInputAestheticsFromType( new LabelElement( 'position' ), 'Vector3' );
  14. const sizeAttenuation = setInputAestheticsFromType( new LabelElement( 'Size Attenuation' ), 'Number' );
  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.update();
  41. }
  42. update() {
  43. const { material, color, opacity, size, position } = this;
  44. color.setEnabledInputs( ! color.getLinkedObject() );
  45. opacity.setEnabledInputs( ! opacity.getLinkedObject() );
  46. material.colorNode = color.getLinkedObject();
  47. material.opacityNode = opacity.getLinkedObject() || null;
  48. material.sizeNode = size.getLinkedObject() || null;
  49. material.positionNode = position.getLinkedObject() || null;
  50. material.dispose();
  51. this.updateTransparent();
  52. // TODO: Fix on NodeMaterial System
  53. material.customProgramCacheKey = () => {
  54. return THREE.MathUtils.generateUUID();
  55. };
  56. }
  57. updateTransparent() {
  58. const { material, opacity } = this;
  59. material.transparent = opacity.getLinkedObject() || material.opacity < 1 ? true : false;
  60. opacity.setIcon( material.transparent ? 'ti ti-layers-intersect' : 'ti ti-layers-subtract' );
  61. }
  62. }