4 Copyright (c) 2022 Sascha Willems
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 layout (binding = 1) uniform sampler2D samplerposition;
28 layout (binding = 2) uniform sampler2D samplerNormal;
29 layout (binding = 3) uniform sampler2D samplerAlbedo;
30 layout (binding = 5) uniform sampler2DArray samplerShadowMap;
32 layout (location = 0) in vec2 inUV;
34 layout (location = 0) out vec4 outFragColor;
37 #define SHADOW_FACTOR 0.25
38 #define AMBIENT_LIGHT 0.1
49 layout (binding = 4) uniform UBO
52 Light lights[LIGHT_COUNT];
54 int debugDisplayTarget;
57 float textureProj(vec4 P, float layer, vec2 offset)
60 vec4 shadowCoord = P / P.w;
61 shadowCoord.st = shadowCoord.st * 0.5 + 0.5;
63 if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
65 float dist = texture(samplerShadowMap, vec3(shadowCoord.st + offset, layer)).r;
66 if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
68 shadow = SHADOW_FACTOR;
74 float filterPCF(vec4 sc, float layer)
76 ivec2 texDim = textureSize(samplerShadowMap, 0).xy;
78 float dx = scale * 1.0 / float(texDim.x);
79 float dy = scale * 1.0 / float(texDim.y);
81 float shadowFactor = 0.0;
85 for (int x = -range; x <= range; x++)
87 for (int y = -range; y <= range; y++)
89 shadowFactor += textureProj(sc, layer, vec2(dx*x, dy*y));
94 return shadowFactor / count;
97 vec3 shadow(vec3 fragcolor, vec3 fragpos) {
98 for(int i = 0; i < LIGHT_COUNT; ++i)
100 vec4 shadowClip = ubo.lights[i].viewMatrix * vec4(fragpos, 1.0);
104 shadowFactor= filterPCF(shadowClip, i);
106 shadowFactor = textureProj(shadowClip, i, vec2(0.0));
109 fragcolor *= shadowFactor;
116 // Get G-Buffer values
117 vec3 fragPos = texture(samplerposition, inUV).rgb;
118 vec3 normal = texture(samplerNormal, inUV).rgb;
119 vec4 albedo = texture(samplerAlbedo, inUV);
122 if (ubo.debugDisplayTarget > 0) {
123 switch (ubo.debugDisplayTarget) {
125 outFragColor.rgb = shadow(vec3(1.0), fragPos).rgb;
128 outFragColor.rgb = fragPos;
131 outFragColor.rgb = normal;
134 outFragColor.rgb = albedo.rgb;
137 outFragColor.rgb = albedo.aaa;
140 outFragColor.a = 1.0;
145 vec3 fragcolor = albedo.rgb * AMBIENT_LIGHT;
147 vec3 N = normalize(normal);
149 for(int i = 0; i < LIGHT_COUNT; ++i)
152 vec3 L = ubo.lights[i].position.xyz - fragPos;
153 // Distance from light to fragment position
154 float dist = length(L);
157 // Viewer to fragment
158 vec3 V = ubo.viewPos.xyz - fragPos;
161 float lightCosInnerAngle = cos(radians(15.0));
162 float lightCosOuterAngle = cos(radians(25.0));
163 float lightRange = 100.0;
165 // Direction vector from source to target
166 vec3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
168 // Dual cone spot light with smooth transition between inner and outer angle
169 float cosDir = dot(L, dir);
170 float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
171 float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
174 float NdotL = max(0.0, dot(N, L));
175 vec3 diff = vec3(NdotL);
178 vec3 R = reflect(-L, N);
179 float NdotR = max(0.0, dot(R, V));
180 vec3 spec = vec3(pow(NdotR, 16.0) * albedo.a * 2.5);
182 fragcolor += vec3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
185 // Shadow calculations in a separate pass
186 if (ubo.useShadows > 0)
188 fragcolor = shadow(fragcolor, fragPos);
191 outFragColor = vec4(fragcolor, 1.0);