UniformsGroup.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { EventDispatcher } from './EventDispatcher.js';
  2. import { StaticDrawUsage } from '../constants.js';
  3. let _id = 0;
  4. class UniformsGroup extends EventDispatcher {
  5. constructor() {
  6. super();
  7. this.isUniformsGroup = true;
  8. Object.defineProperty( this, 'id', { value: _id ++ } );
  9. this.name = '';
  10. this.usage = StaticDrawUsage;
  11. this.uniforms = [];
  12. }
  13. add( uniform ) {
  14. this.uniforms.push( uniform );
  15. return this;
  16. }
  17. remove( uniform ) {
  18. const index = this.uniforms.indexOf( uniform );
  19. if ( index !== - 1 ) this.uniforms.splice( index, 1 );
  20. return this;
  21. }
  22. setName( name ) {
  23. this.name = name;
  24. return this;
  25. }
  26. setUsage( value ) {
  27. this.usage = value;
  28. return this;
  29. }
  30. dispose() {
  31. this.dispatchEvent( { type: 'dispose' } );
  32. return this;
  33. }
  34. copy( source ) {
  35. this.name = source.name;
  36. this.usage = source.usage;
  37. const uniformsSource = source.uniforms;
  38. this.uniforms.length = 0;
  39. for ( let i = 0, l = uniformsSource.length; i < l; i ++ ) {
  40. const uniforms = Array.isArray( uniformsSource[ i ] ) ? uniformsSource[ i ] : [ uniformsSource[ i ] ];
  41. for ( let j = 0; j < uniforms.length; j ++ ) {
  42. this.uniforms.push( uniforms[ j ].clone() );
  43. }
  44. }
  45. return this;
  46. }
  47. clone() {
  48. return new this.constructor().copy( this );
  49. }
  50. }
  51. export { UniformsGroup };