5

According to Apple’s documentation “To update a value once, use key-value coding: Call the setValue:forKey: method, providing the uniform name from shader source code as the key and an appropriate type of data as the value.” (taken from SCNProgram Class Reference).

However, I can’t get this to work.

I have a SCNMaterial subclass, set an new SCNProgram instance, load vertex and fragment shader. I have been using handleBindingOfSymbol to set custom variables, such as

self.stringMaterial.handleBindingOfSymbol("waveAmplitude") {
    programID, location, renderedNode, renderer in
    glUniform1f(GLint(location), Float(self.amplitude))
}

This is working correctly.

For efficiency I want to move to being able to use key-value coding. But if I replace the above code with

self.stringMaterial.setValue(Float(self.amplitude), forKey: "waveAmplitude")

the uniform’s value in the shader is 0.0.

Does anyone have any experience with this? (I'm doing this on MacOS but I expect it’s the same on iOS.)

Gary Makin
  • 3,109
  • 1
  • 19
  • 27

0 Answers0