iOS Developer Library — Pre-Release

Developer

SpriteKit Framework Reference SKShader Class Reference

Options
Deployment Target:

On This Page
Language:

SKShader

An SKShader object holds a custom OpenGL ES fragment shader. Shader objects are used to customize the drawing behavior of many different kinds of nodes in Sprite Kit. More...

Inheritance


Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 8.0 and later.
  • Creates a new shader object by loading the source for a fragment shader from a file stored in the app’s bundle.

    Declaration

    Swift

    convenience init!(fileNamed name: String)

    Objective-C

    + (instancetype)shaderWithFileNamed:(NSString *)name

    Parameters

    name

    The name of the fragment shader to load. The file must be present in your app bundle with the same name and a .fsh file extension.

    Return Value

    A newly initialized shader object whose initial source is loaded from the shader file.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Creates a new shader object using the specified source and uniform data.

    Declaration

    Objective-C

    + (instancetype)shaderWithSource:(NSString *)source uniforms:(NSArray *)uniforms

    Parameters

    source

    A string that holds the source for a fragment shader.

    uniforms

    A list of uniforms to add to the shader object.

    Return Value

    A newly initialized shader object.

    Availability

    Available in iOS 8.0 and later.

  • Creates a new shader object using the specified source code.

    Declaration

    Objective-C

    + (instancetype)shaderWithSource:(NSString *)source

    Parameters

    source

    A string that holds the source code for a fragment shader.

    Return Value

    A newly initialized shader object.

    Availability

    Available in iOS 8.0 and later.

  • Creates a new empty shader object.

    Declaration

    Objective-C

    + (instancetype)shader

    Return Value

    A newly initialized shader object.

    Discussion

    The shader is initialized without any source code or uniform data. You must assign source code to the shader before using it.

    Availability

    Available in iOS 8.0 and later.

    See Also

    source

  • Initializes a new shader object using the specified source and uniform data.

    Declaration

    Swift

    init(source source: String!, uniforms uniforms: [AnyObject]!)

    Objective-C

    - (instancetype)initWithSource:(NSString *)source uniforms:(NSArray *)uniforms

    Parameters

    source

    A string that holds the initial source for the shader.

    uniforms

    A list of uniforms to add to the shader object.

    Return Value

    An initialized shader object.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Initializes a new shader object using the specified source code.

    Declaration

    Swift

    init(source source: String!)

    Objective-C

    - (instancetype)initWithSource:(NSString *)source

    Parameters

    source

    A string that holds the initial source for the shader.

    Return Value

    An initialized shader object.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Adds a uniform to the shader object.

    Declaration

    Swift

    func addUniform(_ uniform: SKUniform)

    Objective-C

    - (void)addUniform:(SKUniform *)uniform

    Parameters

    uniform

    The new uniform object to add. The uniform object’s name must not already be in use by another uniform attached to the shader.

    Discussion

    The uniform variable is automatically accessible inside your shader; do not add a declaration for it in your shader’s source code. The uniform must be accessed in the fragment shader.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Removes a uniform from the shader.

    Declaration

    Swift

    func removeUniformNamed(_ name: String)

    Objective-C

    - (void)removeUniformNamed:(NSString *)name

    Parameters

    name

    The name of the uniform to remove.

    Discussion

    If a uniform with that name does not exist in the shader, nothing happens.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • uniforms uniforms Property

    The list of uniforms associated with the shader.

    Declaration

    Swift

    var uniforms: [AnyObject]

    Objective-C

    @property(copy) NSArray *uniforms

    Discussion

    This property is not read-only, so you can also use it to provide all of the uniforms in a single operation. Each of the uniforms should be uniquely named.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • Returns the uniform object corresponding to a particular uniform variable.

    Declaration

    Swift

    func uniformNamed(_ name: String) -> SKUniform?

    Objective-C

    - (SKUniform *)uniformNamed:(NSString *)name

    Parameters

    name

    The name of the uniform to search for.

    Return Value

    The uniform object corresponding to the name, or nil if that uniform cannot be found.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • source source Property

    The source code for the shader.

    Declaration

    Swift

    var source: String!

    Objective-C

    @property(copy) NSString *source

    Discussion

    The source code for a shader object can be updated at runtime. However, recompiling the fragment shader can be an expensive operation.

    Import Statement

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.