(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.13使用CCTexture2DMutable调换调色盘

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 

1.13使用CCTexture2DMutable调换调色盘

 

 
 

这是另一种调换到指定的颜色或者纹理的方法。这个方法可以节省一些额外的磁盘空间以及内存消耗,但是有点需要CPU时间。相比之前的,这个技术在混合更复杂一点。

 

 

如何工作…

 

 
 

我们要做的第一件事就是绘制一个精灵,并用指定颜色在不同区域上上色。指定颜色是原色通常容易分辨并且可以取代编程。这次我们使用红蓝绿三色。

 

使用这个技术混合图像的时候,我们尽可能的避免锯齿。Adjustingyour coloring algorithm's tolerance according to your texture can be tricky.

 

现在让我们来看看代码:

 

#import "CCTexture2DMutable.h"

@implementationCh1_MutablePaletteSwapping

-(CCLayer*) runRecipe

{

   //Create a nice looking background

   //再次创建一个看起来特别nice的背景

   CCSprite *bg = [CCSpritespriteWithFile:@"baseball_bg_01.png"];

   [bg setPosition:ccp(240,160)];

   bg.opacity = 100;

   [self addChild:bg z:0 tag:0];

   /*** Animate 4 different fielders with different color combinations ***/

   //4个不同的有不同颜色的运动员

   //Set color arrays

   //设置颜色

   ccColor4B colors1[] = { ccc4(255,217,161,255), ccc4(225,225,225,255),ccc4(0,0,150,255) };

   ccColor4B colors2[] = { ccc4(140,100,46,255), ccc4(150,150,150,255),ccc4(255,0,0,255) };

   ccColor4B colors3[] = { ccc4(255,217,161,255), ccc4(115,170,115,255),ccc4(115,170,115,255) };

   ccColor4B colors4[] = { ccc4(140,100,46,255), ccc4(50,50,50,255),ccc4(255,255,0,255) };

   //Create texture copy to use as an immutable guide.

   //创建一个不可改变的纹理副本

    CCTexture2DMutable* textureCopy =[[[CCTexture2DMutable alloc] initWithImage:[UIImageimageNamed:@"fielder_run_sentinel_colors.png"]] autorelease];

   //Create our sprites using mutable textures.

   //使用可变纹理创建精灵

   CCSprite *sprite1 = [CCSprite spriteWithTexture:[[[CCTexture2DMutablealloc] initWithImage:[UIImageimageNamed:@"fielder_run_sentinel_colors.png"]] autorelease]];

   CCSprite *sprite2 = [CCSprite spriteWithTexture:[[[CCTexture2DMutablealloc] initWithImage:[UIImage imageNamed:@"fielder_run_sentinel_colors.png"]]autorelease]];

   CCSprite *sprite3 = [CCSprite spriteWithTexture:[[[CCTexture2DMutablealloc] initWithImage:[UIImageimageNamed:@"fielder_run_sentinel_colors.png"]] autorelease]];

   CCSprite *sprite4 = [CCSprite spriteWithTexture:[[[CCTexture2DMutablealloc] initWithImage:[UIImageimageNamed:@"fielder_run_sentinel_colors.png"]] autorelease]];

   //Set sprite positions

   //设置精灵位置

   [sprite1 setPosition:ccp(125,75)];

   [sprite2 setPosition:ccp(125,225)];

   [sprite3 setPosition:ccp(325,75)];

   [sprite4 setPosition:ccp(325,225)];

   //Swap colors in each sprite mutable texture and apply the changes.

   //在每一个可变纹理中改变精灵的颜色

   [self swapColor:ccc4(0,0,255,255) withColor:colors1[0]inTexture:sprite1.texture withCopy:textureCopy];

   [self swapColor:ccc4(0,255,0,255) withColor:colors1[1]inTexture:sprite1.texture withCopy:textureCopy];

   [self swapColor:ccc4(255,0,0,255) withColor:colors1[2]inTexture:sprite1.texture withCopy:textureCopy];

   [sprite1.texture apply];

   /* CODE OMITTED */

   //省略代码

   //Finally, add the sprites to the scene.

   //最终,再场景上添加他们

   [self addChild:sprite1 z:0 tag:0];

   [self addChild:sprite2 z:0 tag:1];

   [self addChild:sprite3 z:0 tag:2];

   [self addChild:sprite4 z:0 tag:3];

   return self;

}

-(void) swapColor:(ccColor4B)color1withColor:(ccColor4B)color2 inTexture:(CCTexture2DMutable*)texturewithCopy:(CCTexture2DMutable*) copy

{

   //Look through the texture, find all pixels of the specified color andchange them.

   //通过材质,找到那个指定的颜色,并且换掉他

   //We use a tolerance of 200 here.

   //

   for(int x=0; x<texture.pixelsWide; x++)

   {

       for(int y=0; y<texture.pixelsHigh; y++)

       {

            if( [self isColor:[copypixelAt:ccp(x,y)] equalTo:color1 withTolerance:200] )

            {

                [texture setPixelAt:ccp(x,y)rgba:color2];

            }

       }

   }

}

-(bool) isColor:(ccColor4B)color1equalTo:(ccColor4B)color2 withTolerance:(int)tolerance

{

   //If the colors are equal within a tolerance we change them.

   //如果有color1的颜色与color2的颜色不同,就返回YES

   bool equal = YES;

   if( abs(color1.r - color2.r) + abs(color1.g - color2.g) + abs(color1.b -color2.b) + abs(color1.a - color2.a) > tolerance )

   {

       equal = NO;

   }

   return equal;

}

@end

 

如何工作…

 

这个技术的工作方式与PS里选择替换颜色的方式差不多,并且绘制方式也相似。使用CCTexture2DMutable通常是一个比较慢的过程并且对于像素要求来说非常的严格。耗费很大的CPU的计算量。

 

我琢磨了一下,有两件事要说:

1.这种方法其实就是挨个算像素点的颜色,真是特别占计算量。

2.这个CCTexture2DMutable类是CCTexture2D的子类,不过 个人认为是类似于扩展包之类的东西,下个案例的那个AWTextureFilter类好像也是这样。

 

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值