【问题标题】:How to Change CCTexture2D Color如何更改 CCTexture2D 颜色
【发布时间】:2013-07-23 08:34:45
【问题描述】:

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

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

有没有办法在 cocos2d 中改变纹理的颜色,类似于使用[mySprite changeColor:ccRed] 改变精灵的颜色?所以如果我有我的初始纹理,比如 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];

感谢任何帮助。

【问题讨论】:

    标签: ios objective-c opengl-es cocos2d-iphone textures


    【解决方案1】:

    也许您正在寻找的是可变纹理?

    这是一篇很棒的博客文章,它使用了 CCMutableTextures http://www.cocos2d-iphone.org/pixel-based-destructible-ground-with-cocos2d/

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

    这是一个开源项目,我在整个夏天一直致力于创建可破坏的地形环境。我刚刚发布的 repo 没有物理(即将推出),但提供了一个界面,可以为 sprite 包装可变纹理。一个月前我开始研究它时,它相当原始,但它演示了如何使用 CCMutableTexture 类。

    大约两年前,Lam Hoang Pham 将 CCMutableTexture 类作为开源发布。我建立在他的图书馆之上和周围,以提供更多的绘图实用程序和其他各种小功能。使用 CCMutableTexture 类的一个警告是您不能使用 PVR 并且必须使用 UIImage 来提供纹理。我没有注意到这种方法有很多性能问题。主要问题是您不能使用精灵表。

    无论如何,这里有一些如何使用它的示例:

     // 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 来应用更改。这允许一些灵活性和性能调整,因为 apply 可能是一个昂贵的调用。

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 2014-05-02
      • 2018-08-20
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多