我真正想做的是让SCNCamera使用用户可控制的镜头虚拟焦距渲染我的SCNScene。
以下是我可以通过调整此着色器的最佳方法。结果是整个视口中的单一颜色。我绝对不会使用这个GLSL。
我没有停止使用Open GL而不介意使用Metal shader,我只是没有看到任何关于如何在这里使用它的例子。
片段
uniform sampler2D tex0;
varying vec2 uv;
uniform vec2 size;
uniform float lensSize; // 0.4
void main(void)
{
vec2 p = gl_FragCoord.xy / uv.xy;
vec2 m = uv.xy;
//vec2 m = mouse.xy / a_position.xy;
vec2 d = p - m;
float r = sqrt(dot(d, d)); // distance of pixel from mouse
vec2 uv2;
vec3 col = vec3(0.0, 0.0, 0.0);
if (r > lensSize+0.01)
{
uv2 = p;
col = texture2D(tex0, vec2(uv2.x, -uv2.y)).xyz;
}
else if (r < lensSize-0.01)
{
// Thanks to Paul Bourke for these formulas; see
// http://paulbourke.net/miscellaneous/lenscorrection/
// and .../lenscorrection/lens.c
// Choose one formula to uncomment:
// SQUAREXY:
// uv = m + vec2(d.x * abs(d.x), d.y * abs(d.y));
// SQUARER:
uv2 = m + d * r; // a.k.a. m + normalize(d) * r * r
// SINER:
// uv = m + normalize(d) * sin(r * 3.14159 * 0.5);
// ASINR:
// uv = m + normalize(d) * asin(r) / (3.14159 * 0.5);
col = texture2D(tex0, vec2(uv2.x, -uv2.y)).xyz;
}
gl_FragColor = vec4(col, 1.0);
}
顶点
attribute vec4 a_position;
varying vec2 uv;
void main()
{
gl_Position = a_position;
uv = (a_position.xy + 1.0) * 0.5;
}
SCNTechnique init词典
{
"passes": {
"barrel": {
"outputs": {
"color": "COLOR"
},
"inputs": {
"tex0": "COLOR",
"a_position": "a_position-symbol"
},
"program": "art.scnassets/barrel",
"draw": "DRAW_QUAD"
}
},
"sequence": [
"barrel"
],
"symbols": {
"a_position-symbol": {
"semantic": "vertex"
},
"lensSize": {
"type": "float"
}
}
}
夫特
let technique = SCNTechnique(dictionary: propsDict as! Dictionary)
technique?.setValue(1, forKey: "lensSize")
myNode.camera = SCNCamera()
myNode.camera?.technique = technique