如何更改CCTexture2D颜色

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

我希望能够使用在游戏中随机生成的纯色填充我的多边形。 要使用本教程中的技巧来做到这一点,我需要dynamic地创build一个纯色的纹理(例如,我可能想要生成一个1×1的红色正方形,并用它来填充我的多边形)。

有没有办法改变cocos2d纹理的颜色,类似于你将如何使用[mySprite changeColor:ccRed]来改变一个精灵的颜色? 所以,如果我有我的初始纹理,说一个1×1的白色正方形,有没有办法我可以改变这个纹理到一个1×1的红色正方形?

我已经尝试使用CCRenderTexture(如本教程中所述: http ://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x),但是,作为我将填充许多多边形,这种方法certificate是相当缓慢的。

我也尝试使用下面的代码来创build我的纹理:

 // 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抓取纹理还要慢。 基本上,我正在寻找一种尽可能高效地生成dynamic纹理的方法。

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

  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

这是一个我在夏天正在开发的开源项目,用来创build可破坏的地形环境。 我刚刚发布的回购是没有物理(即将到来),但提供了一个接口,包装可变的纹理的精灵。 我在一个月前开始研究它是相当原始的,但它演示了如何使用CCMutableTexture类。

大约两年前,Lam Hoang Pham发布了CCMutableTexture类作为开源。 我build立在他的图书馆附近,提供更多的绘图工具和各种其他小function。 使用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来应用更改。 这允许一些灵活性和性能调整,因为可以是昂贵的呼叫。

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

希望这可以帮助