WebGLAttributes.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. function WebGLAttributes( gl ) {
  5. var buffers = {};
  6. function createBuffer( attribute, bufferType ) {
  7. var array = attribute.array;
  8. var usage = attribute.dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
  9. var buffer = gl.createBuffer();
  10. gl.bindBuffer( bufferType, buffer );
  11. gl.bufferData( bufferType, array, usage );
  12. attribute.onUploadCallback();
  13. var type = gl.FLOAT;
  14. if ( array instanceof Float32Array ) {
  15. type = gl.FLOAT;
  16. } else if ( array instanceof Float64Array ) {
  17. console.warn( 'THREE.WebGLAttributes: Unsupported data buffer format: Float64Array.' );
  18. } else if ( array instanceof Uint16Array ) {
  19. type = gl.UNSIGNED_SHORT;
  20. } else if ( array instanceof Int16Array ) {
  21. type = gl.SHORT;
  22. } else if ( array instanceof Uint32Array ) {
  23. type = gl.UNSIGNED_INT;
  24. } else if ( array instanceof Int32Array ) {
  25. type = gl.INT;
  26. } else if ( array instanceof Int8Array ) {
  27. type = gl.BYTE;
  28. } else if ( array instanceof Uint8Array ) {
  29. type = gl.UNSIGNED_BYTE;
  30. }
  31. return {
  32. buffer: buffer,
  33. type: type,
  34. bytesPerElement: array.BYTES_PER_ELEMENT,
  35. version: attribute.version
  36. };
  37. }
  38. function updateBuffer( buffer, attribute, bufferType ) {
  39. var array = attribute.array;
  40. var updateRange = attribute.updateRange;
  41. gl.bindBuffer( bufferType, buffer );
  42. if ( attribute.dynamic === false ) {
  43. gl.bufferData( bufferType, array, gl.STATIC_DRAW );
  44. } else if ( updateRange.count === - 1 ) {
  45. // Not using update ranges
  46. gl.bufferSubData( bufferType, 0, array );
  47. } else if ( updateRange.count === 0 ) {
  48. console.error( 'THREE.WebGLObjects.updateBuffer: dynamic THREE.BufferAttribute marked as needsUpdate but updateRange.count is 0, ensure you are using set methods or updating manually.' );
  49. } else {
  50. gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
  51. array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) );
  52. updateRange.count = - 1; // reset range
  53. }
  54. }
  55. //
  56. function get( attribute ) {
  57. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  58. return buffers[ attribute.uuid ];
  59. }
  60. function remove( attribute ) {
  61. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  62. var data = buffers[ attribute.uuid ];
  63. if ( data ) {
  64. gl.deleteBuffer( data.buffer );
  65. delete buffers[ attribute.uuid ];
  66. }
  67. }
  68. function update( attribute, bufferType ) {
  69. if (attribute.isDynamicBufferAttribute) {
  70. var cached = buffers[ attribute.uuid ];
  71. if (cached && cached.version >= attribute.version) {
  72. return;
  73. }
  74. buffers[ attribute.uuid ] = {
  75. buffer: attribute.buffer,
  76. type: attribute.type,
  77. bytesPerElement: attribute.elementSize,
  78. version: attribute.version
  79. };
  80. return;
  81. }
  82. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  83. var data = buffers[ attribute.uuid ];
  84. if ( data === undefined ) {
  85. buffers[ attribute.uuid ] = createBuffer( attribute, bufferType );
  86. } else if ( data.version < attribute.version ) {
  87. updateBuffer( data.buffer, attribute, bufferType );
  88. data.version = attribute.version;
  89. }
  90. }
  91. return {
  92. get: get,
  93. remove: remove,
  94. update: update
  95. };
  96. }
  97. export { WebGLAttributes };