Beginner question: CG Unity Shader to GLSL Shader [closed]

asked 2014-09-04 13:05:10 -0600

Reyny gravatar image

updated 2015-08-24 18:34:32 -0600

Hey ladies and gentleman,

I'm trying to undistort a webcam by using a shader and I already have the specific parameters for the camera. I never used shader before and I'm also not using unity, but the only bit of code that i found which could be useful for me was this, which was written for CG in Unity:

http://i.imgur.com/4tN8Pg4.png (shown as text below)

So I want to know how you translate that bit of code to a GLSL shader so i can use it (namely in x3d) I already tried several programs to automaticly do it, but they always failed.

I hope somebody can help me out!

uniform sampler2D _MainTex;
uniform float _F1;
uniform float _F2;
uniform float _OX;
uniform float _OY;
uniform float _K1;
uniform float _K2;
uniform float _K3;
uniform float _K4;

fixed4 frag (v2f_img i) : COLOR {

    float2 ff = {_F1,_F2};
    float2 xy = (i.uv - float2(_OX,_OY) / _ScreenParams.xy) / ff;
    float r = sqrt(dot(xy,xy));
    float r2 = r*r;
    float r4 = r2 * r2;
    float coeff = (_K1 * r2 + _K2 * r4);

    float dx = _K3 * 2.0 * xy.x*xy.y + _K4*(r2+2.0*xy.x*xy.x);
    float dy = _K3*(r2+2.0*xy.y*xy.y) + _K4*2.0*xy.x * xy.y;

    xy = (((xy + xy * coeff.xx + float2(dx,dy)) * ff) + float2(_OX,_OY)) / _ScreenParams.xy;
    fixed4 main = tex2D(_MainTex, xy);
    return main;
}
edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by StevenPuttemans
close date 2014-09-04 15:13:51.615988