GLProgram* TextureBlur::getBlurShader(Vec2 pixelSize, Vec2 direction, const int radius, float* weights)
{
	std::string blurShaderPath = FileUtils::getInstance()->fullPathForFilename("Shaders/TextureBlur.fsh");
	const GLchar* blur_frag = String::createWithContentsOfFile(blurShaderPath.c_str())->getCString();

	GLProgram* blur = GLProgram::createWithByteArrays(ccPositionTextureColor_vert, blur_frag);
	
	GLProgramState* state = GLProgramState::getOrCreateWithGLProgram(blur);
	state->setUniformVec2("pixelSize", pixelSize);
	state->setUniformVec2("direction", direction);
	state->setUniformInt("radius", radius);
	state->setUniformFloatv("weights", radius, weights);

    return blur;
}
示例#2
0
// 全屏实时模糊,上真机效率很差,iPad mini1只能15帧左右。本例仅为演示用法
void P001::testShader()
{
    // --- Blur
    m_render = RenderTexture::create(m_visibleSize.width, m_visibleSize.height);
    m_render->setPosition(SCREEN_CENTER);
    m_render->clear(0, 0, 0, 0);
    
    m_render->getSprite()->setGLProgram(WJSpriteShaderBlurQuick::getShader());
    GLProgramState* glState = m_render->getSprite()->getGLProgramState();
    
    Size size = m_render->getSprite()->getTexture()->getContentSizeInPixels();
    size.width = 1.0f / size.width;
    size.height = 1.0f / size.height;
    glState->setUniformVec2("resolution", size);
    
    m_render->getSprite()->getTexture()->setAntiAliasTexParameters();
    
    this->addChild(m_render, m_sceneJson->getLocalZOrder());
    
    BlendFunc bf = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
    m_render->getSprite()->setBlendFunc(bf);
    m_render->getSprite()->runAction(RepeatForever::create(
                                        Sequence::create(
                                            DelayTime::create(2.0f),
                                            FadeOut::create(1.0f),
                                            DelayTime::create(1.0f),
                                            FadeIn::create(1.0f),
                                            NULL)));
    
    //m_sceneJson->setVisible(false);
    
    // --- Gray
    WJButton* btnStart = m_sceneJson->getSubButtonByKey("start");
    //btnStart->setGLProgram(WJSpriteShaderGray::getShader());  // 因为现在WJButton支持九宫格功能,对它设置Shader会无效
    btnStart->runAction(RepeatForever::create(
                                              Sequence::create(
                                                   DelayTime::create(2.0f),
                                                   CallFunc::create([=]()
                                                   {
													   btnStart->setGrayEnabled(true);
                                                   }),
                                                   DelayTime::create(2.0f),
                                                   CallFunc::create([=]()
                                                   {
													   btnStart->setGrayEnabled(false);
                                                   }),
                                                   NULL)));
}