Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #fragment shader
- #version 330 core
- out vec4 OutColor;
- in vec4 FragColor;
- in vec4 FragPosition;
- in vec2 BoxCenter;
- in vec2 BoxSize;
- in vec2 ScreenResolution;
- in float BoxCornerRadius;
- const float threshold = 0.004;
- float square(float val)
- {
- return (val * val);
- }
- float distance_squared(vec2 p1, vec2 p2)
- {
- vec2 vector = p2 - p1;
- return (vector.x * vector.x) + (vector.y * vector.y);
- }
- /// @brief Calculate a circular gradient with cut off edges
- /// @param pixel_position : opengl coordindate of the pixel
- /// @param max_size : do not apply gradient past this boundary
- /// @param center : center of the circle
- /// @param radius : radius of the circle
- /// @param threshold : how far out from the radius to allow gradient
- float calc_corner_alpha(vec2 pixel_position, vec2 center, float radius, float threshold)
- {
- if (radius <= 0.0)
- {
- return 1.0;
- }
- float lower_squared = square(radius);
- float dist_squared = distance_squared(pixel_position, center);
- float upper_squared = square(radius + threshold);
- return smoothstep(upper_squared, lower_squared, distance_squared(pixel_position, center));
- }
- void main()
- {
- float half_width = (BoxSize.x / 2.0) - BoxCornerRadius;
- float half_height = (BoxSize.y / 2.0) - BoxCornerRadius;
- float left = BoxCenter.x - half_width;
- float right = BoxCenter.x + half_width;
- float top = BoxCenter.y + half_height;
- float bottom = BoxCenter.y - half_height;
- float a = 0.0;
- float aspect_ratio = ScreenResolution.x / ScreenResolution.y;
- vec2 curPosition = FragPosition.xy;
- if ((curPosition.x >= left && curPosition.x <= right) || // sits within vertical section
- (curPosition.y >= bottom && curPosition.y <= top)) // sits within horizontal section
- {
- // forms a sort of cross shape where the alpha is 1.0
- a = 1.0;
- }
- else if (curPosition.x < left)
- {
- if (curPosition.y > top)
- {
- a = calc_corner_alpha(FragPosition.xy, vec2(left, top), BoxCornerRadius, threshold);
- }
- else
- {
- a = calc_corner_alpha(FragPosition.xy, vec2(left, bottom), BoxCornerRadius, threshold);
- }
- }
- else if (curPosition.x > right)
- {
- if (curPosition.y > top)
- {
- a = calc_corner_alpha(FragPosition.xy, vec2(right, top), BoxCornerRadius, threshold);
- }
- else
- {
- a = calc_corner_alpha(FragPosition.xy, vec2(right, bottom), BoxCornerRadius, threshold);
- }
- }
- OutColor = vec4(FragColor.rgb, a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement