PointsMaterialEditor.js 2.4 KB

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