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

98 lines
2.7 KiB
GLSL
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;
2024-07-31 07:23:57 +00:00
light.radius = radius;// * 2.0;
2024-07-29 22:30:32 +00:00
2024-07-31 07:03:41 +00:00
float alpha = kpc(distance(light.position, v_position), light.radius * sharpness, light.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-31 07:03:41 +00:00
//diff *= 1.0 - alpha;
2024-07-29 23:54:14 +00:00
power *= 0.5;
2024-07-31 07:03:41 +00:00
//power = 0.5;//alpha;
vec3 specular = power * spec * light.diffuse * diff;
vec4 res = vec4(vec3(diff) + specular, 1.0) * (1.0 - alpha);
res *= vec4(light.diffuse, 1);
2024-07-29 22:30:32 +00:00
2024-07-31 07:03:41 +00:00
//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);
2024-07-29 22:06:31 +00:00
return res;
}
2024-07-22 07:48:42 +00:00
void main()
{
2024-07-31 07:03:41 +00:00
vec3 tangentNormal = normalize(texture(u_normal_texture, v_texcoord).xyz * 2.0 - 1.0);
2024-07-29 22:06:31 +00:00
vec3 T = normalize(v_tangent);
vec3 B = normalize(v_bitangent);
2024-08-14 05:37:23 +00:00
vec3 N = normalize((v_normal));
2024-07-29 22:06:31 +00:00
mat3 TBN = mat3(T, B, N);
2024-07-31 07:03:41 +00:00
vec3 norm = normalize(TBN * tangentNormal);
if (texture(u_normal_texture, v_texcoord).x == 1.0)
{
// disable normal mapping if there's no normal texture
// is this the best way to go about this? idk, but it works for now
norm = vec3(0,1,0);
}
2024-07-31 07:03:41 +00:00
vec4 texv = texture(u_texture, v_texcoord);
2024-07-22 07:48:42 +00:00
2024-08-14 05:37:23 +00:00
vec4 ambient = vec4(0.1, 0.1, 0.1, 1.0) * 5.5, light = ambient;
// debug: output normal texture only
// v_output = texture(u_normal_texture, v_texcoord);
// return;
// debug: output normal
// v_output = vec4(norm, 1.0);
// return;
2024-07-29 22:06:31 +00:00
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);
2024-07-31 07:23:57 +00:00
v_output = texv * light;// * 2.0;
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
}