我有一個使用紋理和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:¶ms];
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];
任何幫助表示讚賞。