WebGLAttributes.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. function WebGLAttributes( gl ) {
  5. var buffers = new WeakMap();
  6. function createBuffer( attribute, bufferType ) {
  7. var array = attribute.array;
  8. var usage = attribute.usage;
  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 ( updateRange.count === - 1 ) {
  43. // Not using update ranges
  44. gl.bufferSubData( bufferType, 0, array );
  45. } else {
  46. gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
  47. array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) );
  48. updateRange.count = - 1; // reset range
  49. }
  50. }
  51. //
  52. function get( attribute ) {
  53. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  54. return buffers.get( attribute );
  55. }
  56. function remove( attribute ) {
  57. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  58. var data = buffers.get( attribute );
  59. if ( data ) {
  60. gl.deleteBuffer( data.buffer );
  61. buffers.delete( attribute );
  62. }
  63. }
  64. function update( attribute, bufferType ) {
  65. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  66. var data = buffers.get( attribute );
  67. if ( data === undefined ) {
  68. buffers.set( attribute, createBuffer( attribute, bufferType ) );
  69. } else if ( data.version < attribute.version ) {
  70. updateBuffer( data.buffer, attribute, bufferType );
  71. data.version = attribute.version;
  72. }
  73. }
  74. return {
  75. get: get,
  76. remove: remove,
  77. update: update
  78. };
  79. }
  80. export { WebGLAttributes };