CLASSES
TagsA - Z

Engine

Engine

Description

class Engine

The engine class is responsible for interfacing with all lower-level APIs such as WebGL and Audio.

Constructor

new Engine(canvas, antialias, options, adaptToDeviceRatio)

@constructor

Parameters

Name Type Description
canvas HTMLCanvasElement The canvas
optional antialias boolean True if this engine should support antialiasing, false otherwise.
optional options EngineOptions - further options to be sent to the getContext function Can contains these parameters : generateDepthBuffer, generateMipMaps, samplingMode

Members

static NEVER : number

static ALWAYS : number

static LESS : number

static EQUAL : number

static LEQUAL : number

static GREATER : number

static GEQUAL : number

static NOTEQUAL : number

static KEEP : number

static REPLACE : number

static INCR : number

static DECR : number

static INVERT : number

static INCR_WRAP : number

static DECR_WRAP : number

static ALPHA_DISABLE : number

Alpha disable

static ALPHA_ONEONE : number

static ALPHA_ADD : number

Add Alpha

static ALPHA_COMBINE : number

Combine Alpha

static ALPHA_SUBTRACT : number

static ALPHA_MULTIPLY : number

static ALPHA_MAXIMIZED : number

static DELAYLOADSTATE_NONE : number

The delay when you don't load

static DELAYLOADSTATE_LOADED : number

The delay for loaded

static DELAYLOADSTATE_LOADING : number

The delay of loading

static DELAYLOADSTATE_NOTLOADED : number

The delay

static TEXTUREFORMAT_ALPHA : number

static TEXTUREFORMAT_LUMINANCE : number

static TEXTUREFORMAT_LUMINANCE_ALPHA : number

static TEXTUREFORMAT_RGB : number

static TEXTUREFORMAT_RGBA : number

static TEXTURETYPE_UNSIGNED_INT : number

static TEXTURETYPE_FLOAT : number

static TEXTURETYPE_HALF_FLOAT : number

static Version : string

static CollisionsEpsilon : number

This member is static : use Engine.CollisionsEpsilon

Default value : 0.001

static CodeRepository : string

static ShadersRepository : string

This member is static : use Engine.ShadersRepository

Default value : "Babylon/Shaders/"

Used as the source directory of shaders on the host machine

isFullscreen : boolean

Default value: false

True if fullscreen, false otherwise

isPointerLock : boolean

Default value: false

True if the pointer is locked, false otherwise

cullBackFaces : boolean

Default value: true

True if back faces should be culled, false otherwise

renderEvenInBackground : boolean

Default value: true

If true, the engine will compute all frames even if the app is in background

enableOfflineSupport : boolean

scenes : Scene[]

An array of Scene

vrDisplaysPromise : any

static audioEngine : AudioEngine

texturesSupported : Array<string>

textureFormatInUse : string

webGLVersion : string

isStencilEnable : boolean

Returns true if the stencil buffer has been enabled through the creation option of the context.

drawCalls : number

The number of draw calls submitted last frame

drawCallsPerfCounter : PerfCounter

WebGLTexture : undefined

loadingScreen : ILoadingScreen

loadingUIText : string

Default Value: "Loading"

Sets the innerHTML for the loading UI created in to displayLoadingUI : void

loadingUIBackgroundColor : string

Get/Set the background color for the loading UI created in to displayLoadingUI : void

void : undefined

void : undefined

Methods

resetTextureCache() → void

getGlInfo() → { vendor: string, renderer: string, version: string }

getAspectRatio(camera, useScreen) → number

Parameters

Name Type Description
camera Camera @param camera
optional useScreen boolean

getRenderWidth(useScreen) → number

Parameters

Name Type Description
optional useScreen boolean

getRenderHeight(useScreen) → number

Parameters

Name Type Description
optional useScreen boolean

getRenderingCanvas() → HTMLCanvasElement

Returns the rendering canvas

getRenderingCanvasClientRect() → ClientRect

setHardwareScalingLevel(level) → void

Set the hardware scaling level. The engine is then resized with these new parameters (width/level, height/level).

Parameters

Name Type Description
level number @param level

getHardwareScalingLevel() → number

Returns the hardware scaling level

getLoadedTexturesCache() → WebGLTexture[]

Returns all loaded textures from the caches

getCaps() → EngineCapabilities

Returns the engine capabilities

getDepthFunction() → number

setDepthFunction(depthFunc) → void

Parameters

Name Type Description
depthFunc number

setDepthFunctionToGreater() → void

Set the Depth function to greater

setDepthFunctionToGreaterOrEqual() → void

Set the Depth function to greater or equal

setDepthFunctionToLess() → void

Set the Depth function to less

setDepthFunctionToLessOrEqual() → void

Set the Depth function to less or equal

getStencilBuffer() → boolean

setStencilBuffer(enable) → void

Parameters

Name Type Description
enable boolean Set true if the alpha testing is enabled, false otherwise.

getStencilMask() → number

setStencilMask(mask) → void

Parameters

Name Type Description
mask number

getStencilFunction() → number

getStencilFunctionReference() → number

getStencilFunctionMask() → number

setStencilFunction(stencilFunc) → void

Parameters

Name Type Description
stencilFunc number

setStencilFunctionReference(reference) → void

Parameters

Name Type Description
reference number

setStencilFunctionMask(mask) → void

Parameters

Name Type Description
mask number

getStencilOperationFail() → number

getStencilOperationDepthFail() → number

getStencilOperationPass() → number

setStencilOperationFail(operation) → void

Parameters

Name Type Description
operation number

setStencilOperationDepthFail(operation) → void

Parameters

Name Type Description
operation number

setStencilOperationPass(operation) → void

Parameters

Name Type Description
operation number

stopRenderLoop(renderFunction) → void

stop executing a render loop function and remove it from the execution array

Parameters

Name Type Description
optional renderFunction () => void @param renderFunction

runRenderLoop(renderFunction) → void

Register and execute a render loop. The engine can have more than one render function.

@example

engine.runRenderLoop(function () {

 scene.render()

})

Parameters

Name Type Description
renderFunction () => void @param renderFunction

switchFullscreen(requestPointerLock) → void

Toggle full screen mode.

Parameters

Name Type Description
requestPointerLock boolean If true, the user requests a pointer lock

clear(color, backBuffer, depth, stencil) → void

Parameters

Name Type Description
color any The clear color used
backBuffer boolean True if this method should clear the color buffer
depth boolean

scissorClear(x, y, width, height, clearColor) → void

Parameters

Name Type Description
x number @param x
y number @param y
width number The width of the rectangle
height number The height of the rectangle

setViewport(viewport, requiredWidth, requiredHeight) → void

Set the WebGL's viewport

Parameters

Name Type Description
viewport Viewport @param viewport
optional requiredWidth number The required width of the viewport. By defaults, equals to the rendering canvas width
optional requiredHeight number The required height of the viewport. By defaults, equals to the rendering canvas height

setDirectViewport(x, y, width, height) → Viewport

Directly set the WebGL Viewport

The x, y, width & height are directly passed to the WebGL call

@return the current viewport Object (if any) that is being replaced by this call. You can restore this viewport later on to go back to the original state.

Parameters

Name Type Description
x number @param x
y number @param y
width number The width of the rectangle

beginFrame() → void

Method used at the beginning of the frame rendering. Currently, measure the number of frames per seconds (FPS).

endFrame() → void

Method used at the end of a frame rendering. Flushes the frame buffer of the canvas.

resize() → void

resize the view according to the canvas' size.

@example

window.addEventListener("resize", function () {

 engine.resize();

});

setSize(width, height) → void

force a specific size of the canvas

Parameters

Name Type Description
width number The width of the rectangle
height number The height of the rectangle

initWebVR() → void

enableVR(vrDevice) → void

Parameters

Name Type Description
vrDevice any

disableVR() → void

bindFramebuffer(texture, faceIndex, requiredWidth, requiredHeight) → void

Parameters

Name Type Description
texture WebGLTexture @param texture
optional faceIndex number
optional requiredWidth number The required width of the viewport. By defaults, equals to the rendering canvas width

unBindFramebuffer(texture, disableGenerateMipMaps) → void

Parameters

Name Type Description
texture WebGLTexture @param texture
optional disableGenerateMipMaps boolean

generateMipMapsForCubemap(texture) → void

Parameters

Name Type Description
texture WebGLTexture @param texture

flushFramebuffer() → void

Flushes the frame buffer

restoreDefaultFramebuffer() → void

Restore the default frame buffer

createVertexBuffer(vertices) → WebGLBuffer

Create a new vertex Buffer object

Parameters

Name Type Description
vertices number[] The array of vertices

createDynamicVertexBuffer(vertices) → WebGLBuffer

Parameters

Name Type Description
vertices number[] The array of vertices

updateDynamicVertexBuffer(vertexBuffer, vertices, offset, count) → void

Parameters

Name Type Description
vertexBuffer WebGLBuffer The given vertex buffer
vertices number[] The array of vertices
optional offset number

createIndexBuffer(indices) → WebGLBuffer

Create a new index buffer

Parameters

Name Type Description
indices number[] @param indices

bindArrayBuffer(buffer) → void

Parameters

Name Type Description
buffer WebGLBuffer The buffer

updateArrayBuffer(data) → void

Parameters

Name Type Description
data Float32Array the data object to associate to the key for this Engine instance

bindBuffersDirectly(vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) → void

Parameters

Name Type Description
vertexBuffer WebGLBuffer The given vertex buffer
indexBuffer WebGLBuffer The given index buffer
vertexDeclaration number[] The given vertex declaration
vertexStrideSize number The given vertex

bindBuffers(vertexBuffers, indexBuffer, effect) → void

Parameters

Name Type Description
vertexBuffers { [key: string]: VertexBuffer } The given vextex buffer
indexBuffer WebGLBuffer The given index buffer
effect Effect @param effect

unbindInstanceAttributes() → void

createInstancesBuffer(capacity) → WebGLBuffer

Create a dynamic instance buffer

Parameters

Name Type Description
capacity number The size of this dynamic buffer

deleteInstancesBuffer(buffer) → void

Delete an existing instance buffer

Parameters

Name Type Description
buffer WebGLBuffer The buffer

updateAndBindInstancesBuffer(instancesBuffer, data, offsetLocations, InstancingAttributeInfo) → void

Parameters

Name Type Description
instancesBuffer WebGLBuffer The instances buffer
data Float32Array the data object to associate to the key for this Engine instance

applyStates() → void

draw(useTriangles, indexStart, indexCount, instancesCount) → void

Draw

Parameters

Name Type Description
useTriangles boolean @param useTriangles
indexStart number @param indexStart
indexCount number @param indexCount

drawPointClouds(verticesStart, verticesCount, instancesCount) → void

Parameters

Name Type Description
verticesStart number
verticesCount number
optional instancesCount number

drawUnIndexed(useTriangles, verticesStart, verticesCount, instancesCount) → void

Parameters

Name Type Description
useTriangles boolean @param useTriangles
verticesStart number
verticesCount number

createEffect(baseName, attributesNames, uniformsNames, samplers, defines, fallbacks, onCompiled, onError, indexParameters) → Effect

Parameters

Name Type Description
baseName any The base name of the effect (The name of file without .fragment.fx or .vertex.fx)
attributesNames string[] The name of the attributes
uniformsNames string[] The uniforms names
samplers string[] An array of samplers (the objects used to read textures)
defines string The shader defines string
optional fallbacks EffectFallbacks @param fallbacks
optional onCompiled (effect: Effect) => void Function launched when the effect is compiled
optional onError (effect: Effect, errors: string) => void Function when error occurs.

createEffectForParticles(fragmentName, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) → Effect

Compiled/linked your shaders into a simple object.

Parameters

Name Type Description
fragmentName string The name of the Particules
optional uniformsNames string[] The uniforms names
optional samplers string[] An array of samplers (the objects used to read textures)
optional defines string The shader defines string
optional fallbacks EffectFallbacks @param fallbacks
optional onCompiled (effect: Effect) => void Function launched when the effect is compiled

createShaderProgram(vertexCode, fragmentCode, defines, context) → WebGLProgram

Parameters

Name Type Description
vertexCode string The vertex shader code
fragmentCode string The fragment shader code
defines string The shader defines string

getUniforms(shaderProgram, uniformsNames) → WebGLUniformLocation[]

Return the uniforms location for the given shader program

Parameters

Name Type Description
shaderProgram WebGLProgram The given shader program
uniformsNames string[] The uniforms names

getAttributes(shaderProgram, attributesNames) → number[]

Return the attributes for the given shader program

Parameters

Name Type Description
shaderProgram WebGLProgram The given shader program
attributesNames string[] The name of the attributes

enableEffect(effect) → void

Enable effect

Parameters

Name Type Description
effect Effect @param effect

setIntArray(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Int32Array

setIntArray2(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Int32Array

setIntArray3(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Int32Array

setIntArray4(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Int32Array

setFloatArray(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Float32Array

setFloatArray2(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Float32Array

setFloatArray3(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Float32Array

setFloatArray4(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array Float32Array

setArray(uniform, array) → void

Set array of given shader

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array number[]

setArray2(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array number[]

setArray3(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array number[]

setArray4(uniform, array) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
array number[]

setMatrices(uniform, matrices) → void

Set matrices for a given shader

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
matrices Float32Array @param matrices

setMatrix(uniform, matrix) → void

Set matrix for a given shader

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
matrix Matrix @param matrix

setMatrix3x3(uniform, matrix) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
matrix Float32Array @param matrix

setMatrix2x2(uniform, matrix) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
matrix Float32Array @param matrix

setFloat(uniform, value) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
value number @param value

setFloat2(uniform, x, y) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
x number @param x
y number @param y

setFloat3(uniform, x, y, z) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
x number @param x
y number @param y

setBool(uniform, bool) → void

Set bool for this given shader

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
bool number @param bool

setFloat4(uniform, x, y, z, w) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
x number @param x
y number @param y
z number The z axis

setColor3(uniform, color3) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
color3 Color3 The color of the shader

setColor4(uniform, color3, alpha) → void

Parameters

Name Type Description
uniform WebGLUniformLocation The uniforms of the shader
color3 Color3 The color of the shader
alpha number The alpha of the shader

setState(culling, zOffset, force, reverseSide) → void

Parameters

Name Type Description
culling boolean @param culling
optional zOffset number
optional force boolean @param force

setDepthBuffer(enable) → void

Enable or disable the depth buffer

Parameters

Name Type Description
enable boolean Set true if the alpha testing is enabled, false otherwise.

getDepthWrite() → boolean

Get the depth mask

setDepthWrite(enable) → void

Enables or disables the depth mask

Parameters

Name Type Description
enable boolean Set true if the alpha testing is enabled, false otherwise.

setColorWrite(enable) → void

Enables or disables the writing or red, blue, green and alpha

Parameters

Name Type Description
enable boolean Set true if the alpha testing is enabled, false otherwise.

setAlphaMode(mode, noDepthWriteChange) → void

Parameters

Name Type Description
mode number @param mode
optional noDepthWriteChange boolean

getAlphaMode() → number

setAlphaTesting(enable) → void

Enables or disables the alpha testing

Parameters

Name Type Description
enable boolean Set true if the alpha testing is enabled, false otherwise.

getAlphaTesting() → boolean

Returns true if the alpha testing is enabled, false otherwise.

wipeCaches() → void

Reset the cache

setSamplingMode(texture, samplingMode) → void

Set the default texture sampling mode

Parameters

Name Type Description
texture WebGLTexture @param texture
samplingMode number

setTextureFormatToUse(formatsAvailable) → string

Set the compressed texture format to use, based on the formats you have,

the formats supported by the hardware / browser, and those currently implemented

in BJS.

     * Note: The result of this call is not taken into account texture is base64 or when

using a database / manifest.

     * @param {Array<string>} formatsAvailable - Extension names including dot.  Case

and order do not matter.

@returns The extension selected.

Parameters

Name Type Description
formatsAvailable Array<string>

createTexture(url, noMipmap, invertY, scene, samplingMode, onLoad, onError, buffer) → WebGLTexture

Create a new texture

Parameters

Name Type Description
url string The texture url
noMipmap boolean Set true if you want to activate Mipmap, false otherwise.
invertY boolean @param invertY
scene Scene The scene where the cube texture
optional samplingMode number
optional onLoad () => void Function when load.
optional onError () => void Function when error occurs.

updateRawTexture(texture, data, format, invertY, compression) → void

Parameters

Name Type Description
texture WebGLTexture @param texture
data ArrayBufferView the data object to associate to the key for this Engine instance
format number
invertY boolean @param invertY

createRawTexture(data, width, height, format, generateMipMaps, invertY, samplingMode, compression) → WebGLTexture

Parameters

Name Type Description
data ArrayBufferView the data object to associate to the key for this Engine instance
width number The width of the rectangle
height number The height of the rectangle
format number
generateMipMaps boolean True if you want to generate Mipmap, false otherwise.
invertY boolean @param invertY
samplingMode number

createDynamicTexture(width, height, generateMipMaps, samplingMode) → WebGLTexture

Parameters

Name Type Description
width number The width of the rectangle
height number The height of the rectangle
generateMipMaps boolean True if you want to generate Mipmap, false otherwise.

updateTextureSamplingMode(samplingMode, texture) → void

Parameters

Name Type Description
samplingMode number
texture WebGLTexture @param texture

updateDynamicTexture(texture, canvas, invertY, premulAlpha) → void

Parameters

Name Type Description
texture WebGLTexture @param texture
canvas HTMLCanvasElement The canvas
invertY boolean @param invertY

updateVideoTexture(texture, video, invertY) → void

Update the given dynamic texture with the video given in parameter

Parameters

Name Type Description
texture WebGLTexture @param texture
video HTMLVideoElement The video
invertY boolean @param invertY

createRenderTargetTexture(size, options) → WebGLTexture

Create a new render target texture old version had a "generateMipMaps" arg instead of options. if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value in the same way, generateDepthBuffer is defaulted to true

Parameters

Name Type Description
size any Can be an object with the two parameters width and height.
options any Can contains these parameters : generateDepthBuffer, generateMipMaps, samplingMode

createRenderTargetCubeTexture(size, options) → WebGLTexture

Parameters

Name Type Description
size number Can be an object with the two parameters width and height.
optional options any Can contains these parameters : generateDepthBuffer, generateMipMaps, samplingMode

createCubeTexture(rootUrl, scene, files, noMipmap, onLoad, onError) → WebGLTexture

Parameters

Name Type Description
rootUrl string @param rootUrl
scene Scene The scene where the cube texture
files string[]
optional noMipmap boolean Set true if you want to activate Mipmap, false otherwise.
optional onLoad () => void Function when load.

updateTextureSize(texture, width, height) → void

Parameters

Name Type Description
texture WebGLTexture @param texture
width number The width of the rectangle
height number The height of the rectangle

createRawCubeTexture(url, scene, size, format, type, noMipmap, callback, mipmmapGenerator) → (url, scene, size, format, type, noMipmap, callback, mipmmapGenerator)

Parameters

Name Type Description
url string The texture url
scene Scene The scene where the cube texture
size number Can be an object with the two parameters width and height.
format number
type number
noMipmap boolean Set true if you want to activate Mipmap, false otherwise.
callback (ArrayBuffer: undefined) => ArrayBufferView[]

bindSamplers(effect) → void

Bind the given effect to samplers

Parameters

Name Type Description
effect Effect @param effect

setTextureFromPostProcess(channel, postProcess) → void

Set texture from post process

Parameters

Name Type Description
channel number The index in the array _activeTexturesCache
postProcess PostProcess @param postProcess

unbindAllTextures() → void

setTexture(channel, uniform, texture) → void

Parameters

Name Type Description
channel number The index in the array _activeTexturesCache
uniform WebGLUniformLocation The uniforms of the shader
texture BaseTexture @param texture

setTextureArray(channel, uniform, textures) → void

Parameters

Name Type Description
channel number The index in the array _activeTexturesCache
uniform WebGLUniformLocation The uniforms of the shader
textures BaseTexture[]

readPixels(x, y, width, height) → Uint8Array

Read pixels in the given rectangle

Parameters

Name Type Description
x number @param x
y number @param y
width number The width of the rectangle

addExternalData(key, data) → boolean

Add an externaly attached data from its key.

This method call will fail and return false, if such key already exists.

If you don't care and just want to get the data no matter what, use the more convenient getOrAddExternalDataWithFactory() method.

@return true if no such key were already present and the data was added successfully, false otherwise

Parameters

Name Type Description
key string the unique key that identifies the data the unique key that identifies the data
data T the data object to associate to the key for this Engine instance the data object to associate to the key for this Engine instance

getExternalData(key) → T

Get an externaly attached data from its key

@return the associated data, if present (can be null), or undefined if not present

Parameters

Name Type Description
key string the unique key that identifies the data the unique key that identifies the data

getOrAddExternalDataWithFactory(key, factory) → T

Get an externaly attached data from its key, create it using a factory if it's not already present

@return the associated data, can be null if the factory returned null.

Parameters

Name Type Description
key string the unique key that identifies the data the unique key that identifies the data
factory (k: string) => T the factory that will be called to create the instance if and only if it doesn't exists the factory that will be called to create the instance if and only if it doesn't exists

removeExternalData(key) → boolean

Remove an externaly attached data from the Engine instance

@return true if the data was successfully removed, false if it doesn't exist

Parameters

Name Type Description
key any the unique key that identifies the data the unique key that identifies the data

releaseInternalTexture(texture) → void

Parameters

Name Type Description
texture WebGLTexture @param texture

unbindAllAttributes() → void

dispose() → void

Delete this engine. Calls dispose on each of the registered scene and deletes all shaders

displayLoadingUI() → void

Display loading User Interface

hideLoadingUI() → void

Hide the loading state to the User Interface

attachContextLostEvent(callback) → (callback)

Parameters

Name Type Description
callback () => (event: WebGLContextEvent) => void

attachContextRestoredEvent(callback) → (callback)

Parameters

Name Type Description
callback () => (event: WebGLContextEvent) => void

getVertexShaderSource(program) → string

Parameters

Name Type Description
program WebGLProgram

getFragmentShaderSource(program) → string

Parameters

Name Type Description
program WebGLProgram

getFps() → number

getDeltaTime() → number

static isSupported() → boolean