Debian Codegraph @

src/extras/shaders/rhi/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 #pragma include light.inc.frag
5
6 int mipLevelCount(const in samplerCube cube)
7 {
8 int baseSize = textureSize(cube, 0).x;
9 int nMips = int(log2(float(baseSize > 0 ? baseSize : 1))) + 1;
10 return nMips;
11 }
12
13 float remapRoughness(const in float roughness)
14 {
15 // As per page 14 of
16 // http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
17 // we remap the roughness to give a more perceptually linear response
18 // of "bluriness" as a function of the roughness specified by the user.
19 // r = roughness^2
20 const float maxSpecPower = 999999.0;
21 const float minRoughness = sqrt(2.0 / (maxSpecPower + 2));
22 return max(roughness * roughness, minRoughness);
23 }
24
25 float alphaToMipLevel(float alpha)
26 {
27 float specPower = 2.0 / (alpha * alpha) - 2.0;
28
29 // We use the mip level calculation from Lys' default power drop, which in
30 // turn is a slight modification of that used in Marmoset Toolbag. See
31 // https://docs.knaldtech.com/doku.php?id=specular_lys for details.
32 // For now we assume a max specular power of 999999 which gives
33 // maxGlossiness = 1.
34 const float k0 = 0.00098;
35 const float k1 = 0.9921;
36 float glossiness = (pow(2.0, -10.0 / sqrt(specPower)) - k0) / k1;
37
38 // TODO: Optimize by doing this on CPU and set as
39 // uniform int envLight.specularMipLevels say (if present in shader).
40 // Lookup the number of mips in the specular envmap
41 int mipLevels = mipLevelCount(envLight_specular);
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 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 float mipLevel = (mipLevels - 1.0 - mipOffset) * (1.0 - glossiness);
52 return mipLevel;
53 }
54
55 float normalDistribution(const in vec3 n, const in vec3 h, const in float alpha)
56 {
57 // Blinn-Phong approximation - see
58 // http://graphicrants.blogspot.co.uk/2013/08/specular-brdf-reference.html
59 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 vec3 fresnelFactor(const in vec3 color, const in float cosineFactor)
64 {
65 // Calculate the Fresnel effect value
66 vec3 f = color;
67 vec3 F = f + (1.0 - f) * pow(1.0 - cosineFactor, 5.0);
68 return clamp(F, f, vec3(1.0));
69 }
70
71 float geometricModel(const in float lDotN,
72 const in float vDotN,
73 const in 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 vec3 specularModel(const in vec3 F0,
82 const in float sDotH,
83 const in float sDotN,
84 const in float vDotN,
85 const in vec3 n,
86 const in 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 float sDotNPrime = max(sDotN, 0.001);
93 float vDotNPrime = max(vDotN, 0.001);
94
95 vec3 F = fresnelFactor(F0, sDotH);
96 float G = geometricModel(sDotNPrime, vDotNPrime, h);
97
98 vec3 cSpec = F * G / (4.0 * sDotNPrime * vDotNPrime);
99 return clamp(cSpec, vec3(0.0), vec3(1.0));
100 }
101
102 vec3 pbrModel(const in int lightIndex,
103 const in vec3 wPosition,
104 const in vec3 wNormal,
105 const in vec3 wView,
106 const in vec3 baseColor,
107 const in float metalness,
108 const in float alpha,
109 const in float ambientOcclusion)
110 {
111 // Calculate some useful quantities
112 vec3 n = wNormal;
113 vec3 s = vec3(0.0);
114 vec3 v = wView;
115 vec3 h = vec3(0.0);
116
117 float vDotN = dot(v, n);
118 float sDotN = 0.0;
119 float sDotH = 0.0;
120 float att = 1.0;
121
122 if (lights[lightIndex].type != TYPE_DIRECTIONAL) {
123 // Point and Spot lights
124 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 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 vec3 diffuseColor = (1.0 - metalness) * baseColor * lights[lightIndex].color;
158 vec3 diffuse = diffuseColor * max(sDotN, 0.0) / 3.14159;
159
160 // Calculate specular component
161 vec3 dielectricColor = vec3(0.04);
162 vec3 F0 = mix(dielectricColor, baseColor, metalness);
163 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 vec3 specularColor = lights[lightIndex].color;
169 vec3 specular = specularColor * specularFactor;
170
171 // Blend between diffuse and specular to conserver energy
172 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 vec3 pbrIblModel(const in vec3 wNormal,
181 const in vec3 wView,
182 const in vec3 baseColor,
183 const in float metalness,
184 const in float alpha,
185 const in 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 vec3 n = wNormal;
193 vec3 l = reflect(-wView, n);
194 vec3 v = wView;
195 vec3 h = normalize(l + v);
196 float vDotN = dot(v, n);
197 float lDotN = dot(l, n);
198 float lDotH = dot(l, h);
199
200 // Calculate diffuse component
201 vec3 diffuseColor = (1.0 - metalness) * baseColor;
202 vec3 diffuse = diffuseColor * texture(envLight_irradiance, l).rgb;
203
204 // Calculate specular component
205 vec3 dielectricColor = vec3(0.04);
206 vec3 F0 = mix(dielectricColor, baseColor, metalness);
207 vec3 specularFactor = specularModel(F0, lDotH, lDotN, vDotN, n, h);
208
209 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 vec3 specularSkyColor = textureLod(envLight_specular, l, lod).rgb;
230 vec3 specular = specularSkyColor * specularFactor;
231
232 // Blend between diffuse and specular to conserve energy
233 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 vec3 toneMap(const in vec3 c)
242 {
243 return c / (c + vec3(1.0));
244 }
245
246 vec3 gammaCorrect(const in vec3 color)
247 {
248 return pow(color, vec3(1.0 / gamma));
249 }
250
251 vec4 metalRoughFunction(const in vec4 baseColor,
252 const in float metalness,
253 const in float roughness,
254 const in float ambientOcclusion,
255 const in vec3 worldPosition,
256 const in vec3 worldView,
257 const in vec3 worldNormal)
258 {
259 vec3 cLinear = vec3(0.0);
260
261 // Remap roughness for a perceptually more linear correspondence
262 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 vec3 cToneMapped = toneMap(cLinear);
289
290 // Apply gamma correction prior to display
291 vec3 cGamma = gammaCorrect(cToneMapped);
292
293 return vec4(cGamma, 1.0);
294 }