Using a fragment shader with SceneKit written in GLSL?

I am trying to use the shader editor that is avaialbe in Xcode 9.3 that lets you attach a fragment shader to a scene kit object in the Scene Kit editor.


In this case, I have a plane. I have taken a shader from Shadertoy.org & re-writren parts of it to conform the instrucitons here:


https://developer.apple.com/documentation/scenekit/scnshadable?language=objc


In the documentation, it states that there are built-in uniforms . E.g u_time.


I get the following shader compiler error in the editor:


use of undeclared identifier 'scn_frame'


Here is the shader fragment that I have been hacking. It should draw a flame like object on the plane.


#arguments

uniform vec2 u_resolution;
uniform float u_time;


float noise(vec3 p) /
{
    vec3 i = floor(p);
    vec4 a = dot(i, vec3(1., 57., 21.)) + vec4(0., 57., 21., 78.);
    vec3 f = cos((p-i)*acos(-1.))*(-.5)+.5;
    a = mix(sin(cos(a)*a),sin(cos(1.+a)*(1.+a)), f.x);
    a.xy = mix(a.xz, a.yw, f.y);
    return mix(a.x, a.y, f.z);
}
float sphere(vec3 p, vec4 spr)
{
    return length(spr.xyz-p) - spr.w;
}
float flame(vec3 p)
{
    float d = sphere(p*vec3(1.,.5,1.), vec4(.0,-1.,.0,1.));
    return d + (noise(p+vec3(.0, u_time*2.,.0)) + noise(p*3.)*.5)*.25*(p.y) ;
}
float scene(vec3 p)
{
    return min(100.-length(p) , abs(flame(p)) );
}
vec4 raymarch(vec3 org, vec3 dir)
{
    float d = 0.0, glow = 0.0, eps = 0.02;
    vec3  p = org;
    bool glowed = false;

    for(int i=0; i<64; i++)
    {
        d = scene(p) + eps;
        p += d * dir;
        if( d>eps )
        {
            if(flame(p) < .0)
                glowed=true;
            if(glowed)
                glow = float(i)/64.;
        }
    }
    return vec4(p,glow);
}

#pragma body
    vec2 v = -1.0 + 2.0 * gl_FragCoord.xy;
    v.x *=  u_resolution.x/u_resolution.y;

    vec3 org = vec3(0., -2., 4.);
    vec3 dir = normalize(vec3(v.x*1.6, -v.y, -1.5));

    vec4 p = raymarch(org, dir);
    float glow = p.w;

    vec4 col = mix(vec4(1.,.5,.1,1.), vec4(0.1,.5,1.,1.), p.y*.02+.4);

    gl_FragColor = mix(vec4(0.), col, pow(glow*2.,4.));

 


The orginal source is from here:


https://www.shadertoy.com/view/MdX3zr


Note using a less complex shader does work. Like the following:


uniform vec2 u_resolution;
uniform float u_time;

#pragma body

vec3 colorA = vec3(0.149,0.141,0.912);
vec3 colorB = vec3(1.000,0.833,0.224);
vec3 color = vec3(0.0);
float pct = abs(sin(u_time));
// Mix uses pct (a value from 0-1) to
// mix the two colors
color = mix(colorA, colorB, pct);
gl_FragColor = vec4(color,1.0);


This shader is taken from the Book of shaders web-site