Fisheye position warp
Monkey Forums/Monkey Programming/Fisheye position warp
| ||
Hi all, sorry I've not quite sure of the precise term, but I'd like to "fisheye" a 2d array of coordinates. Consider each intersection of this image as a fisheyed version of a flat uniform checkered grid. ![]() I've seen it plenty of times in demoscene demos but don't actually know what it's called. Can anyone help? |
| ||
Are you trying to do that with individual point (x,y)'s or with a whole image/screen (which might need a shader)? |
| ||
Just points thanks nullterm0,0 1,0 2,0 3,0, 4,0 0,1 1,1 2,1 3,1, 4,1 0,2 1,2 2,2 3,2, 4,2 0,3 1,3 2,3 3,3, 4,3 0,4 1,4 2,4 3,4, 4,4 |
| ||
You can perform a transformation in two dimensions by applying an easing curve to each dimension individually. I don't know which particular curve a fisheye lens calculation utilizes, however, a cubic interpolation curve that eases both on the in and out should be able to do it. Here is a piece of example code pulled from my framework:Method Ease:Float(p:Float, in:Bool, out:Bool) If in And out If p < 0.5 Then Return 4 * Pow(p, 3) Return 4 * Pow(p - 1, 3) + 1 ElseIf in Return (p * p * p) ElseIf out Return Pow(p - 1, 3.0) + 1 Else Return p 'linear End If End Method It maps a linear value from 0-1 to a cubic easing value. You can Lerp this value between the linear and cubic value to produce a softer effect, or increase the exponent to make the effect more pronounced, iirc. alternate easing calcs for arcs: http://www.flong.com/texts/code/shapers_circ/ |
| ||
![]() Source code for this example: |
| ||
@Raz: Not what you're looking for, but this got me messing around with shaders in Mojo 2: EDIT 02: Well, it's kind of working: ![]() I take it this page is what you were looking at? That tutorial calls the effect you want "Barrel Distortion". Shouldn't be too hard to add. EDIT 03: See below for a Mojo 2 implementation; use at your own risk. |
| ||
After messing around for a bit, I was able to write a basic shader to do what you're looking for. Obviously, this is Mojo2-only, or at least, it requires Mojo 2 to be the backend. I pretty much just followed this. Preview image: ![]() |
| ||
Oh wow, thanks for your effects all :) I'm sorry though I think I should have made myself a bit more clear though, I don't want a shader, I'd like to distort a 2d array of points. I did give your shader code a look but ... well... I'm simple :D Here's a base app (that's currently not working at all, but you might see what I'm getting at), move the mouse to move the center of the distortion |
| ||
Ahh just seen your post too Nobu, will give it a go to see if I can adapt it for what I'm after |
| ||
@Raz Here's your code altered to apply the fragment shader's implied method. |
| ||
Oh wow, thank you so much, that's exactly what I am after :) Thanks all who took time to give me some help! |