add ui 3d model rendering (#4108)

This commit is contained in:
zxg0622
2019-03-27 15:26:57 +08:00
committed by Weiliang Wang
parent 6f5db66104
commit daa2542ef9
9 changed files with 211 additions and 63 deletions
@@ -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 () {
+2 -2
View File
@@ -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 () {
+45
View File
@@ -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) {
}
}
@@ -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();
}
}
+2 -29
View File
@@ -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;
// 特殊渲染标记,在可渲染情况下,因为自身某个原因不给予渲染
+1
View File
@@ -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';
+4
View File
@@ -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();
+12
View File
@@ -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);
}
}
+69 -29
View File
@@ -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);
}