1
0
Fork 0
2022-untitled-game/code/shaders/geo.glsl

108 lines
2.4 KiB
GLSL

//////////////////////////////////////////////////////////////////////////////
#ifdef VERTEX
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec3 a_normal;
layout (location = 2) in vec2 a_texcoord;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform mat4 u_model;
out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main()
{
v_fragpos = vec3(u_model * vec4(a_position, 1.0f));
v_normal = mat3(transpose(inverse(u_model))) * a_normal;
v_texcoord = a_texcoord;
gl_Position = u_proj * u_view * u_model * vec4(a_position, 1.0f);
}
#endif
//////////////////////////////////////////////////////////////////////////////
#ifdef FRAGMENT
in vec3 v_fragpos;
in vec2 v_texcoord;
in vec3 v_normal;
out vec4 f_color;
struct light_t {
vec3 position;
vec3 color;
float intensity;
};
uniform sampler2D u_texture;
uniform float u_texture_alpha;
uniform int u_is_emitter;
uniform int u_light_count;
uniform float u_uv_scale;
uniform vec2 u_uv_translation;
uniform float u_powerup_timer;
uniform float u_ambient_adjust;
uniform light_t u_lights[30];
const float START_TIMER = 15.0f;
const float START_ALPHA = 0.25f;
const float TIMER_STEPS = (1.0f-START_ALPHA) / START_TIMER;
vec3 calc_light(light_t light, vec3 frag_normal, vec3 frag_pos)
{
const float ambient_strength = 0.5f;
const float atten_constant = 1.0f;
const float atten_linear = 0.05f;
const float atten_quadratic = 20.0f;
vec3 light_dir = normalize(light.position - frag_pos);
float distance = length(light.position - frag_pos);
float attenuation = 1.0f / (atten_constant + atten_linear * distance +
atten_quadratic * (distance * distance));
vec3 ambient = attenuation * vec3(ambient_strength);
vec3 diffuse = attenuation * light.intensity * light.color * max(dot(normalize(frag_normal), light_dir), 0.0f);
return ambient + diffuse;
}
void main()
{
vec3 light = vec3(0.05f);
if (u_is_emitter == 1)
{
light = vec3(u_ambient_adjust);
}
else
{
for (int i = 0; i < u_light_count; i++)
{
light += calc_light(u_lights[i], v_normal, v_fragpos);
}
}
vec2 texcoord = v_texcoord + (u_uv_translation * u_uv_scale);
vec4 tex = texture(u_texture, texcoord);
tex.a *= u_texture_alpha;
f_color = tex * vec4(light, 1.0f);
if (u_powerup_timer > 0.0f)
{
float timer = u_powerup_timer / 100.0f;
f_color = vec4(f_color.rgb, START_ALPHA + (TIMER_STEPS*(START_TIMER-u_powerup_timer)));
}
}
#endif
//////////////////////////////////////////////////////////////////////////////