WebGLAttributes.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. function WebGLAttributes( gl, capabilities ) {
  2. const isWebGL2 = capabilities.isWebGL2;
  3. const buffers = new WeakMap();
  4. function createBuffer( attribute, bufferType ) {
  5. const array = attribute.array;
  6. const usage = attribute.usage;
  7. const buffer = gl.createBuffer();
  8. gl.bindBuffer( bufferType, buffer );
  9. gl.bufferData( bufferType, array, usage );
  10. attribute.onUploadCallback();
  11. let type = gl.FLOAT;
  12. if ( array instanceof Float32Array ) {
  13. type = gl.FLOAT;
  14. } else if ( array instanceof Float64Array ) {
  15. console.warn( 'THREE.WebGLAttributes: Unsupported data buffer format: Float64Array.' );
  16. } else if ( array instanceof Uint16Array ) {
  17. if ( attribute.isFloat16BufferAttribute ) {
  18. if ( isWebGL2 ) {
  19. type = gl.HALF_FLOAT;
  20. } else {
  21. console.warn( 'THREE.WebGLAttributes: Usage of Float16BufferAttribute requires WebGL2.' );
  22. }
  23. } else {
  24. type = gl.UNSIGNED_SHORT;
  25. }
  26. } else if ( array instanceof Int16Array ) {
  27. type = gl.SHORT;
  28. } else if ( array instanceof Uint32Array ) {
  29. type = gl.UNSIGNED_INT;
  30. } else if ( array instanceof Int32Array ) {
  31. type = gl.INT;
  32. } else if ( array instanceof Int8Array ) {
  33. type = gl.BYTE;
  34. } else if ( array instanceof Uint8Array ) {
  35. type = gl.UNSIGNED_BYTE;
  36. } else if ( array instanceof Uint8ClampedArray ) {
  37. type = gl.UNSIGNED_BYTE;
  38. }
  39. return {
  40. buffer: buffer,
  41. type: type,
  42. bytesPerElement: array.BYTES_PER_ELEMENT,
  43. version: attribute.version
  44. };
  45. }
  46. function updateBuffer( buffer, attribute, bufferType ) {
  47. const array = attribute.array;
  48. const updateRange = attribute.updateRange;
  49. gl.bindBuffer( bufferType, buffer );
  50. if ( updateRange.count === - 1 ) {
  51. // Not using update ranges
  52. gl.bufferSubData( bufferType, 0, array );
  53. } else {
  54. if ( isWebGL2 ) {
  55. gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
  56. array, updateRange.offset, updateRange.count );
  57. } else {
  58. gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
  59. array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) );
  60. }
  61. updateRange.count = - 1; // reset range
  62. }
  63. }
  64. //
  65. function get( attribute ) {
  66. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  67. return buffers.get( attribute );
  68. }
  69. function remove( attribute ) {
  70. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  71. const data = buffers.get( attribute );
  72. if ( data ) {
  73. gl.deleteBuffer( data.buffer );
  74. buffers.delete( attribute );
  75. }
  76. }
  77. function update( attribute, bufferType ) {
  78. if ( attribute.isGLBufferAttribute ) {
  79. const cached = buffers.get( attribute );
  80. if ( ! cached || cached.version < attribute.version ) {
  81. buffers.set( attribute, {
  82. buffer: attribute.buffer,
  83. type: attribute.type,
  84. bytesPerElement: attribute.elementSize,
  85. version: attribute.version
  86. } );
  87. }
  88. return;
  89. }
  90. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  91. const data = buffers.get( attribute );
  92. if ( data === undefined ) {
  93. buffers.set( attribute, createBuffer( attribute, bufferType ) );
  94. } else if ( data.version < attribute.version ) {
  95. updateBuffer( data.buffer, attribute, bufferType );
  96. data.version = attribute.version;
  97. }
  98. }
  99. return {
  100. get: get,
  101. remove: remove,
  102. update: update
  103. };
  104. }
  105. export { WebGLAttributes };
粤ICP备19079148号