Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uniform vec3 lightPosition;
- uniform float lightZnear;
- uniform float lightZfar;
- uniform float lightSize;
- uniform vec4 lightColor;
- uniform float isGround;
- uniform float brightness;
- uniform float shadowMapSize;
- uniform sampler2D shadowMap;
- uniform float samples;
- uniform float sampleSize;
- uniform float alpha;
- varying vec3 vertPosition;
- varying vec3 vertNormal;
- varying vec2 vertTexCoord;
- varying vec4 shadowMapCoord;
- const float pi = 3.14159265;
- const float tau = pi * 2.0;
- float texelSize = 1.0 / shadowMapSize;
- float colorToFloat(vec4 c) {
- return c.r + c.g / 255.0 + c.b / (255.0 * 255.0);
- }
- vec4 texture2DBilinear(sampler2D textureSampler, vec2 uv) {
- vec4 tl = texture2D(textureSampler, uv);
- vec4 tr = texture2D(textureSampler, uv + vec2(texelSize, 0.0));
- vec4 bl = texture2D(textureSampler, uv + vec2(0.0, texelSize));
- vec4 br = texture2D(textureSampler, uv + vec2(texelSize , texelSize));
- vec2 f = fract(uv.xy * shadowMapSize); // get the decimal part
- vec4 tA = mix(tl, tr, f.x); // will interpolate the red dot in the image
- vec4 tB = mix(bl, br, f.x); // will interpolate the blue dot in the image
- return mix(tA, tB, f.y); // will interpolate the green dot in the image
- }
- void main() {
- float light, dif = 1.0;
- if (isGround < 0.5) dif = max(0.0, dot(normalize(vertNormal), normalize(lightPosition - vertPosition))); // Diffuse
- light = dif;
- if (light > 0.0 && shadowMapCoord.w > 0.0 && brightness < 1.0) {
- vec2 tex = vec2((shadowMapCoord.x / shadowMapCoord.z + 1.0) / 2.0, 1.0 - (shadowMapCoord.y / shadowMapCoord.z + 1.0) / 2.0);
- if (tex.x > 0.0 && tex.y > 0.0 && tex.x < 1.0 && tex.y < 1.0) { // Texture position must be valid
- float sampleDepth, depth = shadowMapCoord.z, shadow = 1.0;
- float pixel = texelSize * ((sampleSize * shadowMapSize) / distance(vertPosition, lightPosition));
- for (int i = 1; i < 100; i++) {
- if (float(i) <= samples) {
- float angle = tau * (float(i) / samples);
- sampleDepth = lightZnear + (lightZfar - lightZnear) * colorToFloat(texture2DBilinear(shadowMap, tex + vec2(cos(angle) * pixel, sin(angle) * pixel)));
- shadow -= clamp(depth - sampleDepth, 0.0, 1.0) / float(samples);
- } else break;
- }
- light *= shadow;
- }
- }
- gl_FragColor = vec4(mix(lightColor.rgb * light, vec3(1.0, 1.0, 1.0), brightness), alpha*texture2D(gm_BaseTexture, vertTexCoord).a);
- }
Advertisement
Add Comment
Please, Sign In to add comment