cocos2d-x 3.8 - 如何设置后期处理

时间:2015-11-14 04:53:42

标签: cocos2d-x cocos2d-x-3.0

如何将后处理过滤器应用于当前主图层(如模糊整个屏幕等...)?我想我应该创建一个新的RenderTexture并让主层每帧访问它,但现在我不知道接下来该做什么。
这是正确的方法吗?

已解决:是的,我在那里50%,我所缺少的是从RenderTexture生成的精灵,只需将它添加到场景中,瞧,它就在那里! 重要:为此工作你需要重新构建你的场景,创建2层,1个叫做mainLayer,另一个叫postProcessingLayer,他们必须是兄弟姐妹,mainLayer包含所有精灵,postProcessingLayer包含你通过RenderTexture创建的结果精灵。
很抱歉,如果我的解释不清楚,请输入以下代码:
PP.cpp

PP::PP(){
    width = Director::getInstance()->getVisibleSize().width;
    height = Director::getInstance()->getVisibleSize().height;
    rt = RenderTexture::create(width, height);
    rt->retain();
}
Sprite* PP::getSprite(){
    if (rt){
        auto layer = Sprite::createWithTexture(rt->getSprite()->getTexture());
        layer->setFlipY(true);
        return layer;
    }
}
void PP::draw(Layer* layer){
    rt->begin();
    layer->visit();
    rt->end();
}

GameScene.cpp

bool GameScene::init()
{
    scheduleUpdate();

    mainLayer = Layer::create();
    this->addChild(mainLayer);
    mainLayer->addChild(background);

    postProcessingLayer = Layer::create();
    this->addChild(postProcessingLayer, 1);
    filter= new PP();
    auto reverseRGBSprite = filter->getSprite();
    reverseRGBSprite->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    CustomShader::reverseRGB(reverseRGBSprite);
    postProcessingLayer->addChild(reverseRGBSprite);

    postProcessingLayer2 = Layer::create();
    this->addChild(postProcessingLayer2, 2);
    auto blur = filter->getSprite();
    blur->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    CustomShader::gaussianBlur(blur);
    postProcessingLayer2->addChild(blur);

    return true;
}

void GameScene::update(float delta){
    filter->draw(mainLayer);
    filter->draw(postProcessingLayer);
}

CustomShader.cpp

GLProgram* CustomShader::init(std::string const &vert,std::string const &frag){
    auto prog = new GLProgram();

    prog->initWithFilenames(vert, frag);
    prog->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    prog->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
    prog->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD);
    prog->link();
    prog->updateUniforms();

    return prog;
}

void CustomShader::reverseRGB(Sprite *sprite){
    auto prog = CustomShader::init("shaders/reverseRGB.vert", "shaders/reverseRGB.frag");

    sprite->setShaderProgram(prog);
}

0 个答案:

没有答案