加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-iphone – cocos2d大纲

发布时间:2020-12-14 18:57:57 所属栏目:百科 来源:网络整理
导读:如何在cocos2d中基于alpha进行纹理轮廓?我有一个纹理,我希望无处不在的alpha从0到非零或类似的东西画一条线,所以它只是勾勒出一切. 解决方法 我通过将图像复制到内存然后使用它来解决它: 我使用了渲染纹理,但是如果你不想使用它,我添加了关于要改变的内容
如何在cocos2d中基于alpha进行纹理轮廓?我有一个纹理,我希望无处不在的alpha从0到非零或类似的东西画一条线,所以它只是勾勒出一切.

解决方法

我通过将图像复制到内存然后使用它来解决它:
我使用了渲染纹理,但是如果你不想使用它,我添加了关于要改变的内容的注释

// Get the size of the render texture,change to size of screen if you don't
    // use render texture
    CGSize s = RenderedWall.sprite.texture.contentSizeInPixels;
    int tx = s.width;
    int ty = s.height;

    int bitsPerComponent            = 8;
    int bytesPerPixel               = (bitsPerComponent * 4)/8;
    int bytesPerRow                 = bytesPerPixel * tx;
    NSInteger myDataLength          = bytesPerRow * ty;

    NSMutableData *buffer = [[NSMutableData alloc] initWithCapacity:myDataLength];
    Byte *bytes = (Byte *)[buffer mutableBytes];

    glReadPixels(0,tx,ty,GL_RGBA,GL_UNSIGNED_BYTE,bytes);

    NSMutableData *newBuffer = [[NSMutableData alloc] initWithBytes:bytes length:myDataLength];
    Byte *newBytes = (Byte *)[newBuffer mutableBytes];

    // free the render texture,remove this if you don't use one
    [RenderedWall end];

    for(int x = 0; x < tx; x++)
    {
        for(int y = 0; y < ty; y++)
        {
            int idx = y * bytesPerRow + x * bytesPerPixel + 3;

            int w = 1;

            if(bytes[idx] <= 254)
            {
                bool ok = false;
                for(int nx = -w; nx <= w; nx++)
                {
                    for(int ny = -w; ny <= w; ny++)
                    {
                        if(x + nx < 0 || y + ny < 0 || x + nx >= tx || y + ny >= ty)
                            continue;
                        if(bytes[idx + nx * bytesPerPixel + ny * bytesPerRow] == 255)
                        {
                            ok = true;
                            break;
                        }
                    }
                }
                if(ok)
                {
                    newBytes[idx - 3] = 0;
                    newBytes[idx - 2] = 0;
                    newBytes[idx - 1] = 0;
                    newBytes[idx] = 255;
                }
            }
        }
    }

    CCTexture2D *texture = [[CCTexture2D alloc] initWithData:newBytes
                                                 pixelFormat:kCCTexture2DPixelFormat_RGBA8888
                                                  pixelsWide:tx pixelsHigh:ty
                                                 contentSize:RenderedWall.sprite.texture.contentSize];

    // The outlined sprite comes here. If you don't use render texture,you need to add clear here.

    CCSprite *RenderedWallSprite = [CCSprite spriteWithTexture:texture];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读