add a new scene manager

This commit is contained in:
jerrywwl
2019-01-02 15:28:51 +08:00
parent 01a2b85f5b
commit 56b53b2298
6 changed files with 237 additions and 35 deletions
+4
View File
@@ -711,6 +711,10 @@ class Director extends EventTarget {
cc.Camera.main.backgroundColor = clearColor;
}
get root() {
return this._root;
}
/**
* !#en Returns current logic Scene.
* !#zh 获取当前逻辑场景。
+39 -23
View File
@@ -1,13 +1,17 @@
import Scene from "../renderer/scene/scene";
import { GFXWindow, GFXWindowInfo } from "../gfx/window";
import { GFXDevice } from "../gfx/device";
import { RenderView, RenderViewInfo } from "../pipeline/render-view";
import { RenderView, RenderViewInfo, RenderViewPriority } from "../pipeline/render-view";
import { RenderPipeline } from "../pipeline/render-pipeline";
import { ForwardPipeline } from "../pipeline/forward/forward-pipeline";
import { SceneManager, SceneManagerInfo } from "../scene/scene-manager";
export interface RootInfo {
};
export interface SceneInfo {
name: string;
};
export class Root {
constructor(device: GFXDevice) {
@@ -15,6 +19,11 @@ export class Root {
}
public initialize(info: RootInfo): boolean {
if (!this._device.mainWindow) {
return false;
}
this._mainWindow = this._device.mainWindow;
let pipeline = new ForwardPipeline(this);
@@ -29,7 +38,7 @@ export class Root {
public destroy() {
this.destroyViews();
this.destroyScenes();
this.destroySceneManagers();
if (this._pipeline) {
this._pipeline.destroy();
@@ -39,15 +48,14 @@ export class Root {
public frameMove(deltaTime: number) {
if (this._pipeline) {
for (let i = 0; i < this._views.length; ++i) {
this._pipeline.render(this._views[i]);
for (let i = 0; i < this._views.length; ++i) {
let view = this._views[i];
if(view.isEnable() && view.isAttached) {
(<RenderPipeline>this._pipeline).render(view);
}
}
if (this._device) {
this._device.present();
}
(<GFXDevice>this._device).present();
}
public createWindow(info: GFXWindowInfo): GFXWindow | null {
@@ -79,23 +87,31 @@ export class Root {
this._windows = [];
}
public createScene(): Scene | null {
let scene = new Scene;
this._scenes.push(scene);
return scene;
public createSceneManager(info: SceneManagerInfo): SceneManager | null {
let scene = new SceneManager;
if(scene.initialize(info)) {
this._sceneMgrs.push(scene);
return scene;
} else {
return null;
}
}
public destroyScene(scene: Scene) {
for (let i = 0; i < this._scenes.length; ++i) {
if (this._scenes[i] === scene) {
this._scenes.splice(i);
public destroySceneManager(sceneMgr: SceneManager) {
for (let i = 0; i < this._sceneMgrs.length; ++i) {
if (this._sceneMgrs[i] === sceneMgr) {
sceneMgr.destroy();
this._sceneMgrs.splice(i);
return;
}
}
}
public destroyScenes() {
this._scenes = [];
public destroySceneManagers() {
for (let i = 0; i < this._sceneMgrs.length; ++i) {
this._sceneMgrs[i].destroy();
}
this._sceneMgrs = [];
}
public createView(info: RenderViewInfo): RenderView | null {
@@ -139,8 +155,8 @@ export class Root {
return this._pipeline;
}
public get scenes(): Scene[] {
return this._scenes;
public get sceneManagers(): SceneManager[] {
return this._sceneMgrs;
}
public get views(): RenderView[] {
@@ -148,9 +164,9 @@ export class Root {
}
private _device: GFXDevice;
private _mainWindow: GFXWindow | null = null;
private _windows: GFXWindow[] = [];
private _mainWindow: GFXWindow | null = null;
private _pipeline: RenderPipeline | null = null;
private _scenes: Scene[] = [];
private _sceneMgrs: SceneManager[] = [];
private _views: RenderView[] = [];
};
+37 -10
View File
@@ -1,8 +1,13 @@
import { Scene } from "../renderer/scene/scene";
import { Camera } from "../renderer/scene/camera";
import { GFXWindow } from "../gfx/window";
import { SceneManager } from "../scene/scene-manager";
export enum RenderViewPriority {
GENERAL = 100,
};
export interface RenderViewInfo {
name: string;
window: GFXWindow;
priority: number;
width: number;
@@ -23,6 +28,7 @@ export class RenderView {
return false;
}
this._name = info.name;
this._window = info.window;
this._priority = info.priority;
this._width = info.width;
@@ -42,14 +48,32 @@ export class RenderView {
}
public attach(scene: Scene, camera: Camera) {
this._scene = scene;
public attach(camera: Camera) {
this._sceneMgr = camera.sceneMgr;
this._camera = camera;
this._isAttached = true;
}
public detach() {
this._scene = null;
this._sceneMgr = null;
this._camera = null;
this._isAttached = false;
}
public enable(isEnable: boolean) {
this._isEnable = isEnable;
}
public isEnable() : boolean {
return this._isEnable;
}
public isAttached() : boolean {
return this._isAttached;
}
public get name(): string {
return this._name;
}
public get window(): GFXWindow | null {
@@ -68,10 +92,13 @@ export class RenderView {
return this._height;
}
protected _window: GFXWindow | null = null;
protected _priority: number = 0;
protected _width: number = 0;
protected _height: number = 0;
protected _scene: Scene | null = null;
protected _camera: Camera | null = null;
private _name: string = "";
private _window: GFXWindow | null = null;
private _priority: number = 0;
private _width: number = 0;
private _height: number = 0;
private _sceneMgr: SceneManager | null = null;
private _camera: Camera | null = null;
private _isAttached: boolean = false;
private _isEnable: boolean = true;
};
+13 -1
View File
@@ -18,7 +18,11 @@ export default class Camera {
/**
* Setup a default perspective camera
*/
constructor() {
constructor(sceneMgr, name) {
this._sceneMgr = sceneMgr;
this._name = name;
this._poolID = -1;
this._node = null;
@@ -51,6 +55,14 @@ export default class Camera {
this._orthoHeight = 10;
}
get sceneMgr() {
return this._sceneMgr;
}
get name() {
return this._name;
}
/**
* Get the hosting node of this camera
* @returns {Node} the hosting node
+13 -1
View File
@@ -51,7 +51,11 @@ export default class Light {
/**
* Setup a default directional light with no shadows
*/
constructor() {
constructor(sceneMgr, name) {
this._sceneMgr = sceneMgr;
this._name = name;
this._poolID = -1;
this._node = null;
@@ -90,6 +94,14 @@ export default class Light {
this._shadowFrustumSize = 50; // used for directional light.
}
get sceneMgr() {
return this._sceneMgr;
}
get name() {
return this._name;
}
/**
* Get the hosting node of this camera
* @returns {Node} the hosting node
+131
View File
@@ -0,0 +1,131 @@
import Node from "../scene-graph/node";
import Camera from "../renderer/scene/camera";
import Light from "../renderer/scene/light";
export interface SceneManagerInfo {
name: string;
};
export interface SceneNodeInfo {
name: string;
isStatic: boolean;
//parent: Node;
}
export class SceneManager {
constructor() {
}
public initialize(info: SceneManagerInfo): boolean {
this._name = info.name;
return true;
}
public destroy() {
this.destroyCameras();
this.destroyLights();
this.destroyNodes();
}
public createNode(info: SceneNodeInfo): Node {
let node = new Node(name);
this._nodes.set(node.uuid, node);
return node;
}
public destroyNode(node: Node) {
this._nodes.delete(node.uuid);
}
public destroyNodes() {
this._nodes.clear();
}
public getNode(id: number): Node | null {
let node = this._nodes.get(id);
if (node) {
return node;
} else {
return null;
}
}
public createCamera(name: string): Camera {
let camera = new Camera(this, name);
this._cameras.push(camera);
return camera;
}
public destroyCamera(camera: Camera) {
for (let i = 0; i < this._cameras.length; ++i) {
if (this._cameras[i] === camera) {
this._cameras.slice(i);
return;
}
}
}
public destroyCameras() {
this._cameras = [];
}
public getCamera(name: string): Camera | null {
for (let i = 0; i < this._cameras.length; ++i) {
let camera = this._cameras[i];
if (camera.getName() === name) {
return camera;
}
}
return null;
}
public createLight(name: string): Light {
let light = new Light(this, name);
this._lights.push(light);
return light;
}
public destroyLight(light: Light) {
for (let i = 0; i < this._lights.length; ++i) {
if (this._lights[i] === light) {
this._lights.slice(i);
return;
}
}
}
public destroyLights() {
this._lights = [];
}
public getLight(name: string): Light | null {
for (let i = 0; i < this._lights.length; ++i) {
let light = this._lights[i];
if (light.getName() === name) {
return light;
}
}
return null;
}
public get name(): string {
return this._name;
}
public get cameras(): Camera[] {
return this._cameras;
}
public get lights(): Light[] {
return this._lights;
}
protected _name: string = "";
protected _nodes: Map<number, Node> = new Map;
protected _cameras: Camera[] = [];
protected _lights: Light[] = [];
};