SceneKit diffuse color in fragment shader

Hi guys,

I’m trying to apply a fragment shader on a SceneKit node to achieve non-photorealistic render. I would like to use the diffuse and ambiant colors of each vertex group as the base color in the fragment shader.

In swift:

program.vertexFunctionName = "toonVertex"
program.fragmentFunctionName  = "toonFragment"

if let materials = node.geometry?.materials
	for material in materials
		material.program = program
		material.setValue(lightNode.position, forKey: "lightPosition")
		material.setValue(material.diffuse.contents, forKey: "baseColor")

I’m not sure on how I should input this in the fragment function. Can anyone help me on this?


I have never done this, I’m afraid. You need another attribute in VertexIn:

float4 color[[attribute(SCNVertexSemanticColor)]];

Add the same attribute to VertexOut. Then you can pass the color from the vertex shader to the fragment shader.

However, this means that you’d need an extra semantic in SCNGeometrySource for the color. I don’t know how to do that.


I indeed thought about this solution, but I’m stuck at the same point.