在CCRenderTexture中绘制抗锯齿圆

Joh*_*ker 2 antialiasing cocos2d-iphone ios opengl-es-2.0

我是cocos2d/OpenGLES的新手,我遇到了一个无法找到解决方案的问题.基本上,我想在CCRenderTexture中绘制一个抗锯齿的圆,然后在多个精灵上使用该纹理.除了抗锯齿部分之外的一切都很简单,但是我被困住了,无法弄清楚接下来要去哪里.

我现在的代码是:

int textureSize = 64;
CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];
[rt beginWithClear:spriteColor.r g:spriteColor.g b:spriteColor.b a:0.0f];

ccDrawColor4F(spriteColor.r, spriteColor.g, spriteColor.b, spriteColor.a);
ccDrawCircle(CGPointMake(textureSize / 2.0f, textureSize / 2.0f), textureSize / 2.0f, 0.0f, 360, false);

[rt end];
Run Code Online (Sandbox Code Playgroud)

然而,这导致了锯齿状的混乱,我无法弄清楚从哪里开始.我在网上看过使用点绘制光滑圆圈的例子,但这似乎不适用于OpenGLES 2.0.

性能不是一个问题,因为我正在绘制纹理一次并重复使用纹理.

Ben*_*ove 9

在Core Graphics中创建圆形纹理,并将其作为CGImage添加到纹理缓存中.Core Graphics自动使用抗锯齿功能.圆圈看起来像这样.

圈

示例代码:

//Setup Core Graphics
CGSize circleSize = CGSizeMake(100, 100);
CGPoint circlePosition = ccp(50, 50);
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();

//Add the circle to the context and draw it.
CGContextAddArc(context, circlePosition.x, circlePosition.y , circleSize.width/2, 0,2*M_PI,1);
CGContextDrawPath(context,kCGPathStroke);

//Get an image so we can store it in the Texture Cache
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Add the image to the texture cache
[[CCTextureCache sharedTextureCache] addCGImage:[img CGImage] forKey:@"circleKey"];
Run Code Online (Sandbox Code Playgroud)

然后你可以使用精灵

CCSprite *circle = [CCSprite spriteWithTexture:[[CCTextureCache sharedTextureCache] textureForKey:@"circleKey"]];
Run Code Online (Sandbox Code Playgroud)