-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBR.frag
127 lines (110 loc) · 3.59 KB
/
PBR.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
in vec3 WorldPos;
in vec3 Normal;
uniform vec3 camPos;
uniform vec3 lightPos;
const float PI = 3.14159265359;
struct Material {
// //vec3 ambient;//环境光颜色在几乎所有情况下都等于漫反射颜色
// sampler2D diffuse;
// //vec3 diffuse;
// sampler2D specular;
// float shininess;
sampler2D albedoMap;
sampler2D normalMap;
sampler2D metallicMap;
sampler2D roughnessMap;
sampler2D aoMap;
};
struct Light {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
//uniform Light light;
uniform Material material;
//uniform vec4 ourColor;
vec3 getNormalFromMap()
{
vec3 tangentNormal = texture(material.normalMap, TexCoords).xyz * 2.0 - 1.0;
vec3 Q1 = dFdx(WorldPos);
vec3 Q2 = dFdy(WorldPos);
vec2 st1 = dFdx(TexCoords);
vec2 st2 = dFdy(TexCoords);
vec3 N = normalize(Normal);
vec3 T = normalize(Q1*st2.t - Q2*st1.t);
vec3 B = -normalize(cross(N, T));
mat3 TBN = mat3(T, B, N);
return normalize(TBN * tangentNormal);
}
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
void main()
{
vec3 albedo = pow(texture( material.albedoMap, TexCoords).rgb, vec3(2.2));
vec3 Normal = getNormalFromMap();
float metallic = texture(material.metallicMap, TexCoords).r;
float roughness = texture(material.roughnessMap, TexCoords).r;
//float ao = texture(material.aoMap, TexCoords).r;
float ao=1;//先设置为全白
vec3 N = normalize(Normal);
vec3 V = normalize(camPos - WorldPos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 Lo = vec3(0.0);//辐照度
vec3 L = normalize(lightPos - WorldPos);
vec3 H = normalize(V + L);
float distance = length(lightPos- WorldPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = vec3(1.0) * attenuation;
// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 nominator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
vec3 specular = nominator / denominator;
// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL; vec3 ambient = vec3(0.04) * albedo * ao;
vec3 color = /*ambient*/ + Lo;
//gamma矫正
color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
FragColor =vec4(color,1);
}