summaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-15 17:59:02 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-15 17:59:02 +1000
commitb3b19f081231c7c13322c7b0d577afb6084d48df (patch)
tree0bec011f39175c3f168571dd135f3c653f91a21e /assets
parentc5808488875484aca814bfc8e526f37f3f447166 (diff)
point lights for pbr example
Diffstat (limited to 'assets')
-rw-r--r--assets/shaders/pbr_params.frag63
-rw-r--r--assets/shaders/pbr_params.vert2
2 files changed, 44 insertions, 21 deletions
diff --git a/assets/shaders/pbr_params.frag b/assets/shaders/pbr_params.frag
index d467aa8..068fb3c 100644
--- a/assets/shaders/pbr_params.frag
+++ b/assets/shaders/pbr_params.frag
@@ -1,10 +1,10 @@
-#version 410
+#version 410 core
out vec4 FragColor;
-in vec3 WorldPos;
-in vec3 Normal;
-in vec2 TexCoords;
+in vec3 fragWorldPos;
+in vec3 fragNormal;
+in vec2 fragTexCoords;
struct PointLight {
vec3 position;
@@ -35,22 +35,45 @@ float GeometrySchlickGGX(float NdotV, float roughness);
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
void main() {
- vec3 norm = normalize(Normal);
- vec3 viewDir = normalize(scene.viewPos - WorldPos);
-
- // vec3 radiance = vec3(0.0); // denoted L in the radiance equation
- // for (int i = 0; i < 4; i++) {
- // vec3 lightVec = normalize(pointLights[i].position - WorldPos);
- // vec3 halfway = normalize(Normal + lightVec);
- // float distance = length(pointLights[i].position - WorldPos);
- // float attenuation = 1.0 / (distance * distance);
- // vec3 radiance = pointLights[i].color * attenuation;
-
- // vec3 F0 = vec3(0.04);
- // F0 = mix(F0, albedo, metallic);
- // vec3 F = fresnelSchlick(max(dot(halfway, lightVec), 0.0), F0);
- // }
-
+ vec3 norm = normalize(fragNormal);
+ vec3 viewDir = normalize(scene.viewPos - fragWorldPos);
+
+ vec3 F0 = vec3(0.04);
+ F0 = mix(F0, pbr.albedo, pbr.metallic);
+
+ vec3 Lo = vec3(0.0); // denoted L in the radiance equation
+ for (int i = 0; i < 4; i++) {
+ vec3 lightVec = normalize(scene.pointLights[i].position - fragWorldPos);
+ vec3 halfway = normalize(viewDir + lightVec);
+ float distance = length(scene.pointLights[i].position - fragWorldPos);
+ float attenuation = 1.0 / (distance * distance);
+ vec3 radiance = scene.pointLights[i].color * attenuation;
+
+ // cook-torrance brdf
+ float NDF = DistributionGGX(norm, halfway, pbr.roughness);
+ float G = GeometrySmith(norm, viewDir, lightVec, pbr.roughness);
+ vec3 F = fresnelSchlick(max(dot(halfway, viewDir), 0.0), F0);
+
+ vec3 kS = F;
+ vec3 kD = vec3(1.0) - kS;
+ kD *= 1.0 - pbr.metallic;
+
+ vec3 numerator = NDF * G * F;
+ float denominator = 4.0 * max(dot(norm, viewDir), 0.0) * max(dot(norm, lightVec), 0.0) + 0.0001;
+ vec3 specular = numerator / denominator;
+
+ // add to outgoing radiance Lo
+ float NdotL = max(dot(norm, lightVec), 0.0);
+ Lo += (kD * pbr.albedo / PI + specular) * radiance * NdotL;
+ }
+
+ vec3 ambient = vec3(0.03) * pbr.albedo * pbr.ao;
+ vec3 color = ambient + Lo;
+
+ // color = color / (color + vec3(1.0));
+ // color = pow(color, vec3(1.0/2.2));
+
+ // FragColor = vec4(color, 1.0);
FragColor = vec4(1.0);
}
diff --git a/assets/shaders/pbr_params.vert b/assets/shaders/pbr_params.vert
index 0908246..e3cbe96 100644
--- a/assets/shaders/pbr_params.vert
+++ b/assets/shaders/pbr_params.vert
@@ -1,4 +1,4 @@
-#version 410
+#version 410 core
// Vertex attributes
layout(location = 0) in vec3 inPosition;