123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- function WebGLAttributes( gl ) {
- var buffers = {};
- function createBuffer( attribute, bufferType ) {
- var array = attribute.array;
- var usage = attribute.dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
- var buffer = gl.createBuffer();
- gl.bindBuffer( bufferType, buffer );
- gl.bufferData( bufferType, array, usage );
- attribute.onUploadCallback();
- var type = gl.FLOAT;
- if ( array instanceof Float32Array ) {
- type = gl.FLOAT;
- } else if ( array instanceof Float64Array ) {
- console.warn( 'THREE.WebGLAttributes: Unsupported data buffer format: Float64Array.' );
- } else if ( array instanceof Uint16Array ) {
- type = gl.UNSIGNED_SHORT;
- } else if ( array instanceof Int16Array ) {
- type = gl.SHORT;
- } else if ( array instanceof Uint32Array ) {
- type = gl.UNSIGNED_INT;
- } else if ( array instanceof Int32Array ) {
- type = gl.INT;
- } else if ( array instanceof Int8Array ) {
- type = gl.BYTE;
- } else if ( array instanceof Uint8Array ) {
- type = gl.UNSIGNED_BYTE;
- }
- return {
- buffer: buffer,
- type: type,
- bytesPerElement: array.BYTES_PER_ELEMENT,
- version: attribute.version
- };
- }
- function updateBuffer( buffer, attribute, bufferType ) {
- var array = attribute.array;
- var updateRange = attribute.updateRange;
- gl.bindBuffer( bufferType, buffer );
- if ( attribute.dynamic === false ) {
- gl.bufferData( bufferType, array, gl.STATIC_DRAW );
- } else if ( updateRange.count === - 1 ) {
- // Not using update ranges
- gl.bufferSubData( bufferType, 0, array );
- } else if ( updateRange.count === 0 ) {
- 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.' );
- } else {
- gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
- array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) );
- updateRange.count = - 1; // reset range
- }
- }
- //
- function get( attribute ) {
- if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
- return buffers[ attribute.uuid ];
- }
- function remove( attribute ) {
- if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
- var data = buffers[ attribute.uuid ];
- if ( data ) {
- gl.deleteBuffer( data.buffer );
- delete buffers[ attribute.uuid ];
- }
- }
- function update( attribute, bufferType ) {
- if (attribute.isDynamicBufferAttribute) {
- var cached = buffers[ attribute.uuid ];
- if (cached && cached.version >= attribute.version) {
- return;
- }
- buffers[ attribute.uuid ] = {
- buffer: attribute.buffer,
- type: attribute.type,
- bytesPerElement: attribute.elementSize,
- version: attribute.version
- };
- return;
- }
- if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
- var data = buffers[ attribute.uuid ];
- if ( data === undefined ) {
- buffers[ attribute.uuid ] = createBuffer( attribute, bufferType );
- } else if ( data.version < attribute.version ) {
- updateBuffer( data.buffer, attribute, bufferType );
- data.version = attribute.version;
- }
- }
- return {
- get: get,
- remove: remove,
- update: update
- };
- }
- export { WebGLAttributes };
|