首页 > 解决方案 > 为什么我的场景相机在使用 SCNTechnique 时只显示黑屏?

问题描述

所以我将 SceneKit 与 iOS 12 (Swift 4.2) 一起使用,并且我想为相机添加一个旋转/凹凸失真。我在这里发现了类似的东西(带有场景套件相机的鱼眼广角:可能吗?),据说会产生桶形失真。但是当我尝试将它添加到我的项目中时,场景只是变黑并且我在控制台中收到错误

2019-03-07 13:35:14.982232+0000 TestingSCN[551:66202] [框架] CUIThemeStore: 没有主题注册 id=0

2019-03-07 13:35:15.064859+0000 TestingSCN[551:66202] [框架] CUIThemeStore: 没有主题注册 id=0

2019-03-07 13:35:15.097517+0000 TestingSCN[551:66270] [框架] CUIThemeStore: 没有主题注册 id=0

2019-03-07 13:35:15.118445+0000 TestingSCN[551:66270] [SceneKit] 错误:没有程序无法渲染,使用默认值

现在该方法基本上是使用 JSON 字典文件来定义技术和 GLSL 顶点和片段着色器文件。然后在我的主 swift 文件中,我将该技术添加到相机中。这是我使用的代码:barrel.json(位于 art.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"
        }
    }
}

桶.fsh

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);
}

桶.vsh

attribute vec4 a_position;
varying vec2 uv;

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

GameViewController.swift(在 viewDidLoad 中)

let url: URL = Bundle.main.url(forResource: "art.scnassets/barrel", withExtension: "json")!

do {
    let jsonData = try Data(contentsOf: url)
    let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options:JSONSerialization.ReadingOptions(rawValue: 0))
    guard let dictionary = jsonObject as? Dictionary<String, Any> else {
        print("Not a Dictionary")
        return
    }
    var technique: SCNTechnique? = nil
    technique = SCNTechnique(dictionary: dictionary)
    technique?.setValue(NSNumber(value: 0.5), forKey: "barrelPower")
    cameraNode.camera?.technique = technique
}
catch let error as NSError {
    print("Found an error - \(error)")
}

我并不是真正的着色器专家,我知道编写 SCNProgram 或其他东西可能更好,但我不知道从哪里开始。任何帮助表示赞赏:)

标签: iosswiftshaderscenekitscntechnique

解决方案


你可以试试

    let scnView = self.view as! SCNView
    scnView.technique = technique

当您使用 ("draw" : "DRAW_QUAD") 渲染渲染对象的纹理时。

还有一个编译器错误:

 float d = length(xy);  // `xy` is not defined

推荐阅读