Cocos2d iphone cocos2d轮廓

Cocos2d iphone cocos2d轮廓,cocos2d-iphone,Cocos2d Iphone,如何在cocos2d中基于alpha为纹理绘制轮廓?我想在alpha从0到非0或类似的地方画一条线,这样它就可以勾勒出所有的东西。我自己解决了这个问题,将图像复制到内存中,然后使用它: 我使用了渲染纹理,但如果您不想使用,我添加了有关更改内容的注释 // Get the size of the render texture, change to size of screen if you don't // use render texture CGSize s = Ren

如何在cocos2d中基于alpha为纹理绘制轮廓?我想在alpha从0到非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, 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];
//获取渲染纹理的大小,如果没有,请更改为屏幕大小
//使用渲染纹理
CGSize=RenderedWall.sprite.texture.contentSizeInPixels;
int tx=s.宽度;
int ty=s.高度;
int bitsPerComponent=8;
int字节/像素=(比特分量*4)/8;
int bytesPerRow=bytesperpoixel*tx;
NSInteger myDataLength=bytesPerRow*ty;
NSMutableData*缓冲区=[[NSMutableData alloc]initWithCapacity:myDataLength];
字节*字节=(字节*)[缓冲区可变字节];
glReadPixels(0、0、tx、ty、GL_RGBA、GL_无符号字节、字节);
NSMutableData*newBuffer=[[NSMutableData alloc]initWithBytes:bytes-length:myDataLength];
字节*newBytes=(字节*)[newBuffer可变字节];
//释放渲染纹理,如果不使用该纹理,请将其移除
[渲染墙端];
对于(int x=0;xif(bytes[idx]我对此也很感兴趣,但我认为我们需要使用原始的openGL命令来完成,这意味着它非常重要。@Lukman:我确实解决了这个问题,但它不够快,无法在任何帧中绘制(但我不需要)。我所做的是使用
glCopyPixels
,然后对像素运行我自己的算法,并将它们加载回我在屏幕上绘制的纹理。如果愿意,请回答您自己的问题。这样将来的观众可能会了解。@done@Lukman当前位置如果您仍然感兴趣,我会发布我的解决方案。