PixelWars/shaders/world.frag.glsl

41 lines
1.4 KiB
GLSL

#version 330 core
uniform sampler2DRect colorMap;
uniform vec3 camera;
uniform vec2 worldSize;
uniform vec2 screenSize;
uniform float flatSphere;
uniform float surfaceRatio;
in vec2 texCoord;
in vec3 normal;
out vec4 outColor;
const vec3 lightDir = normalize(vec3(0.5, -1, -1));
vec3 phongLighting(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 normal, in vec3 lightDir, in vec3 halfVec){
float diffuseComponent = max(dot(normal, lightDir), 0);
float specularComponent = max(dot(halfVec, normal), 0);
return color*diffuseComponent*(kd+ks*pow(specularComponent, ns));
}
void main()
{
// looping the world in the shape of a toreiller (infinite flat surface)
vec2 worldCoord = texCoord*worldSize + camera.xy*worldSize.y + (texCoord-vec2(0.25, 0.5))*surfaceRatio*worldSize;
ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize));
if(abs(mod(nbRevolutions.y, 2)) > 0.5)
{
worldCoord.x += worldSize.x/2;
nbRevolutions.x = int(floor(worldCoord.x / worldSize.x));
}
worldCoord = worldCoord - nbRevolutions*worldSize;
vec3 texColor = texelFetch(colorMap, ivec2(worldCoord)).xyz;
// computing lighting
vec3 lighting = phongLighting(texColor, vec3(0.5), 50, vec3(1), normalize(normal), lightDir, normalize(lightDir+vec3(0, 0, -1)));
outColor = vec4(mix(lighting, texColor, flatSphere*0.8 + 0.2), 1.0);
}