我看了源代码,感觉有点不合理,很多地方会调用下面这个updateGraphics,比如设置masktype,或者onenable或者图形内容重塑onRestore。然而,调用这个函数之后,graphic就被清除了,但是GRAPHICS_STENCIL类型这里没有任何代码,所以要保证自定义graphics是在mask的最后初始化好之后再写,不然很容易被clear掉
protected _updateGraphics () {
if (!this._graphics) {
return;
}
const node = this.node;
const graphics = this._graphics;
// Share render data with graphics content
graphics.clear();
const size = node.getContentSize();
const width = size.width;
const height = size.height;
const ap = node.getAnchorPoint();
const x = -width * ap.x;
const y = -height * ap.y;
if (this._type === MaskType.RECT) {
graphics.rect(x, y, width, height);
} else if (this._type === MaskType.ELLIPSE) {
const center = new Vec3(x + width / 2, y + height / 2, 0);
const radius = new Vec3(width / 2, height / 2, 0,
);
const points = _calculateCircle(center, radius, this._segments);
for (let i = 0; i < points.length; ++i) {
const point = points[i];
if (i === 0) {
graphics.moveTo(point.x, point.y);
} else {
graphics.lineTo(point.x, point.y);
}
}
graphics.close();
}
graphics.fill();
}