diff --git a/cocos/3d/framework/particle/renderer/particle-system-renderer.ts b/cocos/3d/framework/particle/renderer/particle-system-renderer.ts index 106a311085..ea86be42dc 100644 --- a/cocos/3d/framework/particle/renderer/particle-system-renderer.ts +++ b/cocos/3d/framework/particle/renderer/particle-system-renderer.ts @@ -133,9 +133,6 @@ export default class ParticleSystemRenderer extends RenderableComponent { this._particles = new RecyclePool(() => { return new Particle(this); }, 16); - this.onEnable(); - this._updateMaterialParams(); - this._updateModel(); } public onEnable () { @@ -151,6 +148,8 @@ export default class ParticleSystemRenderer extends RenderableComponent { this._model.node = this.node; } this._model.enabled = this.enabledInHierarchy; + this._updateMaterialParams(); + this._updateModel(); } public onDisable () { diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index 689ce1bdba..1fce47f4cc 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, - stages: ['UIFlow'], + flows: ['UIFlow'], }); this._camera!.fov = 45; @@ -223,8 +223,8 @@ export class CanvasComponent extends Component { public onDisable () { if (this._camera) { this._getRenderScene().destroyCamera(this._camera); - cc.director.root.ui.removeScreen(this); } + cc.director.root.ui.removeScreen(this); } public onDestroy () { diff --git a/cocos/3d/ui/components/ui-component.ts b/cocos/3d/ui/components/ui-component.ts new file mode 100644 index 0000000000..b10f1ada2e --- /dev/null +++ b/cocos/3d/ui/components/ui-component.ts @@ -0,0 +1,45 @@ +import { Component } from '../../../components/component'; +import { ccclass, executeInEditMode, executionOrder, property } from '../../../core/data/class-decorator'; +import { UI } from '../../../renderer/ui/ui'; + +@ccclass('cc.UIComponent') +@executionOrder(100) +@executeInEditMode +export class UIComponent extends Component { + + /** + * !#en render order, render order according to width, and arrange once under the same level node. + * !#zh 渲染先后顺序,按照广度渲染排列,同级节点下进行一次排列 + */ + @property + get priority () { + return this._priority; + } + + set priority (value) { + if (this._priority === value) { + return; + } + + this._priority = value; + } + + /** + * !#en find the rendered camera + * !#zh 查找被渲染相机 + */ + get visibility () { + return this._visibility; + } + + @property + protected _priority = 0; + + protected _visibility = -1; + + public updateAssembler (render: UI) { + } + + public postUpdateAssembler (render: UI) { + } +} diff --git a/cocos/3d/ui/components/ui-model-component.ts b/cocos/3d/ui/components/ui-model-component.ts new file mode 100644 index 0000000000..3a4501cffb --- /dev/null +++ b/cocos/3d/ui/components/ui-model-component.ts @@ -0,0 +1,74 @@ +import { ccclass, executionOrder, menu } from '../../../core/data/class-decorator'; +import { UIRenderComponent } from './ui-render-component'; +import { Component } from '../../../components/component'; +import { RenderableComponent } from '../../framework/renderable-component'; +import { UI } from '../../../renderer/ui/ui'; +import { Effect } from '../../../renderer'; +import { RenderPriority } from '../../../pipeline/define'; +import { UIComponent } from './ui-component'; + +@ccclass('cc.UIModelComponent') +@executionOrder(100) +@menu('UI/Model') +export class UIModelComponent extends UIComponent { + + private _modelComponent: RenderableComponent | null = null; + + public get modelComponent () { + return this._modelComponent; + } + + public onLoad () { + this._modelComponent = this.getComponent(cc.RenderableComponent) as RenderableComponent; + (this._modelComponent as any)._sceneGetter = cc.director.root.ui.getRenderSceneGetter(); + } + + public onEnable () { + this._fitUIRenderQueue(); + } + + private _fitUIRenderQueue () { + if (!this._modelComponent) { + return; + } + const matNum = this._modelComponent.sharedMaterials.length; + for (let i = 0; i < matNum; i++) { + const material = this._modelComponent.getMaterial(i, CC_EDITOR)! as any; + const passes = material.passes; + const ea = material.effectAsset!; + const techIdx = material.technique; + const passNum = passes.length; + let needReconstruct = false; + for (let j = 0; j < passNum; j++) { + if (!passes[j].blendState.targets[0].blend) { + needReconstruct = true; + const bs = passes[j].blendState.targets[0]; + bs.blend = true; + passes[j].overridePipelineStates(Effect.getPassesInfo(ea, techIdx)[j], { blendState: passes[j].blendState }); + } + } + if (needReconstruct) { + material._onPassesChange(); + } + } + for (let i = 0; i < matNum; i++) { + const passes = this._modelComponent.getMaterial(i, CC_EDITOR)!.passes; + for (const p of passes as any[]) { + p._priority = RenderPriority.MAX - 11; + } + } + } + + public updateAssembler (render: UI) { + if (this._modelComponent) { + render.commitComp.call(render, this); + return true; + } + + return false; + } + + public update () { + this._fitUIRenderQueue(); + } +} diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 79ceb5405d..844bf6d856 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -40,6 +40,7 @@ import { UI } from '../../../renderer/ui/ui'; import { Material } from '../../assets/material'; import { IAssembler, IAssemblerManager } from '../assembler/assembler'; import { CanvasComponent } from './canvas-component'; +import { UIComponent } from './ui-component'; import { UITransformComponent } from './ui-transfrom-component'; // hack @@ -58,7 +59,7 @@ ccenum(GFXBlendFactor); @executionOrder(100) @requireComponent(UITransformComponent) @executeInEditMode -export class UIRenderComponent extends Component { +export class UIRenderComponent extends UIComponent { public static BlendState = GFXBlendFactor; @@ -109,23 +110,6 @@ export class UIRenderComponent extends Component { this._updateBlendFunc(); } - /** - * !#en render order, render order according to width, and arrange once under the same level node. - * !#zh 渲染先后顺序,按照广度渲染排列,同级节点下进行一次排列 - */ - @property - get priority () { - return this._priority; - } - - set priority (value) { - if (this._priority === value) { - return; - } - - this._priority = value; - } - /** * !#en render color * !#zh 渲染颜色 @@ -172,14 +156,6 @@ export class UIRenderComponent extends Component { return this._material; } - /** - * !#en find the rendered camera - * !#zh 查找被渲染相机 - */ - get visibility () { - return this._visibility; - } - get renderData () { return this._renderData; } @@ -194,14 +170,11 @@ export class UIRenderComponent extends Component { @property protected _color: Color = Color.WHITE; @property - protected _priority = 0; - @property protected _sharedMaterial: Material | null = null; protected _assembler: IAssembler | null = null; protected _postAssembler: IAssembler | null = null; protected _renderDataPoolID = -1; - protected _visibility = -1; protected _renderData: RenderData | null = null; protected _renderDataDirty = false; // 特殊渲染标记,在可渲染情况下,因为自身某个原因不给予渲染 diff --git a/cocos/3d/ui/index.ts b/cocos/3d/ui/index.ts index e24a867a0b..12082bde54 100644 --- a/cocos/3d/ui/index.ts +++ b/cocos/3d/ui/index.ts @@ -13,6 +13,7 @@ import './components/slider-component'; import './components/sprite-component'; import './components/toggle-component'; import './components/toggle-group-component'; +import './components/ui-model-component'; import './components/ui-render-component'; import './components/ui-transfrom-component'; import './components/ui-transfrom-component'; diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 0665aa70ea..74aa709b7b 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -146,6 +146,10 @@ export abstract class RenderPipeline { return this._macros; } + public get defaultGlobalUBOData (): Float32Array { + return this._defaultUboGlobal!.view; + } + protected _root: Root; protected _device: GFXDevice; protected _queue: RenderQueue = new RenderQueue(); diff --git a/cocos/pipeline/ui/ui-flow.ts b/cocos/pipeline/ui/ui-flow.ts index 3a23ba3327..b6b45ac5f4 100644 --- a/cocos/pipeline/ui/ui-flow.ts +++ b/cocos/pipeline/ui/ui-flow.ts @@ -1,6 +1,8 @@ import { IRenderFlowInfo, RenderFlow } from '../render-flow'; import { RenderPipeline } from '../render-pipeline'; import { UIStage } from './ui-stage'; +import { RenderView } from '../render-view'; +import { UBOGlobal } from '../define'; export class UIFlow extends RenderFlow { @@ -37,4 +39,14 @@ export class UIFlow extends RenderFlow { public rebuild () { } + public render (view: RenderView) { + const isHDR = this.pipeline.defaultGlobalUBOData[UBOGlobal.EXPOSURE_OFFSET + 2]; + this.pipeline.defaultGlobalUBOData[UBOGlobal.EXPOSURE_OFFSET + 2] = 0; + const globalUBOBuffer = this.pipeline.globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!; + globalUBOBuffer.update(this.pipeline.defaultGlobalUBOData.buffer); + super.render(view); + this.pipeline.defaultGlobalUBOData[UBOGlobal.EXPOSURE_OFFSET + 2] = isHDR; + this.pipeline.globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this.pipeline.defaultGlobalUBOData.buffer); + } + } diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index e3dc1324a5..3a6c6d94e7 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -25,10 +25,13 @@ import { RenderScene } from '../scene/render-scene'; import { UIBatchModel } from './ui-batch-model'; import { UIMaterial } from './ui-material'; import { CameraComponent } from '../../3d'; +import { Model } from '../scene/model'; +import { UIComponent } from '../../3d/ui/components/ui-component'; export class UIDrawBatch { public camera: Camera | null = null; public bufferBatch: MeshBuffer | null = null; + public model: Model | null = null; public material: Material | null = null; public texView: GFXTextureView | null = null; public firstIdx: number = 0; @@ -58,6 +61,7 @@ export class UIDrawBatch { this.texView = null; this.firstIdx = 0; this.idxCount = 0; + this.model = null; } } @@ -230,39 +234,75 @@ export class UI { for (let i = 0; i < this._batches.length; ++i) { const batch = this._batches.array[i]; - const bindingLayout = batch.bindingLayout!; - bindingLayout.bindTextureView(0, batch.texView!); - bindingLayout.update(); + if (batch.model) { + if (batch.camera) { + batch.model.viewID = batch.camera.view.visibility; + } + for (let j = 0; j < batch.model.subModelNum; j++) { + batch.model.getSubModel(j).priority = batchPriority++; + } + } else { + const bindingLayout = batch.bindingLayout!; + bindingLayout.bindTextureView(0, batch.texView!); + bindingLayout.update(); - const ia = batch.bufferBatch!.ia!; - ia.firstIndex = batch.firstIdx; - ia.indexCount = batch.idxCount; + const ia = batch.bufferBatch!.ia!; + ia.firstIndex = batch.firstIdx; + ia.indexCount = batch.idxCount; - const uiModel = this._uiModelPool!.alloc(); - uiModel.initialize(ia, batch); - uiModel.enabled = true; - uiModel.getSubModel(0).priority = batchPriority++; - if (batch.camera) { - uiModel.viewID = batch.camera.view.visibility; + const uiModel = this._uiModelPool!.alloc(); + uiModel.initialize(ia, batch); + uiModel.enabled = true; + uiModel.getSubModel(0).priority = batchPriority++; + if (batch.camera) { + uiModel.viewID = batch.camera.view.visibility; + } + this._modelInUse.push(uiModel); } - this._modelInUse.push(uiModel); } } } - public commitComp (comp: UIRenderComponent, frame: SpriteFrame | null, assembler: IAssembler) { - const texView = frame && frame.getGFXTextureView(); - if (this._currMaterial.hash !== comp.material!.hash || - this._currTexView !== texView || - this._currCanvas !== comp.visibility - ) { - this.autoMergeBatches(); - this._currMaterial = comp.material!; - this._currTexView = texView; - this._currCanvas = comp.visibility; - } + public commitComp (comp: UIComponent, frame: SpriteFrame | null, assembler: IAssembler) { + if (comp instanceof UIRenderComponent) { + const renderComp = comp as UIRenderComponent; + const texView = frame && frame.getGFXTextureView(); + if (this._currMaterial.hash !== renderComp.material!.hash || + this._currTexView !== texView || + this._currCanvas !== renderComp.visibility + ) { + this.autoMergeBatches(); + this._currMaterial = renderComp.material!; + this._currTexView = texView; + this._currCanvas = renderComp.visibility; + } - assembler.fillBuffers(comp, this); + assembler.fillBuffers(renderComp, this); + } else { + // if the last comp is spriteComp, previous comps should be batched. + if (this._currMaterial !== this._emptyMaterial) { + this.autoMergeBatches(); + } + const uiCanvas = this.getScreen(comp.visibility); + const curDrawBatch = this._drawBatchPool.alloc(); + curDrawBatch.camera = uiCanvas && uiCanvas.camera; + curDrawBatch.model = (comp as any).modelComponent._model; + curDrawBatch.bufferBatch = null; + curDrawBatch.material = null; + curDrawBatch.texView = null; + curDrawBatch.firstIdx = 0; + curDrawBatch.idxCount = 0; + + curDrawBatch.pipelineState = null; + curDrawBatch.bindingLayout = null; + + // reset current render state to null + this._currMaterial = this._emptyMaterial; + this._currTexView = null; + this._currCanvas = comp.visibility; + + this._batches.push(curDrawBatch); + } } public autoMergeBatches (){ @@ -339,8 +379,8 @@ export class UI { sortList = node.children.slice(); sortList.sort((a, b) => { - const ca = a.getComponent(UIRenderComponent); - const cb = b.getComponent(UIRenderComponent); + const ca = a.getComponent(UIComponent); + const cb = b.getComponent(UIComponent); if (ca && cb) { return -(ca.priority - cb.priority); } @@ -361,12 +401,12 @@ export class UI { } this._walk(screen.node, (c: Node) => { - const render = c.getComponent(UIRenderComponent); + const render = c.getComponent(UIComponent); if (render && render.enabledInHierarchy) { render.updateAssembler(this); } }, (c: Node) => { - const render = c.getComponent(UIRenderComponent); + const render = c.getComponent(UIComponent); if (render && render.enabledInHierarchy) { render.postUpdateAssembler(this); }