图形裁切

c++----------------------------------------------------------------------------------------------------------------------------------------------
CCSprite * UnivCoreTest::maskedSprite(CCSprite *textureSprite)
{
    CCSprite * maskSprite = CCSprite::create("circle_mask.png");
    CCRenderTexture * renderTexture = CCRenderTexture::create(maskSprite->getContentSize().width, maskSprite->getContentSize().height);
    
    maskSprite->setPosition(ccp(maskSprite->getContentSize().width / 2, maskSprite->getContentSize().height / 2));
    textureSprite->setPosition(ccp(textureSprite->getContentSize().width / 2, textureSprite->getContentSize().height / 2));
 
    maskSprite->setBlendFunc((ccBlendFunc){GL_ONE, GL_ZERO});
    textureSprite->setBlendFunc((ccBlendFunc){GL_DST_ALPHA, GL_ZERO});
    
    renderTexture->begin();
    maskSprite->visit();
    textureSprite->visit();
    renderTexture->end();
 
    CCSprite * retval = CCSprite::createWithTexture(renderTexture->getSprite()->getTexture());
    retval->setFlipY(true);
    return retval;
}

函数形参“textureSprite”是待裁剪的方形图片,当然读者需要先将其创建成CCSprite再传入。

方法中出现的“circle_mask.png”图片是整个裁剪过程的关键,它决定了你的原始图片会被裁剪成什么形状。

lua ---------------------------------------------------------------------------------------------------------------------------------------

--对纹理进行裁剪  裁剪的图片路径spritePath   裁剪的形状图片路径maskPath
function cc.exports.maskedSprite(spritePath, maskPath)
    local textureSprite = cc.Sprite:create(spritePath)
    local textureSize = textureSprite:getContentSize()

    local maskSprite = cc.Sprite:create(maskPath)
    local maskSize = maskSprite:getContentSize()

    local renderTexture = cc.RenderTexture:create(maskSize.width,maskSize.height)
    maskSprite:setPosition(cc.p(maskSize.width/2,maskSize.height/2))
    textureSprite:setPosition(cc.p(textureSize.width/2,textureSize.height/2))

    maskSprite:setBlendFunc(cc.blendFunc(GL_ONE,GL_ZERO))
    textureSprite:setBlendFunc(cc.blendFunc(GL_DST_ALPHA,GL_ZERO))

    renderTexture:begin()
    maskSprite:visit()
    textureSprite:visit()
    renderTexture:endToLua()

    local retSprite = cc.Sprite:createWithTexture(renderTexture:getSprite():getTexture())
    retSprite:setFlippedY(true)
    return retSprite
end
 

**************************************************************************************************

--picPath and stencilPath 尺寸要一致, 更新节点需要移除之前的节点
function cc.exports.exchangeImageToClipNode(imgNode, picPath, stencilPath,index)
    imgNode:setVisible(false)
    local parent = imgNode:getParent()
    local clipnode = parent:getChildByName("ClipNode"..tostring(index))
    if clipnode then
        clipnode:removeFromParent()
    end

    local clipNodeEx = cc.ClippingNode:create()
    parent:addChild(clipNodeEx)
    local maskNode = imgNode:clone()
    maskNode:loadTexture(picPath, ccui.TextureResType.localType)
    maskNode:setVisible(true)
    local sprite = cc.Sprite:create(stencilPath)
    clipNodeEx:addChild(maskNode)
    clipNodeEx:setStencil(sprite)
    maskNode:setPosition(imgNode:getPosition())
    sprite:setPosition(imgNode:getPosition())
    clipNodeEx:setName("ClipNode"..tostring(index))
    clipNodeEx:setInverted(true)
    clipNodeEx:setAlphaThreshold(0.5)

    return clipNodeEx
end
 

可以根据imageview的尺寸自适应,不过有个bug,如果有弹出窗的层级高于在遮罩图片的被创建前父节点的层级,当遮罩图片被创建以后,弹出窗也会被遮罩裁剪

 

 

 

转自:

https://blog.csdn.net/univcore/article/details/42149725

https://blog.csdn.net/wzufjp/article/details/71191724

上一篇:每次都能让人头大的 Shader -- 从一次简单的功能说起


下一篇:后处理之径向模糊