KP3Dii/Data/resources/shaders/.kp3d/map_f.glsl

80 lines
2.1 KiB
Text
Raw Normal View History

2024-07-22 07:48:42 +00:00
#version 330 core
2024-07-29 22:06:31 +00:00
uniform sampler2D u_texture;
uniform sampler2D u_normal_texture;
2024-07-22 07:48:42 +00:00
2024-07-29 22:06:31 +00:00
uniform vec3 u_campos;
2024-07-29 23:54:14 +00:00
uniform vec3 u_camdir;
2024-07-28 02:07:41 +00:00
uniform vec3 u_highlight;
2024-07-29 22:06:31 +00:00
uniform float u_time;
2024-07-28 02:07:41 +00:00
2024-07-22 07:48:42 +00:00
in vec3 v_position;
in vec3 v_normal;
in vec3 v_proj_normal;
in vec2 v_texcoord;
2024-07-29 22:06:31 +00:00
in vec3 v_tangent;
in vec3 v_bitangent;
2024-07-22 07:48:42 +00:00
layout (location=0) out vec4 v_output;
2024-07-29 22:06:31 +00:00
float kpc(float d, float min, float max)
2024-07-22 07:48:42 +00:00
{
2024-07-29 22:06:31 +00:00
if (d >= max) return 1.0;
if (d <= min) return 0.0;
2024-07-22 07:48:42 +00:00
2024-07-29 22:06:31 +00:00
return 1.0 - (max - d) / (max - min);
2024-07-22 07:48:42 +00:00
}
struct Light
{
vec3 position;
vec3 diffuse;
2024-07-29 22:06:31 +00:00
float radius;
2024-07-22 07:48:42 +00:00
};
2024-07-29 22:06:31 +00:00
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;
2024-07-29 22:30:32 +00:00
vec3 light_dir = normalize(light.position - v_position);
2024-07-29 23:54:14 +00:00
//vec3 view_dir = normalize(u_camdir - v_position);
2024-07-29 22:30:32 +00:00
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);
2024-07-29 23:54:14 +00:00
power *= 0.5;
vec3 ddiffuse = diff * vec3(1.0);// * light.diffuse;
2024-07-29 22:30:32 +00:00
vec3 specular = power * spec * light.diffuse;
vec4 res = vec4(ddiffuse + specular, 1.0);
2024-07-29 22:06:31 +00:00
float alpha = kpc(distance(light.position, v_position), light.radius * sharpness, light.radius);
2024-07-29 23:54:14 +00:00
res *= mix(vec4(light.diffuse, 1), vec4(0,0,0,1), alpha);
2024-07-29 22:06:31 +00:00
return res;
}
2024-07-22 07:48:42 +00:00
void main()
{
2024-07-29 22:06:31 +00:00
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);
2024-07-29 22:10:48 +00:00
vec3 norm = normalize(TBN * tangentNormal) * 2.0;
vec4 texv = texture(u_texture, v_texcoord);
2024-07-22 07:48:42 +00:00
2024-07-29 22:06:31 +00:00
vec4 ambient = vec4(0.1, 0.1, 0.1, 1.0) * 1.5, light = ambient;
2024-07-29 23:54:14 +00:00
light += MakeLight(norm, vec3(2.0, 10.0, -24.0), vec3(0.7), /*5.0*/ 1000.0, 1.0, 1.0);
2024-07-29 22:06:31 +00:00
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;
2024-07-22 07:48:42 +00:00
2024-07-29 22:06:31 +00:00
// + editor highlighting
v_output.rgb *= u_highlight;
2024-07-22 07:48:42 +00:00
}