鱼眼广角与场景套件相机:可能吗?

时间:2015-07-21 22:50:23

标签: camera scenekit

如何在场景套件中使用SCNCamera获取类似鱼眼镜头的失真?

像图像的这种“鞠躬”:

enter image description here

//正如里克斯特指出的那样,这种失真被称为“桶形失真”。

从文档来看,这是让我对使用相机做这种失真的可能性感兴趣的部分:

  

如果您计算自己的投影变换矩阵,则可以使用此矩阵   直接设置它的方法,覆盖合成的转换   来自相机的几何属性。

不幸的是,我对计算自己的投影变换矩阵的能力和可能性一无所知。我希望通过它可以做到这种失真......但不知道,因此问题。

通过相机的任何其他方式都是理想的选择。太。想要避免后期处理技巧,并在相机旋转并在场景中移动时获得更加“有机”的这种失真外观。

请参阅任何滑板视频,了解其在现实生活中的表现。

1 个答案:

答案 0 :(得分:8)

您正在寻找的是Barrel Distrortion。

有几种方法可以做到这一点,所有这些方法都使用GLSL着色器。

您可以使用经典的OpenGL代码,例如this example用于Occulus Rift(您需要稍微更改着色器),或者我个人最喜欢的代码:SCNTechnique

创建包含Barrel Fragment Shader(.fsh)的技术,并将其draw参数设置为DRAW_QUAD。然后,只需将该技术应用到您的相机。

您可以在此处找到Barrel Distortion着色器的示例:http://www.geeks3d.com/20140213/glsl-shader-library-fish-eye-and-dome-and-barrel-distortion-post-processing-filters/2/

编辑:这是一个示例代码:

barrel.json (这应该放在你的scnassets包中)

{
  "passes" : {
    "barrel" : {
      "outputs" : {
        "color" : "COLOR"
      },
      "inputs" : {
        "colorSampler" : "COLOR",
        "noiseSampler" : "noiseSymbol",
        "a_position" : "a_position-symbol"
      },
      "program" : "art.scnassets/barrel",
      "draw" : "DRAW_QUAD"
    }
  },
  "sequence" : [
    "barrel"
  ],
  "symbols" : {
    "a_position-symbol" : {
      "semantic" : "vertex"
    },
    "noiseSymbol" : {
      "image" : "noise.png",
      "type" : "sampler2D"
    },
    "barrelPower" : {
      "type" : "float"
    }
  }
}

<强> barrel.vsh

attribute vec4 a_position;
varying vec2 uv;

void main() {
    gl_Position = a_position;
    uv = a_position.xy;
}

<强> barrel.fsh

// Adapted from :
// http://www.geeks3d.com/20140213/glsl-shader-library-fish-eye-and-dome-and-barrel-distortion-post-processing-filters/2/

uniform sampler2D colorSampler;
const float PI = 3.1415926535;
uniform float barrelPower;

varying vec2 uv;


vec2 Distort(vec2 p)
{
    float theta  = atan(p.y, p.x);
    float radius = length(p);
    radius = pow(radius, barrelPower);
    p.x = radius * cos(theta);
    p.y = radius * sin(theta);
    return 0.5 * (p + 1.0);
}


void main() {

    vec2 rg = 2.0 * uv.xy - 1.0;
    vec2 uv2;
    float d = length(xy);
    if (d < 1.0){
        uv2 = Distort(xy);
    }else{
        uv2 = uv.xy;
    }

    gl_FragColor = texture2D(colorSampler, uv2);
}

<强> something.m

NSURL *url = [[NSBundle mainBundle] URLForResource:@"art.scnassets/barrel" withExtension:@"json"];
NSDictionary *tecDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL: url] options:nil error:nil];

SCNTechnique* technique = [SCNTechnique techniqueWithDictionary:tecDic];

[technique setValue: [NSNumber numberWithFloat:0.5]  forKey:@"barrelPower"];


cameraNode.technique = technique;
相关问题