VoxelEngine/fragment.glsl

75 lines
2.7 KiB
Plaintext
Raw Normal View History

2017-08-27 12:51:26 +02:00
#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];
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;
2020-07-19 10:41:08 +02:00
const float circlelength = 100000.0;
2017-08-27 12:51:26 +02:00
bool isVisible(int i, vec2 offs, float lambertian)
{
2020-07-19 10:41:08 +02:00
// float bias = 0.005*tan(acos(lambertian));
// bias = clamp(bias,0.0,0.01);//*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))));
// return !(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x < (lightviewpos[i].z - bias));
vec2 size = textureSize(ShadowMaps[i], 0);
float bias = (1.0 / (10.0 * max(size.x, size.y)))*sin(acos(lambertian));
// bias = 0.0001*(1.0 - lambertian);
// bias = max(bias, 0.001);
return !((texture(ShadowMaps[i],lightviewpos[i].xy + offs).x) < (lightviewpos[i].z - bias));
}
2017-08-27 12:51:26 +02:00
void main()
{
vec3 colorLinear = ambientFactor * colorin;
for(int i = 0; i < numLights; i++){
vec3 lightDir = -normalize(lightpos[i] - pos.xyz);
2020-07-19 10:41:08 +02:00
float lambertian = dot(normalize(normal),lightDir);
float cosTheta = clamp(dot(normalize(normal),-lightDir), 0.0, 1.0);
2017-08-27 12:51:26 +02:00
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;
2020-07-19 10:41:08 +02:00
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;
2017-08-27 12:51:26 +02:00
}
2020-07-19 10:41:08 +02:00
}
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)));
2017-08-27 12:51:26 +02:00
}
vec3 colorGammaCorrected = colorLinear;//pow(colorLinear, vec3(1.0/screenGamma));
2020-07-19 10:41:08 +02:00
colorOut = vec4(colorGammaCorrected,1.0);
2017-08-27 12:51:26 +02:00
depth = gl_FragCoord.z;
}