KP3Dii/Data/resources/shaders/.kp3d/map_f.glsl
2024-07-29 18:54:14 -05:00

79 lines
2.1 KiB
GLSL

#version 330 core
uniform sampler2D u_texture;
uniform sampler2D u_normal_texture;
uniform vec3 u_campos;
uniform vec3 u_camdir;
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;
};
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;
vec3 light_dir = normalize(light.position - v_position);
//vec3 view_dir = normalize(u_camdir - v_position);
vec3 view_dir = normalize(u_campos - v_position);
vec3 reflect_dir = reflect(-light_dir, norm);
float spec = max(dot(view_dir, reflect_dir), 0.0);
float diff = max(dot(norm, light_dir), 0.0);
power *= 0.5;
vec3 ddiffuse = diff * vec3(1.0);// * light.diffuse;
vec3 specular = power * spec * light.diffuse;
vec4 res = vec4(ddiffuse + specular, 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), 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) * 2.0;
vec4 texv = texture(u_texture, v_texcoord);
vec4 ambient = vec4(0.1, 0.1, 0.1, 1.0) * 1.5, light = ambient;
light += MakeLight(norm, vec3(2.0, 10.0, -24.0), vec3(0.7), /*5.0*/ 1000.0, 1.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;
}