Browse Source

Extract updateAttribute from updateObject

dubejf 10 years ago
parent
commit
e29f28d648
1 changed files with 34 additions and 27 deletions
  1. 34 27
      src/renderers/webgl/WebGLObjects.js

+ 34 - 27
src/renderers/webgl/WebGLObjects.js

@@ -168,60 +168,67 @@ THREE.WebGLObjects = function ( gl, properties, info ) {
 		for ( var name in attributes ) {
 
 			var attribute = attributes[ name ];
+			updateAttribute( attribute, name );
 
-			var bufferType = ( name === 'index' ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
+		}
 
-			var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;
+	}
 
-			var attributeProperties = properties.get( data );
+	function updateAttribute ( attribute, name ) {
 
-			if ( attributeProperties.__webglBuffer === undefined ) {
+		var bufferType = ( name === 'index' ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
 
-				attributeProperties.__webglBuffer = gl.createBuffer();
-				gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
+		var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;
 
-				var usage = gl.STATIC_DRAW;
+		var attributeProperties = properties.get( data );
 
-				if ( data instanceof THREE.DynamicBufferAttribute
-						 || ( data instanceof THREE.InstancedBufferAttribute && data.dynamic === true )
-						 || ( data instanceof THREE.InterleavedBuffer && data.dynamic === true ) ) {
+		if ( attributeProperties.__webglBuffer === undefined ) {
 
-					usage = gl.DYNAMIC_DRAW;
+			attributeProperties.__webglBuffer = gl.createBuffer();
+			gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
 
-				}
+			var usage = gl.STATIC_DRAW;
 
-				gl.bufferData( bufferType, data.array, usage );
+			if ( data instanceof THREE.DynamicBufferAttribute
+				 || ( data instanceof THREE.InstancedBufferAttribute && data.dynamic === true )
+				 || ( data instanceof THREE.InterleavedBuffer && data.dynamic === true ) ) {
 
-				data.needsUpdate = false;
+				usage = gl.DYNAMIC_DRAW;
 
-			} else if ( data.needsUpdate === true ) {
+			}
 
-				gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
+			gl.bufferData( bufferType, data.array, usage );
 
-				if ( data.updateRange === undefined || data.updateRange.count === -1 ) { // Not using update ranges
+			data.needsUpdate = false;
 
-					gl.bufferSubData( bufferType, 0, data.array );
+		} else if ( data.needsUpdate === true ) {
 
-				} else if ( data.updateRange.count === 0 ) {
+			gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
 
-					console.error( 'THREE.WebGLRenderer.updateObject: using updateRange for THREE.DynamicBufferAttribute and marked as needsUpdate but count is 0, ensure you are using set methods or updating manually.' );
+			if ( data.updateRange === undefined || data.updateRange.count === -1 ) { // Not using update ranges
 
-				} else {
+				gl.bufferSubData( bufferType, 0, data.array );
 
-					gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
-									 data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
+			} else if ( data.updateRange.count === 0 ) {
 
-					data.updateRange.count = 0; // reset range
+				console.error( 'THREE.WebGLRenderer.updateObject: using updateRange for THREE.DynamicBufferAttribute and marked as needsUpdate but count is 0, ensure you are using set methods or updating manually.' );
 
-				}
+			} else {
 
-				data.needsUpdate = false;
+				gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
+								  data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
+
+				data.updateRange.count = 0; // reset range
 
 			}
 
+			data.needsUpdate = false;
+
 		}
 
-	};
+	}
+
+
 
 	// returns the webgl buffer for a specified attribute
 	this.getAttributeBuffer = function ( attribute ) {