From 0cb8509e952dd954fd96d7c6774090dda96357d5 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Fri, 2 Aug 2019 18:34:27 +0800 Subject: [PATCH] ui code adjust (#5008) --- cocos/3d/ui/assembler/label/ttf.ts | 94 ++++++++++++++---- cocos/3d/ui/assembler/sprite/simple.ts | 12 +-- cocos/3d/ui/assembler/utils.ts | 97 ------------------- cocos/3d/ui/components/graphics-component.ts | 21 ++-- cocos/3d/ui/components/label-component.ts | 34 ++----- cocos/3d/ui/components/mask-component.ts | 31 +++--- cocos/3d/ui/components/sprite-component.ts | 17 ++-- cocos/3d/ui/components/ui-component.ts | 18 +++- cocos/3d/ui/components/ui-render-component.ts | 52 ++++++---- cocos/3d/ui/mesh-buffer.ts | 2 +- 10 files changed, 162 insertions(+), 216 deletions(-) diff --git a/cocos/3d/ui/assembler/label/ttf.ts b/cocos/3d/ui/assembler/label/ttf.ts index 79fb3cf418..f3243aad4c 100644 --- a/cocos/3d/ui/assembler/label/ttf.ts +++ b/cocos/3d/ui/assembler/label/ttf.ts @@ -29,13 +29,12 @@ */ import * as js from '../../../../core/utils/js'; -import { Color, Mat4, Vec3 } from '../../../../core/math'; -import { RenderData } from '../../../../renderer/ui/renderData'; +import { Color, Vec3 } from '../../../../core/math'; import { UI } from '../../../../renderer/ui/ui'; import { LabelComponent } from '../../components/label-component'; import { IAssembler} from '../base'; -import { fillMeshVertices3D } from '../utils'; import { ttfUtils } from './ttfUtils'; +import { IRenderData } from '../../../../renderer/ui/renderData'; const WHITE = Color.WHITE; @@ -44,29 +43,86 @@ const WHITE = Color.WHITE; * 可通过 cc.UI.ttf 获取该组装器。 */ export const ttf: IAssembler = { - useModel: false, - createData (comp: LabelComponent) { - const renderData: RenderData|null = comp.requestRenderData(); + const renderData = comp.requestRenderData(); renderData!.dataLength = 4; renderData!.vertexCount = 4; renderData!.indiceCount = 6; - const datas = renderData!.datas; - datas[0].u = 0; - datas[0].v = 1; - datas[1].u = 1; - datas[1].v = 1; - datas[2].u = 0; - datas[2].v = 0; - datas[3].u = 1; - datas[3].v = 0; - return renderData as RenderData; + const vData = renderData.vData = new Float32Array(4 * 9); + + vData[3] = vData[21] = vData[22] = vData[31] = 0; + vData[4] = vData[12] = vData[13] = vData[30] = 1; + let offset = 5; + for (let i = 0; i < 4; i++) { + Color.array(vData, WHITE, offset); + offset += 9; + } + return renderData; }, fillBuffers (comp: LabelComponent, renderer: UI) { - fillMeshVertices3D(comp.node, renderer, comp.renderData!, WHITE); + const renderData = comp.renderData!; + const datas: IRenderData[] = renderData.datas; + const node = comp.node; + + let buffer = renderer.currBufferBatch!; + let vertexCount = renderData.vertexCount; + let vertexOffset = buffer.byteOffset >> 2; + let indiceOffset = buffer.indiceOffset; + let vertexId = buffer.vertexOffset; + const isRecreate = buffer.request(); + if (!isRecreate) { + buffer = renderer.currBufferBatch!; + vertexCount = 0; + indiceOffset = 0; + vertexId = 0; + } + + // buffer data may be reallocated, need get reference after request. + const vbuf = buffer.vData!; + const ibuf = buffer.iData!; + const vData = renderData.vData!; + const data0 = datas[0]; + const data3 = datas[3]; + /* */ + node.updateWorldTransform(); + // @ts-ignore + const pos = node._pos as Vec3; const rot = node._rot; const scale = node._scale; + const ax = data0.x * scale.x; const bx = data3.x * scale.x; + const ay = data0.y * scale.y; const by = data3.y * scale.y; + const qx = rot.x; const qy = rot.y; const qz = rot.z; const qw = rot.w; + const qxy = qx * qy; const qzw = qz * qw; + const qxy2 = qx * qx - qy * qy; + const qzw2 = qw * qw - qz * qz; + const cx1 = qzw2 + qxy2; + const cx2 = (qxy - qzw) * 2; + const cy1 = qzw2 - qxy2; + const cy2 = (qxy + qzw) * 2; + const x = pos.x; const y = pos.y; + // left bottom + vData[0] = cx1 * ax + cx2 * ay + x; + vData[1] = cy1 * ay + cy2 * ax + y; + // right bottom + vData[9] = cx1 * bx + cx2 * ay + x; + vData[10] = cy1 * ay + cy2 * bx + y; + // left top + vData[18] = cx1 * ax + cx2 * by + x; + vData[19] = cy1 * by + cy2 * ax + y; + // right top + vData[27] = cx1 * bx + cx2 * by + x; + vData[28] = cy1 * by + cy2 * bx + y; + + vbuf.set(vData, vertexOffset); + + // fill indice data + ibuf[indiceOffset++] = vertexId; + ibuf[indiceOffset++] = vertexId + 1; + ibuf[indiceOffset++] = vertexId + 2; + ibuf[indiceOffset++] = vertexId + 2; + ibuf[indiceOffset++] = vertexId + 1; + ibuf[indiceOffset++] = vertexId + 3; }, updateVerts (comp: LabelComponent) { @@ -84,10 +140,6 @@ export const ttf: IAssembler = { const datas = renderData!.datas; datas[0].x = -appx; datas[0].y = -appy; - datas[1].x = width - appx; - datas[1].y = -appy; - datas[2].x = -appx; - datas[2].y = height - appy; datas[3].x = width - appx; datas[3].y = height - appy; }, diff --git a/cocos/3d/ui/assembler/sprite/simple.ts b/cocos/3d/ui/assembler/sprite/simple.ts index 2f174d2363..d78afbc94d 100644 --- a/cocos/3d/ui/assembler/sprite/simple.ts +++ b/cocos/3d/ui/assembler/sprite/simple.ts @@ -97,7 +97,7 @@ export const simple: IAssembler = { let indiceOffset = buffer.indiceOffset; let vertexId = buffer.vertexOffset; - const isRecreate = buffer.request(4, 6); + const isRecreate = buffer.request(); if (!isRecreate) { buffer = renderer.currBufferBatch!; vertexOffset = 0; @@ -114,7 +114,7 @@ export const simple: IAssembler = { /* */ node.updateWorldTransform(); // @ts-ignore - const pos = node._pos; const rot = node._rot; const scale = node._scale; + const pos = node._pos as Vec3; const rot = node._rot; const scale = node._scale; const ax = data0.x * scale.x; const bx = data3.x * scale.x; const ay = data0.y * scale.y; const by = data3.y * scale.y; const qx = rot.x; const qy = rot.y; const qz = rot.z; const qw = rot.w; @@ -231,8 +231,8 @@ export const simple: IAssembler = { renderData.vertDirty = false; }, - updateUvs (sprite) { - const renderData = sprite.renderData; + updateUvs (sprite: SpriteComponent) { + const renderData = sprite.renderData!; const vData = renderData.vData!; const uv = sprite!.spriteFrame!.uv; vData[3] = uv[0]; @@ -247,8 +247,8 @@ export const simple: IAssembler = { renderData.uvDirty = false; }, - updateColor (sprite) { - const vData = sprite.renderData.vData; + updateColor (sprite: SpriteComponent) { + const vData = sprite.renderData!.vData; let colorOffset = 5; const color = sprite.color; diff --git a/cocos/3d/ui/assembler/utils.ts b/cocos/3d/ui/assembler/utils.ts index a34310344d..273d34cff7 100644 --- a/cocos/3d/ui/assembler/utils.ts +++ b/cocos/3d/ui/assembler/utils.ts @@ -5,86 +5,11 @@ import { Color, Mat4, Vec3 } from '../../../core/math'; import { RenderData } from '../../../renderer/ui/renderData'; import { UI } from '../../../renderer/ui/ui'; -import { MeshBuffer } from '../mesh-buffer'; import { INode } from '../../../core/utils/interfaces'; const vec3_temp = new Vec3(); const _worldMatrix = new Mat4(); -export function fillVertices (node: INode, buffer: MeshBuffer, renderData: RenderData, color: Color) { - const data = renderData.datas; - let vertexOffset = buffer.byteOffset >> 2; - - const vertexCount = renderData.vertexCount; - buffer.request(vertexCount, renderData.indiceCount); - - // buffer data may be realloc, need get reference after request. - const vbuf = buffer.vData!; - // uintbuf = buffer._uintVData; - - // let matrix = node._worldMatrix; - node.getWorldMatrix(_worldMatrix); - const matrix = _worldMatrix; - const a = matrix.m00; - const b = matrix.m01; - const c = matrix.m04; - const d = matrix.m05; - const tx = matrix.m12; - const ty = matrix.m13; - for (let i = 0; i < vertexCount; i++) { - const vert = data[i]; - vbuf[vertexOffset++] = vert.x * a + vert.y * c + tx; - vbuf[vertexOffset++] = vert.x * b + vert.y * d + ty; - vbuf[vertexOffset++] = vert.u; - vbuf[vertexOffset++] = vert.v; - Color.array(vbuf!, color, vertexOffset); - vertexOffset += 4; - } -} - -export function fillMeshVertices (node: INode, buffer: MeshBuffer, renderData: RenderData, color: Color) { - const datas = renderData.datas; - let vertexOffset = buffer.byteOffset >> 2; - - const vertexCount = renderData.vertexCount; - let indiceOffset = buffer.indiceOffset; - const vertexId = buffer.vertexOffset; - buffer.request(vertexCount, renderData.indiceCount); - - // buffer data may be realloc, need get reference after request. - const vbuf = buffer.vData!; - const ibuf = buffer.iData!; - - node.getWorldMatrix(_worldMatrix); - const matrix = _worldMatrix; - const a = matrix.m00; - const b = matrix.m01; - const c = matrix.m04; - const d = matrix.m05; - const tx = matrix.m12; - const ty = matrix.m13; - for (let i = 0; i < vertexCount; i++) { - const vert = datas[i]; - vbuf[vertexOffset++] = vert.x * a + vert.y * c + tx; - vbuf[vertexOffset++] = vert.x * b + vert.y * d + ty; - vbuf[vertexOffset++] = vert.u; - vbuf[vertexOffset++] = vert.v; - Color.array(vbuf!, color, vertexOffset); - vertexOffset += 4; - } - - // fill indice data - for (let i = 0, count = vertexCount / 4; i < count; i++) { - const start = vertexId + i * 4; - ibuf[indiceOffset++] = start; - ibuf[indiceOffset++] = start + 1; - ibuf[indiceOffset++] = start + 2; - ibuf[indiceOffset++] = start + 1; - ibuf[indiceOffset++] = start + 3; - ibuf[indiceOffset++] = start + 2; - } -} - export function fillVertices3D (node: INode, renderer: UI, renderData: RenderData, color: Color) { const datas = renderData.datas; let buffer = renderer.currBufferBatch!; @@ -174,28 +99,6 @@ export function fillMeshVertices3D (node: INode, renderer: UI, renderData: Rende } } -export function fillVerticesWithoutCalc (node: INode, buffer: MeshBuffer, renderData: RenderData, color: Color) { - const datas = renderData.datas; - let vertexOffset = buffer.byteOffset >> 2; - - const vertexCount = renderData.vertexCount; - buffer.request(vertexCount, renderData.indiceCount); - - // buffer data may be realloc, need get reference after request. - const vbuf = buffer.vData!; - // uintbuf = buffer._uintVData; - - for (let i = 0; i < vertexCount; i++) { - const vert = datas[i]; - vbuf[vertexOffset++] = vert.x; - vbuf[vertexOffset++] = vert.y; - vbuf[vertexOffset++] = vert.u; - vbuf[vertexOffset++] = vert.v; - Color.array(vbuf!, color, vertexOffset); - vertexOffset += 4; - } -} - export function fillVerticesWithoutCalc3D (node: INode, renderer: UI, renderData: RenderData, color: Color) { const datas = renderData.datas; let buffer = renderer.currBufferBatch!; diff --git a/cocos/3d/ui/components/graphics-component.ts b/cocos/3d/ui/components/graphics-component.ts index 7e224d666f..485af0d39f 100644 --- a/cocos/3d/ui/components/graphics-component.ts +++ b/cocos/3d/ui/components/graphics-component.ts @@ -207,9 +207,7 @@ export class GraphicsComponent extends UIRenderComponent { } public onEnable () { - if (super.onEnable) { - super.onEnable(); - } + super.onEnable(); if (this.model){ this.model.enabled = true; @@ -225,9 +223,7 @@ export class GraphicsComponent extends UIRenderComponent { } public onDestroy () { - if (super.onDestroy) { - super.onDestroy(); - } + super.onDestroy(); this._sceneGetter = null; if (this.model) { @@ -463,15 +459,6 @@ export class GraphicsComponent extends UIRenderComponent { (this._assembler as IAssembler).fill!(this); } - public updateAssembler (render: UI) { - if (super.updateAssembler(render)) { - render.commitModel(this, this.model, this._material); - return true; - } - - return false; - } - /** * @zh * 辅助材质实例化。可用于只取数据而无实体情况下渲染使用。特殊情况可参考:[[_instanceMaterial]] @@ -493,6 +480,10 @@ export class GraphicsComponent extends UIRenderComponent { } } + protected _render(render: UI) { + render.commitModel(this, this.model, this._material); + } + protected _instanceMaterial (){ this.helpInstanceMaterial(); } diff --git a/cocos/3d/ui/components/label-component.ts b/cocos/3d/ui/components/label-component.ts index 41ae3af6bb..288c9a0dfc 100644 --- a/cocos/3d/ui/components/label-component.ts +++ b/cocos/3d/ui/components/label-component.ts @@ -172,6 +172,7 @@ export class LabelComponent extends UIRenderComponent { */ @property({ displayOrder: 4, + multiline: true, }) get string () { return this._string; @@ -183,7 +184,6 @@ export class LabelComponent extends UIRenderComponent { } this._string = value; - this._checkStringEmpty(); this.updateRenderData(); } @@ -419,7 +419,6 @@ export class LabelComponent extends UIRenderComponent { this.font = null; this._flushAssembler(); this.updateRenderData(); - this._checkStringEmpty(); } // else if (!this._userDefinedFont) { // this.disableRender(); @@ -568,9 +567,9 @@ export class LabelComponent extends UIRenderComponent { @property private _string = 'label'; @property - private _horizontalAlign = HorizontalTextAlignment.LEFT; + private _horizontalAlign = HorizontalTextAlignment.CENTER; @property - private _verticalAlign = VerticalTextAlignment.TOP; + private _verticalAlign = VerticalTextAlignment.CENTER; @property private _actualFontSize = 0; @property @@ -628,7 +627,6 @@ export class LabelComponent extends UIRenderComponent { this.fontFamily = 'Arial'; } - this._checkStringEmpty(); this.updateRenderData(true); } @@ -651,7 +649,7 @@ export class LabelComponent extends UIRenderComponent { } public updateRenderData (force = false) { - this.markForUpdateRenderData(true); + this.markForUpdateRenderData(); if (force) { this._flushAssembler(); @@ -659,13 +657,8 @@ export class LabelComponent extends UIRenderComponent { } } - public updateAssembler (render: UI) { - if (super.updateAssembler(render) && this._texture) { - render.commitComp(this, this._texture.getGFXTextureView(), this._assembler!); - return true; - } - - return false; + protected _render (render: UI) { + render.commitComp(this, this._texture!.getGFXTextureView(), this._assembler!); } protected _updateColor () { @@ -677,7 +670,7 @@ export class LabelComponent extends UIRenderComponent { } protected _canRender () { - if (!super._canRender() || !this.node.width || !this.node.height){ + if (!super._canRender() || !this.node.width || !this.node.height || !this._string) { return false; } @@ -709,15 +702,6 @@ export class LabelComponent extends UIRenderComponent { } } - /** - * @zh - * 检查是否空字符串,空字符串不渲染。 - * - */ - private _checkStringEmpty () { - this._renderPermit = !!this.string; - } - private _flushMaterial () { const material = this._material; // Setup blend function for premultiplied ttf label texture @@ -768,8 +752,8 @@ export class LabelComponent extends UIRenderComponent { this._flushMaterial(); } - if (force && this._assembler && this._canRender() && this._assembler.updateRenderData ) { - this._assembler.updateRenderData(this); + if (force && this._assembler ) { + this.markForUpdateRenderData(); } } } diff --git a/cocos/3d/ui/components/mask-component.ts b/cocos/3d/ui/components/mask-component.ts index f01e9d12e6..cd2c6d385b 100644 --- a/cocos/3d/ui/components/mask-component.ts +++ b/cocos/3d/ui/components/mask-component.ts @@ -331,23 +331,6 @@ export class MaskComponent extends UIRenderComponent { this._removeGraphics(); } - public updateAssembler (render: UI) { - if (super.updateAssembler(render)) { - render.commitComp(this, null, this._assembler!); - return true; - } - - return false; - } - - public postUpdateAssembler (render: UI) { - if (!this._canRender() || !this._postAssembler) { - return; - } - - render.commitComp(this, null, this._postAssembler!); - } - /** * @zh * 根据屏幕坐标计算点击事件。 @@ -395,6 +378,18 @@ export class MaskComponent extends UIRenderComponent { // } } + protected _render(render: UI) { + render.commitComp(this, null, this._assembler!); + } + + protected _postRender(render: UI) { + if (!this._postAssembler) { + return; + } + + render.commitComp(this, null, this._postAssembler!); + } + protected _nodeStateChange (type: SystemEventType) { if (type === SystemEventType.POSITION_PART) { return; @@ -414,7 +409,7 @@ export class MaskComponent extends UIRenderComponent { return false; } - return this._clearGraphics !== null && this._graphics !== null && this._renderPermit; + return this._clearGraphics !== null && this._graphics !== null; } protected _flushAssembler () { diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index 488b2cedf6..b39bcf37d1 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -426,15 +426,6 @@ export class SpriteComponent extends UIRenderComponent { this._activateMaterial(); } - public updateAssembler (render: UI) { - if (super.updateAssembler(render) && this._spriteFrame) { - render.commitComp(this, this._spriteFrame.getGFXTextureView(), this._assembler!); - return true; - } - - return false; - } - public onDestroy () { super.onDestroy(); this.destroyRenderData(); @@ -443,6 +434,10 @@ export class SpriteComponent extends UIRenderComponent { } } + protected _render(render: UI) { + render.commitComp(this, this._spriteFrame!.getGFXTextureView(), this._assembler!); + } + protected _canRender () { // if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) { // if (!this._enabled) { return false; } @@ -555,7 +550,7 @@ export class SpriteComponent extends UIRenderComponent { this._renderData.material = material; } } else { - this.markForUpdateRenderData(true); + this.markForUpdateRenderData(); // this.markForRender(true); } } @@ -606,7 +601,7 @@ export class SpriteComponent extends UIRenderComponent { this._renderData!.uvDirty = true; } - this._renderDataDirty = this._renderData!.uvDirty + this._renderDataFlag = this._renderData!.uvDirty } if (spriteFrame) { diff --git a/cocos/3d/ui/components/ui-component.ts b/cocos/3d/ui/components/ui-component.ts index e6c1e96ba9..f4dd876811 100644 --- a/cocos/3d/ui/components/ui-component.ts +++ b/cocos/3d/ui/components/ui-component.ts @@ -93,10 +93,24 @@ export class UIComponent extends Component { } } - public updateAssembler (render: UI) { + /** + * @zh + * 渲染数据收集。每个渲染组件都由此自身决定是否渲染以及渲染状态的更新。 + * + * @param render 数据处理中转站。 + */ + public updateAssembler(render: UI) { } - public postUpdateAssembler (render: UI) { + + /** + * @zh + * 后渲染数据收集。每个渲染组件都由此接口决定是否渲染以及渲染状态的更新。 + * 一般是在自身子节点 updateAssembler 执行完调用。 + * + * @param render 数据处理中转站。 + */ + public postUpdateAssembler(render: UI) { } /** diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 61b97978c4..4e0d58f823 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -211,9 +211,10 @@ export class UIRenderComponent extends UIComponent { protected _assembler: IAssembler | null = null; protected _postAssembler: IAssembler | null = null; protected _renderData: RenderData | null = null; - protected _renderDataDirty = false; + protected _renderDataFlag = true; + protected _renderFlag = true; // 特殊渲染标记,在可渲染情况下,因为自身某个原因不给予渲染 - protected _renderPermit = true; + // protected _renderPermit = true; protected _material: Material | null = null; protected _instanceMaterialType = InstanceMaterialType.ADDCOLORANDTEXTURE; protected _blendTemplate = { @@ -242,13 +243,14 @@ export class UIRenderComponent extends UIComponent { super.onEnable(); this.node.on(SystemEventType.ANCHOR_CHANGED, this._nodeStateChange, this); this.node.on(SystemEventType.SIZE_CHANGED, this._nodeStateChange, this); - this._renderDataDirty = true; + this._renderFlag = this._canRender(); } public onDisable () { super.onDisable(); this.node.off(SystemEventType.ANCHOR_CHANGED, this._nodeStateChange, this); this.node.off(SystemEventType.SIZE_CHANGED, this._nodeStateChange, this); + this._renderFlag = false; } public onDestroy () { @@ -269,16 +271,17 @@ export class UIRenderComponent extends UIComponent { * @param enable 是否标记为已修改。 */ public markForUpdateRenderData (enable: boolean = true) { - if (enable && this._canRender()) { + this._renderFlag = this._canRender(); + if (enable && this._renderFlag) { const renderData = this._renderData; if (renderData) { renderData.vertDirty = true; } - this._renderDataDirty = enable; + this._renderDataFlag = enable; } else if (!enable) { - this._renderDataDirty = enable; + this._renderDataFlag = enable; } } @@ -307,32 +310,41 @@ export class UIRenderComponent extends UIComponent { this._renderData = null; } - /** - * @zh - * 每个渲染组件都由此接口决定是否渲染以及渲染状态的更新。 - * - * @param render 数据处理中转站。 - */ public updateAssembler (render: UI) { - if (!this._canRender()) { - return false; + super.updateAssembler(render); + if(this._renderFlag){ + this._checkAndUpdateRenderData(); + this._render(render); } - - this._checkAndUpdateRenderData(); - return true; } + public postUpdateAssembler(render: UI) { + super.postUpdateAssembler(render); + if (this._renderFlag) { + this._postRender(render); + } + } + + // 开始提交渲染数据给中转站 + protected _render(render: UI) { } + + // 开始提交渲染数据给中转站 + protected _postRender(render: UI) { } + + // 像组装器更新渲染数据 protected _checkAndUpdateRenderData (){ - if (this._renderDataDirty) { + if (this._renderDataFlag) { this._assembler!.updateRenderData!(this); - this._renderDataDirty = false; + this._renderDataFlag = false; } } protected _canRender () { - return this.material !== null && this._renderPermit; + return this.material !== null && this.enabled && this.enabledInHierarchy; } + protected _postCanRender(){} + protected _updateColor () { if (this._assembler && this._assembler.updateColor) { this._assembler!.updateColor(this); diff --git a/cocos/3d/ui/mesh-buffer.ts b/cocos/3d/ui/mesh-buffer.ts index ef1a8005c2..1d42bb2c84 100644 --- a/cocos/3d/ui/mesh-buffer.ts +++ b/cocos/3d/ui/mesh-buffer.ts @@ -78,7 +78,7 @@ export class MeshBuffer { // batch.ibSize = ib.size; } - public request (vertexCount: number, indiceCount: number) { + public request (vertexCount = 4, indiceCount = 6) { const byteOffset = this.byteOffset + vertexCount * this._vertexFormatBytes; const indiceOffset = this.indiceOffset + indiceCount;