| 1 |
////////////////////////////////////////////////////////////////////// |
| 2 |
// |
| 3 |
// First-stage screen-space fragment shader for the solid pipeline |
| 4 |
// |
| 5 |
// It offers ambient occlusion and edge detection capabilities. |
| 6 |
// |
| 7 |
////////////////////////////////////////////////////////////////////// |
| 8 |
|
| 9 |
#version 400 |
| 10 |
precision highp float; |
| 11 |
|
| 12 |
// |
| 13 |
// Input |
| 14 |
// |
| 15 |
|
| 16 |
// texture coordinates |
| 17 |
in vec2 UV; |
| 18 |
|
| 19 |
out vec4 outColor; |
| 20 |
|
| 21 |
// |
| 22 |
// Uniforms |
| 23 |
// |
| 24 |
|
| 25 |
// RGB rendered texture |
| 26 |
uniform sampler2D inRGBTex; |
| 27 |
|
| 28 |
// RGB color for applying fog |
| 29 |
uniform float fogR; |
| 30 |
uniform float fogG; |
| 31 |
uniform float fogB; |
| 32 |
uniform float offset; |
| 33 |
|
| 34 |
// Depth rendered texture |
| 35 |
uniform sampler2D inDepthTex; |
| 36 |
// 1.0 if enabled, 0.0 if disabled |
| 37 |
uniform float inAoEnabled; |
| 38 |
// 0.0 if disabled |
| 39 |
uniform float inFogStrength; |
| 40 |
// Shadow strength for SSAO |
| 41 |
uniform float inAoStrength; |
| 42 |
// 1.0 if enabled, 0.0 if disabled |
| 43 |
uniform float inEdStrength; |
| 44 |
// amount of offset when zoom-in or zoom-out. |
| 45 |
uniform float uoffset; |
| 46 |
// Dof strength |
| 47 |
uniform float inDofStrength; |
| 48 |
// Dof position |
| 49 |
uniform float inDofPosition; |
| 50 |
// position for other molecules. |
| 51 |
uniform float inFogPosition; |
| 52 |
// Rendering surface dimensions, in pixels |
| 53 |
uniform float width, height; |
| 54 |
|
| 55 |
vec3 getNormalAt(vec2 normalUV) |
| 56 |
{ |
| 57 |
float xpos = texture(inDepthTex, normalUV + vec2(1.0 / width, 0.0)).x; |
| 58 |
float xneg = texture(inDepthTex, normalUV - vec2(1.0 / width, 0.0)).x; |
| 59 |
float ypos = texture(inDepthTex, normalUV + vec2(0.0, 1.0 / height)).x; |
| 60 |
float yneg = texture(inDepthTex, normalUV - vec2(0.0, 1.0 / height)).x; |
| 61 |
float xdelta = xpos - xneg; |
| 62 |
float ydelta = ypos - yneg; |
| 63 |
vec3 r = vec3(xdelta, ydelta, 1.0 / width + 1.0 / height); |
| 64 |
return normalize(r); |
| 65 |
} |
| 66 |
|
| 67 |
vec3 getNormalNear(vec2 normalUV) |
| 68 |
{ |
| 69 |
float cent = texture(inDepthTex, normalUV).x; |
| 70 |
float xpos = texture(inDepthTex, normalUV + vec2(1.0 / width, 0.0)).x; |
| 71 |
float xneg = texture(inDepthTex, normalUV - vec2(1.0 / width, 0.0)).x; |
| 72 |
float ypos = texture(inDepthTex, normalUV + vec2(0.0, 1.0 / height)).x; |
| 73 |
float yneg = texture(inDepthTex, normalUV - vec2(0.0, 1.0 / height)).x; |
| 74 |
float xposdelta = xpos - cent; |
| 75 |
float xnegdelta = cent - xneg; |
| 76 |
float yposdelta = ypos - cent; |
| 77 |
float ynegdelta = cent - yneg; |
| 78 |
float xdelta = abs(xposdelta) > abs(xnegdelta) ? xnegdelta : xposdelta; |
| 79 |
float ydelta = abs(yposdelta) > abs(ynegdelta) ? ynegdelta : yposdelta; |
| 80 |
vec3 r = vec3(xdelta, ydelta, 0.5 / width + 0.5 / height); |
| 81 |
return normalize(r); |
| 82 |
} |
| 83 |
|
| 84 |
float lerp(float a, float b, float f) |
| 85 |
{ |
| 86 |
return a + f * (b - a); |
| 87 |
} |
| 88 |
|
| 89 |
float rand(vec2 co) { |
| 90 |
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); |
| 91 |
} |
| 92 |
|
| 93 |
float depthToZ(float depth) { |
| 94 |
float eyeZ = ((height * 0.57735) / 2.0); |
| 95 |
float near = 2.0; |
| 96 |
float far = 8000.0; |
| 97 |
float depthNormalized = 2.0 * depth - 1.0; |
| 98 |
return 2.0 * near * far / (far + near - depthNormalized * (far - near)); |
| 99 |
} |
| 100 |
|
| 101 |
float calcBlur(float z, float pixelScale) { |
| 102 |
return clamp(abs(z - 39.0), 0.0, 0.5 * pixelScale); |
| 103 |
} |
| 104 |
|
| 105 |
vec4 applyBlur(vec2 texCoord) { |
| 106 |
float pixelScale = max(width, height); |
| 107 |
float origZ = depthToZ(texture(inDepthTex, texCoord).x); |
| 108 |
float blurAmt = calcBlur(origZ, pixelScale); |
| 109 |
// Skip blurring if the original depth is less than the threshold |
| 110 |
if (origZ < uoffset * inDofPosition) { |
| 111 |
return texture(inRGBTex, texCoord); |
| 112 |
} |
| 113 |
float total = 1.0; |
| 114 |
vec4 color = texture(inRGBTex, texCoord); |
| 115 |
for (int i = 0; i < 32; i++) { |
| 116 |
float t = (float(i) / float(64)); |
| 117 |
float angle = (t * 4.0) * 6.28319; |
| 118 |
float radius = (t * 2. - 1.); |
| 119 |
angle += 1.0 * rand(gl_FragCoord.xy); |
| 120 |
vec2 offset = (vec2(cos(angle), sin(angle)) * radius * 0.05 * inDofStrength) / pixelScale; |
| 121 |
float z = depthToZ(texture(inDepthTex, texCoord + offset).x); |
| 122 |
float sampleBlur = calcBlur(z, pixelScale); |
| 123 |
float weight = 1.0 - smoothstep(0.0, 1.0, abs(z - origZ) / blurAmt); |
| 124 |
vec4 texSample = texture(inRGBTex, texCoord+offset); |
| 125 |
color += weight * texSample; |
| 126 |
total += weight; |
| 127 |
} |
| 128 |
return color / total; |
| 129 |
} |
| 130 |
|
| 131 |
vec4 applyFog(vec2 texCoord) { |
| 132 |
vec4 finalColor = mix( |
| 133 |
texture(inRGBTex, texCoord), |
| 134 |
vec4(vec3(fogR, fogG, fogB), 1.), |
| 135 |
pow(texture(inDepthTex, texCoord.xy).r, uoffset * inFogPosition / 10.0) |
| 136 |
) + inFogStrength / 100.0; |
| 137 |
return finalColor; |
| 138 |
} |
| 139 |
|
| 140 |
const vec2 SSAOkernel[16] = vec2[16]( |
| 141 |
vec2(0.072170, 0.081556), |
| 142 |
vec2(-0.035126, 0.056701), |
| 143 |
vec2(-0.034186, -0.083598), |
| 144 |
vec2(-0.056102, -0.009235), |
| 145 |
vec2(0.017487, -0.099822), |
| 146 |
vec2(0.071065, 0.015921), |
| 147 |
vec2(0.040950, 0.079834), |
| 148 |
vec2(-0.087751, 0.065326), |
| 149 |
vec2(0.061108, -0.025829), |
| 150 |
vec2(0.081262, -0.025854), |
| 151 |
vec2(-0.063816, 0.083857), |
| 152 |
vec2(0.043747, -0.068586), |
| 153 |
vec2(-0.089848, 0.049046), |
| 154 |
vec2(-0.065370, 0.058761), |
| 155 |
vec2(0.099581, -0.089322), |
| 156 |
vec2(-0.032077, -0.042826) |
| 157 |
); |
| 158 |
|
| 159 |
float computeSSAOLuminosity(vec3 normal) |
| 160 |
{ |
| 161 |
float totalOcclusion = 0.0; |
| 162 |
float depth = texture(inDepthTex, UV).x; |
| 163 |
float A = (width * UV.x + 10 * height * UV.y) * 2.0 * 3.14159265358979 * 5.0 / 16.0; |
| 164 |
float S = sin(A); |
| 165 |
float C = cos(A); |
| 166 |
mat2 rotation = mat2( |
| 167 |
C, -S, |
| 168 |
S, C |
| 169 |
); |
| 170 |
for (int i = 0; i < 16; i++) { |
| 171 |
vec2 samplePoint = rotation * SSAOkernel[i]; |
| 172 |
float occluderDepth = texture(inDepthTex, UV + samplePoint).x; |
| 173 |
vec3 occluder = vec3(samplePoint.xy, depth - occluderDepth); |
| 174 |
float d = length(occluder); |
| 175 |
float occlusion = max(0.0, dot(normal, occluder)) * (1.0 / (1.0 + d)); |
| 176 |
totalOcclusion += occlusion; |
| 177 |
} |
| 178 |
|
| 179 |
return max(0.0, 1.2 - inAoStrength * totalOcclusion); |
| 180 |
} |
| 181 |
|
| 182 |
float computeEdgeLuminosity(vec3 normal) |
| 183 |
{ |
| 184 |
return max(0.0, pow(normal.z - 0.1, 1.0 / 3.0)); |
| 185 |
} |
| 186 |
|
| 187 |
void main() { |
| 188 |
float luminosity = 1.0; |
| 189 |
vec4 color = texture(inRGBTex, UV); |
| 190 |
vec4 finalColor = color; // Initialize finalColor with base color |
| 191 |
|
| 192 |
// Compute luminosity based on Ambient Occlusion (AO) and Edge Detection |
| 193 |
if (inAoEnabled != 0.0) { |
| 194 |
luminosity *= max(1.2 * (1.0 - inAoEnabled), computeSSAOLuminosity(getNormalNear(UV))); |
| 195 |
} |
| 196 |
if (inEdStrength != 0.0) { |
| 197 |
luminosity *= max(1.0 - inEdStrength, computeEdgeLuminosity(getNormalAt(UV))); |
| 198 |
} |
| 199 |
|
| 200 |
// Compute foggedColor if Fog is enabled |
| 201 |
vec4 foggedColor = color; |
| 202 |
if (inFogStrength != 0.0) { |
| 203 |
foggedColor = applyFog(UV); |
| 204 |
} |
| 205 |
|
| 206 |
// Compute blurredColor if DOF is enabled |
| 207 |
vec4 blurredColor = color; |
| 208 |
if (inDofStrength != 0.0) { |
| 209 |
blurredColor = applyBlur(UV); |
| 210 |
} |
| 211 |
|
| 212 |
// Determine finalColor based on enabled effects |
| 213 |
if (inAoEnabled != 0.0 || inEdStrength != 0.0 || inDofStrength != 0.0) { |
| 214 |
if (inFogStrength != 0.0 && inDofStrength != 0.0) { |
| 215 |
// Both Fog and DOF are enabled |
| 216 |
vec4 mixedColor = mix(foggedColor, blurredColor, 0.5); |
| 217 |
finalColor = vec4(mixedColor.rgb * luminosity, mixedColor.a); |
| 218 |
} else if (inFogStrength != 0.0) { |
| 219 |
// Only Fog is enabled with ao/edge-detection |
| 220 |
finalColor = vec4(foggedColor.rgb * luminosity, foggedColor.a); |
| 221 |
} else if (inDofStrength != 0.0) { |
| 222 |
// Only DOF is enabled with/without ao/edge |
| 223 |
finalColor = vec4(blurredColor.rgb * luminosity, blurredColor.a); |
| 224 |
} else { |
| 225 |
// Only AO and/or Edge Detection are enabled |
| 226 |
finalColor = vec4(color.rgb * luminosity, color.a); |
| 227 |
} |
| 228 |
} else { |
| 229 |
// Neither AO, DOF, nor Edge Detection is enabled |
| 230 |
if (inFogStrength != 0.0) { |
| 231 |
// Only Fog is enabled |
| 232 |
finalColor = foggedColor; |
| 233 |
} else { |
| 234 |
// No effects are enabled |
| 235 |
finalColor = color; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Set the final fragment color |
| 240 |
outColor = finalColor; |
| 241 |
gl_FragDepth = texture(inDepthTex, UV).x; |
| 242 |
} |