• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
43%
SHARE
TWEET

# BRDF

a guest Feb 23rd, 2013 216 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. analytic
2.
3. ::begin parameters
4. color Diffuse 1 0 0
5. color Specular 1 1 1
6. float DiffuseScale 0 1 0.5
7. float SpecularScale 0 1 .028
8. float Roughness 0.0005 2 0.2
9. ::end parameters
10.
12.
13. //Chris_F - Cook-Torrance GGX Smith specular, qualitative Oren-Nayar diffuse.
14.
15. vec3 BRDF( vec3 L, vec3 V, vec3 N, vec3 X, vec3 Y )
16. {
17.     float PI = 3.14159265358979323846;
18.     vec3 Kd = Diffuse * DiffuseScale;
19.     vec3 Ks = Specular * SpecularScale;
20.
21.     vec3 H = normalize(L + V);
22.     float NdotL = clamp(dot(N, L), 0, 1);
23.     float NdotV = dot(N, V);
24.     float NdotH = dot(N, H);
25.     float LdotH = dot(L, H);
26.
27.     float a_2 = Roughness * Roughness;
28.     float NdotL_2 = NdotL * NdotL;
29.     float NdotV_2 = NdotV * NdotV;
30.     float NdotH_2 = NdotH * NdotH;
31.     float OneMinusNdotL_2 = 1.0 - NdotL_2;
32.     float OneMinusNdotV_2 = 1.0 - NdotV_2;
33.
34.     vec3 Fd = 1.0 - Ks;
35.
36.     float gamma = clamp(dot(V - N * NdotV, L - N * NdotL), 0, 1);
37.     float A = 1.0 - 0.5 * (a_2 / (a_2 + 0.33));
38.     float B = 0.45 * (a_2 / (a_2 + 0.09));
39.     float C = sqrt(OneMinusNdotL_2 * OneMinusNdotV_2) / max(NdotL, NdotV);
40.
41.     vec3 Rd = Kd * Fd * (A + B * gamma * C) * NdotL * 1/PI;
42.
43.     float D = NdotH_2 * (a_2 - 1.0) + 1.0;
44.
45.     vec3 Fs = Ks + Fd * pow(1-LdotH, 5);
46.
47.     float G1_1 = 1.0 + sqrt(1.0 + a_2 * (OneMinusNdotL_2 / NdotL_2));
48.     float G1_2 = 1.0 + sqrt(1.0 + a_2 * (OneMinusNdotV_2 / NdotV_2));
49.     float G = G1_1 * G1_2;
50.
51.     vec3 Rs = (a_2 * Fs) / (D * D * G * NdotV * PI);
52.
53.         float inv = NdotL > 0 ? 1/NdotL : 0;
54.
55.         vec3 final = vec3(Rd + Rs);
56.     return final * inv;
57. }
58.