From 5d46714c3d20642d1cbb9da66fca04b13bbc72a5 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 10 Jan 2019 21:43:13 +0800 Subject: [PATCH] camera with view --- cocos/3d/framework/camera-component.ts | 23 +++++----- cocos/core/root.ts | 4 ++ cocos/renderer/scene/camera.ts | 59 +++++++++++++++++++------- cocos/renderer/scene/render-scene.ts | 16 +++---- playground/simple.html | 12 ------ 5 files changed, 64 insertions(+), 50 deletions(-) diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index e23e1c7d11..e41313ff37 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -99,6 +99,8 @@ export class CameraComponent extends Component { protected _clearFlags = CameraClearFlag.SOLID_COLOR; @property protected _rect = new Rect(0, 0, 1, 1); + @property + protected _targetDisplay = 0; protected _camera: Camera | null = null; @@ -279,23 +281,20 @@ export class CameraComponent extends Component { if (this._camera) { this._camera.viewport = val; } } + @property + get targetDisplay () { + return this._targetDisplay; + } + set targetDisplay (val) { + this._targetDisplay = val; + } + public onLoad () { } public onEnable () { - this._camera = this._getRenderScene().createCamera(this.name); - this._camera.node = this.node; - this.projection = this._projection; - this.fov = this._fov; - this.orthoHeight = this._orthoHeight; - this.near = this._near; - this.far = this._far; - this.color = this._color; - this.depth = this._depth; - this.stencil = this._stencil; - this.clearFlags = this._clearFlags; - this.rect = this._rect; + this._camera = this._getRenderScene().createCamera(this); } public onDisable () { diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 4bf75dd063..3896c88d95 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -25,6 +25,10 @@ export class Root { return this._mainWindow; } + public get windows (): GFXWindow[] { + return this._windows; + } + public get pipeline (): RenderPipeline | null { return this._pipeline; } diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index acbeb0a130..6af66580ac 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -1,13 +1,30 @@ import { frustum, ray } from '../../3d/geom-utils'; -import { Mat4, Rect } from '../../core/value-types'; -import { color4, lerp, mat4, vec3 } from '../../core/vmath'; +import { Color, Mat4, Rect } from '../../core/value-types'; +import { color4, lerp, mat4, toRadian, vec3 } from '../../core/vmath'; import { GFXClearFlag, IGFXColor } from '../../gfx/define'; +import { RenderView, RenderViewPriority } from '../../pipeline/render-view'; import { Node } from '../../scene-graph/node'; import { RenderScene } from './render-scene'; export enum CameraProjection { - PERSPECTIVE, ORTHO, + PERSPECTIVE, +} + +export interface ICameraInfo { + name: string; + node: Node; + projection: number; + fov: number; + orthoHeight: number; + near: number; + far: number; + color: Color; + depth: number; + stencil: number; + clearFlags: GFXClearFlag; + rect: Rect; + targetDisplay: number; } const v_a = cc.v3(); @@ -36,30 +53,42 @@ export class Camera { private _matViewProjInv: mat4 = new Mat4(); private _frustum: frustum = new frustum(); private _node: Node | null = null; + private _view: RenderView | null = null; - constructor (scene: RenderScene, name: string) { + constructor (scene: RenderScene, info: ICameraInfo) { this._scene = scene; this._name = name; - this._proj = CameraProjection.PERSPECTIVE; + this._node = info.node; + this._proj = info.projection; + this._fov = toRadian(info.fov); + this._orthoHeight = info.orthoHeight; + this._nearClip = info.near; + this._farClip = info.far; + this._viewport = info.rect; - const win = scene.root.mainWindow; + const c = info.color; + color4.set(this._clearColor, c.r / 255, c.g / 255, c.b / 255, c.a / 255); + this._clearDepth = info.depth; + this._clearStencil = info.stencil; + this._clearFlag = info.clearFlags; + + const win = scene.root.windows[info.targetDisplay - 1] || scene.root.mainWindow; if (win) { this._width = win.width; this._height = win.height; + const view = this._view = scene.root.createView({ + name: `${name}View`, + window: win, + priority: RenderViewPriority.GENERAL, + width: win.width, + height: win.height, + }); + if (view) { view.attach(this); } } else { this._width = 1; this._height = 1; } - this._aspect = this._width / this._height; - this._orthoHeight = 10; - this._fov = Math.PI / 4; - this._nearClip = 1.0; - this._farClip = 10000.0; - - this._clearDepth = 1; - this._clearStencil = 0; - this._clearFlag = GFXClearFlag.COLOR | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL; } public resize (width: number, height: number) { diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index bc62661518..52fe7ba324 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -1,6 +1,7 @@ -import { Root, _createSceneFun } from '../../core/root'; +import { _createSceneFun, Root } from '../../core/root'; +import { IGFXColor } from '../../gfx/define'; import { Node } from '../../scene-graph/node'; -import { Camera } from './camera'; +import { Camera, ICameraInfo } from './camera'; import Light from './light'; import Model from './model'; @@ -28,10 +29,6 @@ export class RenderScene { return this._cameras; } - public get mainCamera (): Camera | null { - return this._mainCamera; - } - public get lights (): Light[] { return this._lights; } @@ -48,7 +45,6 @@ export class RenderScene { private _name: string = ''; private _nodes: Map = new Map(); private _cameras: Camera[] = []; - private _mainCamera: Camera | null = null; private _lights: Light[] = []; private _models: Model[] = []; private _modelId: number = 0; @@ -59,7 +55,6 @@ export class RenderScene { public initialize (info: IRenderSceneInfo): boolean { this._name = info.name; - this._mainCamera = this.createCamera('mainCamera'); return true; } @@ -94,8 +89,8 @@ export class RenderScene { } } - public createCamera (name: string): Camera { - const camera = new Camera(this, name); + public createCamera (info: ICameraInfo): Camera { + const camera = new Camera(this, info); this._cameras.push(camera); return camera; } @@ -111,7 +106,6 @@ export class RenderScene { public destroyCameras () { this._cameras = []; - this._mainCamera = null; } public getCamera (name: string): Camera | null { diff --git a/playground/simple.html b/playground/simple.html index d54ca1a78b..02559d6886 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -63,18 +63,6 @@ let mainWindow = root.mainWindow; cc.director.runSceneImmediate(scene); - let view = root.createView({ - name: "defaultView", - window: mainWindow, - //priority: cc.RenderViewPriority.GENERAL, - priority: 100, - width: mainWindow.width, - height: mainWindow.height, - }); - view.attach(camComp._camera); - - - // let camera = scene.mainCamera; // camera.position = { x: 0.0, y: 0.0, z: 3.0 }; // camera.direction = { x: 0.0, y: 0.0, z: -1.0 };