Define const array in SKShader

Hi,


As a SpriteKit user, i'm aware that it relies on openGL ES and that there is no mechanism for initializing arrays at declaration time from within a shader.


However, this piece of code compiles and behaves as expected in my fragment shader :


__constant float tiny[8] = {52,48,16,20,6,2,35,39};

void main() {
//Using tiny array
}


1. Is this a documented feature ? Can i use it safely in production code across iOS devices ?

2. If yes, is there a possibility to handle multidimensional array as well ?


Cheers !

Replies

Arrays aren't often used in shaders because there is typically a better way. Biggest problem being that any kind of array iteration (loop) in a shader slows down the pipeline considerably.


If you trying to access one of the array elements per node, you should use SKAttribute instead.

I am facing a similar problem. There might be an alternative solution but I am not sure what that would be.


I am trying to implement a "Metaball" type of effect on a bunch of circles (SKShapeNode) to which physics is applied. I would expect to pass metaballs coordinates and radius to the shader on every pass -- but I can't find a way to declare a SKUniform that is an array of vec3.

Currently, my idea is to have a shader on a SKEffectNode to which each metaball is added as a child. Any advice ?