Table Of Contents

Previous topic

24. Protolib API

Next topic

25. Tools

This Page

24.1. The Protolib Object

Added

2013-05-02 - 0.1.0 - First BETA release (Open Source Only)

Updated

2013-05-21 - 0.1.1 - Turbulenz Webcast Build (Open Source Only)

2013-05-04 - 0.2.0 - First SDK release (SDK 0.26.0)

2014-01-29 - 0.2.1 - Behavior change to 2D text rendering sequence and addition/modification of advanced callbacks (SDK 0.28.0)

BETA

This feature is currently in beta and does not represent the final set of available APIs.

24.1.1. Introduction

Protolib is library/framework providing simple graphics, input and sound for prototyping games. It provides a subset of the complete Turbulenz Engine features with simple interfaces for:

24.1.1.1. Using Protolib

To use Protolib you must create an instance using:

var protolibConfig = {
    onIntialized: function onInitializedFn(protolib)
    {
        // Protolib has loaded requirements
    }
};

var protolib = Protolib.create(protolibConfig);

You can then use functions provided by Protolib to load any files and set any configuration required before rendering:

protolib.setClearColor(bgColor);
protolib.setAmbientLightColor(ambientColor);

var mesh = protolib.loadMesh({
    mesh: "models/mymesh.dae",
    v3Position: meshPosition
});

var pointLight = protolib.addPointLight({
    v3Position: lightPos,
    radius: 300,
    v3Color: color
});

Some functions will provide a wrapper to the object type you created. The wrapper is your reference to the instance of the type e.g. mesh, point light, spot light, sound. When the resource is available, the library will render/play them automatically.

To update properties of the library during a frame you should make calls between the beginFrame, endFrame functions:

if (protolib.beginFrame())
{
    pointLight.setPosition(newLightPosition);

    protolib.draw3DLine({
        pos1: startPos,
        pos2: endPos,
        v3Color: lineColor
    });

    if (protolib.isKeyDown(protolib.keyCodes.UP))
    {
        protolib.moveCamera(cameraUpVector);
    }

    protolib.drawText({
        text: "Hello World!",
        position: textScreenPosition
    });

    protolib.endFrame();
}

The state of protolib will be updated by the functions called between these functions, then any rendering required will be done at the end of the frame. Functions like draw2DSprite will efficiently batch the function calls together drawing them together.

24.1.1.2. Library Files

Protolib is made up of multiple files and all these files should be included for the library to work:

protolib:The core protolib file. Creation, destruction and configuration.
simplesprite:The rendering for the 3D sprites.
simplefonts:The rendering for the gui text.
sceneloader:The loading utility for scenes, used by simplesceneloader.
simplesceneloader:
 The module for loading and managing meshes.
debugdraw:The debug drawing utility for lines, etc.
soundsourcemanager:
 The manager for handling sound sources.
jqueryextend:A minimal subset of jquery providing the extend functionality.
duimanager:The game code side component of the dynamic UI.

24.1.1.3. Requirements

To use protolib you will need to include the following library code in your template file:

/*{{ javascript('jslib/aabbtree.js') }}*/
/*{{ javascript('jslib/assettracker.js') }}*/
/*{{ javascript('jslib/camera.js') }}*/
/*{{ javascript('jslib/draw2d.js') }}*/
/*{{ javascript('jslib/effectmanager.js') }}*/
/*{{ javascript('jslib/fontmanager.js') }}*/
/*{{ javascript('jslib/forwardrendering.js') }}*/
/*{{ javascript('jslib/geometry.js') }}*/
/*{{ javascript('jslib/indexbuffermanager.js') }}*/
/*{{ javascript('jslib/light.js') }}*/
/*{{ javascript('jslib/loadingscreen.js') }}*/
/*{{ javascript('jslib/material.js') }}*/
/*{{ javascript('jslib/observer.js') }}*/
/*{{ javascript('jslib/renderingcommon.js') }}*/
/*{{ javascript('jslib/requesthandler.js') }}*/
/*{{ javascript('jslib/resourceloader.js') }}*/
/*{{ javascript('jslib/scene.js') }}*/
/*{{ javascript('jslib/scenenode.js') }}*/
/*{{ javascript('jslib/shadermanager.js') }}*/
/*{{ javascript('jslib/shadowmapping.js') }}*/
/*{{ javascript('jslib/soundmanager.js') }}*/
/*{{ javascript('jslib/texturemanager.js') }}*/
/*{{ javascript('jslib/utilities.js') }}*/
/*{{ javascript('jslib/vertexbuffermanager.js') }}*/
/*{{ javascript('jslib/vmath.js') }}*/

/*{{ javascript('jslib/services/gamesession.js') }}*/
/*{{ javascript('jslib/services/mappingtable.js') }}*/
/*{{ javascript('jslib/services/turbulenzbridge.js') }}*/
/*{{ javascript('jslib/services/turbulenzservices.js') }}*/

/*{{ javascript('protolib/debugdraw.js') }}*/
/*{{ javascript('protolib/duimanager.js') }}*/
/*{{ javascript('protolib/jqueryextend.js') }}*/
/*{{ javascript('protolib/protolib.js') }}*/
/*{{ javascript('protolib/sceneloader.js') }}*/
/*{{ javascript('protolib/simplefonts.js') }}*/
/*{{ javascript('protolib/simplesceneloader.js') }}*/
/*{{ javascript('protolib/simplesprite.js') }}*/
/*{{ javascript('protolib/soundsourcemanager.js') }}*/

And the following assets in your mapping_table.json:

- shaders/debug.cgfx
- shaders/shadowmapping.cgfx
- shaders/zonly.cgfx
- shaders/font.cgfx
- shaders/forwardrendering.cgfx
- shaders/forwardrenderingshadows.cgfx
- shaders/simplesprite.cgfx
- textures/default_light.png
- textures/opensans-8_0.png
- textures/opensans-16_0.png
- textures/opensans-32_0.png
- textures/opensans-64_0.png
- textures/opensans-128_0.png
- fonts/opensans-8.fnt
- fonts/opensans-16.fnt
- fonts/opensans-32.fnt
- fonts/opensans-64.fnt
- fonts/opensans-128.fnt

Updated 0.1.1 - Now includes jslib/assettracker.js, jslib/loadingscreen.js and shaders/simplesprite.cgfx

24.1.2. Properties

Added 0.2.0

24.1.2.1. version

Summary

Property containing the version number for the Protolib API. Specified as an 3-dimensional array of integer numbers in the following format:

[ MAJOR, MINOR, REVISION ]

This can be used to determine the expected behaviour of the API arguments.

Syntax

var version = protolib.version;
if (version[0] < 1 && version[1] < 2)
{
    // Version 1.2 required
    console.log("WARNING: Protolib version is incorrect");
}

Note

This property is not available version 0.1.X so to test for versions < 0.2, check if the property is undefined.

24.1.3. Constructor

24.1.3.1. create

Summary

Creates a protolib object.

Syntax

var that = this;

var onInitialized = function onInitializedFn()
{
    that.initGame();
    TurbulenzEngine.setInterval(function()
    {
        that.gameLoop();
    },
    1000/60);
};

var config =
{
    onInitialized: onInitialized,
    useShadows: true,
    maxSoundSources: 50,
    disableSound: false,
    fonts: {
        regular: "opensans"
    },
    defaultMappingSettings: {
        mappingTablePrefix: 'staticmax/',
        assetPrefix: 'missing/',
        mappingTableURL: 'mapping_table.json',
        urnMapping: {}
    },
    enableDynamicUI: false
};

var protolib = Protolib.create(config);
onInitialized
A callback function that is run when Protolib has finished initializing.
useShadows (Optional)
Determines whether the renderer should calculate shadows. Defaults to true.
maxSoundSources (Optional)
The number of sound sources to create. Determines the maximum number of sounds that can play simultaneously. Defaults to 50.
disableSound (Optional)
A boolean that determines whether to provide sound functionality. Calls to playSound will return null. Defaults to false.
fonts (Optional)

An object containing the fonts to load for use with the drawText function in the format:

{
    FONTSTYLE: "FONTNAME"
}

Protolib will attempt to load the following variations of that font: 8, 16, 32, 64, 128 pixel height. If you want to use your own font you will need to provide the following files accessible from the mapping table:

  • fonts/FONTNAME-8.fnt
  • fonts/FONTNAME-16.fnt
  • fonts/FONTNAME-32.fnt
  • fonts/FONTNAME-64.fnt
  • fonts/FONTNAME-128.fnt
  • textures/FONTNAME-8_0.png
  • textures/FONTNAME-16_0.png
  • textures/FONTNAME-32_0.png
  • textures/FONTNAME-64_0.png
  • textures/FONTNAME-128_0.png

If a font is not available or is missing a required pixel height, it will default to “opensans” 16 pixels, then the default Turbulenz font. Defaults to

{
    regular: "opensans"
}
defaultMappingSettings (Optional)

An object specifying the default mapping table settings to use. If a mapping table cannot be found, Protolib will attempt to use the mapping provided by urnMapping. See createMappingTable for more details on defaultMappingSettings. Defaults to

{
    mappingTablePrefix: "staticmax/",
    assetPrefix: "missing/",
    mappingTableURL: "mapping_table.json",
    urnMapping: {}
}
enableDynamicUI (Optional)
Initialises the dynamic UI module. Required to use the addWatchVariable. Defaults to false.

24.1.4. Game Loop

24.1.4.1. beginFrame

Summary

Signals the beginning of a new render frame.

This can fail if the host window is not visible, e.g. the browser is minimized or the window is not on the active tab.

Syntax

if (protolib.beginFrame())
{
    drawScene();

    protolib.endFrame();
}

24.1.4.2. endFrame

Summary

Signals the end of the current render frame.

Syntax

if (protolib.beginFrame())
{
    drawScene();

    protolib.endFrame();
}

24.1.5. Configuration

24.1.5.1. setClearColor

Sets the buffer clear color.

Syntax

protolib.setClearColor(v3Color);
color
A Vector3 object specifying the r, g, b color components. The components are in the range [0, 1].

24.1.5.2. getClearColor

Gets the current buffer clear color.

Syntax

protolib.getClearColor(v3Color);
v3Color
A Vector3 object the r, g, b color components will be written into.

24.1.6. Devices

Protolib creates several device objects on creation. These getter methods provide access to them.

24.1.6.1. getMathDevice

Syntax

var md = protolib.getMathDevice();

24.1.6.2. getInputDevice

Syntax

var id = protolib.getInputDevice();

24.1.6.3. getGraphicsDevice

Syntax

var gd = protolib.getGraphicsDevice();

24.1.6.4. getSoundDevice

Syntax

var sd = protolib.getSoundDevice();

24.1.7. Camera

24.1.7.1. setCameraPosition

Syntax

var cameraPosition = md.v3Build(5, 10, 15);
protolib.setCameraPosition(cameraPosition);
cameraPosition
A Vector3 object representing the 3D position of the camera.

24.1.7.2. getCameraPosition

Syntax

var cameraPosition = md.v3Build(0, 0, 0);
protolib.getCameraPosition(v3Position);
cameraPosition
A Vector3 object the x, y, z position components will be written into.

24.1.7.3. setCameraDirection

Syntax

var cameraDirection = md.v3Build(0, 0, -1);
protolib.setCameraDirection(cameraDirection);
cameraDirection
A Vector3 object representing the direction the camera should face.

24.1.7.4. getCameraDirection

Syntax

var cameraDirection = md.v3Build(0, 0, -1);
protolib.setCameraDirection(cameraDirection);
cameraDirection
A Vector3 object the x, y, z direction components will be written into.

24.1.7.5. getCameraUp

Summary

Gives the current up vector of the camera.

Syntax

var cameraUp = md.v3Build(0, 0, 0);
protolib.getCameraUp(cameraUp);
cameraUp
A Vector3 object the x, y, z direction components will be written into.

24.1.7.6. getCameraRight

Summary

Gives the current right vector of the camera.

Syntax

var cameraRight = md.v3Build(0, 0, 0);
protolib.getCameraRight(cameraRight);
cameraRight
A Vector3 object the x, y, z direction components will be written into.

24.1.7.7. moveCamera

Summary

Moves the camera relative to its current position.

Syntax

var translateVec = md.v3Build(5, 5, 5);
protolib.moveCamera(translateVec);
translateVec
A Vector3 object specifying the position translation to apply.

24.1.7.8. rotateCamera

Summary

Rotates the camera relative to its current orientation.

Syntax

protolib.rotateCamera(yawDelta, pitchDelta);
yawDelta
The angle in radians to rotate the camera around the unit y vector.
pitchDelta
The angle in radians to rotate the camera up and down.

24.1.7.9. setCameraFOV

Syntax

protolib.setCameraFOV(fovX, fovY);
fovX
The horizontal field of view in radians.
fovY
The vertical field of view in radians.

24.1.7.10. getCameraFOV

Syntax

var cameraFov = protolib.getCameraFOV();
var fovX = cameraFov[0];
var fovY = cameraFov[1];

Returns a JavaScript array of length 2, containing the horizontal and vertical field of view angle in radians.

24.1.7.11. setNearFarPlanes

Summary

Sets the near and far plane distances.

Syntax

var nearPlane = 5;
var farPlane = 1000;
protolib.setNearFarPlanes(nearPlane, farPlane);
nearPlane, farPlane
JavaScript numbers representing the distance in front of the camera where the near and far clipping planes are located.

24.1.7.12. getNearFarPlanes

Summary

Gets the near and far plane distances.

Syntax

var nearFarPlanes = protolib.getNearFarPlanes();
var nearPlane = nearFarPlanes[0];
var farPlane = nearFarPlanes[1];

Returns a JavaScript array of length 2 with the near and far plane distances respectively.

24.1.8. 2D

24.1.8.1. draw2DSprite

Summary

Draws the given texture to screen space.

Note

Only power-of-two textures are supported.

Syntax

protolib.draw2DSprite({
    texture: "path/to/texture.png",
    position: [x, y],
    width: w,
    height: h,
    v3Color: color,
    alpha: a,
    rotation: angle
});
texture
The path to the texture to be loaded.
position
A JavaScript array of length 2 representing the coordinates of the top-left pixel of the texture.
width, height
A JavaScript number.
v3Color (Optional)
A Vector3 object specifying the r, g, b color components. The components are in the range [0, 1]. Defaults to white.
alpha (Optional)
A JavaScript number in the range [0, 1] specifying the alpha of the texture. Defaults to 1.
rotation (Optional)
The clockwise angle in radians to rotate the texture around its centre. Defaults to 0.
blendStyle (Optional)
A value in protolib.blendStyles. Defaults to protolib.blendStyles.ALPHA.

24.1.8.2. drawText

Summary

Draws the given text to screen space.

Updated 0.2.1

drawText renderers after draw2DSprite.

Syntax

protolib.drawText({
    text: "Hello World!",
    position: [x, y],
    v3Color: color,
    scale: 2,
    horizontalAlign: protolib.textHorizontalAlign.CENTER,
    verticalAlign: protolib.textVerticalAlign.MIDDLE
});
text
The text to draw to the screen.
position
A JavaScript array of length 2 representing the position to draw the text to, relative to the alignment option chosen.
v3Color (Optional)
A Vector3 object specifying the r, g, b color components. The components are in the range [0, 1]. Defaults to red.
scale (Optional)
A JavaScript number specifying the amount to scale the text by. Defaults to 1.

Added 0.2.0

horizontalAlign (Optional)
A value in protolib.textHorizontalAlign. Defines whether the position given refers to the LEFT, CENTER or RIGHT of the text box. Use in conjunction with verticalAlign. Defaults to protolib.textHorizontalAlign.CENTER.
verticalAlign (Optional)
A value in protolib.textVerticalAlign. Defines whether the position given refers to the TOP, MIDDLE or BOTTOM of the text box. Use in conjunction with horizontalAlign. Defaults to protolib.textVerticalAlign.MIDDLE.

Depricated 0.2.0

alignment (Optional)
Use horizontalAlign and verticalAlign instead. A value in protolib.textAlignment. Defines whether the position given refers to the top-left, top-middle or top-right of the text box. Defaults to protolib.textAlignment.LEFT.

24.1.9. 3D

24.1.9.1. draw3DSprite

Summary

Draws a 3D Sprite.

Note

Only power-of-two textures are supported.

Syntax

protolib.draw3DSprite({
    texture: "path/to/texture.png",
    v3Position  : spritePos,
    size : params.size,
    alpha : 0.5,
    v3Color : color,
    v3Out : params.v3Out,
    rotation: Math.PI/4,
    blendStyle : params.blendStyle
});
texture
The path to the texture to be loaded.
v3Position
A Vector3 object specifying the position of the sprite.
size
A JavaScript number specifying the size of the sprite.
alpha (Optional)
A JavaScript number in the range [0, 1] specifying the transparency of the sprite. Used when the blendStyle is set to protolib.blendStyles.ALPHA. Defaults to 1.
v3Color (Optional)
A Vector3 object specifying the r, g, b of the color to apply to the sprite. The components are in the range [0, 1]. Defaults to white.
v3Out (Optional)
A Vector3 object specifying the normal of the surface of the sprite. If no vector is provided, the sprite is drawn with the normal always facing towards the camera.
rotation (Optional)
The clockwise angle in radians to rotate the sprite around the normal vector. Defaults to 0.
blendStyle (Optional)
A value in protolib.blendStyles. Defaults to protolib.blendStyles.ALPHA.

24.1.9.2. loadMesh

Summary

Loads a 3D mesh and adds it to the scene. Returns a MeshWrapper object to control the loaded mesh.

Syntax

var treeMesh = protolib.loadMesh({
    mesh: "path/to/mesh.dae",
    v3Position: treePos,
    v3Size: treeSize
});
mesh
The path to the mesh file.
v3Position
A Vector3 object specifying the position of the mesh.
v3Size
A Vector3 object specifying the amount to scale the mesh by in the x, y, and z directions.

Returns a MeshWrapper object to control the loaded mesh.

24.1.9.3. draw3DLine

Summary

Draws a line between two end-points in 3d space.

Syntax

protolib.draw3DLine({
    pos1: p1,
    pos2: p2,
    v3Color : color
});
pos1, pos2
A Vector3 object specifying the start and end points of the line.
v3Color (Optional)
A Vector3 object specifying the r, g, b color components of the line. The components are in the range [0, 1]. Defaults to red.

24.1.9.4. drawDebugSphere

Summary

Draws 3 circles in world space representing a sphere.

Syntax

protolib.drawDebugSphere({
    v3Position: spherePos,
    radius: 10,
    v3Color: color
});
v3Position
A Vector3 object specifying the centre of the sphere.
radius
A JavaScript number defining the radius of the sphere.
v3Color
A Vector3 object specifying the r, g, b color components of the circles making up the sphere. The components are in the range [0, 1]. Defaults to red.

24.1.9.5. drawDebugCube

Summary

Draws a wireframe cube.

Syntax

protolib.drawDebugCube({
    v3Position: cubePos,
    length: 10,
    v3Color: color
});
v3Position
A Vector3 object specifying the centre of the cube.
length
A JavaScript number defining the length of an edge on the cube.
v3Color
A Vector3 object specifying the r, g, b color components of the lines making up the cube. The components are in the range [0, 1]. Defaults to red.

24.1.10. Lights

24.1.10.1. setAmbientLightColor

Summary

Sets the ambient light color.

Syntax

protolib.setAmbientLightColor(ambientColor);
ambientColor
A Vector3 object specifying the r, g, b color components to set the ambient light. The components are in the range [0, 1].

24.1.10.2. getAmbientLightColor

Summary

Gets the current ambient light color.

Syntax

protolib.getAmbientLightColor(ambientColor);
ambientColor
A Vector3 object the ambient color will be written into.

24.1.10.3. addPointLight

Summary

Adds a point light to the scene. Returns a PointLightWrapper to control the light.

Syntax

var pointLight = protolib.addPointLight({
    v3Position: lightPos,
    radius: 300,
    v3Color: color
});
v3Position
A Vector3 object specifying the position of light.
radius
A JavaScript number specifying the range of the light.
v3Color
A Vector3 object specifying the r, g, b color components to set the light. The components are in the range [0, 1].

Returns a PointLightWrapper to control the light.

24.1.10.4. addSpotLight

Summary

Adds a spotlight to the scene. Returns a SpotLightWrapper to control the light.

Syntax

var spotLight = protolib.addSpotLight({
    v3Position: lightPos,
    v3Direction: lightDir,
    range: 300,
    spreadAngle: Math.PI/2,
    v3Color: color
});
v3Position
A Vector3 object specifying the position of light.
range
The range of the light.
spreadAngle
The spread angle in radians of the spot light.
v3Color
A Vector3 object specifying the r, g, b color components to set the light. The components are in the range [0, 1].

Returns a SpotLightWrapper to control the light.

24.1.11. Sounds

24.1.11.1. playSound

Summary

Plays the given sound. Returns a SoundWrapper object used to control playback of the sound.

Note

For the 3D positional audio to work, a mono sound must be used.

Syntax

var sound = protolib.playSound({
     sound : "path/to/sound.mp3",
     volume : 2,
     pitch : 1,
     looping : true,
     v3Position : soundPos,
     minDistance : 10,
     maxDistance : 300,
     rollOff : 0.9
});
sound
The path to the sound to be loaded.
volume (Optional)
The volume amplification to be applied to the sound. Defaults to 1.
pitch (Optional)
The pitch to be applied to the sound. Defaults to 1.
looping (Optional)
A boolean specifying whether to loop the sound or not. Defaults to false.
v3Position (Optional)
A Vector3 object specifying the position of sound. Defaults to (0, 0, 0).
minDistance (Optional)
If the distance between the camera and the sound position is less than this, the sound plays at full volume with no attenuation. Defaults to 1.
maxDistance (Optional)
The maximum distance to the listener after which the attenuation will set the volume to zero. Defaults to Infinity.
rollOff (Optional)

The ratio that the sound will drop off as by the inverse square law of the distance to the listener.

A number in the range [0, 1].

0 results in no attenuation. 1 results in the volume being determined fully by attenuation.

Defaults to 1.

background (Optional)

A boolean indicating the sound to be played is a background sound. If set to true, the v3Position, minDistance, maxDistance and rollOff properties should not be set. Stereo sounds are supported as background sounds.

Defaults to false

var bgsound = protolib.playSound({
     sound : "path/to/sound.mp3",
     background : true,
     volume : 2,
     pitch : 1,
     looping : true
});

Returns a SoundWrapper object used to control the playback of the sound.

24.1.12. Keyboard State

24.1.12.1. isKeyDown

Summary

Returns true if the given key is currently pressed.

Syntax

protolib.isKeyDown(keyCode);
keyCode
A value from protolib.keyCodes.

24.1.12.2. isKeyJustDown

Summary

Returns true if the given key was pressed between the previous and the current frame.

Syntax

protolib.isKeyJustDown(keyCode);
keyCode
A value from protolib.keyCodes.

24.1.12.3. isKeyJustUp

Summary

Returns true if the given key was released between the previous and the current frame.

Syntax

protolib.isKeyJustUp(keyCode);
keyCode
A value from protolib.keyCodes.

24.1.13. Mouse State

24.1.13.1. isMouseDown

Summary

Returns true if the given mouse button is currently pressed.

Syntax

var isMouseDown = protolib.isMouseDown(mouseCode);
mouseCode
A value from protolib.mouseCodes.

24.1.13.2. isMouseJustDown

Summary

Returns true if the given mouse button was pressed between the previous and the current frame.

Syntax

var isMouseJustDown = protolib.isMouseJustDown(mouseCode);
mouseCode
A value from protolib.mouseCodes.

24.1.13.3. isMouseJustUp

Summary

Returns true if the given mouse button was released between the previous and the current frame.

Syntax

var isMouseJustUp = protolib.isMouseJustUp(mouseCode);
mouseCode
A value from protolib.mouseCodes.

24.1.13.4. isMouseOnGame

Syntax

var isMouseOnGame = protolib.isMouseOnGame();

Returns true if the mouse is currently over the game canvas.

24.1.13.5. getMousePosition

Syntax

var mousePos = protolib.getMousePosition();
var mouseX = mousePos[0];
var mouseY = mousePos[1];

Returns an array of length 2 giving the coordinates of the mouse.

When the mouse is locked, the mouse position is handled by getMouseDelta instead.

24.1.13.6. getMouseDelta

Syntax

var mouseDelta = protolib.getMouseDelta();
var dx = mouseDelta[0];
var dy = mouseDelta[1];

Returns an array of length 2 giving the difference in position between the previous and current frame.

24.1.13.7. getMouseWheelDelta

Syntax

var mouseWheelDelta = protolib.getMouseWheelDelta();

Returns a number representing the number of mouse wheel scrolls made between the previous and current frame.

24.1.14. Enums

24.1.14.1. keyCodes

This is equal to inputDevice.keyCodes.

24.1.14.2. mouseCodes

This is equal to inputDevice.mouseCodes.

24.1.14.3. textHorizontalAlign

Added 0.2.0

A dictionary with values used to specify horizontal text alignment, for use with the drawText function.

Values are: LEFT, CENTER, RIGHT.

24.1.14.4. textVerticalAlign

Added 0.2.0

A dictionary with values used to specify vertical text alignment, for use with the drawText function.

Values are: TOP, MIDDLE, BOTTOM.

24.1.14.5. blendStyles

A dictionary specifying the possible blend modes, used by draw2DSprite and draw3DSprite.

Values are: ALPHA, ADDITIVE.

24.1.14.6. watchTypes

A dictionary specifying the possible watch types, used by addWatchVariable and removeWatchVariable.

Values are: SLIDER.

Options are:

SLIDER

{
    min: number,    // The minimum value of the slider.
    max: number,    // The maximum value of the slider.
    step: number    // The amount to move the slider by when dragging.
}

24.1.14.7. textAlignment

Depricated 0.2.0 - Use textHorizontalAlign instead.

A dictionary with values used to specify text alignment, for use with the drawText function.

Values are: LEFT, CENTER, RIGHT.

24.1.15. Variable Watch

Allows variables to be exposed and manipulated by the hosting page. The variable uses the “Dynamic User Interface” module to expose the variable. To manipulate/view the result, the page must include the “duiserver.js” and the associated “dynamicui.css”. If these are not available, the variable will not be controllable. For an example of this functionality in use, see the apps in the SDK.

24.1.15.1. addWatchVariable

Summary

Adds a watchable variable to the page that contains the Protolib app. Note: Requires enableDynamicUI to be true as a parameter to the create function.

Syntax

var variableOwner = {
    variableName: 1
};

var watchID = protolib.addWatchVariable({
        title: "Variable Title",
        object: variableOwner,
        property: "variableName",
        group: "Variable Group",
        type: protolib.watchTypes.SLIDER,
        options: {
            min: 0.1,
            max: 10,
            step: 0.1
        }
    });
title
The string title displayed on the page for the variable. This should be something to help recognise the variable.
object
An object that has the variable as a property.
property
The string name of the variable to expose. Property must be accessible on object.
group
The name of the group to store the watch variable entry under. On the page this will be represent name of the variable to expose. Property must be accessible on object.
type
The watchType enum for page control of the variable. This determines what you will be able to do to the variable.
options
The options to pass for the watchType.

Returns a watchID for removing the watch variable later. If any of the arguments are invalid the function will return -1.

24.1.15.2. removeWatchVariable

Summary

Remove a watchable variable by watchID. This removes the control from the page that contains the Protolib app.

Syntax

// watchID returned by addWatchVariable

protolib.removeWatchVariable(watchID);
watchID
The ID to remove. Returned by the addWatchVariable.

Returns a true if successfully removed. Returns false if the dynamic UI is not enabled or if the ID is not recognized.

24.1.16. Advanced

Advanced functionality of Protolib is to give developers more control of how the library works if they need to operate beyond the default behaviour. These functions should only be used by advanced users who understand the behavior and need to extend it.

24.1.16.1. setPreDraw

Updated 0.2.1

Summary

Set a function to call, before any 3D or 2D rendering has happened in a frame, but after the scene and camera update. This function allows you to modify any state before the clear screen. This function will be called during protolib.endFrame function, proceeding the setPreRendererDraw function call. Prior to 0.2.1 this function had the behavior described in setPreRendererDraw. Use setPreRendererDraw callback to use the previous behavior.

Syntax

function preDrawFn()
{
    // Manipulate scene before rendering
    postUpdate.process(protolib.globals.scene);
}

protolib.setPreDraw(preDrawFn);
callback
The function to call before rendering.

24.1.16.2. setPreRendererDraw

Added 0.2.1

Summary

Set a function to call, between the renderer clear call and before the scene rendering. This function allows you to render before the 3D scene content. This function will be called during protolib.endFrame function, after the setPreDraw function call and proceeding the setPostRendererDraw function call.

Syntax

function preRendererDrawFn()
{
    // A 2D sprite to manually draw after the clear screen and behind 3D content.
    draw2D.begin();
    draw2D.drawSprite(sprite);
    draw2D.end();
}

protolib.setPreRendererDraw(preRendererDrawFn);
callback
The function to call after the clear, but before scene rendering.

24.1.16.3. setPostRendererDraw

Added 0.1.1

Summary

Set a function to call, after the protolib has rendered the scene content, but before 2D sprites/text and before graphicsDevice.endFrame(). This function allows you to render on top of the 3D scene content, but under the 2D content. This function will be called after 3D rendering, but before graphicsDevice.endFrame(). This function will be called during protolib.endFrame function, after the setPreRendererDraw function call and proceeding the setPostDraw function call.

Syntax

function postRendererDrawFn()
{
    // Draw a layer of content post scene renderering
    postScene.draw();
}

protolib.setPostRendererDraw(postRendererDrawFn);
callback
The function to call before 2D content rendering.

24.1.16.4. setPostDraw

Summary

Set a function to call, after the protolib has rendered the current frame, but before graphicsDevice.endFrame(). This function allows you to render on top of the final scene content. This function will be called after 2D and 3D rendering, but before graphicsDevice.endFrame(). This function will be called during protolib.endFrame function, after the setPostRendererDraw function call.

Syntax

function postDrawFn()
{
    // Draw a layer of debug on top of the scene
    debug.draw();
}

protolib.setPostDraw(postDrawFn);
callback
The function to call before graphicsDevice.endFrame().

24.2. The MeshWrapper Object

An object returned from protolib.loadMesh, for controlling the loaded mesh.

var mesh = protolib.loadMesh({...});

24.2.1. Methods

24.2.1.1. setPosition

Syntax

mesh.setPosition(v3Position);
v3Position
A Vector3 object specifying the position of the mesh.

24.2.1.2. getPosition

Syntax

mesh.getPosition(v3Position);
v3Position
A Vector3 object the position of the mesh will be written into.

24.2.1.3. setSize

Syntax

mesh.setSize(v3Size);
v3Size
A Vector3 object specifying the amount to scale the mesh by in the x, y, and z directions.

24.2.1.4. getSize

Syntax

mesh.getPosition(v3Size);
v3Size
A Vector3 object the size vector will be written into.

24.2.1.5. setEnabled

Syntax

mesh.setEnabled(enabled);
enabled
A boolean determining whether the mesh should be shown in the scene.

24.2.1.6. getEnabled

Syntax

var isMeshEnabled = mesh.getEnabled();

Returns a boolean representing whether the mesh is enabled in the scene.

24.2.1.7. setRotationMatrix

Summary

Sets the rotation matrix used to orient the mesh.

Syntax

//unitY === md.v3BuildYAxis();

mathDevice.m43SetAxisRotation(rotationMatrix, unitY, Math.PI/4);
mesh.setRotationMatrix(rotationMatrix);
rotationMatrix
A Matrix43 object specifying the rotation matrix to use.

24.2.1.8. getRotationMatrix

Summary

Returns the rotation matrix used to orient the mesh.

Syntax

mesh.getRotationMatrix(rotationMatrix);
rotationMatrix
A Matrix43 object the rotation matrix will be written into.

24.3. The SoundWrapper Object

An object returned from protolib.playSound, used to control the playback of the played sound.

var sound = protolib.playSound({...});

24.3.1. Methods

24.3.1.1. pause

Summary

Pauses the sound.

Syntax

sound.pause();

24.3.1.2. resume

Summary

Resumes playing from where it was paused.

Syntax

sound.resume();

24.3.1.3. stop

Summary

Stops playing the current sound. After this is called, this wrapper object becomes invalid, and should not be used.

Syntax

sound.stop();

24.3.1.4. getStatus

Summary

Returns the current status of the sound playback.

Syntax

var soundstatus = sound.getStatus();

Returns a value in protolib.soundStatus.

24.3.1.5. setVolume

Syntax

sound.setVolume(volume);
volume
The volume amplification to be applied to the sound.

24.3.1.6. getVolume

Syntax

sound.getVolume(volume);

Returns the volume amplification applied to the sound.

24.3.1.7. setPosition

Syntax

sound.setPosition(v3Position);
v3Position
A Vector3 object specifying the position of the sound.

24.3.1.8. getPosition

Syntax

sound.getPosition(v3Position);
v3Position
A Vector3 object the position of the sound will be written into.

24.3.1.9. setPitch

Syntax

sound.setPitch(pitch);
pitch
The pitch to be applied to the sound.

24.3.1.10. getPitch

Syntax

var pitch = sound.getPitch();

Returns the pitch applied to the sound.

24.3.1.11. setMinDistance

Syntax

sound.setMinDistance(minDistance);
minDistance
If the distance between the camera and the sound position is less than this, the sound plays at full volume with no attenuation.

24.3.1.12. getMinDistance

Syntax

var minDistance = sound.getMinDistance();

Returns the distance after which sound attenuation will start to take place.

24.3.1.13. setMaxDistance

Syntax

sound.setMaxDistance(maxDistance);
maxDistance
The maximum distance to the listener after which attenuation will result in volume of zero.

24.3.1.14. getMaxDistance

Syntax

var maxDistance = sound.getMaxDistance();

Returns the maxDistance for sound attenuation.

24.3.1.15. setRollOff

Syntax

sound.setRollOff(rollOff);
rollOff

The ratio that the sound will drop off as by the inverse square law of the distance to the listener.

A number in the range [0, 1].

0 results in no attenuation. 1 results in the volume being determined fully by attenuation.

24.3.1.16. getRollOff

Syntax

var rollOff = sound.getRollOff();

Returns the value of the rollOff ratio.

24.4. The PointLightWrapper Object

An object returned from protolib.addPointLight, used to control the created point light.

var pointLight = protolib.addPointLight({});

24.4.1. Methods

24.4.1.1. setPosition

Syntax

pointLight.setPosition(v3Position);
v3Position
A Vector3 object specifying the position of the light.

24.4.1.2. getPosition

Syntax

pointLight.getPosition(v3Position);
v3Position
A Vector3 object the position of the light will be written into.

24.4.1.3. setColor

Syntax

pointLight.setColor(v3Color);
v3Color
A Vector3 object specifying the r, g, b color components. The components are in the range [0, 1].

24.4.1.4. getColor

Syntax

pointLight.getColor(v3Color);
v3Color
A Vector3 object the r, g, b color components will be written into.

24.4.1.5. setEnabled

Syntax

light.setEnabled(enabled);
enabled
A boolean determining whether the light should be shown in the scene.

24.4.1.6. getEnabled

Syntax

var isMeshEnabled = light.getEnabled();

Returns a boolean representing whether the light is enabled in the scene.

24.5. The SpotLightWrapper Object

An object returned from protolib.addSpotLight, for controlling the created spot light.

var spotLight = protolib.addSpotLight({});

24.5.1. Methods

24.5.1.1. setPosition

Syntax

spotLight.setPosition(v3Position);
v3Position
A Vector3 object specifying the position of the light.

24.5.1.2. getPosition

Syntax

spotLight.getPosition(v3Position);
v3Position
A Vector3 object the position of the light will be written into.

24.5.1.3. setColor

Syntax

spotLight.setColor(v3Color);
v3Color
A Vector3 object specifying the r, g, b color components. The components are in the range [0, 1].

24.5.1.4. getColor

Syntax

spotLight.getColor(v3Color);
v3Color
A Vector3 object the r, g, b color components will be written into.

24.5.1.5. setEnabled

Syntax

light.setEnabled(enabled);
enabled
A boolean determining whether the light should be shown in the scene.

24.5.1.6. getEnabled

Syntax

var isMeshEnabled = light.getEnabled();

Returns a boolean representing whether the light is enabled in the scene.

24.5.1.7. setDirection

Syntax

spotLight.setDirection(v3Direction);
v3Direction
A Vector3 object representing the direction the spotlight should aim.

24.5.1.8. getDirection

Syntax

protolib.setDirection(v3Direction);
v3Direction
A Vector3 object the x, y, z direction components will be written into.