From 4d009fee45d9a34af92391893033b8d116e2b1ea Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Sat, 4 Feb 2023 17:52:55 -0800
Subject: Factor out emissive term, more spec-compliant AO.

---
 gfx/shaders/cook_torrance.frag | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/gfx/shaders/cook_torrance.frag b/gfx/shaders/cook_torrance.frag
index fcedef6..1adb4ae 100644
--- a/gfx/shaders/cook_torrance.frag
+++ b/gfx/shaders/cook_torrance.frag
@@ -112,7 +112,7 @@ vec3 fresnel_schlick_roughness(vec3 F0, float NdotV, float roughness) {
 
 // Cook-Torrance BRDF for a single light direction.
 vec3 cook_torrance(
-    vec3 albedo, float metallic, float roughness, vec3 emissive,
+    vec3 albedo, float metallic, float roughness,
     float NdotL, float NdotV, float NdotH, float HdotV) {
   vec3 F0 = mix(vec3(0.04), albedo, metallic);
   float D = trowbridge_reitz_GGX(roughness, NdotH);
@@ -122,23 +122,20 @@ vec3 cook_torrance(
   vec3 diffuse = Kd*albedo*INV_PI;
   // Take a max to prevent division by 0 when either dot product is 0.
   vec3 specular = (D*F*G) / max(4.0 * NdotV * NdotL, 0.0001);
-  return emissive + diffuse + specular;
+  return diffuse + specular;
 }
 
 // Cook-Torrance BRDF for IBL.
 vec3 cook_torrance_IBL(
-    vec3 albedo, float metallic, float roughness, vec3 emissive, float occlusion,
+    vec3 albedo, float metallic, float roughness, float occlusion,
     float NdotV,
-    vec3 irradiance, vec3 prefiltered_env, vec2 BRDF_env, vec3 ambient) {
+    vec3 irradiance, vec3 prefiltered_env, vec2 BRDF_env) {
   vec3 F0 = mix(vec3(0.04), albedo, metallic);
   vec3  F = fresnel_schlick_roughness(F0, NdotV, roughness);
   vec3 Kd = mix(vec3(1.0) - F, vec3(0.0), metallic);
   vec3 diffuse = Kd * albedo * INV_PI * irradiance;
   vec3 specular = prefiltered_env * (F * BRDF_env.x + BRDF_env.y);
-  float ambient_strength = 0.05; // TODO: Make ambient strength a parameter.
-  ambient *= ambient_strength * occlusion;
-  emissive *= EmissiveFactor;
-  return emissive + ambient + diffuse + specular;
+  return occlusion * (diffuse + specular);
 }
 
 void main()
@@ -182,30 +179,31 @@ void main()
   vec2 metal_roughness = vec2(MetallicFactor, RoughnessFactor);
 #endif
 #ifdef HAS_EMISSIVE_MAP
-  vec3 emissive = texture(EmissiveTexture, Texcoord).rgb;
+  vec3 emissive = EmissiveFactor * texture(EmissiveTexture, Texcoord).rgb;
 #else
-  vec3 emissive = vec3(0.0);
+  vec3 emissive = EmissiveFactor;
 #endif
 #ifdef HAS_OCCLUSION_MAP
   float occlusion = texture(AmbientOcclusionTexture, Texcoord).r;
 #else
-  float occlusion = 0.0;
+  float occlusion = 1.0;
 #endif
   float metallic = metal_roughness.x;
   float roughness = metal_roughness.y;
 
   // For a single light direction:
-  // vec3 brdf = cook_torrance(albedo, metallic, roughness, emissive, NdotL, NdotV, NdotH, HdotV);
+  // vec3 brdf = cook_torrance(albedo, metallic, roughness, NdotL, NdotV, NdotH, HdotV);
   // vec3 Li = texture(Sky, N).rgb;
   // vec3 colour = brdf * Li * NdotL;
 
   // For IBL:
   vec3 irradiance = texture(IrradianceMap, N).rgb;
   vec3 prefiltered_env = textureLod(PrefilteredEnvironmentMap, R, roughness * MaxReflectionLOD).rgb;
-  vec3 ambient = textureLod(PrefilteredEnvironmentMap, R, MaxReflectionLOD).rgb;
   vec2 BRDF_env = texture(BRDFIntegrationMap, vec2(NdotV, roughness)).rg;
   vec3 colour = cook_torrance_IBL(
-    albedo, metallic, roughness, emissive, occlusion, NdotV, irradiance, prefiltered_env, BRDF_env, ambient);
+    albedo, metallic, roughness, occlusion, NdotV, irradiance, prefiltered_env, BRDF_env);
+
+  colour += emissive;
 
   // Reinhard tone mapping.
   colour = colour / (colour + vec3(1.0));
-- 
cgit v1.2.3