Attributes.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import DataMap from './DataMap.js';
  2. import { AttributeType } from './Constants.js';
  3. import { DynamicDrawUsage } from 'three';
  4. class Attributes extends DataMap {
  5. constructor( backend ) {
  6. super();
  7. this.backend = backend;
  8. }
  9. delete( attribute ) {
  10. const attributeData = super.delete( attribute );
  11. if ( attributeData !== undefined ) {
  12. this.backend.destroyAttribute( attribute );
  13. }
  14. }
  15. update( attribute, type ) {
  16. const data = this.get( attribute );
  17. if ( data.version === undefined ) {
  18. if ( type === AttributeType.VERTEX ) {
  19. this.backend.createAttribute( attribute );
  20. } else if ( type === AttributeType.INDEX ) {
  21. this.backend.createIndexAttribute( attribute );
  22. } else if ( type === AttributeType.STORAGE ) {
  23. this.backend.createStorageAttribute( attribute );
  24. }
  25. data.version = this._getBufferAttribute( attribute ).version;
  26. } else {
  27. const bufferAttribute = this._getBufferAttribute( attribute );
  28. if ( data.version < bufferAttribute.version || bufferAttribute.usage === DynamicDrawUsage ) {
  29. this.backend.updateAttribute( attribute );
  30. data.version = bufferAttribute.version;
  31. }
  32. }
  33. }
  34. _getBufferAttribute( attribute ) {
  35. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  36. return attribute;
  37. }
  38. }
  39. export default Attributes;