@@ -97,195 +97,7 @@ layout(location = 0) in struct plShaderIn {
9797 vec2 tUV;
9898} tShaderIn;
9999
100- vec3
101- getDiffuseLight(vec3 n, int iProbeIndex)
102- {
103- // n.z = -n.z; // uncomment if not reverse z
104- // return texture(samplerCube(u_LambertianEnvSampler, tEnvSampler), n).rgb;
105- return texture(samplerCube (atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uLambertianEnvSampler)], tEnvSampler), n).rgb;
106- }
107-
108- vec4
109- getSpecularSample(vec3 reflection, float lod, int iProbeIndex)
110- {
111- // reflection.z = -reflection.z; // uncomment if not reverse z
112- // reflection.x = -reflection.x; // uncomment if not reverse z
113- // return textureLod(samplerCube(u_GGXEnvSampler, tEnvSampler), reflection, lod);
114- return textureLod(samplerCube (atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tEnvSampler), reflection, lod);
115- }
116-
117- vec3
118- getIBLRadianceGGX(vec3 n, vec3 v, float roughness, vec3 F0, float specularWeight, int u_MipCount, int iProbeIndex, vec3 tWorldPos)
119- {
120-
121- float lod = roughness * float (u_MipCount - 1 );
122- vec3 reflection = normalize (reflect (- v, n));
123-
124- if (bool (tProbeData.atData[iProbeIndex].iParallaxCorrection))
125- {
126-
127- // Find the ray intersection with box plane
128- vec3 FirstPlaneIntersect = (tProbeData.atData[iProbeIndex].tMax.xyz - tWorldPos) / reflection;
129- vec3 SecondPlaneIntersect = (tProbeData.atData[iProbeIndex].tMin.xyz - tWorldPos) / reflection;
130-
131- // Get the furthest of these intersections along the ray
132- // (Ok because x/0 give +inf and -x/0 give –inf )
133- vec3 FurthestPlane = max (FirstPlaneIntersect, SecondPlaneIntersect);
134-
135- // Find the closest far intersection
136- float Distance = min (min (FurthestPlane.x, FurthestPlane.y), FurthestPlane.z);
137-
138- // Get the intersection position
139- vec3 IntersectPositionWS = tWorldPos + reflection * Distance;
140-
141- // Get corrected reflection
142- reflection = IntersectPositionWS - tProbeData.atData[iProbeIndex].tPosition;
143- }
144-
145- // End parallax-correction code
146-
147- vec4 specularSample = getSpecularSample(reflection, lod, iProbeIndex);
148-
149- float NdotV = clampedDot(n, v);
150- vec2 brdfSamplePoint = clamp (vec2 (NdotV, roughness), vec2 (0.0 , 0.0 ), vec2 (1.0 , 1.0 ));
151- // vec2 f_ab = texture(sampler2D(u_GGXLUT, tEnvSampler), brdfSamplePoint).rg;
152- vec2 f_ab = texture(sampler2D (at2DTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;
153-
154- vec3 specularLight = specularSample.rgb;
155-
156- vec3 Fr = max (vec3 (1.0 - roughness), F0) - F0;
157- vec3 k_S = F0 + Fr * pow (1.0 - NdotV, 5.0 );
158- vec3 FssEss = k_S * f_ab.x + f_ab.y;
159-
160- return specularWeight * specularLight * FssEss;
161- }
162-
163- // specularWeight is introduced with KHR_materials_specular
164- vec3
165- getIBLRadianceLambertian(vec3 n, vec3 v, float roughness, vec3 diffuseColor, vec3 F0, float specularWeight, int iProbeIndex, vec3 tWorldPos)
166- {
167-
168- // if(bool(tProbeData.atData[iProbeIndex].iParallaxCorrection))
169- // {
170-
171- // // Find the ray intersection with box plane
172- // vec3 FirstPlaneIntersect = (tProbeData.atData[iProbeIndex].tMax.xyz - tWorldPos) / n;
173- // vec3 SecondPlaneIntersect = (tProbeData.atData[iProbeIndex].tMin.xyz - tWorldPos) / n;
174-
175- // // Get the furthest of these intersections along the ray
176- // // (Ok because x/0 give +inf and -x/0 give –inf )
177- // vec3 FurthestPlane = max(FirstPlaneIntersect, SecondPlaneIntersect);
178-
179- // // Find the closest far intersection
180- // float Distance = min(min(FurthestPlane.x, FurthestPlane.y), FurthestPlane.z);
181-
182- // // Get the intersection position
183- // vec3 IntersectPositionWS = tWorldPos + n * Distance;
184-
185- // // Get corrected reflection
186- // n = IntersectPositionWS - tProbeData.atData[iProbeIndex].tPosition;
187- // }
188-
189- // End parallax-correction code
190-
191- vec3 irradiance = getDiffuseLight(n, iProbeIndex);
192-
193- float NdotV = clampedDot(n, v);
194- vec2 brdfSamplePoint = clamp (vec2 (NdotV, roughness), vec2 (0.0 , 0.0 ), vec2 (1.0 , 1.0 ));
195- // vec2 f_ab = texture(sampler2D(u_GGXLUT, tEnvSampler), brdfSamplePoint).rg;
196- vec2 f_ab = texture(sampler2D (at2DTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXLUT)], tEnvSampler), brdfSamplePoint).rg;
197-
198- // see https://bruop.github.io/ibl/#single_scattering_results at Single Scattering Results
199- // Roughness dependent fresnel, from Fdez-Aguera
200-
201- vec3 Fr = max (vec3 (1.0 - roughness), F0) - F0;
202- vec3 k_S = F0 + Fr * pow (1.0 - NdotV, 5.0 );
203- vec3 FssEss = specularWeight * k_S * f_ab.x + f_ab.y; // <--- GGX / specular light contribution (scale it down if the specularWeight is low)
204-
205- // Multiple scattering, from Fdez-Aguera
206- float Ems = (1.0 - (f_ab.x + f_ab.y));
207- vec3 F_avg = specularWeight * (F0 + (1.0 - F0) / 21.0 );
208- vec3 FmsEms = Ems * FssEss * F_avg / (1.0 - F_avg * Ems);
209- vec3 k_D = diffuseColor * (1.0 - FssEss + FmsEms); // we use +FmsEms as indicated by the formula in the blog post (might be a typo in the implementation)
210-
211- return (FmsEms + k_D) * irradiance;
212- }
213-
214- float
215- textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
216- {
217- float shadow = 1.0 ;
218- vec2 comp2 = shadowCoord.st + offset;
219-
220- if ( shadowCoord.z > - 1.0 && shadowCoord.z < 1.0 )
221- {
222- float dist = 1.0 - texture(sampler2D (at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r * shadowCoord.w;
223- if (shadowCoord.w > 0 && dist < shadowCoord.z)
224- {
225- shadow = 0.0 ; // ambient
226- }
227- }
228- return shadow;
229- }
230-
231- float
232- textureProj2(vec4 shadowCoord, vec2 offset, int textureIndex)
233- {
234- float shadow = 1.0 ;
235- vec2 comp2 = shadowCoord.st + offset;
236-
237- if ( shadowCoord.z > - 1.0 && shadowCoord.z < 1.0 )
238- {
239- float dist = texture(sampler2D (at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
240- if (shadowCoord.w > 0 && dist > shadowCoord.z)
241- {
242- shadow = 0.0 ; // ambient
243- }
244- }
245- return shadow;
246- }
247-
248- float
249- filterPCF(vec4 sc, vec2 offset, int textureIndex)
250- {
251- ivec2 texDim = textureSize(sampler2D (at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), 0 ).xy;
252- float scale = 1.0 ;
253- float dx = scale * 1.0 / (float (texDim.x));
254- float dy = scale * 1.0 / (float (texDim.y));
255-
256- float shadowFactor = 0.0 ;
257- int count = 0 ;
258- int range = 1 ;
259-
260- for (int x = - range; x <= range; x++ ) {
261- for (int y = - range; y <= range; y++ ) {
262- shadowFactor += textureProj(sc, vec2 (dx* x, dy* y) + offset, textureIndex);
263- count++ ;
264- }
265- }
266- return shadowFactor / count;
267- }
268-
269- float
270- filterPCF2(vec4 sc, vec2 offset, int textureIndex)
271- {
272- ivec2 texDim = textureSize(sampler2D (at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), 0 ).xy;
273- float scale = 1.0 ;
274- float dx = scale * 1.0 / (float (texDim.x));
275- float dy = scale * 1.0 / (float (texDim.y));
276-
277- float shadowFactor = 0.0 ;
278- int count = 0 ;
279- int range = 1 ;
280-
281- for (int x = - range; x <= range; x++ ) {
282- for (int y = - range; y <= range; y++ ) {
283- shadowFactor += textureProj2(sc, vec2 (dx* x, dy* y) + offset, textureIndex);
284- count++ ;
285- }
286- }
287- return shadowFactor / count;
288- }
100+ #include "lighting.glsl"
289101
290102void main()
291103{
0 commit comments