Selaa lähdekoodia

WebGLAttributes refactoring.

Mr.doob 8 vuotta sitten
vanhempi
commit
a55ecbeba1

+ 4 - 0
src/core/BufferAttribute.js

@@ -8,6 +8,8 @@ import { _Math } from '../math/Math';
  * @author mrdoob / http://mrdoob.com/
  */
 
+var bufferAttributeId = 0;
+
 function BufferAttribute( array, itemSize, normalized ) {
 
 	if ( Array.isArray( array ) ) {
@@ -16,6 +18,8 @@ function BufferAttribute( array, itemSize, normalized ) {
 
 	}
 
+	Object.defineProperty( this, 'id', { value: bufferAttributeId ++ } );
+
 	this.uuid = _Math.generateUUID();
 
 	this.array = array;

+ 36 - 20
src/renderers/WebGLRenderer.js

@@ -26,7 +26,7 @@ import { WebGLCapabilities } from './webgl/WebGLCapabilities';
 import { BufferGeometry } from '../core/BufferGeometry';
 import { WebGLExtensions } from './webgl/WebGLExtensions';
 import { Vector3 } from '../math/Vector3';
-import { Sphere } from '../math/Sphere';
+// import { Sphere } from '../math/Sphere';
 import { WebGLClipping } from './webgl/WebGLClipping';
 import { Frustum } from '../math/Frustum';
 import { Vector4 } from '../math/Vector4';
@@ -286,7 +286,7 @@ function WebGLRenderer( parameters ) {
 
 	var properties = new WebGLProperties();
 	var textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, paramThreeToGL, this.info );
-	var attributes = new WebGLAttributes( _gl, properties );
+	var attributes = new WebGLAttributes( _gl );
 	var objects = new WebGLObjects( _gl, attributes, properties, this.info );
 	var programCache = new WebGLPrograms( this, capabilities );
 	var lightCache = new WebGLLights();
@@ -790,7 +790,7 @@ function WebGLRenderer( parameters ) {
 
 			if ( index !== null ) {
 
-				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attributes.get( index ).__webglBuffer );
+				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attributes.get( index ).buffer );
 
 			}
 
@@ -939,7 +939,7 @@ function WebGLRenderer( parameters ) {
 
 					var attributeProperties = attributes.get( geometryAttribute );
 
-					var buffer = attributeProperties.__webglBuffer;
+					var buffer = attributeProperties.buffer;
 					var type = attributeProperties.type;
 					var bytesPerElement = attributeProperties.bytesPerElement;
 
@@ -1303,7 +1303,7 @@ function WebGLRenderer( parameters ) {
 
 		var renderItem = array[ index ];
 
-		if ( renderItem !== undefined ) {
+		if ( renderItem ) {
 
 			renderItem.id = object.id;
 			renderItem.object = object;
@@ -2310,7 +2310,7 @@ function WebGLRenderer( parameters ) {
 
 	function setupLights( lights, camera ) {
 
-		var l, ll, light,
+		var l, ll, light, shadow,
 			r = 0, g = 0, b = 0,
 			color,
 			intensity,
@@ -2355,15 +2355,19 @@ function WebGLRenderer( parameters ) {
 
 				if ( light.castShadow ) {
 
-					uniforms.shadowBias = light.shadow.bias;
-					uniforms.shadowRadius = light.shadow.radius;
-					uniforms.shadowMapSize = light.shadow.mapSize;
+					shadow = light.shadow;
+
+					uniforms.shadowBias = shadow.bias;
+					uniforms.shadowRadius = shadow.radius;
+					uniforms.shadowMapSize = shadow.mapSize;
 
 				}
 
 				_lights.directionalShadowMap[ directionalLength ] = shadowMap;
 				_lights.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
-				_lights.directional[ directionalLength ++ ] = uniforms;
+				_lights.directional[ directionalLength ] = uniforms;
+
+				directionalLength ++;
 
 			} else if ( light.isSpotLight ) {
 
@@ -2388,15 +2392,19 @@ function WebGLRenderer( parameters ) {
 
 				if ( light.castShadow ) {
 
-					uniforms.shadowBias = light.shadow.bias;
-					uniforms.shadowRadius = light.shadow.radius;
-					uniforms.shadowMapSize = light.shadow.mapSize;
+					shadow = light.shadow;
+
+					uniforms.shadowBias = shadow.bias;
+					uniforms.shadowRadius = shadow.radius;
+					uniforms.shadowMapSize = shadow.mapSize;
 
 				}
 
 				_lights.spotShadowMap[ spotLength ] = shadowMap;
 				_lights.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
-				_lights.spot[ spotLength ++ ] = uniforms;
+				_lights.spot[ spotLength ] = uniforms;
+
+				spotLength ++;
 
 			} else if ( light.isRectAreaLight ) {
 
@@ -2428,7 +2436,9 @@ function WebGLRenderer( parameters ) {
 				// TODO (abelnation): RectAreaLight distance?
 				// uniforms.distance = distance;
 
-				_lights.rectArea[ rectAreaLength ++ ] = uniforms;
+				_lights.rectArea[ rectAreaLength ] = uniforms;
+
+				rectAreaLength ++;
 
 			} else if ( light.isPointLight ) {
 
@@ -2445,9 +2455,11 @@ function WebGLRenderer( parameters ) {
 
 				if ( light.castShadow ) {
 
-					uniforms.shadowBias = light.shadow.bias;
-					uniforms.shadowRadius = light.shadow.radius;
-					uniforms.shadowMapSize = light.shadow.mapSize;
+					shadow = light.shadow;
+
+					uniforms.shadowBias = shadow.bias;
+					uniforms.shadowRadius = shadow.radius;
+					uniforms.shadowMapSize = shadow.mapSize;
 
 				}
 
@@ -2464,7 +2476,9 @@ function WebGLRenderer( parameters ) {
 				_vector3.setFromMatrixPosition( light.matrixWorld ).negate();
 				_lights.pointShadowMatrix[ pointLength ].identity().setPosition( _vector3 );
 
-				_lights.point[ pointLength ++ ] = uniforms;
+				_lights.point[ pointLength ] = uniforms;
+
+				pointLength ++;
 
 			} else if ( light.isHemisphereLight ) {
 
@@ -2477,7 +2491,9 @@ function WebGLRenderer( parameters ) {
 				uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
 				uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
 
-				_lights.hemi[ hemiLength ++ ] = uniforms;
+				_lights.hemi[ hemiLength ] = uniforms;
+
+				hemiLength ++;
 
 			}
 

+ 43 - 39
src/renderers/webgl/WebGLAttributes.js

@@ -2,19 +2,24 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-function WebGLAttributes( gl, properties ) {
+function WebGLAttributes( gl ) {
 
-	function createBuffer( attributeProperties, data, bufferType ) {
+	var buffers = {};
 
-		attributeProperties.__webglBuffer = gl.createBuffer();
-		gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
+	function createBuffer( attribute, bufferType ) {
 
-		var usage = data.dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
+		var buffer = gl.createBuffer();
 
-		gl.bufferData( bufferType, data.array, usage );
+		gl.bindBuffer( bufferType, buffer );
+
+		var array = attribute.array;
+		var usage = attribute.dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
+
+		gl.bufferData( bufferType, array, usage );
+
+		attribute.onUploadCallback();
 
 		var type = gl.FLOAT;
-		var array = data.array;
 
 		if ( array instanceof Float32Array ) {
 
@@ -50,42 +55,43 @@ function WebGLAttributes( gl, properties ) {
 
 		}
 
-		attributeProperties.bytesPerElement = array.BYTES_PER_ELEMENT;
-		attributeProperties.type = type;
-		attributeProperties.version = data.version;
-
-		data.onUploadCallback();
+		return {
+			buffer: buffer,
+			type: type,
+			bytesPerElement: array.BYTES_PER_ELEMENT,
+			version: attribute.version
+		};
 
 	}
 
-	function updateBuffer( attributeProperties, data, bufferType ) {
+	function updateBuffer( buffer, attribute, bufferType ) {
 
-		gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
+		gl.bindBuffer( bufferType, buffer );
 
-		if ( data.dynamic === false ) {
+		if ( attribute.dynamic === false ) {
 
-			gl.bufferData( bufferType, data.array, gl.STATIC_DRAW );
+			gl.bufferData( bufferType, attribute.array, gl.STATIC_DRAW );
 
-		} else if ( data.updateRange.count === - 1 ) {
+		} else if ( attribute.updateRange.count === - 1 ) {
 
 			// Not using update ranges
 
-			gl.bufferSubData( bufferType, 0, data.array );
+			gl.bufferSubData( bufferType, 0, attribute.array );
 
-		} else if ( data.updateRange.count === 0 ) {
+		} else if ( attribute.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, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
-							  data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
+			gl.bufferSubData( bufferType, attribute.updateRange.offset * attribute.array.BYTES_PER_ELEMENT,
+							  attribute.array.subarray( attribute.updateRange.offset, attribute.updateRange.offset + attribute.updateRange.count ) );
 
-			data.updateRange.count = 0; // reset range
+			attribute.updateRange.count = 0; // reset range
 
 		}
 
-		attributeProperties.version = data.version;
+
 
 	}
 
@@ -93,24 +99,21 @@ function WebGLAttributes( gl, properties ) {
 
 	function get( attribute ) {
 
-		if ( attribute.isInterleavedBufferAttribute ) {
+		if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
 
-			return properties.get( attribute.data );
-
-		}
-
-		return properties.get( attribute );
+		return buffers[ attribute.id ];
 
 	}
 
 	function remove( attribute ) {
 
-		var attributeProperties = get( attribute );
+		var data = buffers[ attribute.id ];
+
+		if ( data !== undefined ) {
 
-		if ( attributeProperties.__webglBuffer !== undefined ) {
+			gl.deleteBuffer( data.buffer );
 
-			gl.deleteBuffer( attributeProperties.__webglBuffer );
-			properties.remove( attributeProperties );
+			delete buffers[ attribute.id ];
 
 		}
 
@@ -118,17 +121,18 @@ function WebGLAttributes( gl, properties ) {
 
 	function update( attribute, bufferType ) {
 
-		var data = ( attribute.isInterleavedBufferAttribute ) ? attribute.data : attribute;
+		if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
 
-		var attributeProperties = properties.get( data );
+		var data = buffers[ attribute.id ];
 
-		if ( attributeProperties.__webglBuffer === undefined ) {
+		if ( data === undefined ) {
 
-			createBuffer( attributeProperties, data, bufferType );
+			buffers[ attribute.id ] = createBuffer( attribute, bufferType );
 
-		} else if ( attributeProperties.version !== data.version ) {
+		} else if ( data.version !== attribute.version ) {
 
-			updateBuffer( attributeProperties, data, bufferType );
+			updateBuffer( data.buffer, attribute, bufferType );
+			data.version = attribute.version;
 
 		}
 

+ 22 - 20
src/renderers/webgl/WebGLGeometries.js

@@ -57,45 +57,47 @@ function WebGLGeometries( gl, attributes, properties, info ) {
 
 	}
 
-	return {
+	function get( object ) {
 
-		get: function ( object ) {
+		var geometry = object.geometry;
 
-			var geometry = object.geometry;
+		if ( geometries[ geometry.id ] !== undefined ) {
 
-			if ( geometries[ geometry.id ] !== undefined ) {
+			return geometries[ geometry.id ];
 
-				return geometries[ geometry.id ];
+		}
 
-			}
+		geometry.addEventListener( 'dispose', onGeometryDispose );
 
-			geometry.addEventListener( 'dispose', onGeometryDispose );
+		var buffergeometry;
 
-			var buffergeometry;
+		if ( geometry.isBufferGeometry ) {
 
-			if ( geometry.isBufferGeometry ) {
+			buffergeometry = geometry;
 
-				buffergeometry = geometry;
+		} else if ( geometry.isGeometry ) {
 
-			} else if ( geometry.isGeometry ) {
+			if ( geometry._bufferGeometry === undefined ) {
 
-				if ( geometry._bufferGeometry === undefined ) {
+				geometry._bufferGeometry = new BufferGeometry().setFromObject( object );
+
+			}
 
-					geometry._bufferGeometry = new BufferGeometry().setFromObject( object );
+			buffergeometry = geometry._bufferGeometry;
 
-				}
+		}
 
-				buffergeometry = geometry._bufferGeometry;
+		geometries[ geometry.id ] = buffergeometry;
 
-			}
+		info.memory.geometries ++;
 
-			geometries[ geometry.id ] = buffergeometry;
+		return buffergeometry;
 
-			info.memory.geometries ++;
+	}
 
-			return buffergeometry;
+	return {
 
-		}
+		get: get
 
 	};
 

+ 5 - 5
src/renderers/webgl/WebGLObjects.js

@@ -71,14 +71,14 @@ function WebGLObjects( gl, attributes, properties, info ) {
 
 		var indices = [];
 
-		var index = geometry.index;
-		var attributes = geometry.attributes;
+		var geometryIndex = geometry.index;
+		var geometryAttributes = geometry.attributes;
 
 		// console.time( 'wireframe' );
 
-		if ( index !== null ) {
+		if ( geometryIndex !== null ) {
 
-			var array = index.array;
+			var array = geometryIndex.array;
 
 			for ( var i = 0, l = array.length; i < l; i += 3 ) {
 
@@ -92,7 +92,7 @@ function WebGLObjects( gl, attributes, properties, info ) {
 
 		} else {
 
-			var array = attributes.position.array;
+			var array = geometryAttributes.position.array;
 
 			for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {