diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 0f575d2701..4a30166066 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -117,10 +117,11 @@ export class Root { for (const view of this._views) { if (view.isEnable()) { this._pipeline!.render(view); - this._ui!.render(view); } } + this._ui!.update(deltaTime); + ( this._device as GFXDevice).present(); } diff --git a/cocos/renderer/ui/label-widget.ts b/cocos/renderer/ui/label-widget.ts deleted file mode 100644 index df34264fb5..0000000000 --- a/cocos/renderer/ui/label-widget.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { UI } from './ui'; -import { UIWidget, UIWidgetType } from './ui-widget'; - -export class LabelWidget extends UIWidget { - - constructor (ui: UI) { - super(ui, UIWidgetType.LABEL); - } - - public update () { - - } -} diff --git a/cocos/renderer/ui/renderData.ts b/cocos/renderer/ui/renderData.ts new file mode 100644 index 0000000000..0de9969650 --- /dev/null +++ b/cocos/renderer/ui/renderData.ts @@ -0,0 +1,95 @@ + +import { Material } from '../../3d/assets/material'; +import { RecyclePool } from '../../3d/memop/recycle-pool'; + +class BaseRenderData { + public material: Material | null = null; + public vertexCount: number = 0; + public indiceCount: number = 0; +} + +export class RenderData extends BaseRenderData { + + get dataLength () { + return this._data.length; + } + + set dataLength (length: number) { + const data = this._data; + if (data.length !== length) { + // // Free extra data + const value = data.length; + let i = 0; + for (i = value; i < length; i++) { + data[i] = _dataPool.add(); + } + for (i = value; i > length; i--) { + _dataPool.remove(i); + data.splice(i, 1); + } + } + } + + public static add () { + const data = _pool.add(); + return { + pooID: _pool.length - 1, + data, + }; + } + + public static remove (idx: number) { + _pool.data[idx].clear(); + _pool.remove(idx); + } + public _data: number[] = []; + public _indices: number[] = []; + public _pivotX: number = 0; + public _pivotY: number = 0; + public _width: number = 0; + public _height: number = 0; + public uvDirty: boolean = true; + public vertDirty: boolean = true; + + public updateSizeNPivot (width: number, height: number, pivotX: number, pivotY: number) { + if (width !== this._width || + height !== this._height || + pivotX !== this._pivotX || + pivotY !== this._pivotY) { + this._width = width; + this._height = height; + this._pivotX = pivotX; + this._pivotY = pivotY; + this.vertDirty = true; + } + } + + public clear () { + this._data.length = 0; + this._indices.length = 0; + this._pivotX = 0; + this._pivotY = 0; + this._width = 0; + this._height = 0; + this.uvDirty = true; + this.vertDirty = true; + this.material = null; + this.vertexCount = 0; + this.indiceCount = 0; + } +} + +const _dataPool = new RecyclePool(128, () => { + return { + x: 0.0, + y: 0.0, + z: 0.0, + u: 0.0, + v: 0.0, + color: 0, + }; +}); + +const _pool = new RecyclePool(32, () => { + return new RenderData(); +}); diff --git a/cocos/renderer/ui/ui-batch-model.ts b/cocos/renderer/ui/ui-batch-model.ts new file mode 100644 index 0000000000..5f22e8e94b --- /dev/null +++ b/cocos/renderer/ui/ui-batch-model.ts @@ -0,0 +1,5 @@ +import { Model } from "../scene/model"; + +export class UIBatchModel extends Model { + +} diff --git a/cocos/renderer/ui/ui-widget.ts b/cocos/renderer/ui/ui-widget.ts deleted file mode 100644 index 8cd54ed5a8..0000000000 --- a/cocos/renderer/ui/ui-widget.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { UI } from './ui'; - -export enum UIWidgetType { - LABEL, - SPRITE, -} - -export abstract class UIWidget { - - protected _ui: UI; - protected _type: UIWidgetType; - protected _vertices: Float32Array | null = null; - protected _indices: Uint16Array | null = null; - - constructor (ui: UI, type: UIWidgetType) { - this._ui = ui; - this._type = type; - } - - public type (): UIWidgetType { - return this._type; - } - - public abstract update (); -} diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 00fd85d0ac..8fdb22f89f 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -1,57 +1,54 @@ + +import { MeshBuffer} from './mesh-buffer'; +// import { vfmt3D } from '../../2d/renderer/webgl/vertex-format'; +import { RecyclePool } from '../../3d/memop/recycle-pool'; import { Root } from '../../core/root'; -import { RenderView } from '../../pipeline/render-view'; import { RenderScene } from '../scene/render-scene'; -import { UIWidget } from './ui-widget'; +import { UIBatchModel } from './ui-batch-model'; export class UI { + // private _buffer: MeshBuffer | null = new MeshBuffer(this, vfmt3D); + private _batchedModels: UIBatchModel[] = []; + private _iaPool: RecyclePool | null = new RecyclePool(16, () => { + // return this._root.device.createInputAssembler({ - public get root (): Root { - return this._root; - } - - public get scene (): RenderScene { - return this._scene; - } - - private _root: Root; + // }); + }); + private _modelPool: RecyclePool | null = new RecyclePool(16, () => { + return this._scene.createModel(UIBatchModel); + }); private _scene: RenderScene; - private _widgets: UIWidget[] = []; - constructor (root: Root) { - this._root = root; + constructor (private _root: Root) { this._scene = this._root.createScene({ name: 'GUIScene', }); } - public initialize (): boolean { + public initialize () { return true; } public destroy () { - this.destroyWidgets(); + // this._reset(); } - public render (view: RenderView) { - + public addScreen (comp) { } - public createWidget (clazz: new () => T): T { - const widget = new clazz(); - this._widgets.push(widget); - return widget; + public removeScreen (comp) { } - public destroyWidget (widget: UIWidget) { - for (let i = 0; i < this._widgets.length; ++i) { - if (this._widgets[i] === widget) { - this._widgets.splice(i); - return; - } - } + public update (dt: number) { + const models = this._emit(); + this._render(models); } - public destroyWidgets () { - this._widgets = []; + private _emit (): UIBatchModel[] { + return this._batchedModels; + } + + private _render (models: UIBatchModel[]) { + } }