API Reference

Class List

pc.Application

Default application which performs general setup code and initiates the main game loop.

// Create application
var app = new pc.Application(canvas, options);
// Start game loop
app.start()

Summary

Properties

assetsThe assets available to the application.
autoRenderWhen true (the default) the application's render function is called every frame.
batcherThe Batch Manager of the Application
elementInputUsed to handle input for pc.ElementComponents.
gamepadsUsed to access GamePad input.
graphicsDeviceThe graphics device used by the application.
keyboardThe keyboard device.
loaderThe resource loader.
maxDeltaTimeClamps per-frame delta time to an upper bound.
mouseThe mouse device.
renderNextFrameIf pc.Application#autoRender is false, set `app.
rootThe root pc.Entity of the application.
sceneThe current pc.Scene
scriptsThe Script Registry of the Application
systemsThe component systems.
timeScaleScales the global time delta.
touchUsed to get touch events input.

Methods

configureLoad the application configuration file and apply application properties and fill the asset registry
destroyDestroys application and removes all event listeners.
disableFullscreenIf application is currently displaying an element as fullscreen, then stop and return to normal.
enableFullscreenRequest that the browser enters fullscreen mode.
getSceneUrlLook up the URL of the scene hierarchy file via the name given to the scene in the editor.
isFullscreenReturns true if the application is currently running fullscreen
isHiddenQueries the visibility of the window or tab in which the application is running.
loadSceneHierarchyLoad a scene file, create and initialize the Entity hierarchy and add the hierarchy to the application root Entity.
loadSceneSettingsLoad a scene file and apply the scene settings to the current scene
preloadLoad all assets in the asset registry that are marked as 'preload'
renderApplication specific render method.
renderLineRenders a line.
renderLinesDraw an array of lines.
resizeCanvasResize the canvas in line with the current FillMode In KEEP_ASPECT mode, the canvas will grow to fill the window as best it can while maintaining the aspect ratio In FILL_WINDOW mode, the canvas will simply fill the window, changing aspect ratio In NONE mode, the canvas will always match the size provided
setCanvasFillModeControls how the canvas fills the window and resizes when the window changes.
setCanvasResolutionChange the resolution of the canvas, and set the way it behaves when the window is resized
setSkyboxSets the skybox asset to current scene, and subscribes to asset load/change events
startStart the Application updating
updateApplication specific update method.

Details

Constructor

Application(canvas, options)

Create a new Application.

// Create application
var app = new pc.Application(canvas, options);
// Start game loop
app.start()

Parameters

canvasElementThe canvas element
optionsObject
options.keyboardpc.KeyboardKeyboard handler for input
options.mousepc.MouseMouse handler for input
options.touchpc.TouchDeviceTouchDevice handler for input
options.gamepadspc.GamePadsGamepad handler for input
options.scriptPrefixStringPrefix to apply to script urls before loading
options.assetPrefixStringPrefix to apply to asset urls before loading
options.graphicsDeviceOptionsObjectOptions object that is passed into the pc.GraphicsDevice constructor

Properties

pc.AssetRegistryassets

The assets available to the application.

BooleanautoRender

When true (the default) the application's render function is called every frame.

pc.BatchManagerbatcher

The Batch Manager of the Application

pc.ElementInputelementInput

Used to handle input for pc.ElementComponents.

pc.GamePadsgamepads

Used to access GamePad input.

pc.GraphicsDevicegraphicsDevice

The graphics device used by the application.

pc.Keyboardkeyboard

The keyboard device.

pc.ResourceLoaderloader

The resource loader.

NumbermaxDeltaTime

Clamps per-frame delta time to an upper bound. Useful since returning from a tab deactivation can generate huge values for dt, which can adversely affect game state. Defaults to 0.1 (seconds).

pc.Mousemouse

The mouse device.

BooleanrenderNextFrame

If pc.Application#autoRender is false, set `app.renderNextFrame` true to force application to render the scene once next frame.

// render the scene only while space key is pressed
if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
   this.app.renderNextFrame = true;
}
pc.Entityroot

The root pc.Entity of the application.

pc.Scenescene

The current pc.Scene

pc.ScriptRegistryscripts

The Script Registry of the Application

pc.ComponentSystemRegistrysystems

The component systems.

NumbertimeScale

Scales the global time delta.

pc.TouchDevicetouch

Used to get touch events input.

Methods

configure(url, callback)

Load the application configuration file and apply application properties and fill the asset registry

Parameters

urlStringThe URL of the configuration file to load
callbackfunctionThe Function called when the configuration file is loaded and parsed

destroy()

Destroys application and removes all event listeners.

disableFullscreen([success])

If application is currently displaying an element as fullscreen, then stop and return to normal.

Parameters

successfunctionFunction called when transition to normal mode is finished

enableFullscreen([element], [success], [error])

Request that the browser enters fullscreen mode. This is not available on all browsers. Note: Switching to fullscreen can only be initiated by a user action, e.g. in the event hander for a mouse or keyboard input

var button = document.getElementById('my-button');
button.addEventListener('click', function () {
    app.enableFullscreen(canvas, function () {
        console.log('Now fullscreen');
    }, function () {
        console.log('Something went wrong!');
    });
}, false);

Parameters

elementElementThe element to display in fullscreen, if element is not provided the application canvas is used
successfunctionFunction called if the request for fullscreen was successful
errorfunctionFunction called if the request for fullscreen was unsuccessful

getSceneUrl(name)

Look up the URL of the scene hierarchy file via the name given to the scene in the editor. Use this to in pc.Application#loadSceneHierarchy.

Parameters

nameStringThe name of the scene file given in the Editor

Returns

String The URL of the scene file

isFullscreen()

Returns true if the application is currently running fullscreen

Returns

Boolean True if the application is running fullscreen

isHidden()

Queries the visibility of the window or tab in which the application is running.

Returns

Boolean True if the application is not visible and false otherwise.

loadSceneHierarchy(url, callback)

Load a scene file, create and initialize the Entity hierarchy and add the hierarchy to the application root Entity.

app.loadSceneHierarchy("1000.json", function (err, entity) {
    if (!err) {
      var e = app.root.find("My New Entity");
    } else {
      // error
    }
  }
});

Parameters

urlStringThe URL of the scene file. Usually this will be "scene_id.json"
callbackfunctionThe function to call after loading, passed (err, entity) where err is null if no errors occurred.

loadSceneSettings(url, callback)

Load a scene file and apply the scene settings to the current scene

app.loadSceneSettings("1000.json", function (err) {
    if (!err) {
      // success
    } else {
      // error
    }
  }
});

Parameters

urlStringThe URL of the scene file. Usually this will be "scene_id.json"
callbackfunctionThe function called after the settings are applied. Passed (err) where err is null if no error occurred.

preload(callback)

Load all assets in the asset registry that are marked as 'preload'

Parameters

callbackfunctionFunction called when all assets are loaded

render()

Application specific render method. Override this if you have a custom Application

renderLine(start, end, color, [endColor], [options])

Renders a line. Line start and end coordinates are specified in world-space. If a single color is supplied, the line will be flat-shaded with that color. If two colors are supplied, the line will be smooth shaded between those colors. It is also possible to control which scene layer the line is rendered into. By default, lines are rendered into the immediate layer pc.LAYERID_IMMEDIATE.

// Render a 1-unit long white line
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var color = new pc.Color(1, 1, 1);
app.renderLine(start, end, color);
// Render a 1-unit long line that is smooth-shaded from white to red
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var startColor = new pc.Color(1, 1, 1);
var endColor = new pc.Color(1, 0, 0);
app.renderLine(start, end, startColor, endColor);
// Render a 1-unit long white line into the world layer
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var color = new pc.Color(1, 1, 1);
var worldLayer = app.scene.layers.getLayerById(pc.LAYERID_WORLD);
app.renderLine(start, end, color, {
    layer: worldLayer
});
// Render a 1-unit long line that is smooth-shaded from white to red into the world layer
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var startColor = new pc.Color(1, 1, 1);
var endColor = new pc.Color(1, 0, 0);
var worldLayer = app.scene.layers.getLayerById(pc.LAYERID_WORLD);
app.renderLine(start, end, color, {
    layer: worldLayer
});

Parameters

startpc.Vec3The start world-space coordinate of the line.
endpc.Vec3The end world-space coordinate of the line.
colorpc.ColorThe start color of the line.
endColorpc.ColorThe end color of the line.
optionsObjectOptions to set rendering properties
options.layerpc.LayerThe layer to render the line into. Defaults to pc.LAYERID_IMMEDIATE.

renderLines(position, color, [options])

Draw an array of lines.

var points = [new pc.Vec3(0,0,0), new pc.Vec3(1,0,0), new pc.Vec3(1,1,0), new pc.Vec3(1,1,1)];
var colors = [new pc.Color(1,0,0), new pc.Color(1,1,0), new pc.Color(0,1,1), new pc.Color(0,0,1)];
app.renderLines(points, colors);

Parameters

positionpc.Vec3[]An array of points to draw lines between
colorpc.Color[]An array of colors to color the lines. This must be the same size as the position array
optionsObjectOptions to set rendering properties
options.layerpc.LayerThe layer to render the line into

resizeCanvas([width], [height])

Resize the canvas in line with the current FillMode In KEEP_ASPECT mode, the canvas will grow to fill the window as best it can while maintaining the aspect ratio In FILL_WINDOW mode, the canvas will simply fill the window, changing aspect ratio In NONE mode, the canvas will always match the size provided

Parameters

widthNumberThe width of the canvas, only used in NONE mode
heightNumberThe height of the canvas, only used in NONE mode

Returns

Object A object containing the values calculated to use as width and height

setCanvasFillMode(mode, [width], [height])

Controls how the canvas fills the window and resizes when the window changes.

Parameters

modeStringThe mode to use when setting the size of the canvas. Can be:
  • pc.FILLMODE_NONE: the canvas will always match the size provided.
  • pc.FILLMODE_FILL_WINDOW: the canvas will simply fill the window, changing aspect ratio.
  • pc.FILLMODE_KEEP_ASPECT: the canvas will grow to fill the window as best it can while maintaining the aspect ratio.
widthNumberThe width of the canvas (only used when mode is pc.FILLMODE_NONE).
heightNumberThe height of the canvas (only used when mode is pc.FILLMODE_NONE).

setCanvasResolution(mode, [width], [height])

Change the resolution of the canvas, and set the way it behaves when the window is resized

Parameters

modeStringThe mode to use when setting the resolution. Can be:
  • pc.RESOLUTION_AUTO: if width and height are not provided, canvas will be resized to match canvas client size.
  • pc.RESOLUTION_FIXED: resolution of canvas will be fixed.
widthNumberThe horizontal resolution, optional in AUTO mode, if not provided canvas clientWidth is used
heightNumberThe vertical resolution, optional in AUTO mode, if not provided canvas clientHeight is used

setSkybox(asset)

Sets the skybox asset to current scene, and subscribes to asset load/change events

Parameters

assetpc.AssetAsset of type `skybox` to be set to, or null to remove skybox

start()

Start the Application updating

update(dt)

Application specific update method. Override this if you have a custom Application

Parameters

dtNumberThe time delta since the last frame.