Reputation: 41
I have an image and a shape. I need to create a cut out from image with alpha channel of my shape, here is what I mean:
I could use CCMask, but I am not impressed with the result. Can you please point me to an alternative solutions? Greatly appreciate any assistance.
Upvotes: 2
Views: 1696
Reputation: 251
-=(W)=-
I had a similar problem. I read the tutorials and used CCMask. CCMask is good class, but has one problem. When the size of the mask is greater than the size of the texture, we can see the pieces of the mask, which is very bad. Use my decision and enjoy!
-(CCSprite *)maskedSpriteWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite
{
CCSprite *maskSprite_ = [CCSprite spriteWithTexture:[maskSprite texture]];
CCSprite *textureSprite_ = [CCSprite spriteWithTexture:[textureSprite texture]];
maskSprite_.position = maskSprite.position;
maskSprite_.rotation = maskSprite.rotation;
maskSprite_.opacity = maskSprite.opacity;
maskSprite_.scale = maskSprite.scale;
maskSprite_.zOrder = maskSprite.zOrder;
textureSprite_.position = textureSprite.position;
textureSprite_.rotation = textureSprite.rotation;
textureSprite_.opacity = textureSprite.opacity;
textureSprite_.scale = textureSprite.scale;
textureSprite_.zOrder = textureSprite.zOrder;
[maskSprite_ setColor:ccc3(0.0, 0.0, 0.0)];
//Start cutting sprite
CCRenderTexture *rt;
float dx;
float dy;
//Correct mask
rt = [CCRenderTexture renderTextureWithWidth:maskSprite_.contentSize.width*maskSprite_.scale height:maskSprite_.contentSize.height*maskSprite_.scale];
dx = textureSprite_.position.x - maskSprite_.position.x;
dy = textureSprite_.position.y - maskSprite_.position.y;
maskSprite_.position = ccp(maskSprite_.contentSize.width/2*maskSprite_.scale, maskSprite_.contentSize.height/2*maskSprite_.scale);
textureSprite_.position = ccp(maskSprite_.position.x+dx, maskSprite_.position.y+dy);
[textureSprite_ setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
[maskSprite_ setBlendFunc:(ccBlendFunc){GL_DST_ALPHA, GL_ZERO}];
[rt begin];
[textureSprite_ visit];
[maskSprite_ visit];
[rt end];
CCSprite *correctMaskSprite_ = [CCSprite spriteWithTexture:rt.sprite.texture];
correctMaskSprite_.flipY = YES;
correctMaskSprite_.position = maskSprite.position;
correctMaskSprite_.rotation = maskSprite.rotation;
correctMaskSprite_.opacity = maskSprite.opacity;
correctMaskSprite_.scale = maskSprite.scale;
correctMaskSprite_.zOrder = maskSprite.zOrder;
//New sprite with correct mask
[correctMaskSprite_ setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
[textureSprite_ setBlendFunc:(ccBlendFunc){GL_DST_ALPHA, GL_ZERO}];
[rt begin];
[correctMaskSprite_ visit];
[textureSprite_ visit];
[rt end];
CCSprite *retval = [CCSprite spriteWithTexture:rt.sprite.texture];
retval.flipY = YES;
return retval;
}
Upvotes: 0
Reputation: 8729
Have a look at these tutorials,
Cocos2d 2.0: http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0
Cocos2d 1.0: http://www.raywenderlich.com/4421/how-to-mask-a-sprite-with-cocos2d-1-0
Upvotes: 1