所以我在iOS 12(Swift 4.2)中使用SceneKit,我想为相机添加旋转/凹凸失真。我在这里找到了类似的东西(Fish Eye Wide-angle with a Scene Kit Camera: Possible?),据说会产生桶形失真。但是当我尝试将它添加到我的项目中时,场景变黑了,我在控制台中出现错误
2019-03-07 13:35:14.982232 + 0000 TestingSCN [551:66202] [framework] CUIThemeStore:没有使用id = 0注册的主题
2019-03-07 13:35:15.064859 + 0000 TestingSCN [551:66202] [framework] CUIThemeStore:没有使用id = 0注册的主题
2019-03-07 13:35:15.097517 + 0000 TestingSCN [551:66270] [framework] 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"
}
}
}
barrel.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);
}
barrel.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或其他东西可能更好,但我还不知道从哪里开始。任何帮助表示赞赏:)
你可以试试
let scnView = self.view as! SCNView
scnView.technique = technique
因为你正在使用渲染渲染对象纹理的(“draw”:“DRAW_QUAD”)。
还有一个编译器错误:
float d = length(xy); // `xy` is not defined