Skip to content

Commit

Permalink
Fixed ClippingNode stencil needing global z set manually (#1831)
Browse files Browse the repository at this point in the history
This bug has existed since the Cocos2d-x 3.x days. If you set the global z order of a ClippingNode to anything other than the default value, then the ClippingNode fails to clip anything unless the global z order is also set for the Stencil of that ClippingNode. This change makes the ClippingNode._stencil's global z order to follow the ClippingNode's. (Note: you could still manually set the global z order of the stencil as desired to break the clipping. But now the default behavior works correctly and the way one would expect.)
  • Loading branch information
TyelorD committed Apr 17, 2024
1 parent 1cfe6fc commit 332eb9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/2d/ClippingNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ void ClippingNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

void ClippingNode::setGlobalZOrder(float globalZOrder)
{
Node::setGlobalZOrder(globalZOrder);

if (_stencil) {
// Make sure our stencil stays on the same globalZOrder:
_stencil->setGlobalZOrder(globalZOrder);
}
}

void ClippingNode::setCameraMask(unsigned short mask, bool applyChildren)
{
Node::setCameraMask(mask, applyChildren);
Expand Down Expand Up @@ -279,6 +289,9 @@ void ClippingNode::setStencil(Node* stencil)

if (_stencil != nullptr)
{
// Make sure our stencil stays on the same globalZOrder:
_stencil->setGlobalZOrder(getGlobalZOrder());

_originalStencilProgramState[_stencil] = _stencil->getProgramState();
auto& children = _stencil->getChildren();
for (const auto& child : children)
Expand Down
3 changes: 3 additions & 0 deletions core/2d/ClippingNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2013-2016 Chukong Technologies Inc.
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
*
* https://axmolengine.github.io/
*
Expand Down Expand Up @@ -133,6 +134,8 @@ class AX_DLL ClippingNode : public Node
virtual void onExit() override;
virtual void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;

virtual void setGlobalZOrder(float globalZOrder) override;

virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;

ClippingNode();
Expand Down

0 comments on commit 332eb9a

Please sign in to comment.