forked from KhronosGroup/glTF-Sample-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbr.frag
171 lines (123 loc) · 3.39 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#version 460
layout(location = 0) out vec4 fragColor;
layout(location = 0) in VData
{
vec3 Pos;
vec3 Normal;
vec2 Tex0;
};
layout(location = 1, std140) uniform CameraLightBlock
{
vec3 camPos;
vec3 lightPos;
vec4 lightColor;
};
layout(location = 0) uniform vec4 u_baseColorFactor = vec4(1, 1, 1, 1);
layout(location = 1) uniform bool u_hasBaseColorTexture = false;
layout(location = 2) uniform vec2 u_metallicRoughnessFactor = vec2(1,1); //x := metallic Factor, y := roughness Factor
layout(location = 3) uniform bool u_hasMetallicRoughnessTexture = false;
layout(location = 4) uniform vec3 u_normalScale = vec3(1,1,1);
layout(location = 5) uniform bool u_hasNormalTexture = false;
layout(binding = 0) uniform sampler2D baseColorTexture;
layout(binding = 1) uniform sampler2D metallicRoughnessTexture;
layout(binding = 2) uniform sampler2D normalTexture;
/*
BRDF:
material = mix (dialectic_brdf, metal_brdf, metallic)
where:
metal_brdf = conductor_fresnel(
f0 = baseColor,
bsdf = specular_brdf(
a = roughness**2)
)
dialectic_brdf = fresnel_mix(
ior = 1.5, [*1]
base = diffuse_brdf(
color = baseColor),
layer = specular_brdf(a = roughness**2)
)
[*1] extension GLTF_KHR_ior will make it possible to supply IOR
*/
//using
// * throwdbridge-Reitz/GGX microfacet distribution
// * seperable Smith joint shadowing function
struct DotProducts
{
float
VdL,
VdN,
VdH,
LdN,
LdH,
NdH;
} dp;
const float PI = 3.141592;
float Chi(float x)
{
return (x > 0) ? 1 : 0;
}
float specular_brdf (float a)
{
float
a2 = pow(a, 2),
D =
a2 * Chi(dp.NdH)
/
(PI * pow( pow(dp.NdH, 2)*(a2-1) + 1, 2) )
,
V1 =
Chi(dp.LdH)
/
(dp.LdN + sqrt(a2 + (1-a2)*pow(dp.LdN, 2)))
,
V2 =
Chi(dp.VdH)
/
(dp.LdN * sqrt(a2 + (1-a2)*pow(dp.VdN, 2)))
;
return V1 * V2 * D;
}
vec3 diffuse_brdf(vec3 color)
{
return (1 / PI) * color;
}
vec3 conductor_fresnel(vec3 f0, float bsdf, float _1_minus_VdH_pow5 )
{
return bsdf * (f0 + (1-f0) * _1_minus_VdH_pow5);
}
vec3 fresnel_mix(float ior, vec3 base, float layer, float _1_minus_VdH_pow5)
{
float
f0 = pow( (1-ior)/(1+ior), 2),
fr = f0 + (1 - f0)*_1_minus_VdH_pow5
;
return mix(base, layer.xxx, fr);
}
void main(void)
{
vec3
normal = u_hasNormalTexture ? u_normalScale * texture(normalTexture, Tex0).xyz : Normal,
viewDir = normalize(camPos - Pos),
lightDir = normalize(lightPos - Pos),
halfDir = normalize(viewDir + lightDir);
dp.VdL = dot( viewDir, lightDir);
dp.VdN = dot( viewDir, normal);
dp.VdH = dot( viewDir, halfDir);
dp.LdN = dot(lightDir, normal);
dp.LdN = dot(lightDir, halfDir);
dp.LdN = dot( normal, halfDir);
vec4
baseColor = u_baseColorFactor * (u_hasBaseColorTexture ? texture(baseColorTexture, Tex0) : vec4(1,1,1,1)).xyzw;
float
metallic = u_metallicRoughnessFactor.x * (u_hasMetallicRoughnessTexture ? texture(metallicRoughnessTexture, Tex0) : vec4(1,1,1,1)).b,
roughness= u_metallicRoughnessFactor.y * (u_hasMetallicRoughnessTexture ? texture(metallicRoughnessTexture, Tex0) : vec4(1,1,1,1)).g,
r2 = pow(roughness, 2),
spec = specular_brdf(r2),
_1_minus_VdH_pow5 = pow(1-dp.VdH, 5 ),
ior = 1.5;
vec3
metal_brdf = conductor_fresnel(baseColor.rgb, spec, _1_minus_VdH_pow5),
dialectic_brdf = fresnel_mix(ior, baseColor.rgb, spec, _1_minus_VdH_pow5),
material = mix(dialectic_brdf, metal_brdf, metallic);
fragColor = vec4(material, 1.0f);
}