1
\$\begingroup\$

I'm trying to write a simple shader able to pass the color to be used for drawing in the fragment shader, through a uniform. I load the shader modifier and pass the uniform:

let fragmentShaderPath = bundle.pathForResource("Cube", ofType: "fragment")!
let fragmentShader = try String(contentsOfFile: fragmentShaderPath)

let shaderModifiers : [String:String] = [
    SCNShaderModifierEntryPointFragment : fragmentShader
]

cube.geometry?.shaderModifiers = shaderModifiers

SCNTransaction.begin()
cube.geometry?.setValue(NSValue(SCNVector4: SCNVector4Make(0.0,1.0,0.0,1.0)), forKey: "myColor")
SCNTransaction.commit()

And this is my fragment shader modifier code:

#include <metal_stdlib>
using namespace metal;

float4 myColor;

_output.color = myColor;

But nothing happens: the object is drawn as black. If I try declaring myColor as a uniform, I get an error:
enter image description here

\$\endgroup\$
  • \$\begingroup\$ Are you sure you need to include "#include <metal_stdlib> using namespace metal;" in the modifier ? Is it not already included in the main shader the modifier snippet is going to fit in? \$\endgroup\$ – Guig Sep 16 '16 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.