VoxelEngine/fragment.glsl

78 lines
2.7 KiB
Text
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];
2020-07-19 11:21:12 +02:00
uniform float near;
2017-08-27 12:51:26 +02:00
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;
2020-11-08 10:20:23 +01:00
const float pitl = 2.0*3.14159265359 / 24.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
vec2 size = textureSize(ShadowMaps[i], 0);
float bias = (1.0 / (10.0 * max(size.x, size.y)))*sin(acos(lambertian));
2020-07-19 11:21:12 +02:00
return !((texture(ShadowMaps[i],lightviewpos[i].xy + offs).x) < (lightviewpos[i].z - bias)) && lightviewpos[i].z > near * 2;
}
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);
float visible = 0;
2020-07-19 10:41:08 +02:00
int count = 0;
vec2 texelSize = 1.0 / textureSize(ShadowMaps[i], 0);
2020-11-08 10:20:23 +01:00
// 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;
2020-07-19 10:41:08 +02:00
}
2020-11-08 10:20:23 +01: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)));
2020-07-19 11:21:12 +02:00
// colorLinear = vec3(visible, visible, visible);
2017-08-27 12:51:26 +02:00
}
vec3 colorGammaCorrected = colorLinear;//pow(colorLinear, vec3(1.0/screenGamma));
2020-07-19 11:21:12 +02:00
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;
}