How to Change CCTexture2D Color

核能气质少年 提交于 2020-01-06 02:22:41

问题


I have a polygon that I fill using a texture and glDrawArray (using the method described in this tutorial: http://www.raywenderlich.com/32954/how-to-create-a-game-like-tiny-wings-with-cocos2d-2-x-part-1).

I want to be able to fill my polygon using a solid color, which is generated at random during gameplay. To do this using the technique from the tutorial, I need to dynamically create a texture that is just a solid color (for example, I might want to generate a 1x1 red square and use that to fill my polygons).

Is there a way to change the color of a texture in cocos2d, similar to how you would change the color of a sprite using [mySprite changeColor:ccRed]? So if I had my initial texture, say a 1x1 white square, is there a way I can change that texture to a 1x1 red square?

I have already tried using CCRenderTexture (as described in this tutorial: http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x) but, as I will be filling numerous polygons, this method proves to be quite slow.

I have also tried using the following code to create my texture:

// 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];

While the above works fairly well, it is still slower than just grabbing the texture from a CCSprite. Basically, I am looking for a way to generate a dynamic texture as efficiently as possible.

Here is the code I am using to fill my polygons:

    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];

Any help is appreciated.


回答1:


Maybe what you are looking for is a mutable texture?

Here is a great blog post which utilizes CCMutableTextures http://www.cocos2d-iphone.org/pixel-based-destructible-ground-with-cocos2d/

Here is my open source project https://github.com/crebstar/PWNDestructibleTerrain

This is an open source project I've been working on over the Summer to create destructible terrain environments. The repo I just posted is without physics (soon to come), but provides an interface that wraps around mutable textures for sprites. It is fairly primitive as I started working on it a month ago, but it demonstrates how to use the CCMutableTexture class.

Around two or so years ago, Lam Hoang Pham released the CCMutableTexture class as open source. I built upon and around his library to provide more drawing utility and various other small features. The one caveat with using the CCMutableTexture class is you cannot use PVR's and must use a UIImage to provide the texture. I haven't noticed many performance issues with this method. The main problem would be you can't use a spritesheet.

Anyways here are some examples of how it is used:

 // 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

The CCMutableTexture maintains a model of the texture in an array of pixels (row major storage). You can then access, change, and poll for the property of each pixel. After you have modified the array, you can then apply the changes by calling apply. This allows for some flexibility and performance tweaking as apply can be an expensive call.

There is a lot more you can do... But this should be a good starting point. Both links have example code on how to use the CCMutableTexture.

Hope this helps



来源:https://stackoverflow.com/questions/17758982/how-to-change-cctexture2d-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!