WebGLAttributes.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 = 0; // 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. var data = buffers[ attribute.uuid ];
  62. if ( data ) {
  63. gl.deleteBuffer( data.buffer );
  64. delete buffers[ attribute.uuid ];
  65. }
  66. }
  67. function update( attribute, bufferType ) {
  68. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  69. var data = buffers[ attribute.uuid ];
  70. if ( data === undefined ) {
  71. buffers[ attribute.uuid ] = createBuffer( attribute, bufferType );
  72. } else if ( data.version < attribute.version ) {
  73. updateBuffer( data.buffer, attribute, bufferType );
  74. data.version = attribute.version;
  75. }
  76. }
  77. return {
  78. get: get,
  79. remove: remove,
  80. update: update
  81. };
  82. }
  83. export { WebGLAttributes };