So I'm using SceneKit with iOS 12 (Swift 4.2), and I want to add a twirl/bump distortion to the camera. I found something similar here (Fish Eye Wide-angle with a Scene Kit Camera: Possible?) that supposedly creates a barrel distortion. But when I tried adding it to my project the scene just turns black and I get an error in the console
2019-03-07 13:35:14.982232+0000 TestingSCN[551:66202] [framework] CUIThemeStore: No theme registered with id=0
2019-03-07 13:35:15.064859+0000 TestingSCN[551:66202] [framework] CUIThemeStore: No theme registered with id=0
2019-03-07 13:35:15.097517+0000 TestingSCN[551:66270] [framework] CUIThemeStore: No theme registered with id=0
2019-03-07 13:35:15.118445+0000 TestingSCN[551:66270] [SceneKit] Error: can not render without programs, using default
Now the method is basically using a JSON dictionary file to define a technique and GLSL vertex and fragment shader files. Then in my main swift file, I add that technique to the camera. This is the code I used: barrel.json (located in 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 (in 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)")
}
I'm not really an expert in shaders, and I know that it's probably better to write an SCNProgram or something but I have not clue where to even begin with that. Any help is appreciated :)