Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- // Matrices
- uniform mat4 matrixView;
- // Materials
- uniform vec3 materialAmbient;
- uniform vec3 materialDiffuse;
- uniform vec3 materialSpecular;
- uniform float shininess;
- uniform vec3 fogColor;
- uniform bool brightnessTest;
- // Textures
- uniform sampler2D texture0;
- uniform sampler2D textureNormal;
- uniform sampler2DShadow shadowMap;
- uniform float NormalMap;
- vec3 finalNormal;
- uniform float shadowStrength;
- in vec4 color;
- in vec4 position;
- in vec3 normal;
- in float fogStrength;
- in vec2 texCoord0;
- in vec4 shadowCoord;
- in mat3 matrixTangent;
- out vec4 outColor;
- // directional light declaration
- struct DIRECTIONAL
- {
- int on;
- vec3 direction;
- vec3 diffuse;
- };
- uniform DIRECTIONAL lightDir;
- // point light declaration
- struct POINT
- {
- int on;
- vec3 position;
- vec3 diffuse;
- vec3 specular;
- };
- uniform POINT lightPoint;
- // spot light declaration
- struct SPOT
- {
- int on;
- vec3 position;
- vec3 diffuse;
- vec3 specular;
- vec3 direction;
- float cutoff;
- float attenuation;
- float strength;
- mat4 matrix;
- };
- uniform SPOT lightSpot;
- // directional light function
- vec4 DirectionalLight(DIRECTIONAL light)
- {
- vec4 color = vec4(0, 0, 0, 0);
- vec3 L = normalize(mat3(matrixView) * light.direction);
- float NdotL = dot(normal, L);
- if (NdotL > 0)
- {
- color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
- }
- return color;
- }
- // point light function
- vec4 PointLight(POINT light)
- {
- // diffuse light
- vec4 color = vec4(0, 0, 0, 1);
- vec3 L = normalize((matrixView * vec4(light.position, 1)) - position).xyz;
- float NdotL = dot(L, finalNormal);
- if (NdotL > 0)
- {
- color += vec4 (light.diffuse * materialDiffuse, 1) * NdotL;
- }
- // specular specular
- vec3 V = normalize(-position.xyz);
- vec3 R = reflect(-L, finalNormal);
- float RdotV = dot(R, V);
- if (NdotL > 0 && RdotV > 0)
- {
- color += vec4(light.specular * materialSpecular * pow(RdotV, shininess), 1);
- }
- return color;
- }
- // spot light function
- vec4 SpotLight(SPOT light)
- {
- vec4 color = vec4(0, 0, 0, 1);
- vec3 L = normalize(light.matrix * vec4(light.position, 1) - position).xyz;
- // diffuse light
- float NdotL = dot(L, finalNormal);
- if (NdotL > 0)
- {
- color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
- }
- // specular light
- vec3 V = normalize(-position.xyz);
- vec3 R = reflect(-L, finalNormal);
- float RdotV = dot(R, V);
- if (NdotL > 0 && RdotV > 0)
- {
- color += vec4(materialSpecular * light.specular * pow(RdotV, shininess), 1);
- }
- // spot light
- vec3 D = normalize(mat3(light.matrix) * light.direction);
- float S = dot(-L, D);
- float A = acos(S);
- float cutoff = radians(clamp(light.cutoff, 0, 360));
- if (A < cutoff) S = pow(S, light.attenuation);
- else S = 0;
- // attenuation
- float dist = length(light.matrix * vec4(light.position, 1) - position);
- float att = 1 / (dist * dist) / light.strength;
- return color * S * att;
- }
- //--
- // shadows (PCF sampling)
- float shadow;
- // number of samples
- const int samplingPositions = 8;
- // offsets for rectangular PCF sampling
- vec2 samplePositions[8] = vec2[]
- (
- //vec2(2.0, 2.0), vec2(-2.0, 2.0), vec2(-2.0, -2.0), vec2(2.0, -2.0),
- //vec2(1.5, 1.5), vec2(-1.5, 1.5), vec2(-1.5, -1.5), vec2(1.5, -1.5),
- vec2(1.0, 1.0), vec2(-1.0, 1.0), vec2(-1.0, -1.0), vec2(1.0, -1.0),
- vec2(0.5, 0.5), vec2(-0.5, 0.5), vec2(-0.5, -0.5), vec2(0.5, -0.5)
- );
- //--
- void main(void)
- {
- outColor = color;
- if (NormalMap == 1)
- {
- finalNormal = 2.0 * texture(textureNormal, texCoord0).xyz - vec3(1.0, 1.0, 1.0);
- finalNormal = normalize(matrixTangent * finalNormal);
- }
- else finalNormal = normal;
- if (lightPoint.on == 1) outColor += PointLight(lightPoint);
- if (lightSpot.on == 1) outColor += SpotLight(lightSpot);
- if (lightDir.on == 1) outColor += DirectionalLight(lightDir);
- if (shadowCoord.w > 0)
- {
- // shadows (PDF sampling)
- vec2 shadowMapSize = textureSize(shadowMap, 0);
- vec2 shadowMapScale = vec2(1.0 / shadowMapSize.x, 1.0 / shadowMapSize.y);
- // sample each PCF texel around current fragment
- float shadowTotal = 0.0;
- float samplesUsed = 0.0;
- for (int i = 0; i < samplingPositions; i++)
- {
- // performs sampling and accumulates shadowing factor
- shadowTotal += (0.5 + 0.5 * textureProj(shadowMap, vec4(shadowCoord.xy + samplePositions[i] * shadowMapScale * shadowCoord.w, shadowCoord.z, shadowCoord.w)));
- samplesUsed += 1;
- }
- // normalize shadows
- shadow += shadowTotal / samplesUsed;
- }
- outColor *= shadow;
- outColor *= texture(texture0, texCoord0);
- //outColor = mix(vec4(fogColor, 1), outColor, fogStrength);
- // brightness test for bloom PPE
- if (brightnessTest)
- {
- float brightness = dot(outColor.rgb, vec3(0.2126, 0.7152, 0.0722));
- if (brightness > 0.7) outColor = outColor;
- else outColor = vec4(0.0, 0.0, 0.0, 1.0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement