Attributes.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. return attributeData;
  15. }
  16. update( attribute, type ) {
  17. const data = this.get( attribute );
  18. if ( data.version === undefined ) {
  19. if ( type === AttributeType.VERTEX ) {
  20. this.backend.createAttribute( attribute );
  21. } else if ( type === AttributeType.INDEX ) {
  22. this.backend.createIndexAttribute( attribute );
  23. } else if ( type === AttributeType.STORAGE ) {
  24. this.backend.createStorageAttribute( attribute );
  25. }
  26. data.version = this._getBufferAttribute( attribute ).version;
  27. } else {
  28. const bufferAttribute = this._getBufferAttribute( attribute );
  29. if ( data.version < bufferAttribute.version || bufferAttribute.usage === DynamicDrawUsage ) {
  30. this.backend.updateAttribute( attribute );
  31. data.version = bufferAttribute.version;
  32. }
  33. }
  34. }
  35. _getBufferAttribute( attribute ) {
  36. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  37. return attribute;
  38. }
  39. }
  40. export default Attributes;