Browse Source

WebGLObjects: Moved wireframeAttribute code to WebGLGeometries.

Mr.doob 8 years ago
parent
commit
7301fa475d

+ 5 - 3
src/renderers/WebGLRenderer.js

@@ -16,9 +16,10 @@ import { OrthographicCamera } from '../cameras/OrthographicCamera';
 import { WebGLAttributes } from './webgl/WebGLAttributes';
 import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer';
 import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer';
+import { WebGLGeometries } from './webgl/WebGLGeometries';
 import { WebGLLights } from './webgl/WebGLLights';
-import { WebGLPrograms } from './webgl/WebGLPrograms';
 import { WebGLObjects } from './webgl/WebGLObjects';
+import { WebGLPrograms } from './webgl/WebGLPrograms';
 import { WebGLTextures } from './webgl/WebGLTextures';
 import { WebGLProperties } from './webgl/WebGLProperties';
 import { WebGLState } from './webgl/WebGLState';
@@ -287,7 +288,8 @@ function WebGLRenderer( parameters ) {
 	var properties = new WebGLProperties();
 	var textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, paramThreeToGL, this.info );
 	var attributes = new WebGLAttributes( _gl );
-	var objects = new WebGLObjects( _gl, attributes, properties, this.info );
+	var geometries = new WebGLGeometries( _gl, attributes, this.info );
+	var objects = new WebGLObjects( _gl, geometries );
 	var programCache = new WebGLPrograms( this, capabilities );
 	var lightCache = new WebGLLights();
 
@@ -770,7 +772,7 @@ function WebGLRenderer( parameters ) {
 
 		if ( material.wireframe === true ) {
 
-			index = objects.getWireframeAttribute( geometry );
+			index = geometries.getWireframeAttribute( geometry );
 			rangeFactor = 2;
 
 		}

+ 1 - 1
src/renderers/webgl/WebGLAttributes.js

@@ -109,7 +109,7 @@ function WebGLAttributes( gl ) {
 
 		var data = buffers[ attribute.id ];
 
-		if ( data !== undefined ) {
+		if ( data ) {
 
 			gl.deleteBuffer( data.buffer );
 

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

@@ -2,11 +2,14 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
+import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute';
 import { BufferGeometry } from '../../core/BufferGeometry';
+import { arrayMax } from '../../utils';
 
-function WebGLGeometries( gl, attributes, properties, info ) {
+function WebGLGeometries( gl, attributes, info ) {
 
 	var geometries = {};
+	var wireframeAttributes = {};
 
 	function onGeometryDispose( event ) {
 
@@ -29,28 +32,26 @@ function WebGLGeometries( gl, attributes, properties, info ) {
 
 		delete geometries[ geometry.id ];
 
-		// TODO
+		// TODO Remove duplicate code
 
-		var property = properties.get( geometry );
+		var attribute = wireframeAttributes[ geometry.id ];
 
-		if ( property.wireframe ) {
+		if ( attribute ) {
 
-			attributes.remove( property.wireframe );
+			attributes.remove( attribute );
+			delete wireframeAttributes[ geometry.id ];
 
 		}
 
-		properties.remove( geometry );
+		attribute = wireframeAttributes[ buffergeometry.id ];
 
-		var bufferproperty = properties.get( buffergeometry );
+		if ( attribute ) {
 
-		if ( bufferproperty.wireframe ) {
-
-			attributes.remove( bufferproperty.wireframe );
+			attributes.remove( attribute );
+			delete wireframeAttributes[ buffergeometry.id ];
 
 		}
 
-		properties.remove( buffergeometry );
-
 		//
 
 		info.memory.geometries --;
@@ -60,17 +61,12 @@ function WebGLGeometries( gl, attributes, properties, info ) {
 	function get( object ) {
 
 		var geometry = object.geometry;
+		var buffergeometry = geometries[ geometry.id ];
 
-		if ( geometries[ geometry.id ] !== undefined ) {
-
-			return geometries[ geometry.id ];
-
-		}
+		if ( buffergeometry ) return buffergeometry;
 
 		geometry.addEventListener( 'dispose', onGeometryDispose );
 
-		var buffergeometry;
-
 		if ( geometry.isBufferGeometry ) {
 
 			buffergeometry = geometry;
@@ -95,9 +91,101 @@ function WebGLGeometries( gl, attributes, properties, info ) {
 
 	}
 
+	function getWireframeAttribute( geometry ) {
+
+		var attribute = wireframeAttributes[ geometry.id ];
+
+		if ( attribute ) return attribute;
+
+		var indices = [];
+
+		var geometryIndex = geometry.index;
+		var geometryAttributes = geometry.attributes;
+
+		// console.time( 'wireframe' );
+
+		if ( geometryIndex !== null ) {
+
+			var array = geometryIndex.array;
+
+			for ( var i = 0, l = array.length; i < l; i += 3 ) {
+
+				var a = array[ i + 0 ];
+				var b = array[ i + 1 ];
+				var c = array[ i + 2 ];
+
+				indices.push( a, b, b, c, c, a );
+
+			}
+
+		} else {
+
+			var array = geometryAttributes.position.array;
+
+			for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
+
+				var a = i + 0;
+				var b = i + 1;
+				var c = i + 2;
+
+				indices.push( a, b, b, c, c, a );
+
+			}
+
+		}
+
+		// console.timeEnd( 'wireframe' );
+
+		attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
+
+		attributes.update( attribute, gl.ELEMENT_ARRAY_BUFFER );
+
+		wireframeAttributes[ geometry.id ] = attribute;
+
+		return attribute;
+
+	}
+
+	function update( geometry ) {
+
+		var index = geometry.index;
+		var geometryAttributes = geometry.attributes;
+
+		if ( index !== null ) {
+
+			attributes.update( index, gl.ELEMENT_ARRAY_BUFFER );
+
+		}
+
+		for ( var name in geometryAttributes ) {
+
+			attributes.update( geometryAttributes[ name ], gl.ARRAY_BUFFER );
+
+		}
+
+		// morph targets
+
+		var morphAttributes = geometry.morphAttributes;
+
+		for ( var name in morphAttributes ) {
+
+			var array = morphAttributes[ name ];
+
+			for ( var i = 0, l = array.length; i < l; i ++ ) {
+
+				attributes.update( array[ i ], gl.ARRAY_BUFFER );
+
+			}
+
+		}
+
+	}
+
 	return {
 
-		get: get
+		get: get,
+		getWireframeAttribute: getWireframeAttribute,
+		update: update
 
 	};
 

+ 2 - 99
src/renderers/webgl/WebGLObjects.js

@@ -2,15 +2,7 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute';
-import { arrayMax } from '../../utils';
-import { WebGLGeometries } from './WebGLGeometries';
-
-function WebGLObjects( gl, attributes, properties, info ) {
-
-	var geometries = new WebGLGeometries( gl, attributes, properties, info );
-
-	//
+function WebGLObjects( gl, geometries ) {
 
 	function update( object ) {
 
@@ -24,103 +16,14 @@ function WebGLObjects( gl, attributes, properties, info ) {
 
 		}
 
-		var index = geometry.index;
-		var geometryAttributes = geometry.attributes;
-
-		if ( index !== null ) {
-
-			attributes.update( index, gl.ELEMENT_ARRAY_BUFFER );
-
-		}
-
-		for ( var name in geometryAttributes ) {
-
-			attributes.update( geometryAttributes[ name ], gl.ARRAY_BUFFER );
-
-		}
-
-		// morph targets
-
-		var morphAttributes = geometry.morphAttributes;
-
-		for ( var name in morphAttributes ) {
-
-			var array = morphAttributes[ name ];
-
-			for ( var i = 0, l = array.length; i < l; i ++ ) {
-
-				attributes.update( array[ i ], gl.ARRAY_BUFFER );
-
-			}
-
-		}
+		geometries.update( geometry );
 
 		return geometry;
 
 	}
 
-	function getWireframeAttribute( geometry ) {
-
-		var property = properties.get( geometry );
-
-		if ( property.wireframe !== undefined ) {
-
-			return property.wireframe;
-
-		}
-
-		var indices = [];
-
-		var geometryIndex = geometry.index;
-		var geometryAttributes = geometry.attributes;
-
-		// console.time( 'wireframe' );
-
-		if ( geometryIndex !== null ) {
-
-			var array = geometryIndex.array;
-
-			for ( var i = 0, l = array.length; i < l; i += 3 ) {
-
-				var a = array[ i + 0 ];
-				var b = array[ i + 1 ];
-				var c = array[ i + 2 ];
-
-				indices.push( a, b, b, c, c, a );
-
-			}
-
-		} else {
-
-			var array = geometryAttributes.position.array;
-
-			for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
-
-				var a = i + 0;
-				var b = i + 1;
-				var c = i + 2;
-
-				indices.push( a, b, b, c, c, a );
-
-			}
-
-		}
-
-		// console.timeEnd( 'wireframe' );
-
-		var attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
-
-		attributes.update( attribute, gl.ELEMENT_ARRAY_BUFFER );
-
-		property.wireframe = attribute;
-
-		return attribute;
-
-	}
-
 	return {
 
-		getWireframeAttribute: getWireframeAttribute,
 		update: update
 
 	};