Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 120
- uniform sampler2D gcolor;
- uniform sampler2D gdepth;
- uniform sampler2D gnormal;
- uniform sampler2D gaux1; // specular map
- uniform sampler2D shadow;
- varying vec4 texcoord;
- varying vec3 lightVector;
- varying vec3 specMultiplier;
- uniform mat4 gbufferProjectionInverse;
- uniform mat4 gbufferModelViewInverse;
- uniform mat4 shadowProjection;
- uniform mat4 shadowModelView;
- varying vec3 heldLightSpecMultiplier;
- varying float heldLightMagnitude;
- void main() {
- gl_FragData[0] = texture2D(gcolor, texcoord.st);
- gl_FragData[1] = texture2D(gdepth, texcoord.st);
- gl_FragData[2] = texture2D(gnormal, texcoord.st);
- gl_FragData[4] = texture2D(gaux1, texcoord.st);
- vec4 fragposition = gbufferProjectionInverse * vec4(texcoord.s * 2.0 - 1.0, texcoord.t * 2.0 - 1.0, 2.0 * texture2D(gdepth, texcoord.st).x - 1.0, 1.0);
- fragposition /= fragposition.w;
- float distance = sqrt(fragposition.x * fragposition.x + fragposition.y * fragposition.y + fragposition.z * fragposition.z);
- float shading = 1.0;
- if (distance < 25.0 && distance > 0.1) {
- // shadows
- vec4 worldposition = gbufferModelViewInverse * fragposition;
- float xzDistanceSquared = worldposition.x * worldposition.x + worldposition.z * worldposition.z;
- float yDistanceSquared = worldposition.y * worldposition.y;
- if (yDistanceSquared < 225.0) {
- worldposition = shadowModelView * worldposition;
- float comparedepth = -worldposition.z;
- worldposition = shadowProjection * worldposition;
- worldposition /= worldposition.w;
- worldposition.st = worldposition.st * 0.5 + 0.5;
- if (comparedepth > 0.0 && worldposition.s < 1.0 && worldposition.s > 0.0 && worldposition.t < 1.0 && worldposition.t > 0.0){
- float shadowMult = min(1.0 - xzDistanceSquared / 625.0, 1.0) * min(1.0 - yDistanceSquared / 225.0, 1.0);
- float shadowSample = texture2D(shadow, worldposition.st).z;
- float shadowDepth = 2.0 * 0.05 * 256.0 / (256.0 + 0.05 - (2.0 * shadowSample - 1.0) * (256.0 - 0.05));
- shading = 1.0 - shadowMult * (clamp(comparedepth - shadowDepth - 0.2, 0.0, 3.0) / 3.0 * 0.4 - 0.2);
- }
- }
- }
- vec3 npos = normalize(fragposition.xyz);
- vec3 bump = reflect(npos, texture2D(gnormal, texcoord.st).xyz * 2.0 - 1.0);
- vec3 specularColor = texture2D(gaux1, texcoord.st).rgb;
- float s;
- if(shading > 1.1) {
- s = max(dot(bump, lightVector), 0.0);
- gl_FragData[3] = vec4(min(texture2D(gcolor, texcoord.st).rgb + specularColor * s * s * s * specMultiplier, 1.0), 1.0);
- }
- else {
- gl_FragData[3] = texture2D(gcolor, texcoord.st);
- }
- if (heldLightMagnitude > 0.0) {
- if (distance < heldLightMagnitude && distance > 0.1) {
- float intensity = 1.0 - min(distance / heldLightMagnitude, 1.0);
- s = max(dot(bump, -npos), 0.0);
- gl_FragData[3].rgb = min(gl_FragData[3].rgb + intensity * specularColor * s * s * s * heldLightSpecMultiplier, 1.0);
- }
- }
- gl_FragData[3].rgb *= shading;
- }
Advertisement
Add Comment
Please, Sign In to add comment