Skip to main content

The scene, lights and camera

Background​

babylon.scene paints what is behind the model: one colour (backgroundColour), a straight or round gradient between two or many colours, an image, or any CSS background. A skybox (enableSkybox, enableSkyboxCustomTexture) surrounds the scene with a picture of an environment and lights the model with it, which is what makes metal look like metal; hideSkybox keeps the light and hides the picture.

Light and shadow​

drawDirectionalLight adds a sun: light from one direction everywhere, the usual choice for a model, with shadows when enableShadows is on. drawPointLight adds a bulb at a point. Shadows are drawn from a map of the scene seen from the light; in a large or a millimetre-scale scene, raise shadowGeneratorMapSize for sharper shadows and set shadowNormalBias in the scene's own units if the surfaces show stripes. fog fades distant geometry into a colour.

Camera​

The scene starts with an orbiting camera aimed at the origin. adjustActiveArcRotateCamera moves it and sets its limits: how close and how far it may go, how far it may turn, and how fast it pans and zooms. babylon.camera builds other cameras: arcRotate to orbit a target, free to fly, target to look from a fixed spot; activateCamera switches to one.

Physics and the engine​

enablePhysics turns on the physics engine with a gravity vector; in the editors that is Havok. babylon.engine hands you the engine and its canvas, for the renderer's own settings.

Every function​

Scene​

FunctionWhat it does
activateCameraMakes a camera the one the scene renders through, detaching the controls of the camera that was active before.
adjustActiveArcRotateCameraRepositions the default orbiting camera, the one named Camera, and sets its limits and sensitivities.
backgroundColourFills the background of the scene with one plain color and removes any canvas background image or gradient set before.
canvasBackgroundImageShows an image behind the scene with the CSS background options for how it repeats, scales, sits and scrolls; the scene's clear color becomes transparent so the image shows through.
canvasCSSBackgroundImagePaints any CSS background-image value behind the scene, a gradient or an image, by making the scene's clear color transparent and styling the canvas.
clearAllDrawnRemoves everything drawn from the scene: meshes, materials, textures, lights other than the default hemispheric one, transform nodes, shadow generators, fog and the environment texture, and restores the default camera when another was active.
drawDirectionalLightAdds a light that shines the same way everywhere, like the sun, along direction.
drawDirectionalLightNoReturnAdds a directional light to the scene, as drawDirectionalLight does, without giving it back; for scripts that only need the light to exist.
drawPointLightAdds a light that shines in every direction from a point, like a bulb, with an optional small glowing sphere at its position.
drawPointLightNoReturnAdds a point light to the scene, as drawPointLight does, without giving it back; for scripts that only need the light to exist.
enablePhysicsTurns on the physics engine for the scene with the given gravity, so bodies given physics fall and collide; the physics plugin must be set up on the context.
enableSkyboxSurrounds the scene with one of the built-in skyboxes and uses it as the environment lighting that reflective materials pick up.
enableSkyboxCustomTextureSurrounds the scene with a skybox loaded from your own texture and uses it as the environment lighting.
enableSkyboxFromTextureSurrounds the scene with a skybox built from a cube texture you loaded yourself and uses it as the environment lighting.
fogFades distant geometry into a color, the way haze does.
getActiveCameraGives the camera the scene currently renders through.
getSceneGives the scene every draw call goes into, for direct use of the BabylonJS API on it.
getShadowGeneratorsLists the shadow generators of the lights created through this library, one per light with shadows enabled; drawn meshes are registered with them as casters.
multiColorLinearGradientBackgroundPaints a straight gradient through several colors behind the scene, each at its own stop percentage; colors and stops must be the same length, or an error object comes back instead.
multiColorRadialGradientBackgroundPaints a round gradient through several colors behind the scene, each at its own stop percentage; colors and stops must be the same length, or an error object comes back instead.
onPointerDownSets the function that runs when a pointer button is pressed on the canvas, replacing any function set before.
onPointerMoveSets the function that runs whenever the pointer moves over the canvas, replacing any function set before; it runs often, so keep it light.
onPointerUpSets the function that runs when a pointer button is released on the canvas, replacing any function set before.
setAndAttachSceneMakes the given scene the one this library draws into, adding the shadow bookkeeping and root node it expects; for applications that create the scene themselves.
twoColorLinearGradientBackgroundPaints a straight gradient between two colors behind the scene, in the given direction, with the stops as percentages along it.
twoColorRadialGradientBackgroundPaints a round gradient between two colors behind the scene, spreading out from position in the given shape, with the stops as percentages from the center.
useRightHandedSystemSwitches the scene between the left-handed coordinate system BabylonJS uses by default and a right-handed one, the convention of most CAD tools and of glTF; the active camera is refreshed to match.

Lights​

FunctionWhat it does
shadowLight.setDirectionToTargetAims a shadow-casting light at a point in the scene, turning its direction to point from its position toward target.
shadowLight.setPositionMoves a shadow-casting light to a point in the scene; for a directional light the position sets where its shadows are computed from.

Camera​

FunctionWhat it does
arcRotate.createCreates a camera that orbits target at distance radius, controlled by the pointer on the canvas, and adds it to the scene without activating it.
free.createCreates a camera at position looking at target that the keyboard and pointer fly around, and adds it to the scene without activating it.
freezeProjectionMatrixStops a camera from recomputing its projection every frame, saving work when its field of view and clipping distances no longer change.
getPositionReads where a camera is in the scene, as a point.
getSpeedReads how fast a camera moves in response to its controls.
getTargetReads the point a camera is looking at.
makeCameraOrthographicSwitches a camera to orthographic projection, where objects keep their size whatever their distance, as in technical drawings.
makeCameraPerspectiveSwitches a camera back to perspective projection, where distant objects look smaller, the default for cameras.
setMaxZSets the far clipping distance of a camera: anything farther than maxZ scene units is not drawn.
setMinZSets the near clipping distance of a camera: anything closer than minZ scene units is not drawn.
setPositionMoves a camera to a point in the scene; a target camera keeps looking at its target from there.
setSpeedSets how fast a camera moves in response to its keyboard and pointer controls; 1 is the default pace.
setTargetTurns a camera to look at a point in the scene.
target.createCreates a camera at position looking at target with no navigation controls of its own, and adds it to the scene without activating it.
unfreezeProjectionMatrixLets a frozen camera recompute its projection again, needed after changing its field of view or clipping distances.

Engine​

FunctionWhat it does
getEngineGets the engine for the current context
getRenderingCanvasGets the rendering canvas on which scene cameras can be attached