SKAttribute
A specification for dynamic per-node data used with a custom shader.
Declaration
class SKAttributeMentioned in
Overview
To define an attribute for your shader, you create an SKAttribute object with a unique name and data type, which is a SKAttributeType enum. After creating an SKShader object, custom attributes are added to its attributes array. Attribute values are set on the parent node with setValue(_:forAttribute:) and can change for each execution of a shader without the need for recompilation.
The following listing shows how you can use an attribute to pass the size of a sprite into a shader using an attribute. In this example, a_sprite_size is available as a global vec2 within the GLSL code.
Listing 1. Passing an attribute to a shader.
let attributeBasedShader = SKShader(fileNamed: "UsingAttributes.fsh")
attributeBasedShader.attributes = [
SKAttribute(name: "a_sprite_size", type: .vectorFloat2)]
let sprite = SKSpriteNode()
sprite.shader = attributeBasedShader
sprite.size = CGSize(width: 10, height: 10)
let spriteSize = vector_float2(Float(sprite.frame.size.width),
Float(sprite.frame.size.height))
sprite.setValue(SKAttributeValue(vectorFloat2: spriteSize),
forAttribute: "a_sprite_size")