gpt4 book ai didi

cocos2d-iphone - cocos2d 大纲

转载 作者:行者123 更新时间:2023-12-01 01:27:03 30 4
gpt4 key购买 nike

如何在 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, 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];

关于cocos2d-iphone - cocos2d 大纲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001399/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com