| 1 |
#version 120 |
| 2 |
|
| 3 |
uniform sampler3D fg_Clusters; |
| 4 |
uniform sampler2D fg_ClusteredIndices; |
| 5 |
uniform sampler2D fg_ClusteredPointLights; |
| 6 |
uniform sampler2D fg_ClusteredSpotLights; |
| 7 |
|
| 8 |
uniform int fg_ClusteredMaxPointLights; |
| 9 |
uniform int fg_ClusteredMaxSpotLights; |
| 10 |
uniform int fg_ClusteredMaxLightIndices; |
| 11 |
uniform int fg_ClusteredTileSize; |
| 12 |
uniform int fg_ClusteredDepthSlices; |
| 13 |
uniform float fg_ClusteredSliceScale; |
| 14 |
uniform float fg_ClusteredSliceBias; |
| 15 |
uniform int fg_ClusteredHorizontalTiles; |
| 16 |
uniform int fg_ClusteredVerticalTiles; |
| 17 |
|
| 18 |
const bool DEBUG = false; |
| 19 |
|
| 20 |
struct PointLight { |
| 21 |
vec4 position; |
| 22 |
vec4 ambient; |
| 23 |
vec4 diffuse; |
| 24 |
vec4 specular; |
| 25 |
vec4 attenuation; |
| 26 |
}; |
| 27 |
|
| 28 |
struct SpotLight { |
| 29 |
vec4 position; |
| 30 |
vec4 direction; |
| 31 |
vec4 ambient; |
| 32 |
vec4 diffuse; |
| 33 |
vec4 specular; |
| 34 |
vec4 attenuation; |
| 35 |
float cos_cutoff; |
| 36 |
float exponent; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
PointLight unpackPointLight(int index) |
| 41 |
{ |
| 42 |
PointLight light; |
| 43 |
float v = (float(index) + 0.5) / float(fg_ClusteredMaxPointLights); |
| 44 |
light.position = texture2D(fg_ClusteredPointLights, vec2(0.1, v)); |
| 45 |
light.ambient = texture2D(fg_ClusteredPointLights, vec2(0.3, v)); |
| 46 |
light.diffuse = texture2D(fg_ClusteredPointLights, vec2(0.5, v)); |
| 47 |
light.specular = texture2D(fg_ClusteredPointLights, vec2(0.7, v)); |
| 48 |
light.attenuation = texture2D(fg_ClusteredPointLights, vec2(0.9, v)); |
| 49 |
return light; |
| 50 |
} |
| 51 |
|
| 52 |
SpotLight unpackSpotLight(int index) |
| 53 |
{ |
| 54 |
SpotLight light; |
| 55 |
float v = (float(index) + 0.5) / float(fg_ClusteredMaxSpotLights); |
| 56 |
light.position = texture2D(fg_ClusteredSpotLights, vec2(0.0714, v)); |
| 57 |
light.direction = texture2D(fg_ClusteredSpotLights, vec2(0.2143, v)); |
| 58 |
light.ambient = texture2D(fg_ClusteredSpotLights, vec2(0.3571, v)); |
| 59 |
light.diffuse = texture2D(fg_ClusteredSpotLights, vec2(0.5, v)); |
| 60 |
light.specular = texture2D(fg_ClusteredSpotLights, vec2(0.6429, v)); |
| 61 |
light.attenuation = texture2D(fg_ClusteredSpotLights, vec2(0.7857, v)); |
| 62 |
vec2 remainder = texture2D(fg_ClusteredSpotLights, vec2(0.9286, v)).xy; |
| 63 |
light.cos_cutoff = remainder.x; |
| 64 |
light.exponent = remainder.y; |
| 65 |
return light; |
| 66 |
} |
| 67 |
|
| 68 |
int getIndex(int counter) |
| 69 |
{ |
| 70 |
vec2 coords = vec2(mod(float(counter), float(fg_ClusteredMaxLightIndices)) + 0.5, |
| 71 |
float(counter / fg_ClusteredMaxLightIndices) + 0.5); |
| 72 |
// Normalize |
| 73 |
coords /= vec2(fg_ClusteredMaxLightIndices); |
| 74 |
return int(texture2D(fg_ClusteredIndices, coords).r); |
| 75 |
} |
| 76 |
|
| 77 |
// @param p Fragment position in view space. |
| 78 |
// @param n Fragment normal in view space. |
| 79 |
// @param texel The diffuse (or albedo) color of the surface. It's usually just |
| 80 |
// the one on texture unit 0. |
| 81 |
// @return The total color contribution of every light affecting the fragment. |
| 82 |
// This result should be added to the fragment color before applying |
| 83 |
// any haze, fog or post-processing. |
| 84 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel) |
| 85 |
{ |
| 86 |
int slice = int(max(log2(-p.z) * fg_ClusteredSliceScale |
| 87 |
+ fg_ClusteredSliceBias, 0.0)); |
| 88 |
vec3 clusterCoords = vec3(floor(gl_FragCoord.xy / fg_ClusteredTileSize), |
| 89 |
slice) + vec3(0.5); // Pixel center |
| 90 |
// Normalize |
| 91 |
clusterCoords /= vec3(fg_ClusteredHorizontalTiles, |
| 92 |
fg_ClusteredVerticalTiles, |
| 93 |
fg_ClusteredDepthSlices); |
| 94 |
|
| 95 |
vec3 cluster = texture3D(fg_Clusters, clusterCoords).rgb; |
| 96 |
int lightIndex = int(cluster.r); |
| 97 |
int pointCount = int(cluster.g); |
| 98 |
int spotCount = int(cluster.b); |
| 99 |
|
| 100 |
if (DEBUG) { |
| 101 |
vec2 margin = step(1.0, mod(gl_FragCoord.xy, vec2(fg_ClusteredTileSize))); |
| 102 |
return mix(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), |
| 103 |
float(pointCount) / 5.0) * margin.x * margin.y; |
| 104 |
} |
| 105 |
|
| 106 |
vec3 color = vec3(0.0); |
| 107 |
|
| 108 |
for (int i = 0; i < pointCount; ++i) { |
| 109 |
int index = getIndex(lightIndex++); |
| 110 |
PointLight light = unpackPointLight(index); |
| 111 |
|
| 112 |
float range = light.attenuation.w; |
| 113 |
vec3 toLight = light.position.xyz - p; |
| 114 |
// Ignore fragments outside the light volume |
| 115 |
if (dot(toLight, toLight) > (range * range)) |
| 116 |
continue; |
| 117 |
|
| 118 |
float d = length(toLight); |
| 119 |
float att = 1.0 / (light.attenuation.x // constant |
| 120 |
+ light.attenuation.y * d // linear |
| 121 |
+ light.attenuation.z * d * d); // quadratic |
| 122 |
vec3 lightDir = normalize(toLight); |
| 123 |
float NdotL = max(dot(n, lightDir), 0.0); |
| 124 |
|
| 125 |
vec3 Iamb = light.ambient.rgb; |
| 126 |
vec3 Idiff = gl_FrontMaterial.diffuse.rgb * light.diffuse.rgb * NdotL; |
| 127 |
vec3 Ispec = vec3(0.0); |
| 128 |
|
| 129 |
if (NdotL > 0.0) { |
| 130 |
vec3 halfVector = normalize(lightDir + normalize(-p)); |
| 131 |
float NdotHV = max(dot(n, halfVector), 0.0); |
| 132 |
Ispec = gl_FrontMaterial.specular.rgb |
| 133 |
* light.specular.rgb |
| 134 |
* pow(NdotHV, gl_FrontMaterial.shininess); |
| 135 |
} |
| 136 |
|
| 137 |
color += ((Iamb + Idiff) * texel + Ispec) * att; |
| 138 |
} |
| 139 |
|
| 140 |
for (int i = 0; i < spotCount; ++i) { |
| 141 |
int index = getIndex(lightIndex++); |
| 142 |
SpotLight light = unpackSpotLight(index); |
| 143 |
|
| 144 |
vec3 toLight = light.position.xyz - p; |
| 145 |
|
| 146 |
float d = length(toLight); |
| 147 |
float att = 1.0 / (light.attenuation.x // constant |
| 148 |
+ light.attenuation.y * d // linear |
| 149 |
+ light.attenuation.z * d * d); // quadratic |
| 150 |
|
| 151 |
vec3 lightDir = normalize(toLight); |
| 152 |
|
| 153 |
float spotDot = dot(-lightDir, light.direction.xyz); |
| 154 |
if (spotDot < light.cos_cutoff) |
| 155 |
continue; |
| 156 |
|
| 157 |
att *= pow(spotDot, light.exponent); |
| 158 |
|
| 159 |
float NdotL = max(dot(n, lightDir), 0.0); |
| 160 |
|
| 161 |
vec3 Iamb = light.ambient.rgb; |
| 162 |
vec3 Idiff = gl_FrontMaterial.diffuse.rgb * light.diffuse.rgb * NdotL; |
| 163 |
vec3 Ispec = vec3(0.0); |
| 164 |
|
| 165 |
if (NdotL > 0.0) { |
| 166 |
vec3 halfVector = normalize(lightDir + normalize(-p)); |
| 167 |
float NdotHV = max(dot(n, halfVector), 0.0); |
| 168 |
Ispec = gl_FrontMaterial.specular.rgb |
| 169 |
* light.specular.rgb |
| 170 |
* pow(NdotHV, gl_FrontMaterial.shininess); |
| 171 |
} |
| 172 |
|
| 173 |
color += ((Iamb + Idiff) * texel + Ispec) * att; |
| 174 |
} |
| 175 |
|
| 176 |
return clamp(color, 0.0, 1.0); |
| 177 |
} |