فهرست منبع

Support custom BufferGeometry attributes in WebGLRenderer.

- fixes #3017 and possibly #3084
zz85 12 سال پیش
والد
کامیت
8c723f74e1
1فایلهای تغییر یافته به همراه52 افزوده شده و 146 حذف شده
  1. 52 146
      src/renderers/WebGLRenderer.js

+ 52 - 146
src/renderers/WebGLRenderer.js

@@ -3398,11 +3398,13 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 		if ( material.visible === false ) return;
 		if ( material.visible === false ) return;
 
 
-		var program, attributes, linewidth, primitives, a, attribute;
+		var program, programAttributes, linewidth, primitives, a, attribute, geometryAttributes;
+		var attributeItem, attributeName, attributePointer;
 
 
 		program = setProgram( camera, lights, fog, material, object );
 		program = setProgram( camera, lights, fog, material, object );
 
 
-		attributes = program.attributes;
+		programAttributes = program.attributes;
+		geometryAttributes = geometry.attributes;
 
 
 		var updateBuffers = false,
 		var updateBuffers = false,
 			wireframeBit = material.wireframe ? 1 : 0,
 			wireframeBit = material.wireframe ? 1 : 0,
@@ -3425,7 +3427,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 		if ( object instanceof THREE.Mesh ) {
 		if ( object instanceof THREE.Mesh ) {
 
 
-			var index = geometry.attributes[ "index" ];
+			var index = geometryAttributes[ "index" ];
 
 
 			// indexed triangles
 			// indexed triangles
 
 
@@ -3445,68 +3447,21 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 					if ( updateBuffers ) {
 					if ( updateBuffers ) {
 
 
-						// vertices
-
-						var position = geometry.attributes[ "position" ];
-						var positionSize = position.itemSize;
-
-						_gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
-						enableAttribute( attributes.position );
-						_gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, startIndex * positionSize * 4 ); // 4 bytes per Float32
-
-						// normals
-
-						var normal = geometry.attributes[ "normal" ];
-
-						if ( attributes.normal >= 0 && normal ) {
-
-							var normalSize = normal.itemSize;
-
-							_gl.bindBuffer( _gl.ARRAY_BUFFER, normal.buffer );
-							enableAttribute( attributes.normal );
-							_gl.vertexAttribPointer( attributes.normal, normalSize, _gl.FLOAT, false, 0, startIndex * normalSize * 4 );
+						for ( attributeName in geometryAttributes ) {
 
 
-						}
-
-						// uvs
-
-						var uv = geometry.attributes[ "uv" ];
-
-						if ( attributes.uv >= 0 && uv ) {
-
-							var uvSize = uv.itemSize;
-
-							_gl.bindBuffer( _gl.ARRAY_BUFFER, uv.buffer );
-							enableAttribute( attributes.uv );
-							_gl.vertexAttribPointer( attributes.uv, uvSize, _gl.FLOAT, false, 0, startIndex * uvSize * 4 );
-
-						}
+							if ( attributeName === 'index' ) continue;
 
 
-						// colors
+							attributePointer = programAttributes[ attributeName ];
+							attributeItem = geometryAttributes[ attributeName ];
+							attributeSize = attributeItem.itemSize;
 
 
-						var color = geometry.attributes[ "color" ];
+							if ( attributePointer >= 0 ) {
 
 
-						if ( attributes.color >= 0 && color ) {
+								_gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
+								enableAttribute( attributePointer );
+								_gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, startIndex * attributeSize * 4 ); // 4 bytes per Float32
 
 
-							var colorSize = color.itemSize;
-
-							_gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
-							enableAttribute( attributes.color );
-							_gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, startIndex * colorSize * 4 );
-
-						}
-
-						// tangents
-
-						var tangent = geometry.attributes[ "tangent" ];
-
-						if ( attributes.tangent >= 0 && tangent ) {
-
-							var tangentSize = tangent.itemSize;
-
-							_gl.bindBuffer( _gl.ARRAY_BUFFER, tangent.buffer );
-							enableAttribute( attributes.tangent );
-							_gl.vertexAttribPointer( attributes.tangent, tangentSize, _gl.FLOAT, false, 0, startIndex * tangentSize * 4 );
+							}
 
 
 						}
 						}
 
 
@@ -3532,73 +3487,30 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				if ( updateBuffers ) {
 				if ( updateBuffers ) {
 
 
-					// vertices
-
-					var position = geometry.attributes[ "position" ];
-					var positionSize = position.itemSize;
-
-					_gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
-					enableAttribute( attributes.position );
-					_gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, 0 );
-
-					// normals
-
-					var normal = geometry.attributes[ "normal" ];
-
-					if ( attributes.normal >= 0 && normal ) {
-
-						var normalSize = normal.itemSize;
+					var attributeItem, attributeName, attributePointer;
 
 
-						_gl.bindBuffer( _gl.ARRAY_BUFFER, normal.buffer );
-						enableAttribute( attributes.normal );
-						_gl.vertexAttribPointer( attributes.normal, normalSize, _gl.FLOAT, false, 0, 0 );
+					for ( attributeName in geometryAttributes ) {
 
 
-					}
-
-					// uvs
-
-					var uv = geometry.attributes[ "uv" ];
-
-					if ( attributes.uv >= 0 && uv ) {
-
-						var uvSize = uv.itemSize;
-
-						_gl.bindBuffer( _gl.ARRAY_BUFFER, uv.buffer );
-						enableAttribute( attributes.uv );
-						_gl.vertexAttribPointer( attributes.uv, uvSize, _gl.FLOAT, false, 0, 0 );
-
-					}
-
-					// colors
-
-					var color = geometry.attributes[ "color" ];
-
-					if ( attributes.color >= 0 && color ) {
-
-						var colorSize = color.itemSize;
-
-						_gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
-						enableAttribute( attributes.color );
-						_gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, 0 );
-
-					}
-
-					// tangents
+						if ( attributeName === 'index') continue;
 
 
-					var tangent = geometry.attributes[ "tangent" ];
+						attributePointer = programAttributes[ attributeName ];
+						attributeItem = geometryAttributes[ attributeName ];
+						attributeSize = attributeItem.itemSize;
 
 
-					if ( attributes.tangent >= 0 && tangent ) {
+						if ( attributePointer >= 0 ) {
 
 
-						var tangentSize = tangent.itemSize;
+							_gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
+							enableAttribute( attributePointer );
+							_gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 );
 
 
-						_gl.bindBuffer( _gl.ARRAY_BUFFER, tangent.buffer );
-						enableAttribute( attributes.tangent );
-						_gl.vertexAttribPointer( attributes.tangent, tangentSize, _gl.FLOAT, false, 0, 0 );
+						}
 
 
 					}
 					}
 
 
 				}
 				}
 
 
+				var position = geometry.attributes[ "position" ];
+
 				// render non-indexed triangles
 				// render non-indexed triangles
 
 
 				_gl.drawArrays( _gl.TRIANGLES, 0, position.numItems / 3 );
 				_gl.drawArrays( _gl.TRIANGLES, 0, position.numItems / 3 );
@@ -3615,29 +3527,26 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 			if ( updateBuffers ) {
 			if ( updateBuffers ) {
 
 
-				// vertices
-
-				var position = geometry.attributes[ "position" ];
-				var positionSize = position.itemSize;
-
-				_gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
-				enableAttribute( attributes.position );
-				_gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, 0 );
+				for ( attributeName in geometryAttributes ) {
 
 
-				// colors
+					if ( attributeName === 'index') continue;
 
 
-				var color = geometry.attributes[ "color" ];
+					attributePointer = programAttributes[ attributeName ];
+					attributeItem = geometryAttributes[ attributeName ];
+					attributeSize = attributeItem.itemSize;
 
 
-				if ( attributes.color >= 0 && color ) {
+					if ( attributePointer >= 0 ) {
 
 
-					var colorSize = color.itemSize;
+						_gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
+						enableAttribute( attributePointer );
+						_gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 );
 
 
-					_gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
-					enableAttribute( attributes.color );
-					_gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, 0 );
+					}
 
 
 				}
 				}
 
 
+				var position = geometryAttributes[ "position" ];
+
 				// render particles
 				// render particles
 
 
 				_gl.drawArrays( _gl.POINTS, 0, position.numItems / 3 );
 				_gl.drawArrays( _gl.POINTS, 0, position.numItems / 3 );
@@ -3651,26 +3560,21 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 			if ( updateBuffers ) {
 			if ( updateBuffers ) {
 
 
-				// vertices
-
-				var position = geometry.attributes[ "position" ];
-				var positionSize = position.itemSize;
+				for ( attributeName in geometryAttributes ) {
 
 
-				_gl.bindBuffer( _gl.ARRAY_BUFFER, position.buffer );
-				enableAttribute( attributes.position );
-				_gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, 0 );
+					if ( attributeName === 'index') continue;
 
 
-				// colors
+					attributePointer = programAttributes[ attributeName ];
+					attributeItem = geometryAttributes[ attributeName ];
+					attributeSize = attributeItem.itemSize;
 
 
-				var color = geometry.attributes[ "color" ];
+					if ( attributePointer >= 0 ) {
 
 
-				if ( attributes.color >= 0 && color ) {
+						_gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
+						enableAttribute( attributePointer );
+						_gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 );
 
 
-					var colorSize = color.itemSize;
-
-					_gl.bindBuffer( _gl.ARRAY_BUFFER, color.buffer );
-					enableAttribute( attributes.color );
-					_gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, 0 );
+					}
 
 
 				}
 				}
 
 
@@ -3678,6 +3582,8 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				setLineWidth( material.linewidth );
 				setLineWidth( material.linewidth );
 
 
+				var position = geometryAttributes[ "position" ];
+
 				_gl.drawArrays( _gl.LINE_STRIP, 0, position.numItems / 3 );
 				_gl.drawArrays( _gl.LINE_STRIP, 0, position.numItems / 3 );
 
 
 				_this.info.render.calls ++;
 				_this.info.render.calls ++;