VoxelEngine/fragment.glsl
2020-07-19 11:21:12 +02:00

70 lines
No EOL
2.5 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*3.14159265359 / 16.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);
//int visible = int(!(texture(ShadowMaps,lightpos[i].xy/2 + vec2(0.5,0.5)).z < (lightpos[i].z)));
float visible = 0;
int count = 0;
vec2 texelSize = 1.0 / textureSize(ShadowMaps[i], 0);
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;
}
}
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;
}