Bläddra i källkod

refactor Indirect shader using separate functions for env + optimize cos/sin calculus

Nicolas Cannasse 4 år sedan
förälder
incheckning
8e3352b699
2 ändrade filer med 18 tillägg och 12 borttagningar
  1. 2 1
      h3d/scene/pbr/Renderer.hx
  2. 16 11
      h3d/shader/pbr/Lighting.hx

+ 2 - 1
h3d/scene/pbr/Renderer.hx

@@ -377,7 +377,8 @@ class Renderer extends h3d.scene.Renderer {
 		if( env != null ) {
 			pbrIndirect.cameraPosition.load(ctx.camera.pos);
 			pbrIndirect.emissivePower = props.emissive * props.emissive;
-			pbrIndirect.rot = hxd.Math.degToRad(env.rot);
+			var rot = hxd.Math.degToRad(env.rot);
+			pbrIndirect.irrRotation.set(Math.cos(rot), Math.sin(rot));
 			pbrIndirect.irrPower = env.power * env.power;
 			pbrIndirect.irrLut = env.lut;
 			pbrIndirect.irrDiffuse = env.diffuse;

+ 16 - 11
h3d/shader/pbr/Lighting.hx

@@ -16,7 +16,7 @@ class Indirect extends PropsDefinition {
 		@param var irrSpecular : SamplerCube;
 		@param var irrSpecularLevels : Float;
 		@param var irrPower : Float;
-		@param var rot : Float;
+		@param var irrRotation : Vec2;
 
 		// Sky Params
 		@param var skyMap : SamplerCube;
@@ -31,10 +31,19 @@ class Indirect extends PropsDefinition {
 
 		var calculatedUV : Vec2;
 
-		function fragment() {
-			var s = sin(rot);
-			var c = cos(rot);
+		function rotateNormal( n : Vec3 ) : Vec3 {
+			return vec3(n.x * irrRotation.x - n.y * irrRotation.y, n.x * irrRotation.y + n.y * irrRotation.x, n.z);
+		}
+
+		function getEnvSpecular( normal : Vec3, roughness : Float ) : Vec3 {
+			return irrSpecular.textureLod( rotateNormal(normal), roughness * irrSpecularLevels).rgb;
+		}
 
+		function getEnvDiffuse( normal : Vec3 ) : Vec3 {
+			return irrDiffuse.textureLod( rotateNormal(normal), roughness * irrSpecularLevels).rgb;
+		}
+
+		function fragment() {
 			var isSky = normal.dot(normal) <= 0;
 			if( isSky ) {
 				if( showSky ) {
@@ -43,8 +52,7 @@ class Indirect extends PropsDefinition {
 						color = skyColorValue;
 					else {
 						normal = (vec3( uvToScreen(calculatedUV), 1. ) * cameraInvViewProj.mat3x4()).normalize();
-						var rotatedNormal = vec3(normal.x * c - normal.y * s, normal.x * s + normal.y * c, normal.z);
-						color = skyMap.get(rotatedNormal).rgb;
+						color = skyMap.get(rotateNormal(normal)).rgb;
 						color.rgb *= mix(1.0, skyScale, (max( max(color.r, max(color.g, color.b)) - skyThreshold, 0) / max(0.001, (1.0 - skyThreshold))));
 					}
 					if( gammaCorrect )
@@ -60,15 +68,12 @@ class Indirect extends PropsDefinition {
 				var F0 = pbrSpecularColor;
 				var F = F0 + (max(vec3(1 - roughness), F0) - F0) * exp2( ( -5.55473 * NdV - 6.98316) * NdV );
 
-				var rotatedNormal = vec3(normal.x * c - normal.y * s, normal.x * s + normal.y * c, normal.z);
-
 				if( drawIndirectDiffuse ) {
-					diffuse = irrDiffuse.get(rotatedNormal).rgb * albedo;
+					diffuse = getEnvDiffuse(normal) * albedo;
 				}
 				if( drawIndirectSpecular ) {
 					var reflectVec = reflect(-view, normal);
-					var rotatedReflecVec = vec3(reflectVec.x * c - reflectVec.y * s, reflectVec.x * s + reflectVec.y * c, reflectVec.z);
-					var envSpec = textureLod(irrSpecular, rotatedReflecVec, roughness * irrSpecularLevels).rgb;
+					var envSpec = getEnvSpecular(reflectVec, roughness);
 					var envBRDF = irrLut.get(vec2(roughness, NdV));
 					specular = envSpec * (F * envBRDF.x + envBRDF.y);
 				}