Debian Codegraph @

src/extras/shaders/es3/metalrough.inc.frag

raw
1 // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4 #ifndef FP
5 #define FP highp
6 #endif
7
8 // Exposure correction
9 uniform FP float exposure;
10 // Gamma correction
11 const FP float gamma = 2.2;
12
13 #pragma include light.inc.frag
14
15 FP float remapRoughness(const in FP float roughness)
16 {
17 // As per page 14 of
18 // http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
19 // we remap the roughness to give a more perceptually linear response
20 // of "bluriness" as a function of the roughness specified by the user.
21 // r = roughness^2
22 const FP float maxSpecPower = 999999.0;
23 const FP float minRoughness = sqrt(2.0 / (maxSpecPower + 2.0));
24 return max(roughness * roughness, minRoughness);
25 }
26
27 FP float alphaToMipLevel(FP float alpha)
28 {
29 FP float specPower = 2.0 / (alpha * alpha) - 2.0;
30
31 // We use the mip level calculation from Lys' default power drop, which in
32 // turn is a slight modification of that used in Marmoset Toolbag. See
33 // https://docs.knaldtech.com/doku.php?id=specular_lys for details.
34 // For now we assume a max specular power of 999999 which gives
35 // maxGlossiness = 1.
36 const FP float k0 = 0.00098;
37 const FP float k1 = 0.9921;
38 FP float glossiness = (pow(2.0, -10.0 / sqrt(specPower)) - k0) / k1;
39
40 // Lookup the number of mips in the specular envmap
41 int mipLevels = envLight.specularMipLevels;
42
43 // Offset of smallest miplevel we should use (corresponds to specular
44 // power of 1). I.e. in the 32x32 sized mip.
45 const FP float mipOffset = 5.0;
46
47 // The final factor is really 1 - g / g_max but as mentioned above g_max
48 // is 1 by definition here so we can avoid the division. If we make the
49 // max specular power for the spec map configurable, this will need to
50 // be handled properly.
51 FP float mipLevel = (float(mipLevels) - 1.0 - mipOffset) * (1.0 - glossiness);
52 return mipLevel;
53 }
54
55 FP float normalDistribution(const in FP vec3 n, const in FP vec3 h, const in FP float alpha)
56 {
57 // Blinn-Phong approximation - see
58 // http://graphicrants.blogspot.co.uk/2013/08/specular-brdf-reference.html
59 FP float specPower = 2.0 / (alpha * alpha) - 2.0;
60 return (specPower + 2.0) / (2.0 * 3.14159) * pow(max(dot(n, h), 0.0), specPower);
61 }
62
63 FP vec3 fresnelFactor(const in FP vec3 color, const in FP float cosineFactor)
64 {
65 // Calculate the Fresnel effect value
66 FP vec3 f = color;
67 FP vec3 F = f + (1.0 - f) * pow(1.0 - cosineFactor, 5.0);
68 return clamp(F, f, vec3(1.0));
69 }
70
71 FP float geometricModel(const in FP float lDotN,
72 const in FP float vDotN,
73 const in FP vec3 h)
74 {
75 // Implicit geometric model (equal to denominator in specular model).
76 // This currently assumes that there is no attenuation by geometric shadowing or
77 // masking according to the microfacet theory.
78 return lDotN * vDotN;
79 }
80
81 FP vec3 specularModel(const in FP vec3 F0,
82 const in FP float sDotH,
83 const in FP float sDotN,
84 const in FP float vDotN,
85 const in FP vec3 n,
86 const in FP vec3 h)
87 {
88 // Clamp sDotN and vDotN to small positive value to prevent the
89 // denominator in the reflection equation going to infinity. Balance this
90 // by using the clamped values in the geometric factor function to
91 // avoid ugly seams in the specular lighting.
92 FP float sDotNPrime = max(sDotN, 0.001);
93 FP float vDotNPrime = max(vDotN, 0.001);
94
95 FP vec3 F = fresnelFactor(F0, sDotH);
96 FP float G = geometricModel(sDotNPrime, vDotNPrime, h);
97
98 FP vec3 cSpec = F * G / (4.0 * sDotNPrime * vDotNPrime);
99 return clamp(cSpec, vec3(0.0), vec3(1.0));
100 }
101
102 FP vec3 pbrModel(const in int lightIndex,
103 const in FP vec3 wPosition,
104 const in FP vec3 wNormal,
105 const in FP vec3 wView,
106 const in FP vec3 baseColor,
107 const in FP float metalness,
108 const in FP float alpha,
109 const in FP float ambientOcclusion)
110 {
111 // Calculate some useful quantities
112 FP vec3 n = wNormal;
113 FP vec3 s = vec3(0.0);
114 FP vec3 v = wView;
115 FP vec3 h = vec3(0.0);
116
117 FP float vDotN = dot(v, n);
118 FP float sDotN = 0.0;
119 FP float sDotH = 0.0;
120 FP float att = 1.0;
121
122 if (lights[lightIndex].type != TYPE_DIRECTIONAL) {
123 // Point and Spot lights
124 FP vec3 sUnnormalized = vec3(lights[lightIndex].position) - wPosition;
125 s = normalize(sUnnormalized);
126
127 // Calculate the attenuation factor
128 sDotN = dot(s, n);
129 if (sDotN > 0.0) {
130 if (lights[lightIndex].constantAttenuation != 0.0
131 || lights[lightIndex].linearAttenuation != 0.0
132 || lights[lightIndex].quadraticAttenuation != 0.0) {
133 FP float dist = length(sUnnormalized);
134 att = 1.0 / (lights[lightIndex].constantAttenuation +
135 lights[lightIndex].linearAttenuation * dist +
136 lights[lightIndex].quadraticAttenuation * dist * dist);
137 }
138
139 // The light direction is in world space already
140 if (lights[lightIndex].type == TYPE_SPOT) {
141 // Check if fragment is inside or outside of the spot light cone
142 if (degrees(acos(dot(-s, lights[lightIndex].direction))) > lights[lightIndex].cutOffAngle)
143 sDotN = 0.0;
144 }
145 }
146 } else {
147 // Directional lights
148 // The light direction is in world space already
149 s = normalize(-lights[lightIndex].direction);
150 sDotN = dot(s, n);
151 }
152
153 h = normalize(s + v);
154 sDotH = dot(s, h);
155
156 // Calculate diffuse component
157 FP vec3 diffuseColor = (1.0 - metalness) * baseColor * lights[lightIndex].color;
158 FP vec3 diffuse = diffuseColor * max(sDotN, 0.0) / 3.14159;
159
160 // Calculate specular component
161 FP vec3 dielectricColor = vec3(0.04);
162 FP vec3 F0 = mix(dielectricColor, baseColor, metalness);
163 FP vec3 specularFactor = vec3(0.0);
164 if (sDotN > 0.0) {
165 specularFactor = specularModel(F0, sDotH, sDotN, vDotN, n, h);
166 specularFactor *= normalDistribution(n, h, alpha);
167 }
168 FP vec3 specularColor = lights[lightIndex].color;
169 FP vec3 specular = specularColor * specularFactor;
170
171 // Blend between diffuse and specular to conserver energy
172 FP vec3 color = att * lights[lightIndex].intensity * (specular + diffuse * (vec3(1.0) - specular));
173
174 // Reduce by ambient occlusion amount
175 color *= ambientOcclusion;
176
177 return color;
178 }
179
180 FP vec3 pbrIblModel(const in FP vec3 wNormal,
181 const in FP vec3 wView,
182 const in FP vec3 baseColor,
183 const in FP float metalness,
184 const in FP float alpha,
185 const in FP float ambientOcclusion)
186 {
187 // Calculate reflection direction of view vector about surface normal
188 // vector in world space. This is used in the fragment shader to sample
189 // from the environment textures for a light source. This is equivalent
190 // to the l vector for punctual light sources. Armed with this, calculate
191 // the usual factors needed
192 FP vec3 n = wNormal;
193 FP vec3 l = reflect(-wView, n);
194 FP vec3 v = wView;
195 FP vec3 h = normalize(l + v);
196 FP float vDotN = dot(v, n);
197 FP float lDotN = dot(l, n);
198 FP float lDotH = dot(l, h);
199
200 // Calculate diffuse component
201 FP vec3 diffuseColor = (1.0 - metalness) * baseColor;
202 FP vec3 diffuse = diffuseColor * texture(envLight.irradiance, l).rgb;
203
204 // Calculate specular component
205 FP vec3 dielectricColor = vec3(0.04);
206 FP vec3 F0 = mix(dielectricColor, baseColor, metalness);
207 FP vec3 specularFactor = specularModel(F0, lDotH, lDotN, vDotN, n, h);
208
209 FP float lod = alphaToMipLevel(alpha);
210 //#define DEBUG_SPECULAR_LODS
211 #ifdef DEBUG_SPECULAR_LODS
212 if (lod > 7.0)
213 return vec3(1.0, 0.0, 0.0);
214 else if (lod > 6.0)
215 return vec3(1.0, 0.333, 0.0);
216 else if (lod > 5.0)
217 return vec3(1.0, 1.0, 0.0);
218 else if (lod > 4.0)
219 return vec3(0.666, 1.0, 0.0);
220 else if (lod > 3.0)
221 return vec3(0.0, 1.0, 0.666);
222 else if (lod > 2.0)
223 return vec3(0.0, 0.666, 1.0);
224 else if (lod > 1.0)
225 return vec3(0.0, 0.0, 1.0);
226 else if (lod > 0.0)
227 return vec3(1.0, 0.0, 1.0);
228 #endif
229 FP vec3 specularSkyColor = textureLod(envLight.specular, l, lod).rgb;
230 FP vec3 specular = specularSkyColor * specularFactor;
231
232 // Blend between diffuse and specular to conserve energy
233 FP vec3 color = specular + diffuse * (vec3(1.0) - specularFactor);
234
235 // Reduce by ambient occlusion amount
236 color *= ambientOcclusion;
237
238 return color;
239 }
240
241 FP vec3 toneMap(const in FP vec3 c)
242 {
243 return c / (c + vec3(1.0));
244 }
245
246 FP vec3 gammaCorrect(const in FP vec3 color)
247 {
248 return pow(color, vec3(1.0 / gamma));
249 }
250
251 FP vec4 metalRoughFunction(const in FP vec4 baseColor,
252 const in FP float metalness,
253 const in FP float roughness,
254 const in FP float ambientOcclusion,
255 const in FP vec3 worldPosition,
256 const in FP vec3 worldView,
257 const in FP vec3 worldNormal)
258 {
259 FP vec3 cLinear = vec3(0.0);
260
261 // Remap roughness for a perceptually more linear correspondence
262 FP float alpha = remapRoughness(roughness);
263
264 for (int i = 0; i < envLightCount; ++i) {
265 cLinear += pbrIblModel(worldNormal,
266 worldView,
267 baseColor.rgb,
268 metalness,
269 alpha,
270 ambientOcclusion);
271 }
272
273 for (int i = 0; i < lightCount; ++i) {
274 cLinear += pbrModel(i,
275 worldPosition,
276 worldNormal,
277 worldView,
278 baseColor.rgb,
279 metalness,
280 alpha,
281 ambientOcclusion);
282 }
283
284 // Apply exposure correction
285 cLinear *= pow(2.0, exposure);
286
287 // Apply simple (Reinhard) tonemap transform to get into LDR range [0, 1]
288 FP vec3 cToneMapped = toneMap(cLinear);
289
290 // Apply gamma correction prior to display
291 FP vec3 cGamma = gammaCorrect(cToneMapped);
292
293 return vec4(cGamma, 1.0);
294 }