Debian Codegraph @

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