CCShader Class Reference

Inherits from NSObject
Conforms to NSCopying
Declared in CCShader.h

Overview

A wrapper for OpenGL or Metal shader programs. Also gives you access to the built-in shaders used by Cocos2D.

Creating a OpenGL Shader

– initWithVertexShaderSource:fragmentShaderSource:

Creates a shader with the given vertex and fragment shader source code as strings. When the GL renderer is running, GLSL source is expected. When the Metal renderer is running, Metal shading language source is expected.

- (instancetype)initWithVertexShaderSource:(NSString *)vertexSource fragmentShaderSource:(NSString *)fragmentSource

Parameters

vertexSource

The vertex shader’s source code string. Must not be nil.

fragmentSource

The fragment shader’s source code string. Must not be nil.

Return Value

The created CCShader instance, or nil if there was a compile error in either of the two shader programs.

Declared In

CCShader.h

– initWithFragmentShaderSource:

Creates a shader with the given fragment shader source code as string. When the GL renderer is running, GLSL source is expected. When the Metal renderer is running, Metal shading language source is expected.

- (instancetype)initWithFragmentShaderSource:(NSString *)source

Parameters

source

The fragment shader’s source code string. Must not be nil.

Return Value

The created CCShader instance, or nil if there was a compile error in the shader programs.

Declared In

CCShader.h

Creating a Metal Shader

– initWithMetalVertexFunction:fragmentFunction:

Creates a Metal shader with the given vertex and fragment shader functions.

- (instancetype)initWithMetalVertexFunction:(id<MTLFunction>)vertexFunction fragmentFunction:(id<MTLFunction>)fragmentFunction

Parameters

vertexFunction

A vertex shader object that implements the MTLFunction protocol.

fragmentFunction

A fragment shader object that implements the MTLFunction protocol.

Return Value

The created CCShader instance, or nil if there was a compile error in the shader programs.

Availability

v3.3 and later. Only available on supported devices with Cocos2D Metal rendering enabled. Not available when building for iOS Simulator.

Declared In

CCShader.h

Getting a Shader by its Name

+ shaderNamed:

Returns the shader with the given name. Returns nil if there’s no shader for this name. When the GL renderer is running, this searches for a file name “shaderName.fsh” for the fragment shader, and optionally “shaderName.vsh” for the vertex shader. When the Metal renderer is running it searces in CCShaders.metallib for a fragment function named “shaderNameFS” and optionally a vertex function named “shaderNameVS”.

+ (instancetype)shaderNamed:(NSString *)shaderName

Parameters

shaderName

The shader’s unique name.

Declared In

CCShader.h

Obtaining a Built-In Shader

+ positionColorShader

A solide color shader.

+ (instancetype)positionColorShader

Return Value

A solide color shader.

Declared In

CCShader.h

+ positionTextureColorShader

A texture shader with vertex colors.

+ (instancetype)positionTextureColorShader

Return Value

A texture shader with vertex colors.

Declared In

CCShader.h

+ positionTextureColorAlphaTestShader

A texture shader with vertex colors and alpha testing.

+ (instancetype)positionTextureColorAlphaTestShader

Return Value

A texture shader with vertex colors and alpha testing.

Declared In

CCShader.h

+ positionTextureA8ColorShader

An 8-bit color texture shader.

+ (instancetype)positionTextureA8ColorShader

Return Value

An 8-bit color texture shader.

Declared In

CCShader.h

Setting a Shader's Debug Name

  debugName

The shader’s name for debugging purposes.

@property (nonatomic, copy) NSString *debugName

Declared In

CCShader.h