mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
fix(2d): apply cascaded opacity to retained Graphics draws (#421)
* fix(2d): apply cascaded opacity to retained Graphics draws Use the per-instance Graphics effect on Web and Native so a parent fade scales the original fill and stroke alpha at draw time. Rebind retained geometry when its material changes and synchronize Graphics color on enable or assignment. Covers the Graphics behavior reported in #14868 and #17917. * fix(2d): apply Graphics cascade opacity to CPU vertex colors * fix(2d): preserve wide strokes and reuse Graphics opacity buffers Reserve stroke vertices using the emitted caps and joins and split at independent subpath boundaries. Reuse Web upload storage across immediate redraws and use the shared draw-info dirty bit to synchronize only changed Native geometry blocks. Release empty caches on the subsequent renderer update or disable. * fix(2d): handle passless Graphics materials and release empty paint buffers * fix(2d): scope Graphics opacity updates and trim unused buffers * fix(2d): limit the Graphics change to cascaded opacity * fix(2d): preserve Graphics.color behavior during parent opacity fades * fix(2d): use emitted Graphics counts for native opacity uploads
This commit is contained in:
@@ -48,6 +48,7 @@ const attributes = vfmtPosColor.concat([
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const componentPerVertex = getComponentPerVertex(attributes);
|
const componentPerVertex = getComponentPerVertex(attributes);
|
||||||
|
const alphaOffset = 6; // position.xyz, color.rgba, distance
|
||||||
|
|
||||||
const stride = getAttributeStride(attributes);
|
const stride = getAttributeStride(attributes);
|
||||||
|
|
||||||
@@ -62,10 +63,10 @@ function growCapacity (needed: number, current: number, max: number): number {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @en
|
* @en
|
||||||
* Graphics component.
|
* Graphics component. Fill and stroke alpha are multiplied by the node's cascaded UI opacity at draw time.
|
||||||
*
|
*
|
||||||
* @zh
|
* @zh
|
||||||
* 自定义图形类。
|
* 自定义图形组件。绘制时,填充和描边的 alpha 会乘以节点的累计 UI 透明度。
|
||||||
*/
|
*/
|
||||||
@ccclass('cc.Graphics')
|
@ccclass('cc.Graphics')
|
||||||
@help('i18n:cc.Graphics')
|
@help('i18n:cc.Graphics')
|
||||||
@@ -236,6 +237,8 @@ export class Graphics extends UIRenderer {
|
|||||||
protected _isNeedUploadData = true;
|
protected _isNeedUploadData = true;
|
||||||
|
|
||||||
private _graphicsUseSubMeshes: RenderingSubMesh[] = [];
|
private _graphicsUseSubMeshes: RenderingSubMesh[] = [];
|
||||||
|
private _uploadedOpacity = 1;
|
||||||
|
private _opacityScratch: Float32Array | null = null;
|
||||||
|
|
||||||
//nativeObj
|
//nativeObj
|
||||||
protected declare _graphicsNativeProxy: NativeUIModelProxy;
|
protected declare _graphicsNativeProxy: NativeUIModelProxy;
|
||||||
@@ -313,6 +316,7 @@ export class Graphics extends UIRenderer {
|
|||||||
this.impl = null;
|
this.impl = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._opacityScratch = null;
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -567,6 +571,7 @@ export class Graphics extends UIRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.impl.clear();
|
this.impl.clear();
|
||||||
|
this._opacityScratch = null;
|
||||||
this._isDrawing = false;
|
this._isDrawing = false;
|
||||||
if (JSB) {
|
if (JSB) {
|
||||||
this._graphicsNativeProxy.clear();// need native
|
this._graphicsNativeProxy.clear();// need native
|
||||||
@@ -676,41 +681,61 @@ export class Graphics extends UIRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _uploadData (): void {
|
protected _uploadData (opacity = this.node._uiProps.opacity): void {
|
||||||
const impl = this.impl;
|
if (!this.impl || !this.model) {
|
||||||
if (!impl) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const renderDataList = impl && impl.getRenderDataList();
|
|
||||||
if (renderDataList.length <= 0 || !this.model) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const renderDataList = this.impl.getRenderDataList();
|
||||||
const subModelList = this.model.subModels;
|
const subModelList = this.model.subModels;
|
||||||
for (let i = 0; i < renderDataList.length; i++) {
|
for (let i = 0; i < renderDataList.length; i++) {
|
||||||
const renderData = renderDataList[i];
|
const renderData = renderDataList[i];
|
||||||
const ia = subModelList[i].inputAssembler;
|
const indexCount = renderData.indexStart;
|
||||||
if (renderData.lastFilledVertex === renderData.vertexStart) {
|
const vertexCount = indexCount ? renderData.vertexStart : 0;
|
||||||
continue;
|
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;
|
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().
|
* Grow only, never shrink: the capacity is reused after clear().
|
||||||
*/
|
*/
|
||||||
@@ -733,18 +758,9 @@ export class Graphics extends UIRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected _render (render: IBatcher): void {
|
protected _render (render: IBatcher): void {
|
||||||
if (this._isNeedUploadData) {
|
const opacity = this.node._uiProps.opacity;
|
||||||
if (this.impl) {
|
if (this._isNeedUploadData || this._uploadedOpacity !== opacity) {
|
||||||
const renderDataList = this.impl.getRenderDataList();
|
this._uploadData(opacity);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render.commitModel(this, this.model, this.getMaterialInstance(0));
|
render.commitModel(this, this.model, this.getMaterialInstance(0));
|
||||||
|
|||||||
@@ -36,14 +36,76 @@ constexpr uint32_t MAX_VERTEX_COUNT = 65536;
|
|||||||
constexpr uint32_t INDICES_PER_VERTEX = 3;
|
constexpr uint32_t INDICES_PER_VERTEX = 3;
|
||||||
constexpr uint32_t MAX_INDEX_COUNT = MAX_VERTEX_COUNT * INDICES_PER_VERTEX;
|
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) {
|
inline uint32_t growCapacity(uint32_t needed, uint32_t current, uint32_t maxCap) {
|
||||||
uint32_t cap = current > 1U ? current : 1U;
|
uint32_t cap = current > 1U ? current : 1U;
|
||||||
while (cap < needed) {
|
while (cap < needed && cap < maxCap) {
|
||||||
cap *= 2;
|
cap *= 2;
|
||||||
}
|
}
|
||||||
return cap < maxCap ? cap : maxCap;
|
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<uint32_t>(chunk.vertices.size() * sizeof(float)));
|
||||||
|
}
|
||||||
|
_lastOpacity = opacity;
|
||||||
|
}
|
||||||
|
scene::Model::updateUBOs(stamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy() override {
|
||||||
|
_geometry.clear();
|
||||||
|
scene::Model::destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct Chunk {
|
||||||
|
ccstd::vector<float> vertices;
|
||||||
|
ccstd::vector<float> paintAlpha;
|
||||||
|
};
|
||||||
|
ccstd::vector<Chunk> _geometry;
|
||||||
|
float _lastOpacity{-1.F};
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
UIModelProxy::UIModelProxy() {
|
UIModelProxy::UIModelProxy() {
|
||||||
@@ -55,7 +117,7 @@ UIModelProxy::~UIModelProxy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UIModelProxy::initModel(Node* node) {
|
void UIModelProxy::initModel(Node* node) {
|
||||||
_model = Root::getInstance()->createModel<scene::Model>();
|
_model = Root::getInstance()->createModel<GraphicsModel>();
|
||||||
_model->setNode(node);
|
_model->setNode(node);
|
||||||
_model->setTransform(node);
|
_model->setTransform(node);
|
||||||
_node = node;
|
_node = node;
|
||||||
@@ -81,8 +143,8 @@ void UIModelProxy::activeSubModels() {
|
|||||||
auto* vertexBuffer = _device->createBuffer({
|
auto* vertexBuffer = _device->createBuffer({
|
||||||
gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST,
|
gfx::BufferUsageBit::VERTEX | gfx::BufferUsageBit::TRANSFER_DST,
|
||||||
gfx::MemoryUsageBit::DEVICE,
|
gfx::MemoryUsageBit::DEVICE,
|
||||||
vertexCapacity * _stride,
|
vertexCapacity * VERTEX_STRIDE,
|
||||||
_stride,
|
VERTEX_STRIDE,
|
||||||
});
|
});
|
||||||
auto* indexBuffer = _device->createBuffer({
|
auto* indexBuffer = _device->createBuffer({
|
||||||
gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST,
|
gfx::BufferUsageBit::INDEX | gfx::BufferUsageBit::TRANSFER_DST,
|
||||||
@@ -104,27 +166,33 @@ void UIModelProxy::activeSubModels() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UIModelProxy::uploadData() {
|
void UIModelProxy::uploadData() {
|
||||||
|
if (!_model) return;
|
||||||
auto* entity = static_cast<RenderEntity*>(_node->getUserData());
|
auto* entity = static_cast<RenderEntity*>(_node->getUserData());
|
||||||
const auto& drawInfos = entity->getDynamicRenderDrawInfos();
|
const auto& drawInfos = entity->getDynamicRenderDrawInfos();
|
||||||
const auto& subModelList = _model->getSubModels();
|
const auto& subModelList = _model->getSubModels();
|
||||||
for (size_t i = 0; i < drawInfos.size(); i++) {
|
auto* model = static_cast<GraphicsModel*>(_model.get());
|
||||||
auto* drawInfo = drawInfos[i];
|
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();
|
auto* ia = subModelList.at(i)->getInputAssembler();
|
||||||
const uint32_t vertexCount = drawInfo->getVertexOffset();
|
const uint32_t vertexCount = drawInfo ? drawInfo->getVertexOffset() : 0U;
|
||||||
const uint32_t indexCount = drawInfo->getIndexOffset();
|
const uint32_t indexCount = drawInfo ? drawInfo->getIndexOffset() : 0U;
|
||||||
if (vertexCount == 0 || indexCount == 0) continue;
|
const auto& vBuffers = ia->getVertexBuffers();
|
||||||
|
if (!vertexCount || !indexCount || vBuffers.empty() || vertexCount > MAX_VERTEX_COUNT || indexCount > MAX_INDEX_COUNT ||
|
||||||
gfx::BufferList vBuffers = ia->getVertexBuffers();
|
!drawInfo->getVDataBuffer() || !drawInfo->getIDataBuffer()) {
|
||||||
if (!vBuffers.empty()) {
|
model->setVertices(i, nullptr, 0);
|
||||||
gfx::Buffer* vBuffer = vBuffers[0];
|
ia->setVertexCount(0);
|
||||||
const uint32_t neededVBytes = vertexCount * _stride;
|
ia->setIndexCount(0);
|
||||||
if (neededVBytes > vBuffer->getSize()) {
|
continue;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
ia->setVertexCount(vertexCount); // count
|
||||||
|
|
||||||
gfx::Buffer* iBuffer = ia->getIndexBuffer();
|
gfx::Buffer* iBuffer = ia->getIndexBuffer();
|
||||||
@@ -139,7 +207,7 @@ void UIModelProxy::uploadData() {
|
|||||||
// drawInfo->setModel(_model); // hack, render by model
|
// drawInfo->setModel(_model); // hack, render by model
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!drawInfos.empty()) {
|
if (!drawInfos.empty() && drawInfos[0]) {
|
||||||
drawInfos[0]->setModel(_model);
|
drawInfos[0]->setModel(_model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,6 +231,7 @@ void UIModelProxy::clear() {
|
|||||||
if (_model == nullptr) {
|
if (_model == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
static_cast<GraphicsModel*>(_model.get())->resizeGeometry(0);
|
||||||
const auto& subModels = _model->getSubModels();
|
const auto& subModels = _model->getSubModels();
|
||||||
for (const auto& subModel : subModels) {
|
for (const auto& subModel : subModels) {
|
||||||
auto* ia = subModel->getInputAssembler();
|
auto* ia = subModel->getInputAssembler();
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ private:
|
|||||||
ccstd::vector<scene::Model*> _models{};
|
ccstd::vector<scene::Model*> _models{};
|
||||||
|
|
||||||
gfx::Device* _device{nullptr};
|
gfx::Device* _device{nullptr};
|
||||||
uint32_t _stride{32};
|
|
||||||
ccstd::vector<gfx::Attribute> _attributes{
|
ccstd::vector<gfx::Attribute> _attributes{
|
||||||
gfx::Attribute{gfx::ATTR_NAME_POSITION, gfx::Format::RGB32F},
|
gfx::Attribute{gfx::ATTR_NAME_POSITION, gfx::Format::RGB32F},
|
||||||
gfx::Attribute{gfx::ATTR_NAME_COLOR, gfx::Format::RGBA32F},
|
gfx::Attribute{gfx::ATTR_NAME_COLOR, gfx::Format::RGBA32F},
|
||||||
|
|||||||
@@ -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<Buffer, (Float32Array | Uint16Array)[]>();
|
||||||
|
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<typeof recordUploads>, 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user