使用grabber抓屏,在重新输出到屏幕上,播放的粒子有黑边

根据Grabber的截屏方法,新建了个类CaptureNode 封装了 Grabber的grab(Texture2D texture)、beforeRender(Texture2D /texture/)、afterRender(cocos2d::Texture2D* /texture/)。

我的CaptureNode是根据NodeGrid修改的,想要参考它的流程,把我想要做的特效添加进去。 这种方法实现之后,播放的粒子会有黑边,粒子使用的是ONE ONE 混合方式

在初始话的时候使用_grabber->grab(_texture)抓屏
bool CaptureNode::initWithSize(Texture2D *texture, bool flipped, const Rect& rect) {
bool ret = true;
_active = false;
_reuseGrid = 0;
_texture = texture;
CC_SAFE_RETAIN(_texture);
_isTextureFlipped = flipped;

if (rect.equals(Rect::ZERO)) {
    auto size = _texture->getContentSize();
    _gridRect.setRect(0, 0, size.width, size.height);
}
else{
    _gridRect = rect;
}
_grabber = new (std::nothrow) Grabber();
if (_grabber)
{
    //将纹理设置为输出纹理。
    _grabber->grab(_texture);
}
else
{
    ret = false;
}
_shaderProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
//计算所要用到的顶点数据并创建顶点缓冲区。
calculateVertexPoints();
return ret;

}
设置顶点坐标数组、纹理左边数组还有颜色数组
void CaptureNode::calculateVertexPoints(void) {
float width = (float)_texture->getPixelsWide();
float height = (float)_texture->getPixelsHigh();
float imageH = _texture->getContentSizeInPixels().height;
textCoords[0] = Tex2F(0.0, 0.0);
textCoords[1] = Tex2F(0.0, 1.0);
textCoords[2] = Tex2F(1.0, 1.0);
textCoords[3] = Tex2F(0.0, 0.0);
textCoords[4] = Tex2F(1.0, 1.0);
textCoords[5] = Tex2F(1.0, 0.0);
colors[0] = Color4F::WHITE;
colors[1] = Color4F::WHITE;
colors[2] = Color4F::WHITE;
colors[3] = Color4F::WHITE;
colors[4] = Color4F::WHITE;
colors[5] = Color4F::WHITE;
vertices[0] = Vec2(0, 0);
vertices[1] = Vec2(0, height);
vertices[2] = Vec2(width, height);
vertices[3] = Vec2(0, 0);
vertices[4] = Vec2(width, height);
vertices[5] = Vec2(width, 0);
}

在visit的时候调用onGraberBeginDraw()和onGraberEndDraw()
void CaptureNode::onGraberBeginDraw() {
// save projection
Director *director = Director::getInstance();
_directorProjection = director->getProjection();
// 2d projection
// [director setProjection:Director::Projection::_2D];
set2DProjection();
Size size = director->getWinSizeInPixels();
//重新设置视口大小
glViewport(0, 0, (GLsizei)(size.width), (GLsizei)(size.height) );
_grabber->beforeRender(_texture);
}

void CaptureNode::onGraberEndDraw() {
_grabber->afterRender(_texture);
// restore projection
Director *director = Director::getInstance();
director->setProjection(_directorProjection);
director->setViewport();
const auto& vp = Camera::getDefaultViewport();
glViewport(vp._left, vp._bottom, vp._width, vp._height);
GL::bindTexture2D(_texture->getName());
drawCapture();
}

drawCapture()最后的绘制方法
void CaptureNode::drawCapture(void){
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD | GL::VERTEX_ATTRIB_FLAG_COLOR );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();
GL_ONE_MINUS_SRC_ALPHA);//ALPHA_NON_PREMULTIPLIED
GL::blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);//ALPHA_PREMULTIPLIED
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
GL::bindTexture2D(_texture->getName());
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)6);
CHECK_GL_ERROR_DEBUG();
}