在过去的几个小时里,我一直在寻找一种在 cocos2d 中围绕标签创建笔划的方法,但到目前为止,我想出的只是:CCLabelTTF Font Stroke Demo这正是我所需要的,但笔划看起来很块状,我需要看起来更平滑的东西。有没有办法为笔画打开某种抗锯齿,或者可能有其他方法来创建笔画。任何帮助,将不胜感激。
问问题
1442 次
1 回答
1
To create a stroke:
-(CCRenderTexture*) createStroke: (CCLabelTTF*) label size:(float)size color:(ccColor3B)cor
{
CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:label.texture.contentSize.width+size*2 height:label.texture.contentSize.height+size*2];
CGPoint originalPos = [label position];
ccColor3B originalColor = [label color];
BOOL originalVisibility = [label visible];
[label setColor:cor];
[label setVisible:YES];
ccBlendFunc originalBlend = [label blendFunc];
[label setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE }];
CGPoint bottomLeft = ccp(label.texture.contentSize.width * label.anchorPoint.x + size, label.texture.contentSize.height * label.anchorPoint.y + size);
//CGPoint positionOffset = ccp(label.texture.contentSize.width * label.anchorPoint.x - label.texture.contentSize.width/2,label.texture.contentSize.height * label.anchorPoint.y - label.texture.contentSize.height/2);
//use this for adding stoke to its self...
CGPoint positionOffset= ccp(-label.contentSize.width/2,-label.contentSize.height/2);
CGPoint position = ccpSub(originalPos, positionOffset);
[rt begin];
for (int i=0; i<360; i+=60) // you should optimize that for your needs
{
[label setPosition:ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)];
[label visit];
}
[rt end];
[[[rt sprite] texture] setAntiAliasTexParameters];//THIS
[label setPosition:originalPos];
[label setColor:originalColor];
[label setBlendFunc:originalBlend];
[label setVisible:originalVisibility];
[rt setPosition:position];
return rt;
}
Usage:
CCRenderTexture* myStroke = [self createStroke:myCCLabelTTF size:myStrokeSize color:ccYELLOW];
[myCCLabelTTF addChild:myStroke z:-1 tag:kTagStroke];
And to increase smoothness, modify the following function to fit your needs (decrease the +60 increment to perhaps +30). Just note that the more iterations, the more more time spent rendering, which will negatively affect performance:
for (int i=0; i<360; i+=60) // you should optimize that for your needs
{
[label setPosition:ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)];
[label visit];
}
于 2013-08-31T13:16:30.973 回答