camera - 带场景套件相机的鱼眼广角 : Possible?

标签 camera scenekit

如何在 Scene Kit 中使用 SCNCamera 获得像鱼眼镜头那样的失真?

类似于这种图像的“鞠躬”:

enter image description here

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

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

If you compute your own projection transform matrix, you can use this method to set it directly, overriding the transformation synthesized from the camera’s geometric properties.



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

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

查看任何滑板视频,了解它在现实生活中的样子。

最佳答案

您正在寻找的是所谓的桶形失真。

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

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

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

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

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

桶.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"
    }
  }
}

桶.vsh
attribute vec4 a_position;
varying vec2 uv;

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

桶.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;

关于camera - 带场景套件相机的鱼眼广角 : Possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31550971/

相关文章:

ios - 如何确保 SceneKit 相机始终指向特定位置

ios - Scenekit:为什么不应该对 SCNNode 进行子类化?

ios - 将 .dae 文件导入 Xcode,显示 Z 轴朝上

ios - ARKit 使 SCNLight 指向相机正在看的地方?

swift - 如何在快速使用相机拍照后创建警报

android - 如何让Android应用程序(电视盒)识别或访问USB网络摄像头?

android - 使用自定义分辨率录制 Camera2 视频?

ios - 如何使用 Swift scenekit 在 IOS 上绘制相机视频作为背景?

swift - RealityKit 和 ARKit 的挑战

iphone - 如何在ipod中使用前置摄像头进行条码扫描