Only use envmap for lighting

This commit is contained in:
luboslenco
2025-02-12 19:43:34 +01:00
parent 4f96b84d0a
commit d8d77b8dab
17 changed files with 4 additions and 478 deletions
-168
View File
@@ -16,12 +16,6 @@ uniform sampler2D ssaotex;
uniform vec2 camera_proj;
uniform vec3 eye;
uniform vec3 eye_look;
uniform vec3 light_area0;
uniform vec3 light_area1;
uniform vec3 light_area2;
uniform vec3 light_area3;
uniform sampler2D sltc_mat;
uniform sampler2D sltc_mag;
#include "std/gbuffer.glsl"
#include "std/brdf.glsl"
@@ -32,167 +26,6 @@ in vec2 tex_coord;
in vec3 view_ray;
out vec4 frag_color;
const float LUT_SIZE = 64.0;
const float LUT_SCALE = (LUT_SIZE - 1.0) / LUT_SIZE;
const float LUT_BIAS = 0.5 / LUT_SIZE;
float integrate_edge(vec3 v1, vec3 v2) {
float cos_theta = dot(v1, v2);
float theta = acos(cos_theta);
float res = cross(v1, v2).z * ((theta > 0.001) ? theta / sin(theta) : 1.0);
return res;
}
float ltc_evaluate(vec3 N, vec3 V, float dotnv, vec3 P, mat3 Minv, vec3 points0, vec3 points1, vec3 points2, vec3 points3) {
// Construct orthonormal basis around N
vec3 T1, T2;
T1 = normalize(V - N * dotnv);
T2 = cross(N, T1);
// Rotate area light in (T1, T2, R) basis
Minv = mul(transpose(mat3(T1, T2, N)), Minv);
// Polygon (allocate 5 vertices for clipping)
vec3 L0 = mul((points0 - P), Minv);
vec3 L1 = mul((points1 - P), Minv);
vec3 L2 = mul((points2 - P), Minv);
vec3 L3 = mul((points3 - P), Minv);
vec3 L4 = vec3(0.0, 0.0, 0.0);
int n = 0;
// Detect clipping config
int config = 0;
if (L0.z > 0.0) config += 1;
if (L1.z > 0.0) config += 2;
if (L2.z > 0.0) config += 4;
if (L3.z > 0.0) config += 8;
// Clip
if (config == 0) {
// Clip all
}
else if (config == 1) { // V1 clip V2 V3 V4
n = 3;
L1 = -L1.z * L0 + L0.z * L1;
L2 = -L3.z * L0 + L0.z * L3;
}
else if (config == 2) { // V2 clip V1 V3 V4
n = 3;
L0 = -L0.z * L1 + L1.z * L0;
L2 = -L2.z * L1 + L1.z * L2;
}
else if (config == 3) { // V1 V2 clip V3 V4
n = 4;
L2 = -L2.z * L1 + L1.z * L2;
L3 = -L3.z * L0 + L0.z * L3;
}
else if (config == 4) { // V3 clip V1 V2 V4
n = 3;
L0 = -L3.z * L2 + L2.z * L3;
L1 = -L1.z * L2 + L2.z * L1;
}
else if (config == 5) { // V1 V3 clip V2 V4) impossible
n = 0;
}
else if (config == 6) { // V2 V3 clip V1 V4
n = 4;
L0 = -L0.z * L1 + L1.z * L0;
L3 = -L3.z * L2 + L2.z * L3;
}
else if (config == 7) { // V1 V2 V3 clip V4
n = 5;
L4 = -L3.z * L0 + L0.z * L3;
L3 = -L3.z * L2 + L2.z * L3;
}
else if (config == 8) { // V4 clip V1 V2 V3
n = 3;
L0 = -L0.z * L3 + L3.z * L0;
L1 = -L2.z * L3 + L3.z * L2;
L2 = L3;
}
else if (config == 9) { // V1 V4 clip V2 V3
n = 4;
L1 = -L1.z * L0 + L0.z * L1;
L2 = -L2.z * L3 + L3.z * L2;
}
else if (config == 10) { // V2 V4 clip V1 V3) impossible
n = 0;
}
else if (config == 11) { // V1 V2 V4 clip V3
n = 5;
L4 = L3;
L3 = -L2.z * L3 + L3.z * L2;
L2 = -L2.z * L1 + L1.z * L2;
}
else if (config == 12) { // V3 V4 clip V1 V2
n = 4;
L1 = -L1.z * L2 + L2.z * L1;
L0 = -L0.z * L3 + L3.z * L0;
}
else if (config == 13) { // V1 V3 V4 clip V2
n = 5;
L4 = L3;
L3 = L2;
L2 = -L1.z * L2 + L2.z * L1;
L1 = -L1.z * L0 + L0.z * L1;
}
else if (config == 14) { // V2 V3 V4 clip V1
n = 5;
L4 = -L0.z * L3 + L3.z * L0;
L0 = -L0.z * L1 + L1.z * L0;
}
else if (config == 15) { // V1 V2 V3 V4
n = 4;
}
if (n == 0) return 0.0;
if (n == 3) L3 = L0;
if (n == 4) L4 = L0;
// Project onto sphere
L0 = normalize(L0);
L1 = normalize(L1);
L2 = normalize(L2);
L3 = normalize(L3);
L4 = normalize(L4);
// Integrate
float sum = 0.0;
sum += integrate_edge(L0, L1);
sum += integrate_edge(L1, L2);
sum += integrate_edge(L2, L3);
if (n >= 4) sum += integrate_edge(L3, L4);
if (n == 5) sum += integrate_edge(L4, L0);
return max(0.0, -sum);
}
vec3 sample_light(const vec3 p, const vec3 n, const vec3 v, const float dotnv,
const vec3 albedo, const float rough, const vec3 f0, const float occ
) {
float theta = acos(dotnv);
vec2 tuv = vec2(rough, theta / (0.5 * PI));
tuv = tuv * LUT_SCALE + LUT_BIAS;
vec4 t = textureLod(sltc_mat, tuv, 0.0);
mat3 inv = mat3(
vec3(1.0, 0.0, t.y),
vec3(0.0, t.z, 0.0),
vec3(t.w, 0.0, t.x));
float ltcspec = ltc_evaluate(n, v, dotnv, p, inv, light_area0, light_area1, light_area2, light_area3);
ltcspec *= textureLod(sltc_mag, tuv, 0.0).a;
mat3 m1 = mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0));
float ltcdiff = ltc_evaluate(n, v, dotnv, p, m1, light_area0, light_area1, light_area2, light_area3);
vec3 direct = albedo * ltcdiff + ltcspec * 0.05;
direct *= vec3(1000.0, 1000.0, 1000.0);
return direct;
}
void main() {
vec4 g0 = textureLod(gbuffer0, tex_coord, 0.0); // Normal.xy, roughness, metallic/matid
@@ -242,6 +75,5 @@ void main() {
albedo = vec3(0.0, 0.0, 0.0);
}
frag_color.rgb += sample_light(p, n, v, dotnv, albedo, roughness, f0, occ);
frag_color.a = 1.0; // Mark as opaque
}