Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Last active December 26, 2015 14:58
Show Gist options
  • Save t-kashima/7169003 to your computer and use it in GitHub Desktop.
Save t-kashima/7169003 to your computer and use it in GitHub Desktop.
白いスプライトをオリジナルのスプライトの上にはりつける
// AppController.mm
//
// depthFormat: GL_DEPTH24_STENCIL8
//
// -----------------------------------------------------------
// CCSprite* sprite = CCSprite::create("HelloWorld.png");
// this->addChild(sprite);
// sprite->setPosition(ccp(size.width / 2, size.height / 2));
// sprite->setScale(0.5f);
//
// clippingWhite(sprite);
// -----------------------------------------------------------
/**
* スプライトを白くマスクする
*/
void HelloWorld::clippingWhite(CCSprite *sprite)
{
// オリジナルのスプライトからマスクをするスプライトを作成する
CCSprite* mask = CCSprite::createWithTexture(sprite->getTexture());
// マスクされるのは白いノード
CCDrawNode* content = CCDrawNode::create();
CCSize contentSize = sprite->getContentSize();
CCPoint vectors[] = { ccp(-contentSize.width / 2, contentSize.height / 2),
ccp(contentSize.width / 2, contentSize.height / 2),
ccp(contentSize.width / 2, -contentSize.height / 2),
ccp(-contentSize.width / 2, -contentSize.height / 2) };
ccColor4F white = ccc4FFromccc3B(ccWHITE);
content->drawPolygon(vectors, 4, white, 0, white);
// クリッピングする
CCClippingNode* clippingNode = CCClippingNode::create();
clippingNode->setStencil(mask);
clippingNode->setAlphaThreshold(0.01);
clippingNode->setAnchorPoint(ccp(0.5f, 0.5f));
clippingNode->addChild(content);
clippingNode->setPosition(ccp(contentSize.width / 2, contentSize.height / 2));
clippingNode->setTag(kTagClippingNode);
// 白くマスクしたノードをオリジナルのスプライトにつける
sprite->addChild(clippingNode);
}
/**
* スプライトのマスクを削除する
*/
void HelloWorld::clearClippingWhite(CCSprite *sprite)
{
CCNode* node = sprite->getChildByTag(kTagClippingNode);
if (node) {
node->removeFromParent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment