I am learning SceneKit and using GLSL. I find it difficult to understand the use of glsl with SceneKit, for example, how to load glsl shaders into SceneKit and apply it to geometry.
let's say that we have:
SCNBox *box = [SCNBox boxWithWidth:50 height:50 length:50 chamferRadius:0]; SCNNode *bNode = [SCNNode nodeWithGeometry:box]; SCNMaterial *redMaterial = [SCNMaterial material]; redMaterial.diffuse.contents = [UIColor redColor]; redMaterial.locksAmbientWithDiffuse = YES; box.materials = @[redMaterial]; [scene.rootNode addChildNode:bNode];
using the gllsl example code for glass from the year 2006 , how can this effect be added to the geometry. need to bind these parameters from Glass.vert to SceneKit geometry? Initially, I am trying to achieve a glass effect and a water effect.
The glass effect has 2 files: 1. Glass.vert file
varying vec3 Normal; varying vec3 EyeDir; varying vec4 EyePos; varying float LightIntensity; uniform vec3 LightPos; void main(void) { gl_Position = ftransform(); Normal = normalize(gl_NormalMatrix * gl_Normal); vec4 pos = gl_ModelViewMatrix * gl_Vertex; EyeDir = pos.xyz; EyePos = gl_ModelViewProjectionMatrix * gl_Vertex; LightIntensity = max(dot(normalize(LightPos - EyeDir), Normal), 0.0); }
and second file: Glass.frag
const vec3 Xunitvec = vec3 (1.0, 0.0, 0.0); const vec3 Yunitvec = vec3 (0.0, 1.0, 0.0); uniform vec3 BaseColor; uniform float Depth; uniform float MixRatio;
Edit:
I did this to load these shaders into SceneKit:
NSURL *vertexShaderURL = [[NSBundle mainBundle] URLForResource:@"Glass" withExtension:@"vert"]; NSURL *fragmentShaderURL = [[NSBundle mainBundle] URLForResource:@"Glass" withExtension:@"frag"]; NSString *vertexShader = [[NSString alloc] initWithContentsOfURL:vertexShaderURL encoding:NSUTF8StringEncoding error:NULL]; NSString *fragmentShader = [[NSString alloc] initWithContentsOfURL:fragmentShaderURL encoding:NSUTF8StringEncoding error:NULL]; SCNProgram *program = [SCNProgram program]; program.delegate = self; program.vertexShader = vertexShader; program.fragmentShader = fragmentShader; SCNMaterial *redMaterial = [SCNMaterial material]; redMaterial.diffuse.contents = [UIColor redColor]; redMaterial.locksAmbientWithDiffuse = YES; redMaterial.program = program; box.materials = @[redMaterial];
And besides, I initialized them in the shader files:
The box now looks pink with no glass effect. Removing a program from redMaterial, the window appears red, as expected, without a glass effect. therefore, I still cannot achieve the desired effect. any help is much appreciated.
Edit 2:
xcode logs:
2016-11-21 08:08:26.758244 testGame[7837:3366037] [DYMTLInitPlatform] platform initialization successful 2016-11-21 08:08:27.196142 testGame[7837:3365880] Metal GPU Frame Capture Enabled 2016-11-21 08:08:27.196975 testGame[7837:3365880] Metal API Validation Enabled