【问题标题】:Screenshot with CCClippingNode - cocos2d-iphone-2.1-beta4使用 CCClippingNode 的屏幕截图 - cocos2d-iphone-2.1-beta4
【发布时间】:2013-01-12 10:19:43
【问题描述】:

我正在测试 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];
}

【问题讨论】:

  • 我想剪辑不适用于渲染纹理。如果您可以在一个简单的测试用例中验证这一点,您应该将其报告为错误。

标签: ios objective-c cocos2d-iphone screenshot


【解决方案1】:

建议的解决方案

以下代码似乎适用于 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];
}

说明

为了使原始代码正常工作,需要进行以下两项更改:

  1. 在创建CCRenderTexture 对象时指定depthStencilFormat 参数。默认情况下,CCRenderTexture 不会创建深度/模板缓冲区。
    • 至少在 v2.1 中,depthStencilFormat 参数必须设置为 GL_DEPTH24_STENCIL8 以创建模板缓冲区。 CCRenderTexture 初始化代码专门检查该值。
  2. 使用深度值1.0f 而不是begin 调用beginWithClear
    • 似乎只调用begin 永远不会清除深度缓冲区。在CCClippingNode 内部,使用glDepthMask(GL_FALSE) 禁用写入深度缓冲区,但仍启用深度测试。由于深度缓冲区永远不会被清除,我怀疑深度测试失败,导致模板永远不会被绘制。

此外,必须使用depthFormat:GL_DEPTH24_STENCIL8_OES 创建CCGLView,以便CCClippingNode 首先使用模板。

【讨论】:

  • 这非常有效。我们只需要设置深度属性。谢谢!
【解决方案2】:

问题是我没有正确设置我的 CCGLView。我必须将深度格式设置为 GL_DEPTH24_STENCIL8_OES 而不是值 0

在 AppController.mm 中设置 depthFormat

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多