API Reference

Class List

Shader

A shader is a program that is responsible for rendering graphical primitives on a device's graphics processor. The shader is generated from a shader definition. This shader definition specifies the code for processing vertices and fragments processed by the GPU. The language of the code is GLSL (or more specifically ESSL, the OpenGL ES Shading Language). The shader definition also describes how the PlayCanvas engine should map vertex buffer elements onto the attributes specified in the vertex shader code.

// Create a shader that renders primitives with a solid red color

// Vertex shader
const vshader = `
attribute vec3 aPosition;

void main(void) {
    gl_Position = vec4(aPosition, 1.0);
}
`;

// Fragment shader
const fshader = `
precision ${graphicsDevice.precision} float;

void main(void) {
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;

const shaderDefinition = {
    attributes: {
        aPosition: pc.SEMANTIC_POSITION
    },
    vshader,
    fshader
};

const shader = new pc.Shader(graphicsDevice, shaderDefinition);

Summary

Methods

destroy

Frees resources associated with this shader.

Details

Constructor

Shader(graphicsDevice, definition)

Creates a new Shader instance.

Consider createShaderFromCode as a simpler and more powerful way to create a shader.

// Create a shader that renders primitives with a solid red color

// Vertex shader
const vshader = `
attribute vec3 aPosition;

void main(void) {
    gl_Position = vec4(aPosition, 1.0);
}
`;

// Fragment shader
const fshader = `
precision ${graphicsDevice.precision} float;

void main(void) {
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;

const shaderDefinition = {
    attributes: {
        aPosition: pc.SEMANTIC_POSITION
    },
    vshader,
    fshader
};

const shader = new pc.Shader(graphicsDevice, shaderDefinition);

Parameters

graphicsDeviceGraphicsDevice

The graphics device used to manage this shader.

definitionobject

The shader definition from which to build the shader.

definition.namestring

The name of the shader.

definition.attributes{ [string]: string }

Object detailing the mapping of vertex shader attribute names to semantics SEMANTIC_*. This enables the engine to match vertex buffer data as inputs to the shader. When not specified, rendering without vertex buffer is assumed.

definition.vshaderstring

Vertex shader source (GLSL code).

definition.fshaderstring

Fragment shader source (GLSL code). Optional when useTransformFeedback is specified.

definition.useTransformFeedbackboolean

Specifies that this shader outputs post-VS data to a buffer.

definition.shaderLanguagestring

Specifies the shader language of vertex and fragment shaders. Defaults to SHADERLANGUAGE_GLSL.

Methods

destroy()

Frees resources associated with this shader.