split_screen.gdshader 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. shader_type canvas_item;
  2. render_mode unshaded;
  3. uniform vec2 viewport_size; // size in pixels of the viewport
  4. uniform sampler2D viewport1 : source_color;
  5. uniform sampler2D viewport2 : source_color;
  6. uniform bool split_active; // true: split screen, false: use view1
  7. uniform vec2 player1_position; // position of player 1 un UV coordinates
  8. uniform vec2 player2_position; // position of player 2 un UV coordinates
  9. uniform float split_line_thickness : hint_range(0, 10, 0.1); // width of the split boder
  10. uniform vec3 split_line_color : source_color; // color of the split border
  11. // from https://stackoverflow.com/questions/15276454/is-it-possible-to-draw-line-thickness-in-a-fragment-shader
  12. float distance_to_line(vec2 p1, vec2 p2, vec2 point) {
  13. float a = p1.y - p2.y;
  14. float b = p2.x - p1.x;
  15. return abs(a * point.x + b * point.y + p1.x * p2.y - p2.x * p1.y) / sqrt(a * a + b * b);
  16. }
  17. void fragment() {
  18. vec3 view1 = texture(viewport1, UV).rgb;
  19. vec3 view2 = texture(viewport2, UV).rgb;
  20. float width = viewport_size.x;
  21. float height = viewport_size.y;
  22. if (split_active) {
  23. vec2 dx = player2_position - player1_position;
  24. float split_slope;
  25. if (dx.y != 0.0) {
  26. split_slope = dx.x / dx.y;
  27. } else {
  28. split_slope = 100000.0; // High value (vertical split) if dx.y = 0
  29. }
  30. vec2 split_origin = vec2(0.5, 0.5);
  31. vec2 split_line_start = vec2(0.0, height * ((split_origin.x - 0.0) * split_slope + split_origin.y));
  32. vec2 split_line_end = vec2(width, height * ((split_origin.x - 1.0) * split_slope + split_origin.y));
  33. float split_current_y = (split_origin.x - UV.x) * split_slope + split_origin.y;
  34. float split_player1_position_y = (split_origin.x - player1_position.x) * split_slope + split_origin.y;
  35. // Check on which side of the split UV is and select the proper view.
  36. if (UV.y > split_current_y) {
  37. if (player1_position.y > split_player1_position_y) {
  38. COLOR = vec4(view1, 1.0);
  39. } else {
  40. COLOR = vec4(view2, 1.0);
  41. }
  42. } else {
  43. if (player1_position.y < split_player1_position_y) {
  44. COLOR = vec4(view1, 1.0);
  45. } else {
  46. COLOR = vec4(view2, 1.0);
  47. }
  48. }
  49. float distance_to_split_line = distance_to_line(split_line_start, split_line_end, vec2(UV.x * width, UV.y * height));
  50. if (distance_to_split_line < split_line_thickness) {
  51. // Draw antialiased split line.
  52. COLOR.rgb = mix(split_line_color, COLOR.rgb, distance_to_split_line / split_line_thickness);
  53. }
  54. } else {
  55. COLOR = vec4(view1, 1.0);
  56. }
  57. }