2016-10-17 17:39:40 +02:00
|
|
|
// Linearly Transformed Cosines
|
|
|
|
// https://eheitzresearch.wordpress.com/415-2/
|
2016-11-06 15:07:13 +01:00
|
|
|
uniform sampler2D sltcMat;
|
|
|
|
uniform sampler2D sltcMag;
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-08 15:14:56 +01:00
|
|
|
const float LUT_SIZE = 64.0;
|
2016-11-06 15:07:13 +01:00
|
|
|
const float LUT_SCALE = (LUT_SIZE - 1.0) / LUT_SIZE;
|
2016-11-08 15:14:56 +01:00
|
|
|
const float LUT_BIAS = 0.5 / LUT_SIZE;
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
vec3 L0;
|
|
|
|
vec3 L1;
|
|
|
|
vec3 L2;
|
|
|
|
vec3 L3;
|
|
|
|
vec3 L4;
|
|
|
|
|
|
|
|
mat3 transpose2(mat3 v) {
|
|
|
|
mat3 tmp;
|
|
|
|
tmp[0] = vec3(v[0].x, v[1].x, v[2].x);
|
|
|
|
tmp[1] = vec3(v[0].y, v[1].y, v[2].y);
|
|
|
|
tmp[2] = vec3(v[0].z, v[1].z, v[2].z);
|
|
|
|
return tmp;
|
|
|
|
}
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
float integrateEdge(vec3 v1, vec3 v2) {
|
|
|
|
float cosTheta = dot(v1, v2);
|
|
|
|
cosTheta = clamp(cosTheta, -0.9999, 0.9999);
|
|
|
|
float theta = acos(cosTheta);
|
|
|
|
float res = cross(v1, v2).z * theta / sin(theta);
|
|
|
|
return res;
|
|
|
|
}
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
int clipQuadToHorizon(/*inout vec3 L[5], out int n*/) {
|
|
|
|
int n;
|
|
|
|
// 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;
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
if (n == 3) L3 = L0;
|
|
|
|
if (n == 4) L4 = L0;
|
|
|
|
return n;
|
|
|
|
}
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
vec3 ltcEvaluate(vec3 N, vec3 V, float dotNV, vec3 P, mat3 Minv, vec3 points0, vec3 points1, vec3 points2, vec3 points3, bool twoSided) {
|
|
|
|
// Construct orthonormal basis around N
|
|
|
|
vec3 T1, T2;
|
|
|
|
T1 = normalize(V - N * dotNV);
|
|
|
|
T2 = cross(N, T1);
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
// Rotate area light in (T1, T2, R) basis
|
|
|
|
Minv = Minv * transpose2(mat3(T1, T2, N));
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
// Polygon (allocate 5 vertices for clipping)
|
|
|
|
// vec3 L[5];
|
|
|
|
L0 = Minv * (points0 - P);
|
|
|
|
L1 = Minv * (points1 - P);
|
|
|
|
L2 = Minv * (points2 - P);
|
|
|
|
L3 = Minv * (points3 - P);
|
|
|
|
|
|
|
|
// int n;
|
|
|
|
int n = clipQuadToHorizon(/*L, n*/);
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
if (n == 0) return vec3(0.0);
|
|
|
|
|
|
|
|
// Project onto sphere
|
|
|
|
L0 = normalize(L0);
|
|
|
|
L1 = normalize(L1);
|
|
|
|
L2 = normalize(L2);
|
|
|
|
L3 = normalize(L3);
|
|
|
|
L4 = normalize(L4);
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
// Integrate
|
|
|
|
float sum = 0.0;
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
sum += integrateEdge(L0, L1);
|
|
|
|
sum += integrateEdge(L1, L2);
|
|
|
|
sum += integrateEdge(L2, L3);
|
|
|
|
|
|
|
|
if (n >= 4) sum += integrateEdge(L3, L4);
|
|
|
|
if (n == 5) sum += integrateEdge(L4, L0);
|
2016-10-17 17:39:40 +02:00
|
|
|
|
2016-11-06 15:07:13 +01:00
|
|
|
sum = twoSided ? abs(sum) : max(0.0, -sum);
|
|
|
|
return vec3(sum);
|
|
|
|
}
|