2013-07-20 168 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的示例代碼。

希望這會有所幫助