diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index 5bedee0787..fdad6ac0f2 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -74,7 +74,7 @@ const CameraClearFlag = Enum({ */ @ccclass('cc.CameraComponent') @menu('Components/CameraComponent') -@executeInEditMode +// @executeInEditMode export class CameraComponent extends Component { public static ProjectionType = ProjectionType; @@ -370,7 +370,7 @@ export class CameraComponent extends Component { }); this._camera.viewport = this._rect; - this._camera.fov = this._fov; + this._camera.fov = toRadian(this._fov); this._camera.nearClip = this._near; this._camera.farClip = this._far; this._camera.clearColor = color4.create(this._color.r / 255, this._color.g / 255, this._color.b / 255, this._color.a / 255) diff --git a/cocos/3d/framework/editor-camera-component.ts b/cocos/3d/framework/editor-camera-component.ts new file mode 100644 index 0000000000..16280c4e3a --- /dev/null +++ b/cocos/3d/framework/editor-camera-component.ts @@ -0,0 +1,145 @@ +import { CameraComponent } from "./camera-component"; +import { ccclass, executeInEditMode } from "../../core/data/class-decorator"; +import { RenderView } from "../../pipeline/render-view"; +import { Camera } from "../../renderer"; +import { color4, toRadian } from "../../core/vmath"; + +@ccclass('cc.EditorCameraComponent') +@executeInEditMode +export class EditorCameraComponent extends CameraComponent { + + private _uiEditorCamera: Camera | null = null; + + set projection (val) { + super.projection = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.projectionType = val; + } + } + + set fov (val) { + super.fov = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.fov = toRadian(val); + } + } + + set orthoHeight (val) { + super.orthoHeight = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.orthoHeight = val; + } + } + + set near (val) { + super.near = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.nearClip = val; + } + } + + set far (val) { + super.far = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.farClip = val; + } + } + + set color (val) { + super.color = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.clearColor = + color4.create(val.r / 255, val.g / 255, val.b / 255, val.a / 255); + } + } + + set depth (val) { + super.depth = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.clearDepth = val; + } + } + + set stencil (val) { + super.stencil = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.clearStencil = val; + } + } + + set clearFlags (val) { + super.clearFlags = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.clearFlag = val; + } + } + + set rect (val) { + super.rect = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.viewport = val; + } + } + + set screenScale (val) { + super.screenScale = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.screenScale = val; + } + } + + set targetDisplay (val) { + super.targetDisplay = val; + if (this._uiEditorCamera) { + this._uiEditorCamera.changeTargetDisplay(val); + } + } + + public onLoad () { + super.onLoad(); + } + + public onEnable () { + super.onEnable(); + } + + public onDisable () { + super.onDisable(); + } + + public onDestroy () { + super.onDestroy(); + if (this._uiEditorCamera) { + cc.director.root.ui.renderScene.destroyCamera(this._uiEditorCamera); + this._uiEditorCamera = null; + } + } + + protected _createCamera () { + const priorCamera = this._camera; + super._createCamera(); + if (this._camera !== priorCamera && this._camera) { + if (this._uiEditorCamera) { + cc.director.root.ui.renderScene.destroyCamera(this._uiEditorCamera); + this._uiEditorCamera = null; + } + this._uiEditorCamera = cc.director.root.ui.renderScene.createCamera({ + name: 'Editor UICamera', + node: this._camera.node, + projection: this._projection, + targetDisplay: this._targetDisplay, + priority: this._priority, + isUI: true, + stages: ['UIFlow'], + }); + this._uiEditorCamera!.viewport = this._camera.viewport; + this._uiEditorCamera!.fov = this._camera.fov; + this._uiEditorCamera!.nearClip = this._camera.nearClip; + this._uiEditorCamera!.farClip = this._camera.farClip; + this._uiEditorCamera!.clearColor = this._camera.clearColor; + this._uiEditorCamera!.clearDepth = this._camera.clearDepth; + this._uiEditorCamera!.clearStencil = this._camera.clearStencil; + this._uiEditorCamera!.clearFlag = this._camera.clearFlag; + } + } +} diff --git a/cocos/3d/framework/index.ts b/cocos/3d/framework/index.ts index 034a7d4d5b..f7ab5361cb 100644 --- a/cocos/3d/framework/index.ts +++ b/cocos/3d/framework/index.ts @@ -37,6 +37,7 @@ import { RenderableComponent } from './renderable-component'; import { SkinningModelComponent } from './skinning-model-component'; import { SphereLightComponent } from './sphere-light-component'; import { SpotLightComponent } from './spot-light-component'; +import { EditorCameraComponent } from './editor-camera-component'; export * from './physics'; export { @@ -56,6 +57,7 @@ export { cc.AnimationComponent = AnimationComponent; cc.AudioSourceComponent = AudioSourceComponent; cc.CameraComponent = CameraComponent; +cc.EditorComponent = EditorCameraComponent; cc.RenderableComponent = RenderableComponent; cc.ModelComponent = ModelComponent; cc.SkinningModelComponent = SkinningModelComponent; diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index 310a6d38af..689ce1bdba 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -178,7 +178,7 @@ export class CanvasComponent extends Component { targetDisplay: 0, priority: this._priority, isUI: true, - pipeline: 'ui', + stages: ['UIFlow'], }); this._camera!.fov = 45; diff --git a/cocos/components/component.js b/cocos/components/component.js index 50168cb464..dc00992681 100644 --- a/cocos/components/component.js +++ b/cocos/components/component.js @@ -196,8 +196,14 @@ class Component extends CCObject { static system = null; + _sceneGetter = null; + _getRenderScene() { - return this.node._scene._renderScene; + if (this._sceneGetter) { + return this._sceneGetter(); + } else { + return this.node._scene._renderScene; + } } constructor () { diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 6fc333228a..200023c092 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -4,7 +4,6 @@ import { GFXWindow, IGFXWindowInfo } from '../gfx/window'; import { ForwardPipeline } from '../pipeline/forward/forward-pipeline'; import { RenderPipeline } from '../pipeline/render-pipeline'; import { IRenderViewInfo, RenderView } from '../pipeline/render-view'; -import { UIPipeline } from '../pipeline/ui/ui-pipeline'; import { IRenderSceneInfo, RenderScene } from '../renderer/scene/render-scene'; import { UI } from '../renderer/ui/ui'; @@ -41,10 +40,6 @@ export class Root { return this._pipeline!; } - public get uiPipeline (): UIPipeline { - return this._uiPipeline!; - } - public get ui (): UI { return this._ui as UI; } @@ -69,11 +64,9 @@ export class Root { private _mainWindow: GFXWindow | null = null; private _curWindow: GFXWindow | null = null; private _pipeline: RenderPipeline | null = null; - private _uiPipeline: UIPipeline | null = null; private _ui: UI | null = null; private _scenes: RenderScene[] = []; private _views: RenderView[] = []; - private _uiViews: RenderView[] = []; private _frameTime: number = 0; constructor (device: GFXDevice) { @@ -100,12 +93,6 @@ export class Root { return false; } - this._uiPipeline = new UIPipeline(this); - if (!this._uiPipeline.initialize(info)) { - this.destroy(); - return false; - } - this._ui = new UI(this); if (!this._ui.initialize()) { this.destroy(); @@ -172,12 +159,6 @@ export class Root { } } - for (const view of this._uiViews) { - if (view.isEnable && view.window === this._mainWindow) { - this._uiPipeline!.render(view); - } - } - this._device.present(); } @@ -239,37 +220,20 @@ export class Root { view.initialize(info); // view.camera.resize(cc.game.canvas.width, cc.game.canvas.height); - if (!view.isUI) { - this._views.push(view); - this._views.sort((a: RenderView, b: RenderView) => { - return a.priority - b.priority; - }); - } else { - this._uiViews.push(view); - this._uiViews.sort((a: RenderView, b: RenderView) => { - return a.priority - b.priority; - }); - } + this._views.push(view); + this._views.sort((a: RenderView, b: RenderView) => { + return a.priority - b.priority; + }); return view; } public destroyView (view: RenderView) { - if (!view.isUI) { - for (let i = 0; i < this._views.length; ++i) { - if (this._views[i] === view) { - this._views.splice(i, 1); - view.destroy(); - return; - } - } - } else { - for (let i = 0; i < this._uiViews.length; ++i) { - if (this._uiViews[i] === view) { - this._uiViews.splice(i, 1); - view.destroy(); - return; - } + for (let i = 0; i < this._views.length; ++i) { + if (this._views[i] === view) { + this._views.splice(i, 1); + view.destroy(); + return; } } } @@ -279,10 +243,5 @@ export class Root { view.destroy(); } this._views = []; - - for (const view of this._uiViews) { - view.destroy(); - } - this._uiViews = []; } } diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index df683983f6..ca01fc0000 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -17,9 +17,11 @@ import { ToneMapFlow } from '../ppfx/tonemap-flow'; import { IRenderPipelineInfo, RenderPipeline } from '../render-pipeline'; import { RenderView } from '../render-view'; import { ForwardFlow } from './forward-flow'; +import { UIFlow } from '../ui/ui-flow'; export enum ForwardFlowPriority { FORWARD = 0, + UI = 10, } const _vec4Array = new Float32Array(4); @@ -103,6 +105,11 @@ export class ForwardPipeline extends RenderPipeline { priority: 0, }); + this.createFlow(UIFlow, { + name: 'UIFlow', + priority: ForwardFlowPriority.UI, + }); + return true; } diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index fbd88c7735..448cfb91ed 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -191,7 +191,9 @@ export abstract class RenderPipeline { this.updateUBOs(view); for (const flow of this._flows) { - flow.render(view); + if (view.stages.indexOf(flow.name) !== -1) { + flow.render(view); + } } } diff --git a/cocos/pipeline/render-view.ts b/cocos/pipeline/render-view.ts index 7db6da637e..14f8b858dd 100644 --- a/cocos/pipeline/render-view.ts +++ b/cocos/pipeline/render-view.ts @@ -16,6 +16,7 @@ export interface IRenderViewInfo { name: string; priority: number; isUI: boolean; + stages?: string[]; } export interface IRenderTargetInfo { @@ -47,6 +48,9 @@ export class RenderView { public set priority (val: number) { this._priority = val; + if (this.isUI) { + this._priority |= 1 << 30; + } } public set visibility (vis) { @@ -68,6 +72,10 @@ export class RenderView { return this._isUI; } + public get stages (): string[] { + return this._stages; + } + public set isOffscreen (isOffscreen: boolean) { this._isOffscreen = isOffscreen; } @@ -92,6 +100,7 @@ export class RenderView { private _camera: Camera; private _isEnable: boolean = true; private _isUI: boolean = false; + private _stages: string[] = []; private _isOffscreen: boolean = false; // For offscreen @@ -111,7 +120,13 @@ export class RenderView { this._name = info.name; this._isUI = info.isUI; - this._priority = info.priority; + this.priority = info.priority; + if (info.stages) { + this._stages.push(...info.stages); + } else { + this._stages.push('ForwardFlow'); + this._stages.push('ToneMapFlow'); + } return true; } diff --git a/cocos/pipeline/ui/ui-pipeline.ts b/cocos/pipeline/ui/ui-pipeline.ts deleted file mode 100644 index 8f357eabe8..0000000000 --- a/cocos/pipeline/ui/ui-pipeline.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { Root } from '../../core/root'; -import { mat4 } from '../../core/vmath'; -import { UBOGlobal } from '../define'; -import { IRenderPipelineInfo, RenderPipeline } from '../render-pipeline'; -import { RenderView } from '../render-view'; -import { UIFlow } from './ui-flow'; - -const _vec4Array = new Float32Array(4); -const _mat4Array = new Float32Array(16); - -export class UIPipeline extends RenderPipeline { - - constructor (root: Root) { - super(root); - } - - public initialize (info: IRenderPipelineInfo): boolean { - - if (!this.createUBOs()) { - return false; - } - - // create flows - this.createFlow(UIFlow, { - name: 'UIFlow', - priority: 0, - }); - - return true; - } - - public destroy () { - this._destroy(); - } - - public rebuild () { - } - - protected updateUBOs (view: RenderView) { - const camera = view.camera; - mat4.array(_mat4Array, camera.matViewProj); - this._defaultUboGlobal!.view.set(_mat4Array, UBOGlobal.MAT_VIEW_PROJ_OFFSET); - - _vec4Array[0] = camera.exposure; - _vec4Array[1] = _vec4Array[0] * this._fpScaleInv; - _vec4Array[2] = 0.0; - _vec4Array[3] = 1.0 / _vec4Array[1]; - this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.EXPOSURE_OFFSET); - - // update ubos - this._globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this._defaultUboGlobal!.view.buffer); - } -} diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index ef8b1d8314..829b5a1c9f 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -73,6 +73,7 @@ export interface ICameraInfo { priority: number; pipeline?: string; isUI?: boolean; + stages?: string[]; } const v_a = cc.v3(); @@ -96,7 +97,7 @@ export class Camera { private _clearStencil: number = 0; private _clearDepth: number = 1.0; private _clearFlag: GFXClearFlag = GFXClearFlag.NONE; - private _clearColor: IGFXColor = {r: 0, g: 0, b: 0, a: 0}; + private _clearColor: IGFXColor = { r: 0, g: 0, b: 0, a: 0 }; private _viewport: Rect = new Rect(0, 0, 1, 1); private _matView: Mat4 = new Mat4(); private _matProj: Mat4 = new Mat4(); @@ -138,6 +139,7 @@ export class Camera { name: this._name, priority: this._priority, isUI, + stages: info.stages, }); this.changeTargetDisplay(info.targetDisplay); @@ -358,6 +360,7 @@ export class Camera { set visibility (vis) { this._visibility = vis; + this._view.visibility = vis; } get visibility () { return this._visibility; diff --git a/cocos/renderer/ui/ui-material.ts b/cocos/renderer/ui/ui-material.ts index be58a65f4f..afb2b3496c 100644 --- a/cocos/renderer/ui/ui-material.ts +++ b/cocos/renderer/ui/ui-material.ts @@ -38,10 +38,6 @@ export class UIMaterial { this._psos = new Pool(() => { const pso = this._pass!.createPipelineState()!; - if (!CC_EDITOR) { - pso.pipelineLayout.layouts[0].bindBuffer(UBOGlobal.BLOCK.binding, - cc.director.root.uiPipeline.globalBindings.get(UBOGlobal.BLOCK.name).buffer); - } return pso; }, 1); diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 9c84806271..133ad35a5c 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -24,6 +24,7 @@ import { Camera } from '../scene/camera'; import { RenderScene } from '../scene/render-scene'; import { UIBatchModel } from './ui-batch-model'; import { UIMaterial } from './ui-material'; +import { CameraComponent } from '../../3d'; export class UIDrawBatch { public camera: Camera | null = null; @@ -99,19 +100,10 @@ export class UI { constructor (private _root: Root) { this.device = _root.device; - if (CC_EDITOR) { - cc.director.on(cc.Director.EVENT_BEFORE_SCENE_LAUNCH, (scene) => { - this._scene = scene.renderScene; - // the models will be destroyed in renderScene - this._modelInUse.clear(); - this._uiModelPool = new Pool(() => this._scene.createModel(UIBatchModel, null), 2); - }); - } else { - this._scene = this._root.createScene({ - name: 'GUIScene', - }); - this._uiModelPool = new Pool(() => this._scene.createModel(UIBatchModel, null), 2); - } + this._scene = this._root.createScene({ + name: 'GUIScene', + }); + this._uiModelPool = new Pool(() => this._scene.createModel(UIBatchModel, null), 2); this._modelInUse = new CachedArray(10); this._batches = new CachedArray(64); @@ -154,6 +146,10 @@ export class UI { } } + public getRenderSceneGetter () { + return Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), 'renderScene')!.get!.bind(this); + } + public _getUIMaterial (mat: Material): UIMaterial { if (this._uiMaterials.has(mat.hash)) { return this._uiMaterials.get(mat.hash)!;