how to make lens distortion work?

hi there!

I’ve been following a Eat3d tutorial on how to create a lens distortion shader, but I can’t quite figure out how to assign it in the end.
I’ve been looking at the content example for postprocess materials and tried to replicate the process… it sort of works but it shrinks my frame down for some reason. I think it has to do with the Screen Position node, but I’m not sure what to do with it. If I unhook it the results looks even more broken than this.
does anyone know how I can hook this up correctly?

thank you! :slight_smile:

Hey HSbF6 -

Look at clamping from 0 to 1 the Input before you place it into the UVs of the Scene Texture node.

Let me know -

Eric Ketchum

hello eric!

unfortunately that didn’t work. :confused:
any other suggestions?

thank you!

Hey HSbF6 -

I am going to try to reconstruct the issue here, is this the name of the Eat3D tutorial you are using,
UE4 Shader FX: Cubic Lens Distortion

Thank You

Eric Ketchum

heya :slight_smile:

yeah, that’s it. but it’s a bit lengthy.
I uploaded the shader for you, saves you a bit of time hopefully. thanks for looking into it! really appreciated.

link text

Hey -

There is still some UV stretching within the lens but the lens itself has a clear undistributed edge adding this logic to the beginning of the Material:

Thank You

Eric Ketchum

hi eric!
thank you very much, that sort of worked. I mean it does work, so thanks for solving the problem!
but this shader itself is not really what I was hoping I think.
is there a easy way to apply a simple barrell distortion to the camera without going super crazy on the FOV?
if you have any suggestions, that would be amazeballs.

thank you :slight_smile:

Old question, but in case anyone pokes around this question again, the Eat3D video is a bit more complex than is what is needed if you want simple barrel distortion. Doing a little digging around because I wanted to do the same thing, I found this:

However, as I read the shader code, I noticed that it was being completely ridiculous, first it gets the angle of the UV vector, the figures out the “magnification” using the barrel distortion, then it calculates the new x and y positions using sine and cosine on the angle–which is completely redundant. In fact, you can throw away all those trig ops (well and you can’t do atan in a material anyways) and just scale the vector using the barrel power parameter. You end up with a cute little material that you can create barrel or pincushion effects with on a single parameter. Screenshots of two examples and the material to follow.

Just a couple of notes:

  1. If you make the material, create a material instance so you can change the parameter easier without recompiling the material constantly. You can also create a dynamic material instance in blueprints with it and then modify the parameter via blueprints if you want to mess around and figure out what the right “barrel power” should be for your application (e.g., Cardboard).
  2. The final material masks out the pixels beyond the distortion area with black. If you’re already using something like a mask to get rid of those pixels, it’s an unnecessary step (use the previous material). You could also simply move that code to pick between the normal texture coordinate (in place of black) or the calculated texture coordinate by the exact comparison and this would give you a non-distorted image in those areas (more like the image in the links above).

Finally, I noticed that my post processing didn’t work properly in the preview window, at least on my Mac. Instead, I had to build and run in a separate window and then everything looked right.

1 Like

Thank you very much! Ive been looking the whole evening for a workaround until i found your answer.
Also on a side note, if you dont want the black borders on your screen, you can plug the scene texture in the A inferior to B condition. I find it nicer, but whatever, thank’s a lot!

May I know how to make lens distortion with above barrel-distortion-material.png for my map??Thanks.

Thank you so much! This finally works! So many dead ends in trying to get a fish eye distortion and finally one that actually works!