VoxelEngine/fragment.glsl

78 lines
2.7 KiB
GLSL

#version 410
layout(location = 0) in vec3 colorin;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec3 pos;
layout(location = 3) in vec4 lightviewpos[3];
layout(location = 8) in vec2 UV;
uniform int numLights;
uniform sampler2D ShadowMaps[3];
uniform vec3 lightpos[3];
uniform vec3 lightColor[3];
uniform float near;
layout(location = 0) out vec4 colorOut;
layout(location = 1) out float depth;
const float ambientFactor = 0.25;
const float diffuseFactor = 0.5;
const float specFactor = 1.0;
const float shininess = 16.0;
const float screenGamma = 2.2;
const float pitl = 2.0*3.14159265359 / 24.0;
const float circlelength = 100000.0;
bool isVisible(int i, vec2 offs, float lambertian)
{
vec2 size = textureSize(ShadowMaps[i], 0);
float bias = (1.0 / (10.0 * max(size.x, size.y)))*sin(acos(lambertian));
return !((texture(ShadowMaps[i],lightviewpos[i].xy + offs).x) < (lightviewpos[i].z - bias)) && lightviewpos[i].z > near * 2;
}
void main()
{
vec3 colorLinear = ambientFactor * colorin;
for(int i = 0; i < numLights; i++){
vec3 lightDir = -normalize(lightpos[i] - pos.xyz);
float lambertian = dot(normalize(normal),lightDir);
float cosTheta = clamp(dot(normalize(normal),-lightDir), 0.0, 1.0);
float specular = 0;
vec3 viewDir = normalize(-pos.xyz);
vec3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, normalize(normal)), 0.0);
specular = int(lambertian > 0)*pow(specAngle, shininess);
float visible = 0;
int count = 0;
vec2 texelSize = 1.0 / textureSize(ShadowMaps[i], 0);
// Grid sampling
// for(int x = -2; x <= 2; x++){
// for(int y = -2; y <= 2; y++){
// vec2 offs = vec2(x, y) * texelSize;
// visible += float(int(isVisible(i, offs, lambertian))) * 1.0/25.0;
// }
// }
//Circle Sampling
visible += float(int(isVisible(i, vec2(0, 0), lambertian))) * 1.0/25.0;
for(int r = 0; r < 24; r++){
vec2 offs = vec2(sin(r * pitl), cos(r * pitl)) * texelSize;
visible += float(int(isVisible(i, offs, lambertian))) * 1.0/25.0;
}
bool condition = visible >= (1.0/5.0);
visible = float(condition) * 1.0 + float(!condition) * visible;
colorLinear += (visible * 0.5 + 0.0) *(lambertian * diffuseFactor * colorin * lightColor[i] + specular * specFactor*colorin) * (200.0/(length(lightpos[i] - pos.xyz) * length(lightpos[i] - pos.xyz)));
// colorLinear = vec3(visible, visible, visible);
}
vec3 colorGammaCorrected = colorLinear;//pow(colorLinear, vec3(1.0/screenGamma));
colorOut = vec4(colorGammaCorrected,1.0);
depth = gl_FragCoord.z;
}