WebGLGeometries.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.WebGLGeometries = function ( gl,properties, info ) {
  5. var geometries = {};
  6. this.get = function ( object ) {
  7. var geometry = object.geometry;
  8. if ( geometries[ geometry.id ] !== undefined ) {
  9. return geometries[ geometry.id ];
  10. }
  11. geometry.addEventListener( 'dispose', onGeometryDispose );
  12. if ( geometry instanceof THREE.BufferGeometry ) {
  13. geometries[ geometry.id ] = geometry;
  14. } else if ( geometry instanceof THREE.Geometry ) {
  15. if ( object._bufferGeometry === undefined ) {
  16. object._bufferGeometry = new THREE.BufferGeometry().setFromObject( object );
  17. }
  18. geometries[ geometry.id ] = object._bufferGeometry;
  19. }
  20. info.memory.geometries ++;
  21. return geometries[ geometry.id ];
  22. };
  23. function onGeometryDispose( event ) {
  24. var geometry = event.target;
  25. var buffergeometry = geometries[ geometry.id ];
  26. for ( var name in buffergeometry.attributes ) {
  27. var attribute = buffergeometry.attributes[ name ];
  28. var buffer = getAttributeBuffer(attribute);
  29. if ( buffer !== undefined ) {
  30. gl.deleteBuffer( buffer );
  31. removeAttributeBuffer(attribute);
  32. }
  33. }
  34. geometry.removeEventListener( 'dispose', onGeometryDispose );
  35. delete geometries[ geometry.id ];
  36. info.memory.geometries --;
  37. }
  38. function getAttributeBuffer( attribute ) {
  39. if ( attribute instanceof THREE.InterleavedBufferAttribute ) {
  40. return properties.get( attribute.data ).__webglBuffer;
  41. }
  42. return properties.get( attribute ).__webglBuffer;
  43. };
  44. function removeAttributeBuffer( attribute ) {
  45. if ( attribute instanceof THREE.InterleavedBufferAttribute ) {
  46. properties.delete( attribute.data );
  47. } else {
  48. properties.delete( attribute );
  49. };
  50. };
  51. };