gpt4 book ai didi

ios - 如何更改 CCTexture2D 颜色

转载 作者:行者123 更新时间:2023-11-29 13:09:20 24 4
gpt4 key购买 nike

我有一个使用纹理和 glDrawArray 填充的多边形(使用本教程中描述的方法:http://www.raywenderlich.com/32954/how-to-create-a-game-like-tiny-wings-with-cocos2d-2-x-part-1)。

我希望能够使用在游戏过程中随机生成的纯色来填充我的多边形。要使用教程中的技术执行此操作,我需要动态创建纯色纹理(例如,我可能想要生成一个 1x1 红色方 block 并使用它来填充我的多边形)。

有没有办法在 cocos2d 中改变纹理的颜色,类似于使用 [mySprite changeColor:ccRed] 改变 Sprite 的颜色?因此,如果我有初始纹理,比如 1x1 的白色方 block ,有没有办法将该纹理更改为 1x1 的红色方 block ?

我已经尝试使用 CCRenderTexture(如本教程中所述:http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x)但是,因为我将填充大量多边形,所以这种方法被证明非常慢。

我还尝试使用以下代码来创建我的纹理:

// fill with solid red   
GLubyte buffer[3] = {255, 0, 0};

CCTexture2D *texture = [[CCTexture2D alloc] initWithData:buffer pixelFormat:kCCTexture2DPixelFormat_RGB888 pixelsWide:1 pixelsHigh:1 contentSize:m];

虽然上面的方法工作得很好,但它仍然比从 CCSprite 中抓取纹理要慢。基本上,我正在寻找一种尽可能高效地生成动态纹理的方法。

这是我用来填充多边形的代码:

    GLubyte buffer[3] = {arc4random()%256,arc4random()%256,arc4random()%256};

CGSize size;
size.width = 2; size.height = 2;

CCTexture2D *texture = [[CCTexture2D alloc] initWithData:buffer pixelFormat:kCCTexture2DPixelFormat_RGB888 pixelsWide:1 pixelsHigh:1 contentSize:size];

ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
[texture setTexParameters:&params];

ccGLBindTexture2D([texture name]);

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, array); //where array is an array of points defining a polygon
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, array);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)4);

[texture dealloc];

感谢任何帮助。

最佳答案

也许您正在寻找的是可变纹理?

这是一篇很棒的博客文章,它利用了 CCMutableTextures http://www.cocos2d-iphone.org/pixel-based-destructible-ground-with-cocos2d/

这是我的开源项目https://github.com/crebstar/PWNDestructibleTerrain

这是一个开源项目,我整个夏天都在致力于创建可破坏的地形环境。我刚刚发布的 repo 没有物理特性(即将推出),但提供了一个围绕 Sprite 可变纹理的界面。它是相当原始的,因为我在一个月前开始研究它,但它演示了如何使用 CCMutableTexture 类。

大约两年前,Lam Hoang Pham 将 CCMutableTexture 类开源发布。我建立在他的图书馆之上并围绕他的图书馆提供更多绘图实用程序和各种其他小功能。使用 CCMutableTexture 类的一个警告是您不能使用 PVR,而必须使用 UIImage 来提供纹理。我没有注意到这种方法有很多性能问题。主要问题是您不能使用 spritesheet。

无论如何,这里有一些如何使用它的例子:

 // FROM THE GAME LAYER
[destTerrainSystem drawCircle:ccp(300,100) withRadius:30.0f withColor:ccc4(0, 0, 0, 0)];
[destTerrainSystem drawSquare:ccp(500,100) withRadius:30.0f withColor:ccc4(0, 0, 0, 0)];


// IN DESTTERRAIN
-(void) drawCircle:(CGPoint)circleOrigin withRadius:(float)radius withColor:(ccColor4B)color {

int localXOrigin = circleOrigin.x - self.position.x;
int localYOrigin = self.contentSize.height - (circleOrigin.y - self.position.y);

CCMutableTexture2D * terrainTexture = (CCMutableTexture2D *) [self texture];

[terrainTexture drawCircle:ccp(localXOrigin, localYOrigin) withRadius:radius withColor:color];

if ([delegate shouldApplyAfterEachDraw] || self.applyAfterDraw) [terrainTexture apply];

} // end drawCircle

-(void) drawSquare:(CGPoint)squareOrigin withRadius:(float)radius withColor:(ccColor4B)color {

int localXOrigin = squareOrigin.x - self.position.x;
int localYOrigin = self.contentSize.height - (squareOrigin.y - self.position.y);

CCMutableTexture2D * terrainTexture = (CCMutableTexture2D *) [self texture];

[terrainTexture drawSquare:ccp(localXOrigin, localYOrigin) withRadius:radius withColor:color];

if ([delegate shouldApplyAfterEachDraw] || self.applyAfterDraw)
[terrainTexture apply];
} // end drawSquare


// IN CCMUTABLETEXTURE
-(void) drawCircle:(CGPoint)circleOrigin withRadius:(float)radius withColor:(ccColor4B)color {
/*
Draws a circle. There is some overlap here but it is fairly efficient
*/
int x = radius;
int y = 0;
int radiusError = 1 - x;

while (x >= y) {

// Bottom half
[self drawHorizontalLine:(x + circleOrigin.x) :(circleOrigin.x - x) :(y + circleOrigin.y) withColor:color];

// Top half
[self drawHorizontalLine:(x + circleOrigin.x) :(circleOrigin.x - x) :(circleOrigin.y - y) withColor:color];

// left side
[self drawVerticalLine:(x + circleOrigin.y) endY:(circleOrigin.y - x) atX:(-y + circleOrigin.x) withColor:color];

// right side
[self drawVerticalLine:(x + circleOrigin.y) endY:(circleOrigin.y - x) atX:(y + circleOrigin.x) withColor:color];

y++;

if (radiusError < 0) {
radiusError = radiusError + ((2 * y) +1);
} else {
x--; // Comment this out to draw a square
radiusError = radiusError + (2 * (y - x + 1));
} // end if

} // end while

// Cache the altered col values
for (int col = circleOrigin.x - radius; col <= circleOrigin.x + radius; col++) {
if (col < 0 || col >= size_.width) continue;
[alteredColumns addObject:[NSNumber numberWithInt:col]];
} // end for

} // end draw circle

CCMutableTexture 在像素数组(行主要存储)中维护纹理模型。然后,您可以访问、更改和轮询每个像素的属性。修改数组后,您可以通过调用 apply 来应用更改。这允许一些灵 active 和性能调整,因为应用可能是一个昂贵的调用。

还有很多你可以做的......但这应该是一个很好的起点。这两个链接都有关于如何使用 CCMutableTexture 的示例代码。

希望对你有帮助

关于ios - 如何更改 CCTexture2D 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17758982/

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