cakefoot/src/shaders/shader.frag

79 lines
2.8 KiB
GLSL

#version 300 es
/* >> Cakefoot by https://foam.shampoo.ooo/ << */
/* The precision declaration is required by OpenGL ES */
precision mediump float;
#define TRANSFORMATION_NONE 0
#define TRANSFORMATION_SQUIRCLE 1
in vec2 fragment_uv;
in vec3 ex_color;
in float x_center_proximity;
in vec3 original_coordinates;
in vec3 clip_coordinates;
uniform sampler2D model_texture;
uniform int uv_transformation;
uniform float coordinate_bound;
uniform bool texture_enabled;
out vec4 output_color;
/* [-coordinate_bound, coordinate_bound] arbitrary box coordinates to [-1, 1] normalized coordinates */
vec2 normalize_coordinates(vec2 coordinates)
{
return coordinates / coordinate_bound;
}
/* [-1, 1] box coordinates to [0, 1] UV coordinates */
vec2 coordinates_to_uv(vec2 coordinates)
{
return (1.0 + coordinates) / 2.0;
}
/* coordinates in circle with radius <= 1 to box coordinates in [-1, 1] */
vec2 circle_to_box(vec2 circle)
{
float u = circle.x;
float v = circle.y;
float u_sq = pow(u, 2.0);
float v_sq = pow(v, 2.0);
float rt_2 = sqrt(2.0);
float x = 0.5 * sqrt(2.0 + 2.0 * u * rt_2 + u_sq - v_sq) - 0.5 * sqrt(2.0 - 2.0 * u * rt_2 + u_sq - v_sq);
float y = 0.5 * sqrt(2.0 + 2.0 * v * rt_2 - u_sq + v_sq) - 0.5 * sqrt(2.0 - 2.0 * v * rt_2 - u_sq + v_sq);
return vec2(x, y);
}
/* Apply color passed in from the vertex shader, compressing to one of 16 colors. Add retro effect
* by alternately darkening and lightening 2x2 pixel areas in a checker pattern. Add shadowing by
* brightening the color based on how near it is to the center in the X-dimension */
void retro()
{
vec3 shadowed = min(ex_color, 1.0);
float dx = abs(floor(gl_FragCoord[0]) - 480.0) / 480.0;
if (int(floor(gl_FragCoord[0] / 2.0) + floor(gl_FragCoord[1]) / 2.0) % 2 == 0)
{
output_color = vec4(shadowed * 1.2, 1);
}
else
{
output_color = vec4(shadowed * 0.7, 1);
}
output_color[0] = float(int(output_color[0] * 4.0)) / 4.0;
output_color[1] = float(int(output_color[1] * 4.0)) / 4.0;
output_color[2] = float(int(output_color[2] * 4.0)) / 4.0;
output_color *= x_center_proximity;
}
void main()
{
// if (uv_transformation == TRANSFORMATION_SQUIRCLE)
// {
// vec2 normalized_circle_coordinates = normalize_coordinates(vec2(original_coordinates.x, original_coordinates.z));
// fragment_uv = coordinates_to_uv(circle_to_box(normalized_circle_coordinates));
// }
output_color = mix(vec4(ex_color, 1), texture(model_texture, fragment_uv) * vec4(ex_color, 1), float(texture_enabled));
// output_color = float(texture_enabled) * texture(model_texture, fragment_uv) * vec4(1, 0, 0, 0) + (1.0 - float(texture_enabled)) * vec4(1, 0, 0, 0);
// output_color = (1.0 - float(texture_enabled)) * vec4(ex_color, 1);
}