API Reference

Class List

pc.Shader

A shader is a program that is repsonsible 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
var shaderDefinition = {
    attributes: {
        aPosition: pc.SEMANTIC_POSITION
    },
    vshader: [
        "attribute vec3 aPosition;",
        "",
        "void main(void)",
        "{",
        "    gl_Position = vec4(aPosition, 1.0);",
        "}"
    ].join("\n"),
    fshader: [
        "precision " + graphicsDevice.precision + " float;",
        "",
        "void main(void)",
        "{",
        "    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);",
        "}"
    ].join("\n")
};

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

Summary

Methods

destroyFrees resources associated with this shader.

Details

Constructor

Shader(graphicsDevice, definition)

Creates a new shader object.

// Create a shader that renders primitives with a solid red color
var shaderDefinition = {
    attributes: {
        aPosition: pc.SEMANTIC_POSITION
    },
    vshader: [
        "attribute vec3 aPosition;",
        "",
        "void main(void)",
        "{",
        "    gl_Position = vec4(aPosition, 1.0);",
        "}"
    ].join("\n"),
    fshader: [
        "precision " + graphicsDevice.precision + " float;",
        "",
        "void main(void)",
        "{",
        "    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);",
        "}"
    ].join("\n")
};

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

Parameters

graphicsDevicepc.GraphicsDeviceThe graphics device used to manage this shader.
definitionObjectThe shader definition from which to build the shader.
definition.attributesObjectObject detailing the mapping of vertex shader attribute names to semantics (pc.SEMANTIC_*). This enables the engine to match vertex buffer data as inputs to the shader.
definition.vshaderStringVertex shader source (GLSL code).
definition.fshaderStringFragment shader source (GLSL code).
definition.useTransformFeedbackBooleanSpecifies that this shader outputs post-VS data to a buffer

Methods

destroy()

Frees resources associated with this shader.