diff --git a/cocos/2d/components/graphics.ts b/cocos/2d/components/graphics.ts index 3dbc3f469e..c3710f2ba9 100644 --- a/cocos/2d/components/graphics.ts +++ b/cocos/2d/components/graphics.ts @@ -48,6 +48,7 @@ const attributes = vfmtPosColor.concat([ ]); const componentPerVertex = getComponentPerVertex(attributes); +const alphaOffset = 6; // position.xyz, color.rgba, distance const stride = getAttributeStride(attributes); @@ -62,10 +63,10 @@ function growCapacity (needed: number, current: number, max: number): number { /** * @en - * Graphics component. + * Graphics component. Fill and stroke alpha are multiplied by the node's cascaded UI opacity at draw time. * * @zh - * 自定义图形类。 + * 自定义图形组件。绘制时,填充和描边的 alpha 会乘以节点的累计 UI 透明度。 */ @ccclass('cc.Graphics') @help('i18n:cc.Graphics') @@ -236,6 +237,8 @@ export class Graphics extends UIRenderer { protected _isNeedUploadData = true; private _graphicsUseSubMeshes: RenderingSubMesh[] = []; + private _uploadedOpacity = 1; + private _opacityScratch: Float32Array | null = null; //nativeObj protected declare _graphicsNativeProxy: NativeUIModelProxy; @@ -313,6 +316,7 @@ export class Graphics extends UIRenderer { this.impl = null; } + this._opacityScratch = null; super.onDestroy(); } @@ -567,6 +571,7 @@ export class Graphics extends UIRenderer { } this.impl.clear(); + this._opacityScratch = null; this._isDrawing = false; if (JSB) { this._graphicsNativeProxy.clear();// need native @@ -676,41 +681,61 @@ export class Graphics extends UIRenderer { } } - protected _uploadData (): void { - const impl = this.impl; - if (!impl) { - return; - } - - const renderDataList = impl && impl.getRenderDataList(); - if (renderDataList.length <= 0 || !this.model) { + protected _uploadData (opacity = this.node._uiProps.opacity): void { + if (!this.impl || !this.model) { return; } + const renderDataList = this.impl.getRenderDataList(); const subModelList = this.model.subModels; for (let i = 0; i < renderDataList.length; i++) { const renderData = renderDataList[i]; - const ia = subModelList[i].inputAssembler; - if (renderData.lastFilledVertex === renderData.vertexStart) { - continue; + const indexCount = renderData.indexStart; + const vertexCount = indexCount ? renderData.vertexStart : 0; + const newSubModel = i >= subModelList.length; + if (newSubModel) { + this.activeSubModel(i, vertexCount, indexCount); + } + const ia = subModelList[i].inputAssembler; + const verticesChanged = newSubModel || renderData.lastFilledVertex !== vertexCount; + const indicesChanged = newSubModel || renderData.lastFilledIndex !== indexCount; + if (verticesChanged || indicesChanged) { + this._ensureCapacity(renderData, ia); + } + if (vertexCount && (verticesChanged || opacity !== this._uploadedOpacity)) { + ia.vertexBuffers[0].update(this._getUploadVertices(renderData, vertexCount, opacity)); + } + if (verticesChanged) { + ia.vertexCount = vertexCount; + renderData.lastFilledVertex = vertexCount; + } + if (indicesChanged) { + if (indexCount) ia.indexBuffer!.update(renderData.iData.subarray(0, indexCount)); + ia.indexCount = indexCount; + renderData.lastFilledIndex = indexCount; } - - // Buffer.update() does not grow the buffer on WebGL, reserve the capacity first. - this._ensureCapacity(renderData, ia); - - const vb = new Float32Array(renderData.vData.buffer, 0, renderData.vertexStart * componentPerVertex); - ia.vertexBuffers[0].update(vb); - ia.vertexCount = renderData.vertexStart; - const ib = new Uint16Array(renderData.iData.buffer, 0, renderData.indexStart); - ia.indexBuffer!.update(ib); - ia.indexCount = renderData.indexStart; - renderData.lastFilledVertex = renderData.vertexStart; - renderData.lastFilledIndex = renderData.indexStart; } + this._uploadedOpacity = opacity; this._isNeedUploadData = false; } + private _getUploadVertices (data: MeshRenderData, vertexCount: number, opacity: number): Float32Array { + const raw = new Float32Array(data.vData.buffer, data.vData.byteOffset, vertexCount * componentPerVertex); + if (opacity === 1) return raw; + + // Keep the retained paint colors intact so each fade starts from its original alpha. + if (!this._opacityScratch || this._opacityScratch.length < raw.length) { + this._opacityScratch = new Float32Array(growCapacity(vertexCount, 0, MAX_VERTEX_COUNT) * componentPerVertex); + } + const output = this._opacityScratch.subarray(0, raw.length); + output.set(raw); + for (let i = alphaOffset; i < output.length; i += componentPerVertex) { + output[i] = raw[i] * opacity; + } + return output; + } + /** * Grow only, never shrink: the capacity is reused after clear(). */ @@ -733,18 +758,9 @@ export class Graphics extends UIRenderer { } protected _render (render: IBatcher): void { - if (this._isNeedUploadData) { - if (this.impl) { - const renderDataList = this.impl.getRenderDataList(); - const len = this.model!.subModels.length; - if (renderDataList.length > len) { - for (let i = len; i < renderDataList.length; i++) { - const renderData = renderDataList[i]; - this.activeSubModel(i, renderData.vertexStart, renderData.indexStart); - } - } - } - this._uploadData(); + const opacity = this.node._uiProps.opacity; + if (this._isNeedUploadData || this._uploadedOpacity !== opacity) { + this._uploadData(opacity); } render.commitModel(this, this.model, this.getMaterialInstance(0)); diff --git a/native/cocos/2d/renderer/UIModelProxy.cpp b/native/cocos/2d/renderer/UIModelProxy.cpp index 5370840b7c..dc80b97e96 100644 --- a/native/cocos/2d/renderer/UIModelProxy.cpp +++ b/native/cocos/2d/renderer/UIModelProxy.cpp @@ -36,14 +36,76 @@ constexpr uint32_t MAX_VERTEX_COUNT = 65536; constexpr uint32_t INDICES_PER_VERTEX = 3; constexpr uint32_t MAX_INDEX_COUNT = MAX_VERTEX_COUNT * INDICES_PER_VERTEX; +// Graphics vertex layout: position.xyz, color.rgba, distance. +constexpr uint32_t FLOATS_PER_VERTEX = 8; +constexpr uint32_t VERTEX_STRIDE = FLOATS_PER_VERTEX * sizeof(float); +constexpr uint32_t ALPHA_OFFSET = 6; + inline uint32_t growCapacity(uint32_t needed, uint32_t current, uint32_t maxCap) { uint32_t cap = current > 1U ? current : 1U; - while (cap < needed) { + while (cap < needed && cap < maxCap) { cap *= 2; } return cap < maxCap ? cap : maxCap; } +class GraphicsModel final : public scene::Model { +public: + void resizeGeometry(size_t size) { + _geometry.resize(size); + _lastOpacity = -1.F; + } + + void setVertices(size_t index, const float* source, uint32_t count) { + auto& chunk = _geometry[index]; + chunk.paintAlpha.clear(); + if (count) { + // JS can replace its arrays after updateRenderer and before the native UI walk. + chunk.vertices.assign(source, source + count * FLOATS_PER_VERTEX); + } else { + chunk.vertices.clear(); + } + } + + void updateUBOs(uint32_t stamp) override { + const auto* node = getNode(); + if (node && node->_getFinalOpacity() != _lastOpacity) { + // The UI walk resolves this node's opacity immediately before submitting the model. + const float opacity = node->_getFinalOpacity(); + for (size_t i = 0; i < _geometry.size(); ++i) { + auto& chunk = _geometry[i]; + if (chunk.vertices.empty()) continue; + if (opacity != 1.F && chunk.paintAlpha.empty()) { + chunk.paintAlpha.resize(chunk.vertices.size() / FLOATS_PER_VERTEX); + for (size_t v = 0; v < chunk.paintAlpha.size(); ++v) { + chunk.paintAlpha[v] = chunk.vertices[v * FLOATS_PER_VERTEX + ALPHA_OFFSET]; + } + } + for (size_t v = 0; v < chunk.paintAlpha.size(); ++v) { + chunk.vertices[v * FLOATS_PER_VERTEX + ALPHA_OFFSET] = chunk.paintAlpha[v] * opacity; + } + auto* vb = getSubModels()[i]->getInputAssembler()->getVertexBuffers().front(); + vb->update(chunk.vertices.data(), static_cast(chunk.vertices.size() * sizeof(float))); + } + _lastOpacity = opacity; + } + scene::Model::updateUBOs(stamp); + } + + void destroy() override { + _geometry.clear(); + scene::Model::destroy(); + } + +private: + struct Chunk { + ccstd::vector vertices; + ccstd::vector paintAlpha; + }; + ccstd::vector _geometry; + float _lastOpacity{-1.F}; +}; + } // namespace UIModelProxy::UIModelProxy() { @@ -55,7 +117,7 @@ UIModelProxy::~UIModelProxy() { } void UIModelProxy::initModel(Node* node) { - _model = Root::getInstance()->createModel(); + _model = Root::getInstance()->createModel(); _model->setNode(node); _model->setTransform(node); _node = node; @@ -81,8 +143,8 @@ void UIModelProxy::activeSubModels() { auto* vertexBuffer = _device->createBuffer({ gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST, gfx::MemoryUsageBit::DEVICE, - vertexCapacity * _stride, - _stride, + vertexCapacity * VERTEX_STRIDE, + VERTEX_STRIDE, }); auto* indexBuffer = _device->createBuffer({ gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST, @@ -104,27 +166,33 @@ void UIModelProxy::activeSubModels() { } void UIModelProxy::uploadData() { + if (!_model) return; auto* entity = static_cast(_node->getUserData()); const auto& drawInfos = entity->getDynamicRenderDrawInfos(); const auto& subModelList = _model->getSubModels(); - for (size_t i = 0; i < drawInfos.size(); i++) { - auto* drawInfo = drawInfos[i]; + auto* model = static_cast(_model.get()); + model->resizeGeometry(subModelList.size()); + for (size_t i = 0; i < subModelList.size(); i++) { + auto* drawInfo = i < drawInfos.size() ? drawInfos[i] : nullptr; auto* ia = subModelList.at(i)->getInputAssembler(); - const uint32_t vertexCount = drawInfo->getVertexOffset(); - const uint32_t indexCount = drawInfo->getIndexOffset(); - if (vertexCount == 0 || indexCount == 0) continue; - - gfx::BufferList vBuffers = ia->getVertexBuffers(); - if (!vBuffers.empty()) { - gfx::Buffer* vBuffer = vBuffers[0]; - const uint32_t neededVBytes = vertexCount * _stride; - if (neededVBytes > vBuffer->getSize()) { - const uint32_t capacity = growCapacity(vertexCount, vBuffer->getSize() / _stride, MAX_VERTEX_COUNT); - vBuffer->resize(capacity * _stride); - } - // pass the valid size explicitly, update(ptr) uploads the whole capacity - vBuffer->update(drawInfo->getVDataBuffer(), neededVBytes); // vdata + const uint32_t vertexCount = drawInfo ? drawInfo->getVertexOffset() : 0U; + const uint32_t indexCount = drawInfo ? drawInfo->getIndexOffset() : 0U; + const auto& vBuffers = ia->getVertexBuffers(); + if (!vertexCount || !indexCount || vBuffers.empty() || vertexCount > MAX_VERTEX_COUNT || indexCount > MAX_INDEX_COUNT || + !drawInfo->getVDataBuffer() || !drawInfo->getIDataBuffer()) { + model->setVertices(i, nullptr, 0); + ia->setVertexCount(0); + ia->setIndexCount(0); + continue; } + + gfx::Buffer* vBuffer = vBuffers[0]; + const uint32_t neededVBytes = vertexCount * VERTEX_STRIDE; + if (neededVBytes > vBuffer->getSize()) { + const uint32_t capacity = growCapacity(vertexCount, vBuffer->getSize() / VERTEX_STRIDE, MAX_VERTEX_COUNT); + vBuffer->resize(capacity * VERTEX_STRIDE); + } + model->setVertices(i, drawInfo->getVDataBuffer(), vertexCount); ia->setVertexCount(vertexCount); // count gfx::Buffer* iBuffer = ia->getIndexBuffer(); @@ -139,7 +207,7 @@ void UIModelProxy::uploadData() { // drawInfo->setModel(_model); // hack, render by model } - if (!drawInfos.empty()) { + if (!drawInfos.empty() && drawInfos[0]) { drawInfos[0]->setModel(_model); } } @@ -163,6 +231,7 @@ void UIModelProxy::clear() { if (_model == nullptr) { return; } + static_cast(_model.get())->resizeGeometry(0); const auto& subModels = _model->getSubModels(); for (const auto& subModel : subModels) { auto* ia = subModel->getInputAssembler(); diff --git a/native/cocos/2d/renderer/UIModelProxy.h b/native/cocos/2d/renderer/UIModelProxy.h index c069980802..c8ec63212b 100644 --- a/native/cocos/2d/renderer/UIModelProxy.h +++ b/native/cocos/2d/renderer/UIModelProxy.h @@ -56,7 +56,6 @@ private: ccstd::vector _models{}; gfx::Device* _device{nullptr}; - uint32_t _stride{32}; ccstd::vector _attributes{ gfx::Attribute{gfx::ATTR_NAME_POSITION, gfx::Format::RGB32F}, gfx::Attribute{gfx::ATTR_NAME_COLOR, gfx::Format::RGBA32F}, diff --git a/tests/ui/graphics-opacity.test.ts b/tests/ui/graphics-opacity.test.ts new file mode 100644 index 0000000000..bfc1ba40c1 --- /dev/null +++ b/tests/ui/graphics-opacity.test.ts @@ -0,0 +1,154 @@ +import { Canvas, UIOpacity } from '../../cocos/2d'; +import { Batcher2D } from '../../cocos/2d/renderer/batcher-2d'; +import { Color } from '../../cocos/core'; +import type { Buffer } from '../../cocos/gfx'; +import { EmptyBuffer } from '../../cocos/gfx/empty/empty-buffer'; +import { Camera, Node, Scene, director, game } from '../../exports/base'; +import { Graphics } from '../../exports/graphics'; + +// Graphics uploads position.xyz, color.rgba, and distance for each vertex. +const vertexStride = 8; +const alphaOffset = 6; + +function vertices (graphics: Graphics, block = 0): Float32Array { + const data = graphics.impl!.getRenderDataList()[block]; + return data.vData.slice(0, data.vertexStart * vertexStride); +} + +function recordUploads () { + const writes = new Map(); + const update = EmptyBuffer.prototype.update; + jest.spyOn(EmptyBuffer.prototype, 'update').mockImplementation(function (this: EmptyBuffer, data, size) { + if (data instanceof Float32Array || data instanceof Uint16Array) { + const calls = writes.get(this) ?? []; + // The next upload can reuse the source array, including for another block in this frame. + calls.push(data.slice(0, size === undefined ? data.length : size / data.BYTES_PER_ELEMENT)); + writes.set(this, calls); + } + update.call(this, data, size); + }); + return writes; +} + +function expectUpload (writes: ReturnType, graphics: Graphics, block: number, + raw: Float32Array, opacity: number, geometryChanged: boolean): void { + const input = graphics.model!.subModels[block].inputAssembler; + const expected = raw.slice(); + for (let i = alphaOffset; i < expected.length; i += vertexStride) { + expected[i] = raw[i] * opacity; + } + expect(writes.get(input.vertexBuffers[0])).toEqual([expected]); + const data = graphics.impl!.getRenderDataList()[block]; + expect(writes.get(input.indexBuffer!)).toEqual(geometryChanged ? [data.iData.slice(0, data.indexStart)] : undefined); + expect(vertices(graphics, block)).toEqual(raw); +} + +describe('Graphics cascaded opacity', () => { + let batcher: Batcher2D; + let previousBatcher: Batcher2D | null; + let parentOpacity: UIOpacity; + let graphics: Graphics; + const root = () => director.root as unknown as { _batcher: Batcher2D | null }; + + beforeEach(() => { + const scene = new Scene('Graphics opacity'); + director.runSceneImmediate(scene); + previousBatcher = root()._batcher; + batcher = new Batcher2D(director.root!); + root()._batcher = batcher; + batcher.initialize(); + const canvasNode = new Node('Canvas'); + scene.addChild(canvasNode); + const canvas = canvasNode.addComponent(Canvas); + const camera = new Node('Camera'); + canvasNode.addChild(camera); + canvas.cameraComponent = camera.addComponent(Camera); + const group = new Node('Fade group'); + canvasNode.addChild(group); + parentOpacity = group.addComponent(UIOpacity); + const node = new Node('Graphics'); + group.addChild(node); + graphics = node.addComponent(Graphics); + }); + + afterEach(() => { + jest.restoreAllMocks(); + director.runSceneImmediate(new Scene('Cleanup')); + batcher.destroy(); + root()._batcher = previousBatcher; + }); + + test('fades retained fills and strokes from their own alpha and restores them without redrawing', () => { + graphics.fillColor = new Color(255, 0, 0, 64); + graphics.fillRect(0, 0, 16, 16); + graphics.strokeColor = new Color(0, 255, 0, 192); + graphics.moveTo(20, 0); + graphics.lineTo(20, 16); + graphics.stroke(); + // Place a second fill in a separate draw buffer to verify both uploads on a fade. + graphics.impl!.requestRenderData(); + graphics.impl!.dataOffset = 1; + graphics.fillRect(32, 0, 8, 8); + const raw = [vertices(graphics), vertices(graphics, 1)]; + const alpha = raw[0].filter((_, i) => i % vertexStride === alphaOffset); + expect(alpha).toContain(Math.fround(64 / 255)); + expect(alpha).toContain(Math.fround(192 / 255)); + const writes = recordUploads(); + game.step(); + raw.forEach((original, block) => expectUpload(writes, graphics, block, original, 1, true)); + for (const value of [128, 64]) { + writes.clear(); + parentOpacity.opacity = value; + game.step(); + raw.forEach((original, block) => expectUpload(writes, graphics, block, original, value / 255, false)); + } + + writes.clear(); + game.step(); + for (const sub of graphics.model!.subModels) { + expect(writes.has(sub.inputAssembler.vertexBuffers[0])).toBe(false); + expect(writes.has(sub.inputAssembler.indexBuffer!)).toBe(false); + } + const commits = jest.spyOn(batcher, 'commitModel'); + parentOpacity.opacity = 0; + game.step(); + expect(commits).not.toHaveBeenCalled(); + + writes.clear(); + parentOpacity.opacity = 255; + game.step(); + expect(commits).toHaveBeenCalledTimes(1); + raw.forEach((original, block) => expectUpload(writes, graphics, block, original, 1, false)); + }); + + test('applies current opacity when appending paint or replacing paint of the same size while hidden', () => { + parentOpacity.opacity = 128; + graphics.fillColor = new Color(10, 50, 150, 192); + graphics.fillRect(0, 0, 8, 8); + const writes = recordUploads(); + const first = vertices(graphics); + game.step(); + expectUpload(writes, graphics, 0, first, 128 / 255, true); + + writes.clear(); + graphics.fillColor = new Color(180, 20, 5, 64); + graphics.fillRect(10, 0, 8, 8); + const appended = vertices(graphics); + game.step(); + expectUpload(writes, graphics, 0, appended, 128 / 255, true); + + parentOpacity.opacity = 0; + graphics.clear(); + graphics.fillColor = new Color(20, 220, 10, 48); + graphics.fillRect(32, 0, 8, 8); + graphics.fillRect(42, 0, 8, 8); + const replacement = vertices(graphics); + expect(replacement.length).toBe(appended.length); + expect(replacement).not.toEqual(appended); + game.step(); + writes.clear(); + parentOpacity.opacity = 128; + game.step(); + expectUpload(writes, graphics, 0, replacement, 128 / 255, true); + }); +});