Contents

SKAttributeValue

A container for dynamic shader data associated with a node.

Declaration

class SKAttributeValue

Overview

SpriteKit nodes that are rendered with a custom shader can use SKAttributeValue objects to pass dynamic values which can change without requiring that shader to be recompiled. An attribute value is passed to a shader using a node’s setValue(_:forAttribute:) method using the relevant attribute’s name. For example, given a shader with a SKAttributeType.float attribute named a_radius:

Listing 1. Creating an attribute

let attribute = SKAttribute(name: "a_radius", 
                            type: SKAttributeType.float)

The following code sets the value of this attribute to 10 and passes it to a SKSpriteNode object’s shader:

Listing 2. Setting an attribute value

node.setValue(SKAttributeValue(float: 10), 
              forAttribute: "a_radius")

The attribute, a_radius, is available as a global floating-point variable within the shader code.

Using this technique, a single shader can be shared across many nodes and each nodes can supply its own attributes. This approach is an alternative to using SKUniform objects which would require a recompilation for each distinct set of parameters.

Topics

Initializers

Instance Properties

See Also

Shaders