cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.02k stars 7.05k forks source link

ClippingNode with DrawNode stencil is not clipping anything #20715

Open TyelorD opened 2 years ago

TyelorD commented 2 years ago

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:

  1. Create an empty Cocos2d-x project
  2. 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...