Skip to content

Instantly share code, notes, and snippets.

@quiglemj
Forked from henriquelalves/fisheyeeffect
Last active February 1, 2023 03:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quiglemj/971f4cec1b128c58b4864c5200bfc579 to your computer and use it in GitHub Desktop.
Save quiglemj/971f4cec1b128c58b4864c5200bfc579 to your computer and use it in GitHub Desktop.
Godot Shader for FishEye 2D effect
// Modified version of fisheyeeffect by henriquelalves
// Original based on http://www.geeks3d.com/20140213/glsl-shader-library-fish-eye-and-dome-and-barrel-distortion-post-processing-filters/2/
// I created this version because the henrique's did not work in my version of Godot 3.0.5. I haven't tested it in more current versions
// Reddit user krdluzni answered a question I had regarding basic shader functions, so thanks krdluzni!
// This version of the shader works by reading and modifying a portion of the screen, rather than just modifying a texture
// This makes it useful for things like magnifying glasses, black holes or gravity wells, telescopes, etc
shader_type canvas_item;
uniform float PI = 3.1415926535;
uniform float BarrelPower;
vec2 distort(vec2 p) {
if(p.x > 0.0){
float angle = p.y / p.x;
float theta = atan(angle);
float radius = length(p);
radius = pow(radius, BarrelPower);
p.x = radius * cos(theta);
p.y = radius * sin(theta);
} else {
float angle = p.y / p.x;
float theta = atan(angle);
float radius = length(p);
radius = pow(radius, BarrelPower);
p.y = radius * sin(-theta);
p.x = radius * cos(theta);
p.x = - p.x;
}
return 0.5 * (p + vec2(1.0,1.0));
}
void fragment(){
vec2 xy = (2.0 * SCREEN_UV);
xy.x = xy.x-1.0;
xy.y = xy.y-1.0;
float d = length(xy);
if(d < 1.5){
xy = distort(xy);
}
else{
xy = SCREEN_UV;
}
COLOR = texture(SCREEN_TEXTURE, xy);
}
@aZaamBie
Copy link

aZaamBie commented Oct 7, 2019

Does this still work in Godot 3.1?

@quiglemj
Copy link
Author

quiglemj commented Oct 7, 2019

I believe it should, yes. I just loaded up the game I made using this code in Godot 3.1 and it still appears to work as expected, however I haven't done any extensive testing.

@quiglemj
Copy link
Author

quiglemj commented Oct 7, 2019

Sounds good! If you have any further issues or questions I'll be happy to help any way I can.

@aZaamBie
Copy link

aZaamBie commented Oct 8, 2019

I have an issue, I loaded the shader onto a Color rect and I the screen is messed up.
Link to the image: https://imgur.com/gallery/MtIojCH
This is the original game screen: https://imgur.com/TOD07tD
Is there something I should change in the code to fix this?

@aZaamBie
Copy link

aZaamBie commented Oct 8, 2019

Nevermind, sorry its ok! I saw that i had to change the barrel power!

@aZaamBie
Copy link

aZaamBie commented Oct 8, 2019

Thank you so much!!

@aggregate1166877
Copy link

@quiglemj This has been a life saver, thanks so much for the gist. I faced some pretty bad pixelation problems for my exact use case, so I've posted a variation here that helps by crushing the edges inward instead of stretching the center outward: https://gist.github.com/aggregate1166877/a889083801d67917c26c12a98e7f57a7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment