我正在测试Cocos2d 2.1 beta4 CCClippingNode
中添加的新裁剪节点。但是,我无法使用以下方法对截短的节点进行截图。最终结果是未剪切的图像。您可以在这里找到新的版本:http://www.cocos2d-iphone.org/download
+ (UIImage *) screenshotNode:(CCNode*)startNode {
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCRenderTexture * rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
[rtx begin];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
最佳答案
建议的解决方案
以下代码似乎适用于Cocos2d v2.1:
+ (UIImage *) screenshotNode:(CCNode*)startNode {
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCRenderTexture * rtx =
[CCRenderTexture renderTextureWithWidth:winSize.width
height:winSize.height
pixelFormat:kCCTexture2DPixelFormat_RGBA8888
depthStencilFormat:GL_DEPTH24_STENCIL8];
[rtx beginWithClear:0 g:0 b:0 a:0 depth:1.0f];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
说明
为了使原始代码正常工作,必须进行以下两项更改:
depthStencilFormat
对象时指定CCRenderTexture
参数。默认情况下,CCRenderTexture
不创建深度/模板缓冲区。depthStencilFormat
参数设置为GL_DEPTH24_STENCIL8
才能创建模板缓冲区。 CCRenderTexture
初始化代码专门检查此值。 beginWithClear
的深度值而不是1.0f
调用begin
。begin
永远不会清除深度缓冲区。在CCClippingNode
内部,使用glDepthMask(GL_FALSE)
禁止写入深度缓冲区,但深度测试仍处于启用状态。由于深度缓冲区从未清除,因此我怀疑深度测试失败,导致模板永远无法绘制。 另外,必须先使用
CCGLView
创建depthFormat:GL_DEPTH24_STENCIL8_OES
,以便CCClippingNode
首先使用模板。