Cocos2d-x v3:ClippingNode无法处理RenderTexture

时间:2014-07-21 14:20:48

标签: c++ cocos2d-x cocos2d-x-3.0

如果ClippingNode呈现为RenderTexture而不是作为子项添加(或者在我的情况下添加到本身呈现给RenderTexture的容器中),则效果会被破坏:

sprite没有被屏蔽(模板没有效果),屏幕的所有其余部分都填充了白色(在ClippingNode被添加到所有其他层之上的情况下)。

(在ios和win32上测试)

auto stencil = DrawNode::create();
static Point triangle[3];
triangle[0] = Point(-40, -40);
triangle[1] = Point(40, -40);
triangle[2] = Point(0, 40);
static Color4F green(0, 1, 0, 1);
stencil->drawPolygon(triangle, 3, green, 0, green);

auto clipper = ClippingNode::create();
clipper->setAnchorPoint(Point(0.5, 0.5));
clipper->setPosition( Point(100, 100) );
clipper->setStencil(stencil);
clipper->setInverted(true);

// containerAddedAsChild->addChild(clipper, 20);      // this works fine
containerRenderedToTexture->addChild(clipper, 20);    // this breaks 

auto img = Sprite::create("test_sprite.png");
img->setAnchorPoint(Point(0.5, 0.5));
clipper->addChild(img);

如何使用预期结果(在将ClippingNode作为子项添加而不是使用RenderTexture时获得的结果)使用RenderTexture处理ClippingNode?感谢。

1 个答案:

答案 0 :(得分:7)

我不确定这是否正是您所要求的,但您可以通过确保设置深度模板选项让ClippingNodes正确呈现到RenderTexture上。

在Cocos2d-x 3.x中,它看起来像这样:

RenderTexture* renderTexture = RenderTexture::create(paddedSize.width, paddedSize.height,
    Texture2D::PixelFormat::RGBA8888,
    GL_DEPTH24_STENCIL8_OES); // configure for clipping node

renderTexture->beginWithClear(0, 0, 0, 0, 1.0f);
clippingNode->Node::visit();
renderTexture->end();

在Cocos2d-iPhone / Swift 3.x中,它看起来像这样:

CCRenderTexture *renderTexture = [CCRenderTexture renderTextureWithWidth:paddedSize.width height:paddedSize.height
    pixelFormat:CCTexturePixelFormat_RGBA8888
    depthStencilFormat:GL_DEPTH24_STENCIL8_OES];
[renderTexture beginWithClear:0.0f g:0.0f b:0.0f a:0.0f depth:1.0f];
[clippingNode visit];
[renderTexture end];