Advertisement
Jim_Frize

Godot Fisheye (Screen-Space)

Jan 29th, 2021
1,750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A simple (screen space) fisheye shader for Godot, based on: https://gist.github.com/aggregate1166877/a889083801d67917c26c12a98e7f57a7
  2. // It's calibrated to convert a square screen space into a sphere
  3.  
  4. shader_type canvas_item;
  5.  
  6. const float crop_amount = 0.9;
  7.  
  8. vec2 distort(vec2 p)
  9. {
  10.     float d = length(p);
  11.     float z = sqrt(1.0 + d * d * -1.0);
  12.     float r = atan(d, z) / 3.1415926535;
  13.     float phi = atan(p.y, p.x);
  14.     return vec2(r*cos(phi)+.5,r*sin(phi)+.5);
  15. }
  16.  
  17. void fragment()
  18. {
  19.     vec2 xy = (2.0 * SCREEN_UV);
  20.     xy.x = xy.x - 1.0;
  21.     xy.y = xy.y - 1.0;
  22.    
  23.     vec4 tex;
  24.     float d = length(xy);
  25.     if (d < 1.0) {
  26.         xy = distort(xy);
  27.         tex = texture(SCREEN_TEXTURE, xy);
  28.         COLOR = tex;
  29.         COLOR.a = 0.999;
  30.     }
  31.  
  32.     // crop.
  33.     if (d > crop_amount) {
  34.         COLOR = vec4(0,0,0,1);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement