2013-07-20 158 views
1

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

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

有没有办法改变cocos2d中纹理的颜色,类似于你如何使用[mySprite changeColor:ccRed]改变sprite的颜色?所以如果我有我的初始纹理,比如说1x1的白色正方形,有没有办法让纹理更改为1x1的红色正方形?

我已经尝试使用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]; 

任何帮助表示赞赏。

回答

1

也许你在寻找的是一个可变的纹理?

这里是一个伟大的博客文章,其利用CCMutableTextures http://www.cocos2d-iphone.org/pixel-based-destructible-ground-with-cocos2d/

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

这是一个开源项目,我一直在努力在夏天制造破坏的地形环境。我刚刚发布的回购是没有物理(即将到来),但提供了一个接口,包装可变纹理的精灵。我在一个月前开始研究它时相当简单,但它演示了如何使用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来应用更改。这可以调整灵活性和性能,适用于昂贵的通话。

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

希望这会有所帮助