Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何更改CCTexture2D颜色_Ios_Objective C_Opengl Es_Cocos2d Iphone_Textures - Fatal编程技术网

Ios 如何更改CCTexture2D颜色

Ios 如何更改CCTexture2D颜色,ios,objective-c,opengl-es,cocos2d-iphone,textures,Ios,Objective C,Opengl Es,Cocos2d Iphone,Textures,我有一个多边形,我使用纹理和glDrawArray(使用本教程中描述的方法:)填充它 我想能够填补我的多边形使用纯色,这是随机生成的游戏。要使用教程中的技术实现这一点,我需要动态创建一个仅为纯色的纹理(例如,我可能希望生成一个1x1的红色正方形,并使用它填充多边形) 是否有一种方法可以在cocos2d中更改纹理的颜色,类似于使用[mySprite changeColor:ccRed]更改精灵的颜色?所以,如果我有我的初始纹理,比如一个1x1的白色正方形,有没有一种方法可以将该纹理更改为1x1的红

我有一个多边形,我使用纹理和glDrawArray(使用本教程中描述的方法:)填充它

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

是否有一种方法可以在cocos2d中更改纹理的颜色,类似于使用
[mySprite changeColor:ccRed]
更改精灵的颜色?所以,如果我有我的初始纹理,比如一个1x1的白色正方形,有没有一种方法可以将该纹理更改为1x1的红色正方形

我已经尝试过使用CCRenderTexture(如本教程所述:),但是,由于我将填充大量多边形,这种方法被证明非常缓慢

我还尝试使用以下代码创建纹理:

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

非常感谢您的帮助。

也许您需要的是可变纹理

这是一篇利用CCMutableTextures的博客文章

这是我的开源项目

这是一个开放源代码项目,我在夏天一直致力于创建可破坏的地形环境。我刚刚发布的repo没有物理功能(很快就会出现),但它提供了一个界面,为精灵提供了可变纹理。一个月前我开始使用它时,它相当原始,但它演示了如何使用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
//来自游戏层
[DestTerrain系统绘图圈:ccp(300100),半径:30.0f,颜色:ccc4(0,0,0,0)];
[destTerrainSystem drawSquare:ccp(500100),半径:30.0f,颜色:ccc4(0,0,0,0)];
//地形
-(void)drawCircle:(CGPoint)circleOrigin with radius:(float)radius with color:(ccColor4B)color{
int localXOrigin=circleOrigin.x-self.position.x;
int localYOrigin=self.contentSize.height-(circleOrigin.y-self.position.y);
CCMutableTexture2D*terrainTexture=(CCMutableTexture2D*)[自纹理];
[terrainTexture Draw Circle:ccp(localXOrigin,LocalOrigin)带半径:带颜色的半径:颜色];
如果([delegate shouldApplyAfterEachDraw]| | self.applyAfterDraw)[terrainTexture apply];
}//结束拉丝圈
-(void)drawSquare:(CGPoint)squareOrigin with radius:(float)radius with color:(ccColor4B)color{
int localXOrigin=squareOrigin.x-self.position.x;
int localYOrigin=self.contentSize.height-(squareOrigin.y-self.position.y);
CCMutableTexture2D*terrainTexture=(CCMutableTexture2D*)[自纹理];
[terrainTexture drawSquare:ccp(localXOrigin,localYOrigin)带半径:带颜色的半径:颜色];
if([delegate shouldApplyAfterEachDraw]| | self.applyAfterDraw)
[应用土工合成材料];
}//结束拉丝方块
//不规则纹理
-(void)drawCircle:(CGPoint)circleOrigin with radius:(float)radius with color:(ccColor4B)color{
/*
画一个圆。这里有一些重叠,但效率相当高
*/
int x=半径;
int y=0;
int radiusError=1-x;
而(x>=y){
//下半部
[自绘制水平线:(x+circleOrigin.x):(circleOrigin.x-x):(y+circleOrigin.y)with color:color];
//上半场
[自绘制水平线:(x+circleOrigin.x):(circleOrigin.x-x):(circleOrigin.y-y)with color:color];
//左侧
[自画垂直线:(x+circleOrigin.y)endY:(circleOrigin.y-x)atX:(-y+circleOrigin.x)with color:color];
//右侧
[自画垂直线:(x+circleOrigin.y)endY:(circleOrigin.y-x)atX:(y+circleOrigin.x)with color:color];
y++;
if(radiusError<0){
radiusError=radiusError+((2*y)+1);
}否则{
x--;//将其注释掉以绘制正方形
radiusError=radiusError+(2*(y-x+1));
}//如果结束,则结束
}//结束时
//缓存更改的列值
对于(int col=circleOrigin.x-半径;col=大小\宽度)继续;
[alteredColumns addObject:[NSNumber numberWithInt:col]];
}//结束
}//端画圆
CCMutableTexture以像素数组(行主存储)维护纹理模型。然后可以访问、更改和轮询每个像素的属性。修改数组后,可以通过调用apply应用更改。这允许一些灵活性和性能调整,因为apply可能是一个昂贵的调用

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


希望这有帮助

也许你要找的是一个可变的纹理

这是一篇利用CCMutableTextures的博客文章

这是我的开源项目

这是一个开放源代码项目,我在夏天一直致力于创建可破坏的地形环境。我刚刚发布的repo没有物理功能(很快就会出现),但它提供了一个界面,为精灵提供了可变纹理。一个月前我开始使用它时,它相当原始,但它演示了如何使用CCMutableTexture类

大约两年前,Lam Hoang Pham以开源的形式发布了CCMutableTexture类。我建立在他的libr之上和周围