KP3Dii/Data/resources/shaders/.kp3d/map_f.glsl
2024-07-29 17:06:31 -05:00

76 lines
1.9 KiB
GLSL

#version 330 core
uniform sampler2D u_texture;
uniform sampler2D u_normal_texture;
uniform vec3 u_campos;
uniform vec3 u_highlight;
uniform float u_time;
in vec3 v_position;
in vec3 v_normal;
in vec3 v_proj_normal;
in vec2 v_texcoord;
in vec3 v_tangent;
in vec3 v_bitangent;
layout (location=0) out vec4 v_output;
float kpc(float d, float min, float max)
{
if (d >= max) return 1.0;
if (d <= min) return 0.0;
return 1.0 - (max - d) / (max - min);
}
struct Light
{
vec3 position;
vec3 diffuse;
float radius;
};
float Lambert(vec3 n, vec3 l)
{
vec3 nrmn = normalize(n);
vec3 nrml = normalize(l);
float res = dot(nrmn, nrml);
return max(res, 0.0);
}
vec4 MakeLight(vec3 norm, vec3 position, vec3 diffuse, float radius, float sharpness, float power)
{
Light light;
light.position = position;
light.diffuse = diffuse;
light.radius = radius;
vec4 res = vec4(light.diffuse * Lambert(norm, normalize(light.position)), 1.0);
float alpha = kpc(distance(light.position, v_position), light.radius * sharpness, light.radius);
res *= mix(vec4(light.diffuse, 1), vec4(0,0,0,1.0), alpha);
return res;
}
void main()
{
vec3 tangentNormal = texture(u_normal_texture, v_texcoord).xyz * 2.0 - 1.0;
vec3 T = normalize(v_tangent);
vec3 B = normalize(v_bitangent);
vec3 N = normalize(v_normal);
mat3 TBN = mat3(T, B, N);
vec3 norm = normalize(TBN * tangentNormal);
vec4 texv = texture(u_texture, v_texcoord);
//norm = abs(norm);
vec4 ambient = vec4(0.1, 0.1, 0.1, 1.0) * 1.5, light = ambient;
light += MakeLight(norm, u_campos, vec3(1.0, 1.0, 1.0), 5.0, 0.0, 1.0);
light += MakeLight(norm, vec3(5.0, 2.0, -7.0 + cos(u_time/35.0) * 2.0), vec3(1.0, 0.6, 0.0), 5.0, 0.6, 1.0);
light += MakeLight(norm, vec3(7, 2.0, -7.0 + -cos(u_time / 30.0) * 1.0), vec3(0.6, 0.9, 1.0), 5.0, 0.6, 1.0);
v_output = texv * light;
// + editor highlighting
v_output.rgb *= u_highlight;
}