# Scene

> Learn how to quickly build a world in a "scene" within Babylon.js.

Source: https://doc.babylonjs.com/features/featuresDeepDive/scene/fastBuildWorld

## How To Fast Build a World

There are a number of helpers that, once you have put models into a scene, add cameras, lights, and an environment that automatically adjust to the models, allowing you to view them quickly. You can then make adjustments as needed.

For beginners to Babylon.js, the [Fastest Build](https://doc.babylonjs.com/features/featuresDeepDive/scene/fastBuildWorld#fastest-build) and [Import and Fastest Build](https://doc.babylonjs.com/features/featuresDeepDive/scene/fastBuildWorld#import-and-fastest-build) sections will quickly give you a viewable world and are a good way to look at models. As such, they are level 1 material. It is worth considering working through Babylon101 and parts of the level 1 material to make the other information and details on this page easier to understand.

## Helpers

The following is a list of the methods of the `scene` object that help in fast building a world, with a link to their API description:

- [createDefaultCameraOrLight](https://doc.babylonjs.com/typedoc/classes/babylon.scene#createdefaultcameraorlight);
- [createDefaultCamera](https://doc.babylonjs.com/typedoc/classes/babylon.scene#createdefaultcamera);
- [createDefaultLight](https://doc.babylonjs.com/typedoc/classes/babylon.scene#createdefaultlight);
- [createDefaultEnvironment](https://doc.babylonjs.com/typedoc/classes/babylon.scene#createdefaultenvironment);
- [createDefaultSkybox](https://doc.babylonjs.com/typedoc/classes/babylon.scene#createdefaultskybox);

## Fastest Build

To build a world very quickly, just use `createDefaultCameraOrLight` along with `createDefaultEnvironment`, as in this example:

```javascript
scene.createDefaultCameraOrLight(true, true, true);
scene.createDefaultEnvironment();
```

- [Playground: The Quickest Way To Build A World](https://playground.babylonjs.com/#MJNICE) - Simple example of creating a world with createDefaultEnviornment.

You can see how the camera automatically adjusts by adding a second box and repositioning it:

- [Playground: Fast Build With Camera Adjust](https://playground.babylonjs.com/#MJNICE#3) - Simple example of building a world quickly and adjusting the camera.

More information about these methods, including details about parameters, can be found in the individual sections below.

## Create Default Camera or Light

As shown in the _Fastest Build_ section, the `createDefaultCameraOrLight` helper creates both a camera and a light in one line. The three parameters it takes are the same as for the `createDefaultCamera` method. The second parameter also applies to the light and replaces any existing camera or light when `true`. Accessing the camera or light works the same way as with the individual methods.

## Create Default Camera

`createDefaultCamera` takes three boolean parameters, all set to _false_ by default. They are:

- createArcRotateCamera: creates a free camera by default and an arc rotate camera when _true_;
- replace: when _true_ the created camera will replace the existing active one;
- attachCameraControls: when _true_ attaches control to the canvas.

This code will create an arc rotate camera, replace any existing camera, and attach the camera controls to the canvas:

```javascript
scene.createDefaultCamera(true, true, true);
```

- [Playground: Camera Helper Example (no light)](https://playground.babylonjs.com/#MJNICE#4) - Simple example of the camera helper with no light.
- [Playground: Camera Helper Example](https://playground.babylonjs.com/#MJNICE#5) - Simple example of the camera helper.

For a free camera:

```javascript
scene.createDefaultCamera(false, true, true);
```

- [Playground: Camera Helper Example (Free Camera)](https://playground.babylonjs.com/#MJNICE#6) - Simple example of the camera helper with a free camera.

The camera will adjust depending on the size and position of each mesh in the world.

- [Playground: Camera Helper Adjusting](https://playground.babylonjs.com/#MJNICE#7) - Simple example of the camera helper and adjusting to a large box.

### Accessing the Camera

Since creating the camera this way makes the helper-created camera the active camera, the simplest way to access it is:

```javascript
scene.createDefaultCamera(true, true, true);

var helperCamera = scene.activeCamera;
```

- [Playground: Camera Helper With Active Camera Changes](https://playground.babylonjs.com/#MJNICE#11) - Simple example of the camera helper and making changes to the active camera.

An alternative way to access the helper-created camera immediately after creating it is to use the fact that it will be the last one in the `scene.cameras` array:

```javascript
scene.createDefaultCamera(true, true, true);

var helperCamera = scene.cameras[scene.cameras.length - 1];

//OR

var helperCamera = scene.cameras.pop();
scene.cameras.push(helperCamera);
```

- [Playground: Camera Helper With Radius and Angle Changes](https://playground.babylonjs.com/#MJNICE#8) - Simple example of the camera helper and making changes to the camera radius and angles.

## Create Default Light

`createDefaultLight` takes just one boolean parameter, set to _false_ by default:

- replace: when _true_ the created light will replace all the existing ones; when _false_ and there are no existing lights a hemispherical light is created; when _false_ and lights already exist, no change is made to the scene.

When this method is used before any other lights are created, it is usually sufficient to use:

```javascript
scene.createDefaultLight();
```

### Accessing the Light

Provided you access the helper-created light immediately after creating it, it will be the last one in the `scene.lights` array.

You can obtain it using

```javascript
scene.createDefaultLight();

var helperLight = scene.lights[scene.lights.length - 1];

//OR

var helperLight = scene.lights.pop();
scene.lights.push(helperLight);
```

- [Playground: Light Helper With Direction and Color Change](https://playground.babylonjs.com/#MJNICE#9) - Simple example of the light helper and changing the light's color and direction.

## Create Default Environment

The simple code

```javascript
scene.createDefaultEnvironment();
```

adds a skybox and ground to the scene, sets a wide range of environmental parameters, and returns an [environmental helper](https://doc.babylonjs.com/typedoc/classes/babylon.environmenthelper).

You will also find below a helper for [just a skybox](https://doc.babylonjs.com/features/featuresDeepDive/scene/fastBuildWorld#create-default-skybox).

When you look at the following playground:

- [Playground: Playground Fast Build](https://playground.babylonjs.com/#MJNICE) - Simple example of creating the default enviornment in the playground.

it is not obvious that a skybox and ground have been added. The defaults have been set to be very subtle and to help give the objects a grounded feel.

Moving the camera further out shows that the skybox is constructed.

- [Playground: Skybox Example](https://playground.babylonjs.com/#MJNICE#1) - Simple example showing how to create a skybox.

You can also see the skybox and ground by using the options parameter and setting different values for the skybox texture and the ground color.

- [Playground: Skybox and Ground Changes](https://playground.babylonjs.com/#MJNICE#10) - Simple example of creating a skybox and ground and changing their properties.

### Options Parameters

As you can see in the above playground, the `createDefaultEnvironment` method takes an options parameter. The full range of environmental helper option properties is available in the [API](https://doc.babylonjs.com/typedoc/interfaces/babylon.ienvironmenthelperoptions).

So, for example

to prevent the creation of the skybox:

```javascript
var helper = scene.createDefaultEnvironment({
  createSkybox: false,
});
```

to enable ground reflection:

```javascript
var helper = scene.createDefaultEnvironment({
  enableGroundMirror: true,
});
```

when you see z-fighting with the ground, modify the `groundYBias` to a larger number:

```javascript
var helper = scene.createDefaultEnvironment({
  groundYBias: 0.01,
});
```

### Applicable Methods

Since the `createDefaultEnvironment` method returns an `environmentalHelper` object, all the properties and methods of this object (as described in the [API](https://doc.babylonjs.com/typedoc/classes/babylon.environmenthelper)) are available.

So, for example, if the environment color is not your preferred choice, you can modify it after creation:

```javascript
var helper = scene.createDefaultEnvironment();
helper.setMainColor(BABYLON.Color3.Teal());
```

- [Playground: Changing The Main Color](https://playground.babylonjs.com/#MJNICE#12) - Simple example of changing the scene's main color.

Or, if you wish to dispose of the ground after creating the environment, use:

```javascript
var helper = scene.createDefaultEnvironment();
helper.ground.dispose();
```

Or you can change the option parameters:

```javascript
var helper = scene.createDefaultEnvironment();
var options = {
  skyboxTexture: new BABYLON.CubeTexture("/textures/skybox", scene),
};
helper.updateOptions(options);
```

- [Playground: Changing Scene Options](https://playground.babylonjs.com/#MJNICE#13) - Simple example showcasing how to change options of the scene.

### Environmental Helper

NOTE: The environment helper relies exclusively on the [BackgroundMaterial](https://doc.babylonjs.com/features/featuresDeepDive/environment/backgroundMaterial) to be as efficient as possible.

## Create Default Skybox

The `createDefaultSkybox` method can be used when you do not want to create a full environment. The [parameters](https://doc.babylonjs.com/typedoc/classes/babylon.scene#createdefaultskybox) used determine how the skybox is created.

for example

```javascript
var texture = new BABYLON.CubeTexture("/assets/textures/SpecularHDR.dds", scene);
scene.createDefaultSkybox(texture, true, 100);
```

In this case the first two parameters used give the texture for the skybox and specify that [a PBRMaterial](https://doc.babylonjs.com/features/featuresDeepDive/materials/using/introToPBR) is to be used (second parameter, _true_) as opposed to a standard material (second parameter, _false_ - default value).

The third parameter defines the scale of your skybox (this value depends on the scale of your scene), the default value is _1000_.

- [Playground: Skybox Scale Example](https://playground.babylonjs.com/#MJNICE#14) - Simple example showing how to scale a skybox in the playground.

## Import and Fastest Build

To import models and build a world very quickly, just use `createDefaultCameraOrLight` along with `createDefaultEnvironment`, as in this example:

- [Playground: Importing Models and Quickly Building A World](https://playground.babylonjs.com/#MJNICE#15) - Simple example showcasing how to import models and quickly build a world.

The camera adjusts its position automatically to make the world viewable depending on the scale and position of the models. More information on importing models with the scene helpers follows below.

## Importing Models for Your World

Since the `createDefault...` helpers take into account any models in the scene, they can only be applied after the model is loaded, so they are placed in the callback function. For example:

```javascript
BABYLON.AppendSceneAsync("https://www.babylonjs.com/assets/DamagedHelmet/glTF/", "DamagedHelmet.gltf", scene).then(function ({ meshes }) {
  scene.createDefaultCameraOrLight(true, true, true);
  scene.createDefaultEnvironment();
});
```

There is then a problem. When creating a scene, Babylon.js checks for a camera. When importing models, the existence of a camera will be checked before the model has finished loading, so the scene will fail.

The solution is to replace the `createScene` function with the `delayCreateScene` function. Whether in a playground or in your own project this is a direct replacement.

**NOTE:** Other [scene loader](https://doc.babylonjs.com/features/featuresDeepDive/importers/loadingFileTypes) methods are available.

### Using Scene Helpers with Import

All the helper methods described earlier on this page behave in exactly the same way when importing models. Just remember to call them within the scene loader callback.

### Import Playground Examples

- [Playground: Simple Import and Main Color Change](https://playground.babylonjs.com/#10D6YT#33) - Simple example of importing a scene and changing the main color.
- [Playground: Importing Blur, Reflections, and Shadow](https://playground.babylonjs.com/#10D6YT#35) - Simple example showing how to import blur, reflections, and shadows.
