Skip to content

Commit dfc1c71

Browse files
BSDF + BRDF initial code
1 parent 5e37f5e commit dfc1c71

File tree

4 files changed

+69
-31
lines changed

4 files changed

+69
-31
lines changed

src/shaders/common/brdf.glsl

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ float DisneyPdf(in Ray ray, inout State state, in vec3 bsdfDir)
3636
vec3 L = bsdfDir;
3737
vec3 H = normalize(L + V);
3838

39+
//if (dot(N, L) < 0.0)
40+
// return 1.0;
41+
3942
float NDotH = dot(N, H);
4043

44+
float specularAlpha = mix(0.1, 0.001, state.mat.roughness);
4145
float clearcoatAlpha = mix(0.1, 0.001, state.mat.clearcoatGloss);
4246

4347
float diffuseRatio = 0.5 * (1.0 - state.mat.metallic);
@@ -47,15 +51,19 @@ float DisneyPdf(in Ray ray, inout State state, in vec3 bsdfDir)
4751
float ax = max(0.001, (state.mat.roughness * state.mat.roughness) / aspect);
4852
float ay = max(0.001, (state.mat.roughness * state.mat.roughness) * aspect);
4953

50-
// calculate diffuse and specular pdfs and mix ratio
51-
float pdfGTR2 = GTR2_aniso(NDotH, dot(H, state.tangent), dot(H, state.bitangent), ax, ay) * NDotH;
54+
// PDFs for brdf
55+
float pdfGTR2_aniso = GTR2_aniso(NDotH, dot(H, state.tangent), dot(H, state.bitangent), ax, ay) * NDotH;
5256
float pdfGTR1 = GTR1(NDotH, clearcoatAlpha) * NDotH;
5357
float ratio = 1.0 / (1.0 + state.mat.clearcoat);
54-
float pdfSpec = mix(pdfGTR1, pdfGTR2, ratio) / (4.0 * abs(dot(L, H)));
58+
float pdfSpec = mix(pdfGTR1, pdfGTR2_aniso, ratio) / (4.0 * abs(dot(L, H)));
5559
float pdfDiff = abs(dot(L, N)) * (1.0 / PI);
60+
float brdfPdf = diffuseRatio * pdfDiff + specularRatio * pdfSpec;
61+
62+
// PDFs for bsdf
63+
float pdfGTR2 = GTR2(NDotH, specularAlpha) * NDotH;
64+
float bsdfPdf = pdfGTR2 / (4.0 * abs(dot(L, H)));
5665

57-
// weigh pdfs according to ratios
58-
return diffuseRatio * pdfDiff + specularRatio * pdfSpec;
66+
return brdfPdf;//mix(brdfPdf, bsdfPdf, state.mat.transmission);
5967
}
6068

6169
//-----------------------------------------------------------------------
@@ -67,32 +75,47 @@ vec3 DisneySample(in Ray ray, inout State state)
6775

6876
vec3 dir;
6977

70-
float probability = rand();
71-
float diffuseRatio = 0.5 * (1.0 - state.mat.metallic);
72-
7378
float r1 = rand();
7479
float r2 = rand();
80+
//float bsdfProb = rand();
7581

76-
if (probability < diffuseRatio) // sample diffuse
77-
{
78-
dir = CosineSampleHemisphere(r1, r2);
79-
dir = state.tangent * dir.x + state.bitangent * dir.y + N * dir.z;
80-
}
81-
else
82+
// BSDF
83+
/*if (bsdfProb < state.mat.transmission)
8284
{
83-
float a = max(0.001, state.mat.roughness);
84-
85-
float phi = r1 * 2.0 * PI;
85+
float n1 = 1.0;
86+
float n2 = state.mat.ior;
8687
87-
float cosTheta = sqrt((1.0 - r2) / (1.0 + (a*a - 1.0) *r2));
88-
float sinTheta = clamp(sqrt(1.0 - (cosTheta * cosTheta)), 0.0, 1.0);
89-
float sinPhi = sin(phi);
90-
float cosPhi = cos(phi);
88+
float theta = abs(dot(-V, N));
89+
float eta = dot(state.normal, state.ffnormal) > 0.0 ? (n1 / n2) : (n2 / n1);
90+
float cos2t = 1.0 - eta * eta * (1.0 - theta * theta);
9191
92-
vec3 H = vec3(sinTheta*cosPhi, sinTheta*sinPhi, cosTheta);
92+
vec3 H = ImportanceSampleGGX(state.mat.roughness, r1, r2);
9393
H = state.tangent * H.x + state.bitangent * H.y + N * H.z;
9494
95-
dir = 2.0*dot(V, H)*H - V;
95+
float F = Fresnel(theta, n1, n2);
96+
97+
if (cos2t < 0.0 || rand() < F)
98+
dir = normalize(reflect(-V, H));
99+
else
100+
dir = normalize(refract(-V, H, eta));
101+
}
102+
else*/
103+
{
104+
float probability = rand();
105+
float diffuseRatio = 0.5 * (1.0 - state.mat.metallic);
106+
107+
if (probability < diffuseRatio)
108+
{
109+
vec3 H = CosineSampleHemisphere(r1, r2);
110+
H = state.tangent * H.x + state.bitangent * H.y + N * H.z;
111+
dir = H;
112+
}
113+
else
114+
{
115+
vec3 H = ImportanceSampleGGX(state.mat.roughness, r1, r2);
116+
H = state.tangent * H.x + state.bitangent * H.y + N * H.z;
117+
dir = reflect(-V, H);
118+
}
96119
}
97120
return dir;
98121
}

src/shaders/common/glass.glsl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* SOFTWARE.
2828
*/
2929

30+
/*
3031
//-----------------------------------------------------------------------
3132
float GlassPdf(Ray ray, inout State state, in vec3 bsdfDir)
3233
//-----------------------------------------------------------------------
@@ -76,10 +77,6 @@ vec3 GlassSample(in Ray ray, inout State state)
7677
float r1 = rand();
7778
float r2 = rand();
7879
79-
vec3 UpVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
80-
vec3 TangentX = normalize(cross(UpVector, N));
81-
vec3 TangentY = cross(N, TangentX);
82-
8380
float a = max(0.001, state.mat.roughness);
8481
8582
float phi = r1 * 2.0 * PI;
@@ -90,7 +87,7 @@ vec3 GlassSample(in Ray ray, inout State state)
9087
float cosPhi = cos(phi);
9188
9289
vec3 H = vec3(sinTheta * cosPhi, sinTheta * sinPhi, cosTheta);
93-
H = TangentX * H.x + TangentY * H.y + N * H.z;
90+
//H = TangentX * H.x + TangentY * H.y + N * H.z;
9491
9592
float n1 = 1.0;
9693
float n2 = 1.45;
@@ -151,4 +148,5 @@ vec3 GlassEval(in Ray ray, inout State state, in vec3 bsdfDir)
151148
}
152149
else
153150
return state.mat.albedo * F * D * G;
154-
}
151+
}
152+
*/

src/shaders/common/pathtrace.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
void Onb(in vec3 N, inout vec3 T, inout vec3 B)
3232
//-----------------------------------------------------------------------
3333
{
34-
vec3 U = abs(N.z) < 0.999 ? vec3(0, 0, 1) : vec3(1, 0, 0);
35-
T = normalize(cross(U, N));
34+
vec3 UpVector = abs(N.z) < 0.999 ? vec3(0, 0, 1) : vec3(1, 0, 0);
35+
T = normalize(cross(UpVector, N));
3636
B = cross(N, T);
3737
}
3838

@@ -115,6 +115,7 @@ void GetMaterialsAndTextures(inout State state, in Ray r)
115115
vec3 nrm = texture(textureMapsArrayTex, vec3(texUV, int(mat.texIDs.z))).xyz;
116116
nrm = normalize(nrm * 2.0 - 1.0);
117117

118+
// Orthonormal Basis
118119
vec3 T, B;
119120
Onb(state.ffnormal, T, B);
120121

src/shaders/common/sampling.glsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
* SOFTWARE.
2828
*/
2929

30+
//----------------------------------------------------------------------
31+
vec3 ImportanceSampleGGX(float rgh, float r1, float r2)
32+
//----------------------------------------------------------------------
33+
{
34+
float a = max(0.001, rgh);
35+
36+
float phi = r1 * 2.0 * PI;
37+
38+
float cosTheta = sqrt((1.0 - r2) / (1.0 + (a * a - 1.0) * r2));
39+
float sinTheta = clamp(sqrt(1.0 - (cosTheta * cosTheta)), 0.0, 1.0);
40+
float sinPhi = sin(phi);
41+
float cosPhi = cos(phi);
42+
43+
return vec3(sinTheta * cosPhi, sinTheta * sinPhi, cosTheta);
44+
}
45+
3046
//-----------------------------------------------------------------------
3147
float SchlickFresnel(float u)
3248
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)