Class: World

(abstract) World(params)

Basic world, intended to be overridden. Provides function placeholders for implementations, and safe implementation of basic functions, like loading of world file(s) and XR support. A world may contain one or more scene files. Defaults are set for one file loaded from scene.gltf file, as it is, from current directory. To load multiple files, use this.worldObjects structure or objectsFile file, that along the name allow to specifiy position, rotation and scale for each object. (the structure is the same one that is also used by AssetLoader and WorldEditor)

Constructor

(abstract) new World(params)

Constructor takes parems that allow to override default values.
Parameters:
Name Type Description
params object to override world defaults - all properties are copied to world properties
Source:

Classes

World

Members

arHelper

AR helper
Source:

avatar

Reference to own Avatar or VideoAvatar, set by AvatarController during initialization
Source:

avatarController

Reference to AvatarController, set by AvatarController during initialization
Source:

baseUrl

Base URL of related content, default "" (current location)
Source:

camera

Main world camera
Source:

camera1p

First person camera, defaults to main camera
Source:

camera3p

Main 3rd person world camera
Source:

collisionsEnabled

Wheter collisions are enabled, default true
Source:

collisionsEnabledInXR

Wheter collisions are enabled in XR, default true
Source:

file

World scene file name to load, default scene.gltf
Source:

gravityEnabled

Wheter gravity is enabled, default true
Source:

hasXR

WebXR capability indicator, set by VRHelper
Source:

inAR

AR mode indicator, set by VRHelper
Source:

indicator

Progress indicator
Source:

inVR

VR mode indicator, set by VRHelper
Source:

name

World name, default null
Source:

objectsFile

World objects json file, as saved by WorldEditor and AssetLoader, default null
Source:

onProgress

Progress indicator functon
Source:

sceneMeshes

Scene meshes, available once the world loads (in loaded, loadingStop, collisions methods)
Source:

terrain

Terrain, optionally created in createTerrain()
Source:

vrHelper

VR helper
Source:

VRSPACEUI

Handy reference to VRSpaceUI
Source:

worldListeners

List of world listeners. WorldManager executes enter(Welcome) method once user enters the world, after World.enter() method. Methods added(VRObject) and removed(VRObject) are executed whenever scene changes.
Source:

worldManager

Reference to worldManager, set by WorldManager once that user goes online
Source:

worldObjects

World objects to load, default null
Source:

xrHelper

Currently active VR/AR helper
Source:

Methods

_collisions(meshes, state)

Utility method, enables or disables collisions on the given set of meshes.
Parameters:
Name Type Description
meshes array of meshes
state true or false
Source:

addSelectionPredicate()

Add a selection predicate. It takes a mesh and returns true if it can be selected by the pointer.
Source:

assetPath(relativePath)

Utility method, returns baseUrl+relativePath
Parameters:
Name Type Description
relativePath path relative to current world directory
Source:

attachControl()

Attach the control to the camera, called from createScene.
Source:

collisions(state)

Enables or disables collisions in the world. This includes floorMeshes, sceneMeshes, and also applying gravity to camera.
Parameters:
Name Type Description
state true or false
Source:

(async) createCamera()

An implementation must override this method and define at least one camera. Returned camera, if any, is set as main camera (this.camera), and also as 1st person camera (this.camera1p) if one is not set.
Source:

(async) createEffects()

Optional, empty implementation, called from createScene
Source:

(async) createGround()

Optional, empty implementation, called from createScene. Should return a mesh.
Source:

(async) createLights()

Optional, empty implementation, called from createScene. May return a Light
Source:

(async) createPhysics()

Optional, empty implementation, called from createScene
Source:

(async) createScene()

Called from init. If the scene does not exist, creates the scene first. Then calls methods in this order: createCamera, attachControl, createLights, createShadows, createSkybox, createGround, createEffects, createPhysics.
Source:

(async) createShadows()

Optional, empty implementation, called from createScene. Should return a ShadowGenerator.
Source:

(async) createSkyBox()

Optional, empty implementation, called from createScene. Should return a sky Box.
Source:

(async) createTerrain()

Optional, empty implementation, called from createScene
Source:

(async) createUI()

Optional, empty implementation, called from createScene
Source:

(async) dispose()

Disposes of all objects returned by createLights, createCamera, createShadows, createSkybox
Source:

enableBackground()

Called when entering world in AR mode, or entering/exiting AR. VRHelper disables the skybox and terrain, but this method should dissable all other nodes that shouldn't be visible in AR.
Source:

(async) entered(welcome)

Called by WorldManager after the user has entered a multiuser world. Default implementation creates a ChatLog and binds it to the HUD, then it registers remoteEvent() method as a change listener with WorldManager to process remote events. Note that at this point world geometry may not have been loaded.
Parameters:
Name Type Description
welcome message containing users Client object and array of permanent objects
Source:

(async) enterWith(avatarUrl, userName)

Quick enter, with avatar url and optionally user name.
Parameters:
Name Type Description
avatarUrl URL to load avatar from
userName login name of the user
Source:

enterXR()

Called by VR helper after entering XR mode. Default implementation enables virtual keyboard in ChatLog.
Source:

exitXR()

Called by VR helper after exiting XR. Default implementation turns off ChatLog virtual keyboard.
Source:

firstPerson()

Activate first person camera (this.camera1p), if available. Makes a call to AvatarController method that applies camera rotation and takes care of everything else.
Source:

firstPersonCamera(pos, name)

Utility method, calls this.universalCamera with given parameters, and sets the camera speed function. Original Babylon.js camera speed function takes FPS into account, but does not mean anything really. This one attempts to approximate meters per second, and is computationally cheaper. See https://forum.babylonjs.com/t/does-camera-speed-vary-depending-on-fps-performance/20802
Parameters:
Name Type Default Description
pos Vector3 to position camera at
name First Person Camera optional camera name, default First Person Camera
Source:

getFloorMeshes()

Returns this.floorMeshes or this.ground if exist, or empty array. Used for movement in XR.
Source:

(async) init(engine, name, scene, callback, baseUrl, file)

Create, load and and show the world. Enables gravity and collisions, then executes createScene method, optionally creates load indicator, registers render loop, crates terrain, and finally, executes load method. Every method executed can be overridden.
Parameters:
Name Type Description
engine babylonjs engine
name world name
scene babylonjs scene, optional
callback to execute after the world has loaded
baseUrl folder to load scene from, default "", used only if not defined in constructor
file scene file to load, default scene.gltf, used only if not defined in constructor
Source:
Returns:
final scene

initXR(vrHelper, arHelper, activeHelper)

Creates a VRHelper if needed, and initializes it with the current world. Normally called after world is loaded, safe to call elsewhere, or call multiple times, but only after World.init() has finished. (world.scene must be initialized)
Parameters:
Name Type Description
vrHelper optional existing vrHelper
arHelper optional existing arHelper
activeHelper optional, if given both helpers, one that's currently active (e.g. passed while in VR or AR mode)
Source:

inXR()

Returns true if either VR or AR mode is currently active
Source:

isOnline()

Utility method, returns true if the world is online, i.e. the client is connected to the server.
Source:

isSelectableMesh()

Used in mesh selection predicate in XR. Default implementation returns true for members of this.floorMeshes.
Source:

(async) load(callback)

Load the world, then execute given callback passing self as argument. Loads an AssetContainer from file specified by this.file, if any (by default scene.gltf), and adds it to the scene. Then loads all world objects specified in this.objectsFile or this.worldObjects, if any - file takes precedence. Takes care of loading progress. Calls loadingStart, loaded, loadingStop, collisions - each may be overridden.
Parameters:
Name Type Description
callback to execute after the content has loaded
Source:
Returns:
world object

(async) loadAsset(relativePath, file)

Utility method to fix the path and load the file, executes LoadAssetContainerAsync.
Parameters:
Name Type Description
relativePath path relative to current world directory
file file name to load
Source:

loaded(file, mesh)

Called after assets are loaded. By default calls initXR(). Subclasses typically override this with some spatial manipulations, e.g. scaling the world. Subclasses may, but are not required, call super.loaded()
Parameters:
Name Type Description
file world file that has loaded
mesh root mesh of the world
Source:

loadFailed(exception)

Called if loading the world fails. Passes the exception to this.onFailure handler if it exists, otherwise logs it to the console.
Parameters:
Name Type Description
exception whatever caused loading to fail
Source:

loadingStart(name)

Called when loading starts. Calls this.indicator.add if available.
Parameters:
Name Type Description
name
Source:

loadingStop(name)

Called when loading finishes. Calls this.indicator.remove if available.
Parameters:
Name Type Description
name
Source:

loadProgress(evt, name)

Called on loading progress, executes whatever this.onProgress contains, by default LoadProgressListener.
Parameters:
Name Type Description
evt
name
Source:

registerRenderLoop()

Register render loop.
Source:

remoteEvent(obj, field, node)

Receives a remote event. Default implementation handles only 'wrote' event, and sends it to the ChatLog.
Parameters:
Name Type Description
obj a VRObject that has changed
field a field that has changed, obj[field] contains the actual value
node root node in the scene that has received event, may be null
Source:

removeSelectionPredicate()

Remove a selection predicate function
Source:

setMeshCollisions(mesh, state)

Enable or disable collisions for a mesh. Override to fine-tune collisions.
Parameters:
Name Type Description
mesh
state
Source:

thirdPerson()

Activate third person camera (this.camera3p), if available. Makes a call to AvatarController method that applies camera rotation and takes care of everything else.
Source:

thirdPersonCamera(camera1p)

Utility method, creates 3rd person camera. Requires 1st person UniversalCamera already set, and sets rotation and direction based on it.
Parameters:
Name Type Description
camera1p 1st person UniversalCamera, defaults to this.camera
Source:
Returns:
created 3rd person ArcRotateCamera this.camera3p

trackXrDevices()

Called by VRHelper once XR devices are initialized. Default implementation does nothing.
Source:

universalCamera(pos, name)

Utility method, creates a UniversalCamera and sets defaults: gravity, collisions, ellipsoid, keys.
Parameters:
Name Type Default Description
pos Vector3 to position camera at
name Universal Camera optional camera name, default Universal Camera
Source:

write(text)

Write some text to world chat. Usually text appears above avatar's head and/or in chat log, but this method only sends own 'wrote' event.
Parameters:
Name Type Description
text something to say
Source: