0

我需要一些关于 cocos2d DrawNodes 混合的帮助。我有带有精灵的 DrawNode:

在此处输入图像描述 另一个绘制多边形的 DrawNode(多边形面积小于心形精灵)。作为混合的结果,我只需要在多边形内有一部分心形精灵,如下所示: 在此处输入图像描述 我的代码:TextureNode.h

class CTextureNode : public cocos2d::DrawNode
{
public:
static CTextureNode * create(const std::string & fileName, const cocos2d::Rect & rect);

CTextureNode();
virtual ~CTextureNode();

void setPolygonalMask(const cocos2d::Vec2 * verts, int count);

private:
bool initTexture(const std::string & fileName, const cocos2d::Rect & rect);

private:
cocos2d::DrawNode * m_pMask;
cocos2d::Sprite * m_pSprite;

float timeOutline;
bool bShaderInc;
};

纹理节点.cpp

CTextureNode * CTextureNode::create(const std::string & fileName, const cocos2d::Rect & rect)
{
auto ret = new CTextureNode();
if (!ret || !ret->init() || !ret->initTexture(fileName, rect))
{
    CC_SAFE_DELETE(ret);
}

return ret;
}

CTextureNode::CTextureNode()
: timeOutline(0.f)
, bShaderInc(true)
, m_pMask(nullptr)
, m_pSprite(nullptr)
{
}

CTextureNode::~CTextureNode()
{
}

bool CTextureNode::initTexture(const std::string & fileName, const cocos2d::Rect & rect)
{
m_pSprite = cocos2d::Sprite::create(fileName, rect);
m_pSprite->setAnchorPoint(cocos2d::Point::ZERO);
addChild(m_pSprite);

m_pMask = cocos2d::DrawNode::create();
m_pMask->setPosition(cocos2d::Point::ZERO);
m_pMask->setAnchorPoint(cocos2d::Point::ZERO);
addChild(m_pMask);

return true;
}

void CTextureNode::setPolygonalMask(const cocos2d::Vec2 * verts, int count)
{
if (m_pMask)
{
    m_pMask->drawPolygon(verts, count, cocos2d::Color4F::WHITE, 1, cocos2d::Color4F::WHITE);

    cocos2d::BlendFunc maskBlendFunc = { GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA };
    m_pMask->setBlendFunc(maskBlendFunc);

    auto size = m_pSprite->getContentSize();

    auto renderer = cocos2d::RenderTexture::create(size.width, size.height, cocos2d::Texture2D::PixelFormat::RGBA8888);
    renderer->begin();

    m_pMask->visit();

    renderer->end();
}
}

由于这段代码,我看到了没有多边形的完整的哈特精灵。我也尝试更改 BlendFunc 值,但这无济于事。请告诉我我做了什么)

PS谢谢!PSPS 对不起我的英语)

4

1 回答 1

0

您似乎想使用多边形作为模板来剪辑心形精灵。在这种情况下。您不必混合两个DrawNode. 相反,您可以使用ClippingNode如下:

auto heartSprite = cocos2d::Sprite::create(fileName);

auto stencil = DrawNode::create();
stencil->drawPolygon(verts, count, cocos2d::Color4F::WHITE, 1, cocos2d::Color4F::WHITE);

auto clipper = ClippingNode::create();
clipper->setStencil(stencil);
clipper->addChild(heartSprite)

更多ClippingNode示例可以参考官方示例代码:https ://github.com/cocos2d/cocos2d-x/tree/v3/tests/cpp-tests/Classes/ClippingNodeTest

于 2016-02-15T08:29:35.947 回答