cocos2d-x
cocos2d-x copied to clipboard
ClippingNode with DrawNode stencil is not clipping anything
- cocos2d-x version: 3.17.2
- devices test on: iPhone Xr
- developing environments
- Xcode version: 13.2
When attempting to setup a basic ClippingNode using a DrawNode as the stencil, I see no clipping happening at all. I've attempted to toggle the ClippingNode's setInverted, as well as tried different Alpha Thresholds for the ClippingNode, none of them fix the issue I'm seeing.
Steps to Reproduce:
- Create an empty Cocos2d-x project
- Add the following code to the main scene:
// Add to includes:
// [...]
#include "ui/CocosGUI.h"
// [...]
// Add to bottom of HelloWorld::init() just before the return:
auto screenSize = cocos2d::Director::getInstance()->getVisibleSize();
auto screenCenter = Vec2(screenSize) * 0.5f;
// Create Test Mask:
auto veilMask = cocos2d::DrawNode::create();
veilMask->setAnchorPoint(cocos2d::Vec2::ANCHOR_MIDDLE);
veilMask->setPosition(screenCenter);
veilMask->setContentSize(screenSize);
veilMask->drawSolidRect(screenCenter - cocos2d::Vec2(50, 50), screenCenter + cocos2d::Vec2(50, 50), cocos2d::Color4F(1.0f, 1.0f, 1.0f, 1.0f));
// Create Clipper:
auto maskedVeil = cocos2d::ClippingNode::create(veilMask);
maskedVeil->setContentSize(screenSize);
maskedVeil->setInverted(true);
maskedVeil->setAlphaThreshold(0.0f);
addChild(maskedVeil);
// Create Image to be Masked:
auto veil = cocos2d::ui::Layout::create();
veil->setBackGroundColor(cocos2d::Color3B::BLACK);
veil->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
veil->setBackGroundColorOpacity(0xAF);
veil->setCascadeOpacityEnabled(true);
veil->setAnchorPoint(cocos2d::Vec2::ANCHOR_MIDDLE);
veil->setContentSize(screenSize);
veil->setPosition(screenCenter);
maskedVeil->addChild(veil);
You will then see that the "veil" Layout object doesn't get clipped at all, when it should be clipping everything inside of a 100px square at the center of the screen.
Is it possible that clipping Layout classes just doesn't work, or that I'm doing something wrong? To me this looks and feels like a bug with the ClippingNode and/or the ui::Layout class.
Thanks for your time, Tyelor K.
Edit: A quick test using a cocos2d::Sprite object instead of a ui::Layout object results in the exact same issue, no clipping happens at all. So it must be a bug around using a DrawNode as a stencil for a ClippingNode, which is unfortunate because this functionality is supposed to work according to the C++ test files and documentation from 5+ years ago...