Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 2d assembler #17659

Draft
wants to merge 8 commits into
base: v3.8.5
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 34 additions & 42 deletions cocos/2d/assembler/graphics/webgl/graphics-assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { JSB } from 'internal:constants';
import { Color, Vec3 } from '../../../../core';
import { IAssembler } from '../../../renderer/base';
import { MeshRenderData } from '../../../renderer/render-data';
import { IBatcher } from '../../../renderer/i-batcher';
import { Graphics } from '../../../components';
import { LineCap, LineJoin, PointFlags } from '../types';
import { earcut as Earcut } from './earcut';
Expand Down Expand Up @@ -72,25 +71,16 @@ function clamp (v: number, minNum: number, maxNum: number): number {
* graphics 组装器
* 可通过 `UI.graphicsAssembler` 获取该组装器。
*/
export const graphicsAssembler: IAssembler = {
useModel: true,

export class GraphicsAssembler implements IAssembler {
updateRenderData (graphics: Graphics): void {
if (JSB) {
if (graphics.renderData) {
graphics.renderData.material = graphics.getMaterialInstance(0);
}
}
},

fillBuffers (graphics: Graphics, renderer: IBatcher): void {
// this.renderIA!(graphics, renderer);
},

renderIA (graphics: Graphics, renderer: IBatcher): void {
},
}

getRenderData (graphics: Graphics, vertexCount: number): MeshRenderData | null {
private getRenderData (graphics: Graphics, vertexCount: number): MeshRenderData | null {
if (!_impl) {
return null;
}
Expand Down Expand Up @@ -122,7 +112,7 @@ export const graphicsAssembler: IAssembler = {
}

return renderData;
},
}

stroke (graphics: Graphics): void {
Color.copy(_curColor, graphics.strokeColor);
Expand All @@ -137,7 +127,7 @@ export const graphicsAssembler: IAssembler = {
graphics.impl.updatePathOffset = true;

this.end(graphics);
},
}

fill (graphics: Graphics): void {
Color.copy(_curColor, graphics.fillColor);
Expand All @@ -149,13 +139,13 @@ export const graphicsAssembler: IAssembler = {
}

this.end(graphics);
},
}

end (graphics: Graphics): void {
graphics.markForUpdateRenderData();
},
}

_expandStroke (graphics: Graphics): void {
private _expandStroke (graphics: Graphics): void {
const w = graphics.lineWidth * 0.5;
const lineCap = graphics.lineCap;
const lineJoin = graphics.lineJoin;
Expand Down Expand Up @@ -296,9 +286,9 @@ export const graphicsAssembler: IAssembler = {
}
_renderData = null;
_impl = null;
},
}

_expandFill (graphics: Graphics): void {
private _expandFill (graphics: Graphics): void {
_impl = graphics.impl;
if (!_impl) {
return;
Expand Down Expand Up @@ -374,9 +364,9 @@ export const graphicsAssembler: IAssembler = {

_renderData = null;
_impl = null;
},
}

_calculateJoins (impl: Impl, w: number, lineJoin: LineJoin, miterLimit: number): void {
private _calculateJoins (impl: Impl, w: number, lineJoin: LineJoin, miterLimit: number): void {
let iw = 0.0;

if (w > 0.0) {
Expand Down Expand Up @@ -450,9 +440,9 @@ export const graphicsAssembler: IAssembler = {
p1 = pts[j + 1];
}
}
},
}

_flattenPaths (impl: Impl): void {
private _flattenPaths (impl: Impl): void {
const paths = impl.paths;
for (let i = impl.pathOffset, l = impl.pathLength; i < l; i++) {
const path = paths[i];
Expand Down Expand Up @@ -482,9 +472,9 @@ export const graphicsAssembler: IAssembler = {
p1 = pts[j + 1];
}
}
},
}

_chooseBevel (bevel: number, p0: Point, p1: Point, w: number) {
private _chooseBevel (bevel: number, p0: Point, p1: Point, w: number): [number, number, number, number] {
const x = p1.x;
const y = p1.y;
let x0 = 0;
Expand All @@ -503,29 +493,29 @@ export const graphicsAssembler: IAssembler = {
}

return [x0, y0, x1, y1];
},
}

_buttCapStart (p: Point, dx: number, dy: number, w: number, d: number): void {
private _buttCapStart (p: Point, dx: number, dy: number, w: number, d: number): void {
const px = p.x - dx * d;
const py = p.y - dy * d;
const dlx = dy;
const dly = -dx;

this._vSet(px + dlx * w, py + dly * w, 1);
this._vSet(px - dlx * w, py - dly * w, -1);
},
}

_buttCapEnd (p: Point, dx: number, dy: number, w: number, d: number): void {
private _buttCapEnd (p: Point, dx: number, dy: number, w: number, d: number): void {
const px = p.x + dx * d;
const py = p.y + dy * d;
const dlx = dy;
const dly = -dx;

this._vSet(px + dlx * w, py + dly * w, 1);
this._vSet(px - dlx * w, py - dly * w, -1);
},
}

_roundCapStart (p: Point, dx: number, dy: number, w: number, nCap: number): void {
private _roundCapStart (p: Point, dx: number, dy: number, w: number, nCap: number): void {
const px = p.x;
const py = p.y;
const dlx = dy;
Expand All @@ -540,9 +530,9 @@ export const graphicsAssembler: IAssembler = {
}
this._vSet(px + dlx * w, py + dly * w, 1);
this._vSet(px - dlx * w, py - dly * w, -1);
},
}

_roundCapEnd (p: Point, dx: number, dy: number, w: number, nCap: number): void {
private _roundCapEnd (p: Point, dx: number, dy: number, w: number, nCap: number): void {
const px = p.x;
const py = p.y;
const dlx = dy;
Expand All @@ -557,9 +547,9 @@ export const graphicsAssembler: IAssembler = {
this._vSet(px, py, 0);
this._vSet(px - dlx * ax + dx * ay, py - dly * ax + dy * ay, 1);
}
},
}

_roundJoin (p0: Point, p1: Point, lw: number, rw: number, nCap: number): void {
private _roundJoin (p0: Point, p1: Point, lw: number, rw: number, nCap: number): void {
const dlx0 = p0.dy;
const dly0 = -p0.dx;
const dlx1 = p1.dy;
Expand Down Expand Up @@ -621,9 +611,9 @@ export const graphicsAssembler: IAssembler = {
this._vSet(p1x + dlx1 * rw, p1y + dly1 * rw, 1);
this._vSet(rx1, ry1, -1);
}
},
}

_bevelJoin (p0: Point, p1: Point, lw: number, rw: number): void {
private _bevelJoin (p0: Point, p1: Point, lw: number, rw: number): void {
let rx0 = 0;
let ry0 = 0;
let rx1 = 0;
Expand Down Expand Up @@ -662,9 +652,9 @@ export const graphicsAssembler: IAssembler = {
this._vSet(p1.x + dlx1 * lw, p1.y + dly1 * lw, 1);
this._vSet(rx1, ry1, -1);
}
},
}

_vSet (x: number, y: number, distance = 0): void {
private _vSet (x: number, y: number, distance = 0): void {
if (!_renderData) {
return;
}
Expand All @@ -682,5 +672,7 @@ export const graphicsAssembler: IAssembler = {
dataOffset += 4;
vData[dataOffset++] = distance;
meshBuffer.vertexStart++;
},
};
}
}

export const graphicsAssembler = new GraphicsAssembler();
4 changes: 2 additions & 2 deletions cocos/2d/assembler/graphics/webgl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
*/

import { UIRenderer } from '../../../framework/ui-renderer';
import { IAssemblerManager } from '../../../renderer/base';
import { IAssembler, IAssemblerManager } from '../../../renderer/base';
import { Graphics } from '../../../components';
import { graphicsAssembler as graphics } from './graphics-assembler';

const graphicsAssemblerManager: IAssemblerManager = {
getAssembler (sprite: UIRenderer) {
getAssembler (sprite: UIRenderer): IAssembler {
return graphics;
},
};
Expand Down
88 changes: 13 additions & 75 deletions cocos/2d/assembler/label/bmfont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,96 +22,34 @@
THE SOFTWARE.
*/

import { SpriteFrame } from '../../assets/sprite-frame';
import { Color, Rect, js } from '../../../core';
import { IBatcher } from '../../renderer/i-batcher';
import { Label } from '../../components/label';
import { IAssembler } from '../../renderer/base';
import { Color } from '../../../core';
import type { IBatcher } from '../../renderer/i-batcher';
import type { Label } from '../../components/label';
import type { IAssembler } from '../../renderer/base';
import { fillMeshVertices3D } from '../utils';
import { bmfontUtils } from './bmfontUtils';
import { BmfontUtils } from './bmfontUtils';
import type { RenderData } from '../../renderer/render-data';

const tempColor = new Color(255, 255, 255, 255);

/**
* bmfont 组装器
* 可通过 `UI.bmfont` 获取该组装器。
*/
export const bmfont: IAssembler = {
createData (comp: Label) {
class Bmfont extends BmfontUtils implements IAssembler {
createData (comp: Label): RenderData {
const renderData = comp.requestRenderData();
renderData.resize(0, 0);
return renderData;
},
}

fillBuffers (comp: Label, renderer: IBatcher) {
fillBuffers (comp: Label, renderer: IBatcher): void {
const node = comp.node;
tempColor.set(comp.color);
tempColor.a = node._uiProps.opacity * 255;
// Fill All
fillMeshVertices3D(node, renderer, comp.renderData!, tempColor);
},
}
}

appendQuad (comp: Label, spriteFrame: SpriteFrame, rect: Rect, rotated: boolean, x: number, y: number, scale: number) {
const renderData = comp.renderData;
if (!renderData) {
return;
}

const dataOffset = renderData.dataLength;

renderData.dataLength += 4;
renderData.resize(renderData.dataLength, renderData.dataLength / 2 * 3);

const dataList = renderData.data;
const texW = spriteFrame.width;
const texH = spriteFrame.height;

const rectWidth = rect.width;
const rectHeight = rect.height;

let l = 0;
let b = 0;
let t = 0;
let r = 0;
if (!rotated) {
l = (rect.x) / texW;
r = (rect.x + rectWidth) / texW;
b = (rect.y + rectHeight) / texH;
t = (rect.y) / texH;

dataList[dataOffset].u = l;
dataList[dataOffset].v = b;
dataList[dataOffset + 1].u = r;
dataList[dataOffset + 1].v = b;
dataList[dataOffset + 2].u = l;
dataList[dataOffset + 2].v = t;
dataList[dataOffset + 3].u = r;
dataList[dataOffset + 3].v = t;
} else {
l = (rect.x) / texW;
r = (rect.x + rectHeight) / texW;
b = (rect.y + rectWidth) / texH;
t = (rect.y) / texH;

dataList[dataOffset].u = l;
dataList[dataOffset].v = t;
dataList[dataOffset + 1].u = l;
dataList[dataOffset + 1].v = b;
dataList[dataOffset + 2].u = r;
dataList[dataOffset + 2].v = t;
dataList[dataOffset + 3].u = r;
dataList[dataOffset + 3].v = b;
}

dataList[dataOffset].x = x;
dataList[dataOffset].y = y - rectHeight * scale;
dataList[dataOffset + 1].x = x + rectWidth * scale;
dataList[dataOffset + 1].y = y - rectHeight * scale;
dataList[dataOffset + 2].x = x;
dataList[dataOffset + 2].y = y;
dataList[dataOffset + 3].x = x + rectWidth * scale;
dataList[dataOffset + 3].y = y;
},
};

js.addon(bmfont, bmfontUtils);
export const bmfont = new Bmfont();
Loading