Masking Sprites:如何显示没有掩码的区域

时间:2013-02-17 01:21:12

标签: opengl cocos2d-iphone ccsprite glblendfunc

此代码:

-(CCTexture2D*)maskedTextureWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite {
    CCRenderTexture * render = [CCRenderTexture renderTextureWithWidth:textureSprite.contentSizeInPixels.width/2 height:textureSprite.contentSizeInPixels.height/2];

    textureSprite.anchorPoint = ccp(0, 0);
    textureSprite.position = ccp(0, 0);

    [render addChild:textureSprite];

    [render begin];
    [textureSprite draw]; // or [spr visit];
    [render end];

    CCTexture2D *result = render.sprite.texture;
    [render removeChild:textureSprite cleanup:YES];

    CCSprite *temp = [CCSprite spriteWithTexture:result];

    CCRenderTexture * rt = [CCRenderTexture renderTextureWithWidth:maskSprite.contentSizeInPixels.width/2 height:maskSprite.contentSizeInPixels.height/2];

    maskSprite.position = ccp(0,0);
    temp.position = ccp(0,0);
    maskSprite.anchorPoint = ccp(0.0,0.0);
    temp.anchorPoint = ccp(0.0,0.0);

    [maskSprite setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
    [temp setBlendFunc:(ccBlendFunc){GL_DST_ALPHA, GL_ZERO}];
    [rt begin];
    [maskSprite draw];
    [temp draw];
    [rt end];

    return rt.sprite.texture;
}

它基于两个CCSprite s创建一个纹理:一个正常的和一个面具。无论面具精灵是不透明的,都会出现正常的精灵,并且只要面具是透明的,就不会出现正常面具。

我想实现此效果的反向。基本上,它是不透明的我想要什么都不显示,并且它是透明的,我想要正常的精灵出现。

我一直在玩混合选项,但他们让我迷惑,到目前为止没有运气。我需要什么配置?

cocos2d-iphone 1.0.1

0 个答案:

没有答案