I'm trying to pass in an array of points into a shader modifier in SceneKit, but I can't work out the correct syntax. In my shader extension code I have:
uniform vec2 points[100];
If I call…
material.setValue(NSValue(point: CGPoint(x: 100.5, y: 50.5)), forKey: "points")
…then it sets the value of points[0]
, which makes me think that maybe it's not possible. I've tried lots of other combinations for both the key and the value, but nothing seems to work.
Is there a better way to do this? My end goal is to modify the surface diffuse colour for a set of points in the array, and use the default rendering otherwise. Is there a better way to do this in the shader than looping over an array of vec2
s?
Thanks for your help.
A shader array variable is just a blob of data passed from the GL client to the GPU, with some size and layout agreed upon in the shader code.
So, to do this in SceneKit, you'll need to first set up your blob, then wrap it in an
NSData
to pass to the shader. e.g.Also, when you're working with single points, SceneKit will handle the layout conversion from
CGPoint
to GPUvec2
, but when you pass a blob of data, all SceneKit knows is that it's a blob of data... it can't look inside and adjust the layout for what the GPU might expect, because it doesn't know what the layout you provided is.So, you should use types that explicitly match the size and layout that you want in the GPU shader. That means no
CGPoint
—its components areCGFloat
, and that type's size changes between CPU architectures. Thefloat2
type from the SIMD library is a close match for GLvec2
and an exact match for Metalfloat2
.