浏览代码

Optimized rendering of shadow volumes and the shader.

Mikael Emtinger 14 年之前
父节点
当前提交
66f657d84a
共有 2 个文件被更改,包括 56 次插入30 次删除
  1. 52 24
      src/renderers/WebGLRenderer.js
  2. 4 6
      src/renderers/WebGLShaders.js

+ 52 - 24
src/renderers/WebGLRenderer.js

@@ -2553,7 +2553,12 @@ THREE.WebGLRenderer = function ( parameters ) {
 			
 			var l, ll = scene.lights.length;
 			var p;
-			var light;
+			var light, geometryGroup;
+			var dirLight = [];			
+			var	program;
+			var p_uniforms;
+		    var m_uniforms;
+		    var attributes;
 	
 			ol = scene.__webglShadowVolumes.length;
 			
@@ -2562,38 +2567,61 @@ THREE.WebGLRenderer = function ( parameters ) {
 				light = scene.lights[ l ];
 				
 				if( light instanceof THREE.DirectionalLight ) {
+
+					dirLight[ 0 ] = -light.position.x;
+					dirLight[ 1 ] = -light.position.y;
+					dirLight[ 2 ] = -light.position.z;
+
 					
 					// render all volumes
 					
 					for ( o = 0; o < ol; o++ ) {
 			
-						webglObject = scene.__webglShadowVolumes[ o ];
-			
-						object = webglObject.object;
-						buffer = webglObject.buffer;
-						opaque = webglObject.opaque;
-			
-						object.matrixWorld.flattenToArray( object._objectMatrixArray );
-						setupMatrices( object, camera );
-						unrollBufferMaterials( webglObject );
-	
+						object        = scene.__webglShadowVolumes[ o ].object;
+						geometryGroup = scene.__webglShadowVolumes[ o ].buffer;
+						material      = object.materials[ 0 ];
+
+
+						if ( !material.program ) _this.initMaterial( material, lights, fog, object );
 	
-						// render first back, then front
+						program = material.program,
+			  			p_uniforms = program.uniforms,
+		                m_uniforms = material.uniforms,
+		                attributes = program.attributes;
+
+
+						if( _oldProgram !== program ) {
+							
+							_gl.useProgram( program );
+							_oldProgram = program;
+
+							_gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, _projectionMatrixArray );
+							_gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, _viewMatrixArray );
+							_gl.uniform3fv( p_uniforms.directionalLightDirection, dirLight );
+						}
+
+
+						object.matrixWorld.flattenToArray( object._objectMatrixArray );
+						//object._modelViewMatrix.multiplyToArray( camera.matrixWorldInverse, object.matrixWorld, object._modelViewMatrixArray );
+
+						_gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
+						//_gl.uniformMatrix4fv( p_uniforms.modelViewMatrix, false, object._modelViewMatrixArray );
+
 	
+						_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLVertexBuffer );
+						_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
+
+						_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLNormalBuffer );
+						_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
+
+						_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webGLFaceBuffer );
+
 						_gl.cullFace( _gl.FRONT );
-	
-						for( p = 0; p < 2; p++ ) {
-				
-							for( i = 0; i < opaque.count; i++ ) {
-				
-								material = opaque.list[ i ];
-								renderBuffer( camera, light, fog, material, buffer, object );
-				
-							}
+						_gl.drawElements( _gl.TRIANGLES, geometryGroup.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
+
+						_gl.cullFace( _gl.BACK );
+						_gl.drawElements( _gl.TRIANGLES, geometryGroup.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
 				
-							_gl.cullFace( _gl.BACK );
-						}
-	
 					}
 	
 				}

+ 4 - 6
src/renderers/WebGLShaders.js

@@ -596,12 +596,10 @@ THREE.ShaderLib = {
 	
 			"void main() {",
 
-// todo: optimize
-
-				"vec3 pos    = ( objectMatrix * vec4( position, 1.0 )).xyz;",
-				"vec3 norm   = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz) * normal;// normalMatrix * normal;",
-				"vec4 final  = vec4( pos + directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm )), 1.0 );",
-				"gl_Position = projectionMatrix * viewMatrix * final;",
+				"vec4 pos      = objectMatrix * vec4( position, 1.0 );",
+				"vec3 norm     = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
+				"vec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm )), 0.0 );",
+				"gl_Position   = projectionMatrix * viewMatrix * ( pos + extruded );",
 			"}"
 
 		].join( "\n" ),