shaders.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {FlamegraphTheme} from '../flamegraph/flamegraphTheme';
  2. export const vertex = () => `
  3. attribute vec2 a_position;
  4. attribute vec4 a_color;
  5. attribute vec4 a_bounds;
  6. uniform mat3 u_model;
  7. uniform mat3 u_projection;
  8. varying lowp vec4 v_color;
  9. varying vec2 v_pos;
  10. varying vec4 v_bounds;
  11. void main() {
  12. vec2 scaled = (u_model * vec3(a_position.xy, 1)).xy;
  13. vec2 pos = (u_projection * vec3(scaled.xy, 1)).xy;
  14. gl_Position = vec4(pos, 0.0, 1.0);
  15. v_color = a_color;
  16. v_pos = a_position.xy;
  17. v_bounds = a_bounds;
  18. }
  19. `;
  20. export const fragment = (theme: FlamegraphTheme) => `
  21. // fragment shaders don't have a default precision so we need
  22. // to pick one. mediump is a good default
  23. precision mediump float;
  24. uniform bool u_is_search_result;
  25. uniform vec2 u_border_width;
  26. uniform bool u_draw_border;
  27. varying lowp vec4 v_color;
  28. varying vec4 v_bounds;
  29. varying vec2 v_pos;
  30. void main() {
  31. float minX = v_bounds.x + u_border_width.x;
  32. float maxX = v_bounds.z - u_border_width.x;
  33. float minY = v_bounds.y + u_border_width.y;
  34. float maxY = v_bounds.y + 1.0 - u_border_width.y;
  35. float width = maxX - minX;
  36. if (u_is_search_result) {
  37. gl_FragColor = ${theme.COLORS.SEARCH_RESULT_FRAME_COLOR};
  38. } else if (u_draw_border) {
  39. if(width <= u_border_width.x) {
  40. if(v_pos.y > minY && v_pos.y < maxY){
  41. gl_FragColor = vec4(v_color);
  42. } else {
  43. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  44. }
  45. } else if (v_pos.x > minX && v_pos.x < maxX && v_pos.y > minY && v_pos.y < maxY) {
  46. gl_FragColor = vec4(v_color);
  47. } else {
  48. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  49. }
  50. } else {
  51. gl_FragColor = vec4(v_color);
  52. }
  53. }
  54. `;