【发布时间】:2019-07-29 10:39:44
【问题描述】:
所以我将 SceneKit 与 iOS 12 (Swift 4.2) 一起使用,并且我想为相机添加旋转/凹凸失真。我在这里(Fish Eye Wide-angle with a Scene Kit Camera: Possible?)发现了类似的东西,据说会产生桶形失真。但是当我尝试将它添加到我的项目时,场景只是变黑了,我在控制台中收到错误
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 文件中,我将该技术添加到相机中。这是我使用的代码: bucket.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 或其他东西可能会更好,但我什至不知道从哪里开始。 任何帮助表示赞赏:)
【问题讨论】:
标签: ios swift shader scenekit scntechnique