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

Sprite不会被 mask (模板无效),并且屏幕的其余所有部分都填充有白色(在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上工作并获得预期的结果(将ClippingNode作为子级而不是使用RenderTexture时获得的结果)?谢谢。

最佳答案

我不确定这是否正是您要的内容,但是通过确保设置深度模板选项,可以使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];

09-06 19:21