Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Fix/scene #424

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions docs/scene.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ type: Core

Scene can manage the entity tree, especially large game scenes. For example, **scene1** and **scene2** are two different scenes and do not need to be loaded for activation and rendering at the same time. Then we can divide them into different scenes through modeling software or code logic. Activating the corresponding scenes separately or merging scenes at the appropriate time.

Only one active scene `engine.sceneManager.activeScene` will be rendered under an Engine. Each Scene can have multiple root entities, and we manage the entire entity tree through the Scene object.

## Scene management

| Properties | Explanation |
| :------------------------------------------------- | :------- |
| Properties | Explanation |
| :------------------------------------------------- | :-------------- |
| [activeScene](${api}core/SceneManager#activeScene) | Activated scene |

| Methods | Explanation |
| :------------------------------------------------- | :------- |
| [mergeScenes](${api}core/SceneManager#mergeScenes) | Merge scenes|
| [loadScene](${api}core/SceneManager#loadScene) | Load scene |
| Methods | Explanation |
| :------------------------------------------------- | :----------- |
| [mergeScenes](${api}core/SceneManager#mergeScenes) | Merge scenes |
| [loadScene](${api}core/SceneManager#loadScene) | Load scene |

### Basic usage

Expand Down Expand Up @@ -54,7 +56,7 @@ engine.sceneManager.activeScene = destScene;
If you want to load the **Scene** asset in the application, you can pass an url to `engine.sceneManager.loadScene` method.

```typescript
const sceneUrl = '...';
const sceneUrl = "...";

const scenePromise = engine.sceneManager.loadScene(sceneUrl);

Expand All @@ -64,7 +66,11 @@ scenePromise.then((scene) => {
});
```

#### 4. Set background of scene
#### 4. Destroy scene

Call `scene.destroy()` will destroy the scene.

#### 4. Set background of scene

The scene background supports adding pure colors and sky:

Expand All @@ -78,7 +84,7 @@ background.solidColor.set(1, 1, 1, 1); // White

// Add a skybox background
background.mode = BackgroundMode.Sky;
const skyMaterial = (background.sky.material = new SkyBoxMaterial(engine)); // Set the material of skybox
const skyMaterial = (background.sky.material = new SkyBoxMaterial(engine)); // Set the material of skybox
skyMaterial.textureCubeMap = textureCube; // Set the cube texture of material
background.sky.mesh = PrimitiveMesh.createCuboid(engine, 2, 2, 2); // Set the mesh of the skybox
```
Expand All @@ -94,15 +100,14 @@ AmbientLight setting of the scene:
```typescript
const scene = engine.sceneManager.activeScene;
scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1);

```

## Entity tree management

### Basic usage

```typescript
const engine = new WebGLEngine('demo');
const engine = new WebGLEngine("demo");
const scene = engine.sceneManager.activeScene;

// Create root entity
Expand Down Expand Up @@ -134,7 +139,7 @@ const entity2 = scene.getRootEntity(2);
Note that if the rendered image is need to show on the screen or off-screen, the [Camera](${api}core/Camera) must be added on the entity in the current _scene_. The method of mounting the camera is as follows:

```typescript
const cameraEntity = rootEntity.createChild('camera');
const cameraEntity = rootEntity.createChild("camera");

cameraEntity.addComponent(Camera);
```
38 changes: 22 additions & 16 deletions docs/scene.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type: 核心

Scene 作为场景单元,可以方便场景的实体树管理,尤其是大型游戏场景。如,**scene1** 和 **scene2** 作为两个不同的场景,不需要同时加载激活和渲染,那么我们完全可以通过建模软件或者代码逻辑,将之划分为不同的场景,在不同的时机分别地激活相应场景,或者合并场景。

每个 Engine 下面只会渲染一个激活的场景 `engine.sceneManager.activeScene`。每个 Scene 可以有多个根实体,我们通过 Scene 对象去管理整个实体树。

## 场景管理

| 属性名称 | 解释 |
Expand Down Expand Up @@ -51,20 +53,25 @@ engine.sceneManager.activeScene = destScene;

#### 3. 加载 Scene

如果想要加载 **Scene** 资产作为应用中的一个场景,可以使用 `engine.sceneManager.loadScene` 传入 url 即可。
如果想要加载 **Scene** 资产作为应用中的一个场景,可以使用 `engine.resourceManager.load` 传入 url 即可。

```typescript
const sceneUrl = '...';

const scenePromise = engine.sceneManager.loadScene(sceneUrl);
const sceneUrl = "...";

// 至此,加载的场景已经激活,如果还想对加载到的 scene 进行后续操作,如下:
scenePromise.then((scene) => {
console.log(scene);
engine.resourceManager.load({ type: AssetType.Scene, url: "..." }).then(scene=>{
engine.sceneManager.activeScene = scene;
});

```

#### 4. 设置场景背景
> 此 api 更多在编辑器场景中使用,后续编辑器开放后,同时也会开放场景格式标准。

#### 4. 场景销毁

调用 `scene.destroy()` 即可销毁场景。


#### 5. 设置场景背景

目前场景背景支持添加纯色、天空和纹理背景。纯色和天空相对简单,代码示例如下:

Expand Down Expand Up @@ -94,34 +101,33 @@ background.texture = texture;

目前纹理适配模式有以下三种:

| 适配模式 | 说明 |
| --------------- | -------------------------------------------------- |
| [AspectFitWidth](${api}core/BackgroundTextureFillMode#AspectFitWidth) | 保持宽高比,把纹理宽缩放至 Canvas 的宽,上下居中。 |
| 适配模式 | 说明 |
| --- | --- |
| [AspectFitWidth](${api}core/BackgroundTextureFillMode#AspectFitWidth) | 保持宽高比,把纹理宽缩放至 Canvas 的宽,上下居中。 |
| [AspectFitHeight](${api}core/BackgroundTextureFillMode#AspectFitHeight) | 保持宽高比,把纹理高缩放至 Canvas 的高,左右居中。 |
| [Fill](${api}core/BackgroundTextureFillMode#Fill) | 把纹理的宽高填满 Canvas 的宽高。 |
| [Fill](${api}core/BackgroundTextureFillMode#Fill) | 把纹理的宽高填满 Canvas 的宽高。 |

默认的适配模式是 `BackgroundTextureFillMode.AspectFitHeight`。

Playground 示例如下:

<playground src="background.ts"></playground>

#### 5. 设置场景环境光
#### 6. 设置场景环境光

场景环境光(AmbientLight)设置:

```typescript
const scene = engine.sceneManager.activeScene;
scene.ambientLight.diffuseSolidColor.set(1, 1, 1, 1);

```

## 实体树管理

### 基本用法

```typescript
const engine = new WebGLEngine('demo');
const engine = new WebGLEngine("demo");
const scene = engine.sceneManager.activeScene;

// 创建根实体
Expand Down Expand Up @@ -153,7 +159,7 @@ const entity2 = scene.getRootEntity(2);
需要注意的是,当我们熟悉了 [Engine](${api}core/Engine) 和 [Scene](${api}core/Scene) 之后,如果想要将渲染画面输出到屏幕上或者进行离屏渲染,我们还得确保当前 _scene_ 的实体树上挂载了 [Camera](${api}core/Camera),挂载相机的方法如下:

```typescript
const cameraEntity = rootEntity.createChild('camera');
const cameraEntity = rootEntity.createChild("camera");

cameraEntity.addComponent(Camera);
```