2
0

WebGLAttributeUtils.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. let isFloat = false;
  25. if ( array instanceof Float32Array ) {
  26. type = gl.FLOAT;
  27. isFloat = true;
  28. } else if ( array instanceof Uint16Array ) {
  29. if ( attribute.isFloat16BufferAttribute ) {
  30. type = gl.HALF_FLOAT;
  31. isFloat = true;
  32. } else {
  33. type = gl.UNSIGNED_SHORT;
  34. }
  35. } else if ( array instanceof Int16Array ) {
  36. type = gl.SHORT;
  37. } else if ( array instanceof Uint32Array ) {
  38. type = gl.UNSIGNED_INT;
  39. } else if ( array instanceof Int32Array ) {
  40. type = gl.INT;
  41. } else if ( array instanceof Int8Array ) {
  42. type = gl.BYTE;
  43. } else if ( array instanceof Uint8Array ) {
  44. type = gl.UNSIGNED_BYTE;
  45. } else if ( array instanceof Uint8ClampedArray ) {
  46. type = gl.UNSIGNED_BYTE;
  47. } else {
  48. throw new Error( 'THREE.WebGLBackend: Unsupported buffer data format: ' + array );
  49. }
  50. backend.set( attribute, {
  51. bufferGPU,
  52. type,
  53. bytesPerElement: array.BYTES_PER_ELEMENT,
  54. version: attribute.version,
  55. isFloat
  56. } );
  57. }
  58. updateAttribute( attribute ) {
  59. const backend = this.backend;
  60. const { gl } = backend;
  61. const array = attribute.array;
  62. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  63. const bufferData = backend.get( bufferAttribute );
  64. const bufferType = bufferData.bufferType;
  65. gl.bindBuffer( bufferType, bufferData.bufferGPU );
  66. gl.bufferSubData( bufferType, 0, array );
  67. gl.bindBuffer( bufferType, null );
  68. bufferData.version = bufferAttribute.version;
  69. }
  70. }
  71. export default WebGLAttributeUtils;