Here is a sample implementation. Create an Objective-C category on SKShader
, I called it Unions
. Declare a method in .h file:
#import <SpriteKit/SpriteKit.h>
@interface SKShader (Unions)
+(SKShader*)shaderForX:(float)x y:(float)y z:(float)z;
@end
Then go to .m file and implement:
#import "SKShader+Unions.h"
@implementation SKShader (Unions)
+(SKShader*)shaderForX:(float)x y:(float)y z:(float)z
{
SKShader *shader = [SKShader shaderWithFileNamed:@"shader.fsh"];
SKUniform *texture = [SKUniform uniformWithName:@"customTexture" texture:[SKTexture textureWithImageNamed:@"shade"]];
SKUniform *uniformSCNVector = [SKUniform uniformWithName:@"size" floatVector3:GLKVector3Make(x, y, z)];
[shader setUniforms:@[texture,uniformSCNVector]];
return shader;
}
@end
Don't forget to create a bridging header for your project. It's a .h file called "YourProjectName-Bridging-Header.h"
and contains #import "SKShader-Unions.h"
directive.
Now you can call from Swift file:
let shader = SKShader(forX: x, y: y, z: y)