WebGLAttributeUtils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class WebGLAttributeUtils {
  2. constructor( backend ) {
  3. this.backend = backend;
  4. }
  5. createAttribute( attribute, bufferType ) {
  6. const backend = this.backend;
  7. const { gl } = backend;
  8. const array = attribute.array;
  9. const usage = attribute.usage || gl.STATIC_DRAW;
  10. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  11. const bufferData = backend.get( bufferAttribute );
  12. let bufferGPU = bufferData.bufferGPU;
  13. if ( bufferGPU === undefined ) {
  14. bufferGPU = gl.createBuffer();
  15. gl.bindBuffer( bufferType, bufferGPU );
  16. gl.bufferData( bufferType, array, usage );
  17. gl.bindBuffer( bufferType, null );
  18. bufferData.bufferGPU = bufferGPU;
  19. bufferData.bufferType = bufferType;
  20. bufferData.version = bufferAttribute.version;
  21. }
  22. //attribute.onUploadCallback();
  23. let type;
  24. if ( array instanceof Float32Array ) {
  25. type = gl.FLOAT;
  26. } else if ( array instanceof Uint16Array ) {
  27. if ( attribute.isFloat16BufferAttribute ) {
  28. type = gl.HALF_FLOAT;
  29. } else {
  30. type = gl.UNSIGNED_SHORT;
  31. }
  32. } else if ( array instanceof Int16Array ) {
  33. type = gl.SHORT;
  34. } else if ( array instanceof Uint32Array ) {
  35. type = gl.UNSIGNED_INT;
  36. } else if ( array instanceof Int32Array ) {
  37. type = gl.INT;
  38. } else if ( array instanceof Int8Array ) {
  39. type = gl.BYTE;
  40. } else if ( array instanceof Uint8Array ) {
  41. type = gl.UNSIGNED_BYTE;
  42. } else if ( array instanceof Uint8ClampedArray ) {
  43. type = gl.UNSIGNED_BYTE;
  44. } else {
  45. throw new Error( 'THREE.WebGLBackend: Unsupported buffer data format: ' + array );
  46. }
  47. backend.set( attribute, {
  48. bufferGPU,
  49. type,
  50. bytesPerElement: array.BYTES_PER_ELEMENT
  51. } );
  52. }
  53. updateAttribute( attribute ) {
  54. const backend = this.backend;
  55. const { gl } = backend;
  56. const array = attribute.array;
  57. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  58. const bufferData = backend.get( bufferAttribute );
  59. const bufferType = bufferData.bufferType;
  60. gl.bindBuffer( bufferType, bufferData.bufferGPU );
  61. gl.bufferSubData( bufferType, 0, array );
  62. gl.bindBuffer( bufferType, null );
  63. bufferData.version = bufferAttribute.version;
  64. }
  65. }
  66. export default WebGLAttributeUtils;