API Reference

Class List

pc.GraphicsDevice

Extends: pc.EventHandler

The graphics device manages the underlying graphics context. It is responsible for submitting render state changes and graphics primitives to the hardware. A graphics device is tied to a specific canvas HTML element. It is valid to have more than one canvas element per page and create a new graphics device against each.

Summary

Properties

canvasThe canvas DOM element that provides the underlying WebGL context used by the graphics device.
fullscreenFullscreen mode
heightHeight of the back buffer in pixels.[read only]
maxAnisotropyThe maximum supported texture anisotropy setting.[read only]
maxCubeMapSizeThe maximum supported dimension of a cube map.[read only]
maxPixelRatioMaximum pixel ratio
maxTextureSizeThe maximum supported dimension of a texture.[read only]
maxVolumeSizeThe maximum supported dimension of a 3D texture (any axis).[read only]
precisionThe highest shader precision supported by this graphics device.[read only]
scopeThe scope namespace for shader attributes and variables.
textureFloatHighPrecisionCheck if high precision floating-point textures are supported[read only]
textureFloatRenderableDetermines if 32-bit floating-point textures can be used as frame buffer.
textureHalfFloatRenderableDetermines if 16-bit floating-point textures can be used as frame buffer.
widthWidth of the back buffer in pixels.[read only]

Methods

clearClears the frame buffer of the currently set render target.
clearShaderCacheFrees memory from all shaders ever allocated with this device
drawSubmits a graphical primitive to the hardware for immediate rendering.
getBlendingQueries whether blending is enabled.
getDepthTestQueries whether depth testing is enabled.
getDepthWriteQueries whether writes to the depth buffer are enabled.
getRenderTargetQueries the currently set render target on the device.
resizeCanvasSets the width and height of the canvas, then fires the 'resizecanvas' event.
setBlendEquationConfigures the blending equation.
setBlendEquationSeparateConfigures the blending equation.
setBlendFunctionConfigures blending operations.
setBlendFunctionSeparateConfigures blending operations.
setBlendingEnables or disables blending.
setColorWriteEnables or disables writes to the color buffer.
setCullModeControls how triangles are culled based on their face direction.
setDepthFuncConfigures the depth test.
setDepthTestEnables or disables depth testing of fragments.
setDepthWriteEnables or disables writes to the depth buffer.
setIndexBufferSets the current index buffer on the graphics device.
setRenderTargetSets the specified render target on the device.
setScissorSet the active scissor rectangle on the specified device.
setShaderSets the active shader to be used during subsequent draw calls.
setStencilFuncConfigures stencil test for both front and back faces.
setStencilFuncBackConfigures stencil test for back faces.
setStencilFuncFrontConfigures stencil test for front faces.
setStencilOperationConfigures how stencil buffer values should be modified based on the result of depth/stencil tests.
setStencilOperationBackConfigures how stencil buffer values should be modified based on the result of depth/stencil tests.
setStencilOperationFrontConfigures how stencil buffer values should be modified based on the result of depth/stencil tests.
setStencilTestEnables or disables stencil test.
setVertexBufferSets the current vertex buffer for a specific stream index on the graphics device.
setViewportSet the active rectangle for rendering on the specified device.
updateBeginMarks the beginning of a block of rendering.
updateEndMarks the end of a block of rendering.

Events

resizecanvasThe 'resizecanvas' event is fired when the canvas is resized

Inherited

Methods

fireFire an event, all additional arguments are passed on to the event listener
hasEventTest if there are any handlers bound to an event name
offDetach an event handler from an event.
onAttach an event handler to an event
onceAttach an event handler to an event.

Details

Constructor

GraphicsDevice(canvas, [options])

Creates a new graphics device.

Parameters

canvasHTMLCanvasElementThe canvas to which the graphics device will render.
optionsObjectOptions passed when creating the WebGL context. More info here.

Properties

HTMLCanvasElementcanvas

The canvas DOM element that provides the underlying WebGL context used by the graphics device.

Booleanfullscreen

Fullscreen mode

Numberheight

Height of the back buffer in pixels.[read only]

NumbermaxAnisotropy

The maximum supported texture anisotropy setting.[read only]

NumbermaxCubeMapSize

The maximum supported dimension of a cube map.[read only]

NumbermaxPixelRatio

Maximum pixel ratio

NumbermaxTextureSize

The maximum supported dimension of a texture.[read only]

NumbermaxVolumeSize

The maximum supported dimension of a 3D texture (any axis).[read only]

Stringprecision

The highest shader precision supported by this graphics device. Can be 'hiphp', 'mediump' or 'lowp'.[read only]

pc.ScopeSpacescope

The scope namespace for shader attributes and variables. [read only]

NumbertextureFloatHighPrecision

Check if high precision floating-point textures are supported[read only]

BooleantextureFloatRenderable

Determines if 32-bit floating-point textures can be used as frame buffer. [read only]

BooleantextureHalfFloatRenderable

Determines if 16-bit floating-point textures can be used as frame buffer. [read only]

Numberwidth

Width of the back buffer in pixels.[read only]

Methods

clear(options)

Clears the frame buffer of the currently set render target.

// Clear color buffer to black and depth buffer to 1.0
device.clear();

// Clear just the color buffer to red
device.clear({
    color: [1, 0, 0, 1],
    flags: pc.CLEARFLAG_COLOR
});

// Clear color buffer to yellow and depth to 1.0
device.clear({
    color: [1, 1, 0, 1],
    depth: 1.0,
    flags: pc.CLEARFLAG_COLOR | pc.CLEARFLAG_DEPTH
});

Parameters

optionsObjectOptional options object that controls the behavior of the clear operation defined as follows:
options.colorNumber[]The color to clear the color buffer to in the range 0.0 to 1.0 for each component.
options.depthNumberThe depth value to clear the depth buffer to in the range 0.0 to 1.0.
options.flagsNumberThe buffers to clear (the types being color, depth and stencil). Can be any bitwise combination of:
  • pc.CLEARFLAG_COLOR
  • pc.CLEARFLAG_DEPTH
  • pc.CLEARFLAG_STENCIL

clearShaderCache()

Frees memory from all shaders ever allocated with this device

draw(primitive, [numInstances])

Submits a graphical primitive to the hardware for immediate rendering.

// Render a single, unindexed triangle
device.draw({
    type: pc.PRIMITIVE_TRIANGLES,
    base: 0,
    count: 3,
    indexed: false
)};

Parameters

primitiveObjectPrimitive object describing how to submit current vertex/index buffers defined as follows:
primitive.typeNumberThe type of primitive to render. Can be:
primitive.baseNumberThe offset of the first index or vertex to dispatch in the draw call.
primitive.countNumberThe number of indices or vertices to dispatch in the draw call.
primitive.indexedBooleanTrue to interpret the primitive as indexed, thereby using the currently set index buffer and false otherwise.
numInstancesNumberThe number of instances to render when using ANGLE_instanced_arrays. Defaults to 1.

getBlending()

Queries whether blending is enabled.

Returns

Boolean True if blending is enabled and false otherwise.

getDepthTest()

Queries whether depth testing is enabled.

var depthTest = device.getDepthTest();
console.log('Depth testing is ' + depthTest ? 'enabled' : 'disabled');

Returns

Boolean true if depth testing is enabled and false otherwise.

getDepthWrite()

Queries whether writes to the depth buffer are enabled.

var depthWrite = device.getDepthWrite();
console.log('Depth writing is ' + depthWrite ? 'enabled' : 'disabled');

Returns

Boolean true if depth writing is enabled and false otherwise.

getRenderTarget()

Queries the currently set render target on the device.

// Get the current render target
var renderTarget = device.getRenderTarget();

Returns

pc.RenderTarget The current render target.

resizeCanvas(width, height)

Sets the width and height of the canvas, then fires the 'resizecanvas' event. Note that the specified width and height values will be multiplied by the value of pc.GraphicsDevice#maxPixelRatio to give the final resultant width and height for the canvas.

Parameters

widthNumberThe new width of the canvas.
heightNumberThe new height of the canvas.

setBlendEquation(blendEquation)

Configures the blending equation. The default blend equation is pc.BLENDEQUATION_ADD.

Parameters

blendEquationNumberThe blend equation. Can be:
  • pc.BLENDEQUATION_ADD
  • pc.BLENDEQUATION_SUBTRACT
  • pc.BLENDEQUATION_REVERSE_SUBTRACT
  • pc.BLENDEQUATION_MIN
  • pc.BLENDEQUATION_MAX
  • Note that MIN and MAX modes require either EXT_blend_minmax or WebGL2 to work (check device.extBlendMinmax).

setBlendEquationSeparate(blendEquation, blendAlphaEquation)

Configures the blending equation. The default blend equation is pc.BLENDEQUATION_ADD.

Parameters

blendEquationNumberThe blend equation. Can be:
  • pc.BLENDEQUATION_ADD
  • pc.BLENDEQUATION_SUBTRACT
  • pc.BLENDEQUATION_REVERSE_SUBTRACT
  • pc.BLENDEQUATION_MIN
  • pc.BLENDEQUATION_MAX
  • Note that MIN and MAX modes require either EXT_blend_minmax or WebGL2 to work (check device.extBlendMinmax).
blendAlphaEquationNumberA separate blend equation for the alpha channel. Accepts same values as blendEquation.

setBlendFunction(blendSrc, blendDst)

Configures blending operations. Both source and destination blend modes can take the following values:

  • pc.BLENDMODE_ZERO
  • pc.BLENDMODE_ONE
  • pc.BLENDMODE_SRC_COLOR
  • pc.BLENDMODE_ONE_MINUS_SRC_COLOR
  • pc.BLENDMODE_DST_COLOR
  • pc.BLENDMODE_ONE_MINUS_DST_COLOR
  • pc.BLENDMODE_SRC_ALPHA
  • pc.BLENDMODE_SRC_ALPHA_SATURATE
  • pc.BLENDMODE_ONE_MINUS_SRC_ALPHA
  • pc.BLENDMODE_DST_ALPHA
  • pc.BLENDMODE_ONE_MINUS_DST_ALPHA

Parameters

blendSrcNumberThe source blend function.
blendDstNumberThe destination blend function.

setBlendFunctionSeparate(blendSrc, blendDst, blendSrcAlpha, blendDstAlpha)

Configures blending operations. Both source and destination blend modes can take the following values:

  • pc.BLENDMODE_ZERO
  • pc.BLENDMODE_ONE
  • pc.BLENDMODE_SRC_COLOR
  • pc.BLENDMODE_ONE_MINUS_SRC_COLOR
  • pc.BLENDMODE_DST_COLOR
  • pc.BLENDMODE_ONE_MINUS_DST_COLOR
  • pc.BLENDMODE_SRC_ALPHA
  • pc.BLENDMODE_SRC_ALPHA_SATURATE
  • pc.BLENDMODE_ONE_MINUS_SRC_ALPHA
  • pc.BLENDMODE_DST_ALPHA
  • pc.BLENDMODE_ONE_MINUS_DST_ALPHA

Parameters

blendSrcNumberThe source blend function.
blendDstNumberThe destination blend function.
blendSrcAlphaNumberThe separate source blend function for the alpha channel.
blendDstAlphaNumberThe separate destination blend function for the alpha channel.

setBlending(blending)

Enables or disables blending.

Parameters

blendingBooleanTrue to enable blending and false to disable it.

setColorWrite(writeRed, writeGreen, writeBlue, writeAlpha)

Enables or disables writes to the color buffer. Once this state is set, it persists until it is changed. By default, color writes are enabled for all color channels.

// Just write alpha into the frame buffer
device.setColorWrite(false, false, false, true);

Parameters

writeRedBooleantrue to enable writing of the red channel and false otherwise.
writeGreenBooleantrue to enable writing of the green channel and false otherwise.
writeBlueBooleantrue to enable writing of the blue channel and false otherwise.
writeAlphaBooleantrue to enable writing of the alpha channel and false otherwise.

setCullMode(cullMode)

Controls how triangles are culled based on their face direction. The default cull mode is pc.CULLFACE_BACK.

Parameters

cullModeNumberThe cull mode to set. Can be:
  • pc.CULLFACE_NONE
  • pc.CULLFACE_BACK
  • pc.CULLFACE_FRONT
  • pc.CULLFACE_FRONTANDBACK

setDepthFunc(func)

Configures the depth test.

Parameters

funcNumberA function to compare a new depth value with an existing z-buffer value and decide if to write a pixel. Can be:
  • pc.FUNC_NEVER: don't draw
  • pc.FUNC_LESS: draw if new depth < depth buffer
  • pc.FUNC_EQUAL: draw if new depth == depth buffer
  • pc.FUNC_LESSEQUAL: draw if new depth <= depth buffer
  • pc.FUNC_GREATER: draw if new depth > depth buffer
  • pc.FUNC_NOTEQUAL: draw if new depth != depth buffer
  • pc.FUNC_GREATEREQUAL: draw if new depth >= depth buffer
  • pc.FUNC_ALWAYS: always draw

setDepthTest(depthTest)

Enables or disables depth testing of fragments. Once this state is set, it persists until it is changed. By default, depth testing is enabled.

device.setDepthTest(true);

Parameters

depthTestBooleantrue to enable depth testing and false otherwise.

setDepthWrite(writeDepth)

Enables or disables writes to the depth buffer. Once this state is set, it persists until it is changed. By default, depth writes are enabled.

device.setDepthWrite(true);

Parameters

writeDepthBooleantrue to enable depth writing and false otherwise.

setIndexBuffer(indexBuffer)

Sets the current index buffer on the graphics device. On subsequent calls to pc.GraphicsDevice#draw, the specified index buffer will be used to provide index data for any indexed primitives.

Parameters

indexBufferpc.IndexBufferThe index buffer to assign to the device.

setRenderTarget(renderTarget)

Sets the specified render target on the device. If null is passed as a parameter, the back buffer becomes the current target for all rendering operations.

// Set a render target to receive all rendering output
device.setRenderTarget(renderTarget);

// Set the back buffer to receive all rendering output
device.setRenderTarget(null);

Parameters

renderTargetpc.RenderTargetThe render target to activate.

setScissor(x, y, w, h)

Set the active scissor rectangle on the specified device.

Parameters

xNumberThe pixel space x-coordinate of the bottom left corner of the scissor rectangle.
yNumberThe pixel space y-coordinate of the bottom left corner of the scissor rectangle.
wNumberThe width of the scissor rectangle in pixels.
hNumberThe height of the scissor rectangle in pixels.

setShader(shader)

Sets the active shader to be used during subsequent draw calls.

Parameters

shaderpc.ShaderThe shader to set to assign to the device.

Returns

Boolean true if the shader was successfully set, false otherwise.

setStencilFunc(func, ref, mask)

Configures stencil test for both front and back faces.

Parameters

funcNumberA comparison function that decides if the pixel should be written, based on the current stencil buffer value, reference value, and mask value. Can be:
  • pc.FUNC_NEVER: never pass
  • pc.FUNC_LESS: pass if (ref & mask) < (stencil & mask)
  • pc.FUNC_EQUAL: pass if (ref & mask) == (stencil & mask)
  • pc.FUNC_LESSEQUAL: pass if (ref & mask) <= (stencil & mask)
  • pc.FUNC_GREATER: pass if (ref & mask) > (stencil & mask)
  • pc.FUNC_NOTEQUAL: pass if (ref & mask) != (stencil & mask)
  • pc.FUNC_GREATEREQUAL: pass if (ref & mask) >= (stencil & mask)
  • pc.FUNC_ALWAYS: always pass
refNumberReference value used in comparison.
maskNumberMask applied to stencil buffer value and reference value before comparison.

setStencilFuncBack(func, ref, mask)

Configures stencil test for back faces.

Parameters

funcNumberA comparison function that decides if the pixel should be written, based on the current stencil buffer value, reference value, and mask value. Can be:
  • pc.FUNC_NEVER: never pass
  • pc.FUNC_LESS: pass if (ref & mask) < (stencil & mask)
  • pc.FUNC_EQUAL: pass if (ref & mask) == (stencil & mask)
  • pc.FUNC_LESSEQUAL: pass if (ref & mask) <= (stencil & mask)
  • pc.FUNC_GREATER: pass if (ref & mask) > (stencil & mask)
  • pc.FUNC_NOTEQUAL: pass if (ref & mask) != (stencil & mask)
  • pc.FUNC_GREATEREQUAL: pass if (ref & mask) >= (stencil & mask)
  • pc.FUNC_ALWAYS: always pass
refNumberReference value used in comparison.
maskNumberMask applied to stencil buffer value and reference value before comparison.

setStencilFuncFront(func, ref, mask)

Configures stencil test for front faces.

Parameters

funcNumberA comparison function that decides if the pixel should be written, based on the current stencil buffer value, reference value, and mask value. Can be:
  • pc.FUNC_NEVER: never pass
  • pc.FUNC_LESS: pass if (ref & mask) < (stencil & mask)
  • pc.FUNC_EQUAL: pass if (ref & mask) == (stencil & mask)
  • pc.FUNC_LESSEQUAL: pass if (ref & mask) <= (stencil & mask)
  • pc.FUNC_GREATER: pass if (ref & mask) > (stencil & mask)
  • pc.FUNC_NOTEQUAL: pass if (ref & mask) != (stencil & mask)
  • pc.FUNC_GREATEREQUAL: pass if (ref & mask) >= (stencil & mask)
  • pc.FUNC_ALWAYS: always pass
refNumberReference value used in comparison.
maskNumberMask applied to stencil buffer value and reference value before comparison.

setStencilOperation(fail, zfail, zpass, writeMask)

Configures how stencil buffer values should be modified based on the result of depth/stencil tests. Works for both front and back faces.

Parameters

failNumberAction to take if stencil test is failed
zfailNumberAction to take if depth test is failed
zpassNumberAction to take if both depth and stencil test are passed All arguments can be:
  • pc.STENCILOP_KEEP: don't change the stencil buffer value
  • pc.STENCILOP_ZERO: set value to zero
  • pc.STENCILOP_REPLACE: replace value with the reference value (see pc.GraphicsDevice#setStencilFunc)
  • pc.STENCILOP_INCREMENT: increment the value
  • pc.STENCILOP_INCREMENTWRAP: increment the value, but wrap it to zero when it's larger than a maximum representable value
  • pc.STENCILOP_DECREMENT: decrement the value
  • pc.STENCILOP_DECREMENTWRAP: decrement the value, but wrap it to a maximum representable value, if the current value is 0
  • pc.STENCILOP_INVERT: invert the value bitwise
writeMaskNumberA bit mask applied to the reference value, when written.

setStencilOperationBack(fail, zfail, zpass, writeMask)

Configures how stencil buffer values should be modified based on the result of depth/stencil tests. Works for back faces.

Parameters

failNumberAction to take if stencil test is failed
zfailNumberAction to take if depth test is failed
zpassNumberAction to take if both depth and stencil test are passed All arguments can be:
  • pc.STENCILOP_KEEP: don't change the stencil buffer value
  • pc.STENCILOP_ZERO: set value to zero
  • pc.STENCILOP_REPLACE: replace value with the reference value (see pc.GraphicsDevice#setStencilFunc)
  • pc.STENCILOP_INCREMENT: increment the value
  • pc.STENCILOP_INCREMENTWRAP: increment the value, but wrap it to zero when it's larger than a maximum representable value
  • pc.STENCILOP_DECREMENT: decrement the value
  • pc.STENCILOP_DECREMENTWRAP: decrement the value, but wrap it to a maximum representable value, if the current value is 0
  • pc.STENCILOP_INVERT: invert the value bitwise
writeMaskNumberA bit mask applied to the reference value, when written.

setStencilOperationFront(fail, zfail, zpass, writeMask)

Configures how stencil buffer values should be modified based on the result of depth/stencil tests. Works for front faces.

Parameters

failNumberAction to take if stencil test is failed
zfailNumberAction to take if depth test is failed
zpassNumberAction to take if both depth and stencil test are passed All arguments can be:
  • pc.STENCILOP_KEEP: don't change the stencil buffer value
  • pc.STENCILOP_ZERO: set value to zero
  • pc.STENCILOP_REPLACE: replace value with the reference value (see pc.GraphicsDevice#setStencilFunc)
  • pc.STENCILOP_INCREMENT: increment the value
  • pc.STENCILOP_INCREMENTWRAP: increment the value, but wrap it to zero when it's larger than a maximum representable value
  • pc.STENCILOP_DECREMENT: decrement the value
  • pc.STENCILOP_DECREMENTWRAP: decrement the value, but wrap it to a maximum representable value, if the current value is 0
  • pc.STENCILOP_INVERT: invert the value bitwise
writeMaskNumberA bit mask applied to the reference value, when written.

setStencilTest(enable)

Enables or disables stencil test.

Parameters

enableBooleanTrue to enable stencil test and false to disable it.

setVertexBuffer(vertexBuffer, stream, [vbOffset])

Sets the current vertex buffer for a specific stream index on the graphics device. On subsequent calls to pc.GraphicsDevice#draw, the specified vertex buffer will be used to provide vertex data for any primitives.

Parameters

vertexBufferpc.VertexBufferThe vertex buffer to assign to the device.
streamNumberThe stream index for the vertex buffer, indexed from 0 upwards.
vbOffsetNumberThe byte offset into the vertex buffer data. Defaults to 0.

setViewport(x, y, w, h)

Set the active rectangle for rendering on the specified device.

Parameters

xNumberThe pixel space x-coordinate of the bottom left corner of the viewport.
yNumberThe pixel space y-coordinate of the bottom left corner of the viewport.
wNumberThe width of the viewport in pixels.
hNumberThe height of the viewport in pixels.

updateBegin()

Marks the beginning of a block of rendering. Internally, this function binds the render target currently set on the device. This function should be matched with a call to pc.GraphicsDevice#updateEnd. Calls to pc.GraphicsDevice#updateBegin and pc.GraphicsDevice#updateEnd must not be nested.

updateEnd()

Marks the end of a block of rendering. This function should be called after a matching call to pc.GraphicsDevice#updateBegin. Calls to pc.GraphicsDevice#updateBegin and pc.GraphicsDevice#updateEnd must not be nested.

Events

resizecanvas

The 'resizecanvas' event is fired when the canvas is resized

Parameters

widthNumberThe new width of the canvas in pixels
heightNumberThe new height of the canvas in pixels

Inherited

Methods

fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])

Fire an event, all additional arguments are passed on to the event listener

obj.fire('test', 'This is the message');

Parameters

nameObjectName of event to fire
arg1*First argument that is passed to the event handler
arg2*Second argument that is passed to the event handler
arg3*Third argument that is passed to the event handler
arg4*Fourth argument that is passed to the event handler
arg5*Fifth argument that is passed to the event handler
arg6*Sixth argument that is passed to the event handler
arg7*Seventh argument that is passed to the event handler
arg8*Eighth argument that is passed to the event handler

Returns

pc.EventHandler Self for chaining.

hasEvent(name)

Test if there are any handlers bound to an event name

obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false

Parameters

nameStringThe name of the event to test

Returns

Boolean true if the object has handlers bound to the specified event name.

off([name], [callback], [scope])

Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

var handler = function () {
};
obj.on('test', handler);

obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this

Parameters

nameStringName of the event to unbind
callbackpc.callbacks.HandleEventFunction to be unbound.
scopeObjectScope that was used as the this when the event is fired

Returns

pc.EventHandler Self for chaining.

on(name, callback, [scope])

Attach an event handler to an event

obj.on('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console

Parameters

nameStringName of the event to bind the callback to
callbackpc.callbacks.HandleEventFunction that is called when event is fired. Note the callback is limited to 8 arguments.
scopeObjectObject to use as 'this' when the event is fired, defaults to current this

Returns

pc.EventHandler Self for chaining.

once(name, callback, [scope])

Attach an event handler to an event. This handler will be removed after being fired once.

obj.once('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled

Parameters

nameStringName of the event to bind the callback to
callbackpc.callbacks.HandleEventFunction that is called when event is fired. Note the callback is limited to 8 arguments.
scopeObjectObject to use as 'this' when the event is fired, defaults to current this

Returns

pc.EventHandler Self for chaining.