2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
#include "ShaderTest.h"
|
|
|
|
#include "../testResource.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocos2d.h"
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2015-04-09 12:23:47 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
ShaderTests::ShaderTests()
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(ShaderLensFlare);
|
|
|
|
ADD_TEST_CASE(ShaderMandelbrot);
|
|
|
|
ADD_TEST_CASE(ShaderJulia);
|
|
|
|
ADD_TEST_CASE(ShaderHeart);
|
|
|
|
ADD_TEST_CASE(ShaderFlower);
|
|
|
|
ADD_TEST_CASE(ShaderPlasma);
|
|
|
|
ADD_TEST_CASE(ShaderBlur);
|
|
|
|
ADD_TEST_CASE(ShaderRetroEffect);
|
|
|
|
ADD_TEST_CASE(ShaderMonjori);
|
|
|
|
ADD_TEST_CASE(ShaderGlow);
|
|
|
|
ADD_TEST_CASE(ShaderMultiTexture);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///---------------------------------------
|
|
|
|
//
|
|
|
|
// ShaderNode
|
|
|
|
//
|
|
|
|
///---------------------------------------
|
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
SIZE_X = 256,
|
|
|
|
SIZE_Y = 256,
|
2012-03-21 17:27:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ShaderNode::ShaderNode()
|
2014-05-15 01:07:09 +08:00
|
|
|
:_center(Vec2(0.0f, 0.0f))
|
|
|
|
,_resolution(Vec2(0.0f, 0.0f))
|
2013-06-15 14:03:30 +08:00
|
|
|
,_time(0.0f)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-18 10:15:46 +08:00
|
|
|
ShaderNode::~ShaderNode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
ShaderNode* ShaderNode::shaderNodeWithVertex(const std::string &vert, const std::string& frag)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto node = new (std::nothrow) ShaderNode();
|
2012-04-19 14:35:52 +08:00
|
|
|
node->initWithVertex(vert, frag);
|
|
|
|
node->autorelease();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return node;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
bool ShaderNode::initWithVertex(const std::string &vert, const std::string &frag)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2015-08-10 14:40:54 +08:00
|
|
|
_vertFileName = vert;
|
|
|
|
_fragFileName = frag;
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-07-09 23:03:04 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
2014-05-13 10:12:56 +08:00
|
|
|
this->setGLProgramState(nullptr);
|
|
|
|
loadShaderVertex(_vertFileName, _fragFileName);
|
2013-12-31 10:54:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
loadShaderVertex(vert, frag);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_time = 0;
|
2014-05-15 01:07:09 +08:00
|
|
|
_resolution = Vec2(SIZE_X, SIZE_Y);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
scheduleUpdate();
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
setContentSize(Size(SIZE_X, SIZE_Y));
|
2014-05-15 01:07:09 +08:00
|
|
|
setAnchorPoint(Vec2(0.5f, 0.5f));
|
2012-10-18 10:15:46 +08:00
|
|
|
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &frag)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto fileUtiles = FileUtils::getInstance();
|
|
|
|
|
|
|
|
// frag
|
|
|
|
auto fragmentFilePath = fileUtiles->fullPathForFilename(frag);
|
|
|
|
auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
|
|
|
|
|
|
|
|
// vert
|
|
|
|
std::string vertSource;
|
|
|
|
if (vert.empty()) {
|
|
|
|
vertSource = ccPositionTextureColor_vert;
|
|
|
|
} else {
|
|
|
|
std::string vertexFilePath = fileUtiles->fullPathForFilename(vert);
|
|
|
|
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
|
2014-05-14 09:12:58 +08:00
|
|
|
auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(glprogramstate);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void ShaderNode::update(float dt)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_time += dt;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ShaderNode::setPosition(const Vec2 &newPosition)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Node::setPosition(newPosition);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto position = getPosition();
|
2015-07-27 14:18:17 +08:00
|
|
|
auto frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
|
|
|
|
auto visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
auto retinaFactor = Director::getInstance()->getOpenGLView()->getRetinaFactor();
|
|
|
|
_center = Vec2(position.x * frameSize.width / visibleSize.width * retinaFactor, position.y * frameSize.height / visibleSize.height * retinaFactor);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void ShaderNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
2014-05-31 07:42:05 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(ShaderNode::onDraw, this, transform, flags);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2013-12-25 16:38:43 +08:00
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void ShaderNode::onDraw(const Mat4 &transform, uint32_t flags)
|
2013-12-25 16:38:43 +08:00
|
|
|
{
|
2014-03-06 07:49:08 +08:00
|
|
|
float w = SIZE_X, h = SIZE_Y;
|
|
|
|
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
|
|
|
|
|
2014-05-09 06:43:12 +08:00
|
|
|
auto glProgramState = getGLProgramState();
|
2015-08-10 14:40:54 +08:00
|
|
|
glProgramState->setUniformVec2("resolution", _resolution);
|
|
|
|
glProgramState->setUniformVec2("center", _center);
|
2014-05-09 06:43:12 +08:00
|
|
|
glProgramState->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2014-05-09 06:43:12 +08:00
|
|
|
glProgramState->apply(transform);
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderMonjori
|
|
|
|
|
|
|
|
ShaderMonjori::ShaderMonjori()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderMonjori::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Monjori.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMonjori::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMonjori::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Monjori plane deformations";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ShaderMandelbrot
|
|
|
|
ShaderMandelbrot::ShaderMandelbrot()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderMandelbrot::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Mandelbrot.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2014-11-22 02:29:36 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMandelbrot::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderMandelbrot::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Mandelbrot shader with Zoom";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderJulia
|
|
|
|
ShaderJulia::ShaderJulia()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderJulia::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Julia.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2014-11-22 02:29:36 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderJulia::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderJulia::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Julia shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ShaderHeart
|
|
|
|
ShaderHeart::ShaderHeart()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderHeart::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Heart.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderHeart::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderHeart::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Heart";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderFlower
|
|
|
|
ShaderFlower::ShaderFlower()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderFlower::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Flower.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderFlower::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderFlower::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Flower";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ShaderPlasma
|
|
|
|
ShaderPlasma::ShaderPlasma()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderPlasma::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Plasma.fsh");
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sn);
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-21 17:27:13 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderPlasma::title() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Shader: Frag shader";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderPlasma::subtitle() const
|
2012-03-21 17:27:13 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Plasma";
|
2012-03-21 17:27:13 +08:00
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
// ShaderBlur
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
class SpriteBlur : public Sprite
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-10-18 10:39:56 +08:00
|
|
|
~SpriteBlur();
|
2013-06-20 14:17:10 +08:00
|
|
|
bool initWithTexture(Texture2D* texture, const Rect& rect);
|
2014-05-13 10:12:56 +08:00
|
|
|
void initGLProgram();
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
static SpriteBlur* create(const char *pszFileName);
|
2014-06-12 09:49:48 +08:00
|
|
|
void setBlurRadius(float radius);
|
|
|
|
void setBlurSampleNum(float num);
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-12-25 16:54:09 +08:00
|
|
|
protected:
|
2014-06-11 14:54:14 +08:00
|
|
|
float _blurRadius;
|
2014-06-12 09:49:48 +08:00
|
|
|
float _blurSampleNum;
|
2012-04-17 18:31:31 +08:00
|
|
|
};
|
|
|
|
|
2012-10-18 10:39:56 +08:00
|
|
|
SpriteBlur::~SpriteBlur()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
SpriteBlur* SpriteBlur::create(const char *pszFileName)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SpriteBlur* pRet = new (std::nothrow) SpriteBlur();
|
2015-12-03 14:21:00 +08:00
|
|
|
if (pRet)
|
|
|
|
{
|
|
|
|
bool result = pRet->initWithFile("");
|
2016-02-09 03:25:37 +08:00
|
|
|
log("Test call Sprite::initWithFile with bad file name result is : %s", result ? "true" : "false");
|
2015-12-03 14:21:00 +08:00
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
if (pRet && pRet->initWithFile(pszFileName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-02-28 17:34:24 +08:00
|
|
|
_blurRadius = 0;
|
2013-06-20 14:17:10 +08:00
|
|
|
if( Sprite::initWithTexture(texture, rect) )
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-07-09 23:03:04 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
2014-05-13 10:12:56 +08:00
|
|
|
initGLProgram();
|
2013-12-31 10:54:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
2012-10-18 10:39:56 +08:00
|
|
|
|
2014-06-11 14:54:14 +08:00
|
|
|
initGLProgram();
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
void SpriteBlur::initGLProgram()
|
2012-10-18 10:39:56 +08:00
|
|
|
{
|
2015-07-24 12:47:38 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2016-04-11 18:35:44 +08:00
|
|
|
std::string fragSource = FileUtils::getInstance()->getStringFromFile(
|
|
|
|
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh"));
|
2015-07-24 12:47:38 +08:00
|
|
|
#else
|
2016-04-11 18:35:44 +08:00
|
|
|
std::string fragSource = FileUtils::getInstance()->getStringFromFile(
|
|
|
|
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur_winrt.fsh"));
|
2015-07-24 12:47:38 +08:00
|
|
|
#endif
|
2016-04-11 18:35:44 +08:00
|
|
|
auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.data());
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2014-05-14 09:12:58 +08:00
|
|
|
auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program);
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(glProgramState);
|
2014-06-11 14:54:14 +08:00
|
|
|
|
|
|
|
auto size = getTexture()->getContentSizeInPixels();
|
|
|
|
getGLProgramState()->setUniformVec2("resolution", size);
|
2015-07-24 12:47:38 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-06-11 14:54:14 +08:00
|
|
|
getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
|
2014-06-12 09:49:48 +08:00
|
|
|
getGLProgramState()->setUniformFloat("sampleNum", 7.0f);
|
2015-07-24 12:47:38 +08:00
|
|
|
#endif
|
2012-04-17 18:31:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-11 14:54:14 +08:00
|
|
|
void SpriteBlur::setBlurRadius(float radius)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-06-11 14:54:14 +08:00
|
|
|
_blurRadius = radius;
|
|
|
|
getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
|
2012-04-17 18:31:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void SpriteBlur::setBlurSampleNum(float num)
|
|
|
|
{
|
|
|
|
_blurSampleNum = num;
|
|
|
|
getGLProgramState()->setUniformFloat("sampleNum", _blurSampleNum);
|
|
|
|
}
|
|
|
|
|
2012-04-17 18:31:31 +08:00
|
|
|
// ShaderBlur
|
|
|
|
|
|
|
|
ShaderBlur::ShaderBlur()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderBlur::title() const
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
return "Shader: Frag shader";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderBlur::subtitle() const
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
|
|
|
return "Gaussian blur";
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void ShaderBlur::createSliderCtls()
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto screenSize = Director::getInstance()->getWinSize();
|
2014-02-28 17:34:24 +08:00
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
{
|
|
|
|
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
|
|
|
slider->setAnchorPoint(Vec2(0.5f, 1.0f));
|
|
|
|
slider->setMinimumValue(0.0f);
|
|
|
|
slider->setMaximumValue(25.0f);
|
|
|
|
slider->setScale(0.6f);
|
2015-07-24 13:52:40 +08:00
|
|
|
slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f + 24.0f));
|
2014-06-12 09:49:48 +08:00
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onRadiusChanged), Control::EventType::VALUE_CHANGED);
|
|
|
|
slider->setValue(2.0f);
|
|
|
|
addChild(slider);
|
|
|
|
_sliderRadiusCtl = slider;
|
|
|
|
|
|
|
|
auto label = Label::createWithTTF("Blur Radius", "fonts/arial.ttf", 12.0f);
|
|
|
|
addChild(label);
|
2015-07-24 13:52:40 +08:00
|
|
|
label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f));
|
2014-06-12 09:49:48 +08:00
|
|
|
}
|
2014-02-28 17:34:24 +08:00
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
{
|
|
|
|
ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
|
|
|
|
slider->setAnchorPoint(Vec2(0.5f, 1.0f));
|
|
|
|
slider->setMinimumValue(0.0f);
|
|
|
|
slider->setMaximumValue(11.0f);
|
|
|
|
slider->setScale(0.6f);
|
2015-07-24 13:52:40 +08:00
|
|
|
slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 10.0f));
|
2014-06-12 09:49:48 +08:00
|
|
|
slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onSampleNumChanged), Control::EventType::VALUE_CHANGED);
|
|
|
|
slider->setValue(7.0f);
|
|
|
|
addChild(slider);
|
|
|
|
_sliderNumCtrl = slider;
|
|
|
|
|
|
|
|
auto label = Label::createWithTTF("Blur Sample Num", "fonts/arial.ttf", 12.0f);
|
|
|
|
addChild(label);
|
2015-07-24 13:52:40 +08:00
|
|
|
label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 34.0f));
|
2014-06-12 09:49:48 +08:00
|
|
|
}
|
2012-04-17 18:31:31 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderBlur::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blurSprite = SpriteBlur::create("Images/grossini.png");
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sprite = Sprite::create("Images/grossini.png");
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2015-07-24 13:52:40 +08:00
|
|
|
_blurSprite->setPosition(Vec2(s.width/3, s.height/2 + 30.0f));
|
|
|
|
sprite->setPosition(Vec2(2*s.width/3, s.height/2 + 30.0f));
|
2012-04-17 18:31:31 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_blurSprite);
|
2012-04-17 18:31:31 +08:00
|
|
|
addChild(sprite);
|
2015-07-24 13:52:40 +08:00
|
|
|
|
|
|
|
auto label = Label::createWithTTF("Normal Sprite", "fonts/arial.ttf", 12.0f);
|
|
|
|
addChild(label);
|
|
|
|
label->setPosition(Vec2(2*s.width/3, s.height/3.0f));
|
2015-07-24 12:47:38 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-06-12 09:49:48 +08:00
|
|
|
createSliderCtls();
|
2015-07-24 13:52:40 +08:00
|
|
|
#else
|
|
|
|
auto label_blur = Label::createWithTTF("Blur Sprite", "fonts/arial.ttf", 12.0f);
|
|
|
|
addChild(label_blur);
|
|
|
|
label_blur->setPosition(Vec2(s.width/3, s.height/3.0f));
|
2014-11-22 02:29:36 +08:00
|
|
|
#endif
|
2012-04-17 18:31:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void ShaderBlur::onRadiusChanged(Ref* sender, Control::EventType)
|
2012-04-17 18:31:31 +08:00
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
ControlSlider* slider = (ControlSlider*)sender;
|
2014-06-11 14:54:14 +08:00
|
|
|
_blurSprite->setBlurRadius(slider->getValue());
|
2012-04-18 13:58:24 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 09:49:48 +08:00
|
|
|
void ShaderBlur::onSampleNumChanged(Ref* sender, Control::EventType)
|
|
|
|
{
|
|
|
|
ControlSlider* slider = (ControlSlider*)sender;
|
|
|
|
_blurSprite->setBlurSampleNum(slider->getValue());
|
|
|
|
}
|
|
|
|
|
2012-04-18 13:58:24 +08:00
|
|
|
// ShaderRetroEffect
|
|
|
|
|
|
|
|
ShaderRetroEffect::ShaderRetroEffect()
|
2014-04-09 22:53:59 +08:00
|
|
|
: _label(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _accum(0.0f)
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderRetroEffect::init()
|
|
|
|
{
|
|
|
|
if( ShaderTestDemo::init() ) {
|
2015-07-27 10:39:31 +08:00
|
|
|
|
|
|
|
auto fragStr = FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"));
|
|
|
|
GLchar * fragSource = (GLchar*)fragStr.c_str();
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2015-08-10 16:32:24 +08:00
|
|
|
auto p = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto s = director->getWinSize();
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2014-03-05 14:41:13 +08:00
|
|
|
_label = Label::createWithBMFont("fonts/west_england-64.fnt","RETRO EFFECT");
|
2014-05-15 01:07:09 +08:00
|
|
|
_label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2014-05-09 07:04:59 +08:00
|
|
|
_label->setGLProgram(p);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_label->setPosition(Vec2(s.width/2,s.height/2));
|
2012-04-18 13:58:24 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_label);
|
2012-04-18 13:58:24 +08:00
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void ShaderRetroEffect::update(float dt)
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_accum += dt;
|
2014-03-07 14:58:44 +08:00
|
|
|
int letterCount = _label->getStringLength();
|
2014-03-05 14:41:13 +08:00
|
|
|
for (int i = 0; i < letterCount; ++i)
|
|
|
|
{
|
|
|
|
auto sprite = _label->getLetter(i);
|
2017-09-08 14:10:40 +08:00
|
|
|
if (sprite != nullptr)
|
|
|
|
{
|
|
|
|
auto oldPosition = sprite->getPosition();
|
|
|
|
sprite->setPosition(Vec2( oldPosition.x, sinf( _accum * 2 + i/2.0) * 20 ));
|
|
|
|
|
|
|
|
// add fabs() to prevent negative scaling
|
|
|
|
float scaleY = ( sinf( _accum * 2 + i/2.0 + 0.707) );
|
|
|
|
|
|
|
|
sprite->setScaleY(scaleY);
|
|
|
|
}
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-04-18 13:58:24 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderRetroEffect::title() const
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
return "Shader: Retro test";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string ShaderRetroEffect::subtitle() const
|
2012-04-18 13:58:24 +08:00
|
|
|
{
|
|
|
|
return "sin() effect with moving colors";
|
2012-04-17 18:31:31 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 02:48:11 +08:00
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
ShaderLensFlare::ShaderLensFlare()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
std::string ShaderLensFlare::title() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
return "ShaderToy Test";
|
|
|
|
}
|
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
std::string ShaderLensFlare::subtitle() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2015-08-25 10:28:18 +08:00
|
|
|
return "Lens Flare";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-10 11:34:15 +08:00
|
|
|
bool ShaderLensFlare::init()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_LensFlare.fsh");
|
2014-05-10 02:48:11 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2014-05-10 11:34:15 +08:00
|
|
|
sn->setContentSize(Size(s.width/2,s.height/2));
|
2014-05-10 02:48:11 +08:00
|
|
|
addChild(sn);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
//
|
|
|
|
// ShaderGlow
|
|
|
|
//
|
|
|
|
ShaderGlow::ShaderGlow()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderGlow::title() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
return "ShaderToy Test";
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderGlow::subtitle() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
return "Glow";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
bool ShaderGlow::init()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_Glow.fsh");
|
2014-05-10 02:48:11 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
sn->setPosition(Vec2(s.width/2, s.height/2));
|
2014-05-10 11:34:15 +08:00
|
|
|
sn->setContentSize(Size(s.width/2,s.height/2));
|
2014-05-10 02:48:11 +08:00
|
|
|
addChild(sn);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
//
|
|
|
|
// ShaderMultiTexture
|
|
|
|
//
|
2014-06-09 11:39:19 +08:00
|
|
|
ShaderMultiTexture::ShaderMultiTexture():_changedTextureId(0)
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderMultiTexture::title() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
return "MultiTexture test";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
std::string ShaderMultiTexture::subtitle() const
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
2014-05-14 03:55:34 +08:00
|
|
|
return "MultiTexture";
|
2014-05-10 02:48:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 07:45:46 +08:00
|
|
|
ui::Slider* ShaderMultiTexture::createSliderCtl()
|
|
|
|
{
|
|
|
|
auto screenSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
ui::Slider* slider = ui::Slider::create();
|
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2014-05-16 07:52:15 +08:00
|
|
|
slider->setPercent(50);
|
2014-05-16 07:45:46 +08:00
|
|
|
|
|
|
|
slider->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 3.0f));
|
|
|
|
addChild(slider);
|
|
|
|
|
|
|
|
slider->addEventListener([&](Ref* sender, ui::Slider::EventType type) {
|
|
|
|
|
|
|
|
if (type == ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
|
|
|
|
{
|
|
|
|
ui::Slider* slider = dynamic_cast<ui::Slider*>(sender);
|
|
|
|
float p = slider->getPercent() / 100.0f;
|
|
|
|
_sprite->getGLProgramState()->setUniformFloat("u_interpolate",p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return slider;
|
|
|
|
}
|
|
|
|
|
2014-05-14 03:55:34 +08:00
|
|
|
bool ShaderMultiTexture::init()
|
2014-05-10 02:48:11 +08:00
|
|
|
{
|
|
|
|
if (ShaderTestDemo::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-05-14 03:55:34 +08:00
|
|
|
|
2014-05-16 07:45:46 +08:00
|
|
|
// Left: normal sprite
|
|
|
|
auto left = Sprite::create("Images/grossinis_sister1.png");
|
|
|
|
addChild(left);
|
|
|
|
left->setPosition(s.width*1/4, s.height/2);
|
|
|
|
|
|
|
|
// Right: normal sprite
|
|
|
|
auto right = Sprite::create("Images/grossinis_sister2.png");
|
2014-06-09 10:58:28 +08:00
|
|
|
addChild(right, 0, rightSpriteTag);
|
2014-05-16 07:45:46 +08:00
|
|
|
right->setPosition(s.width*3/4, s.height/2);
|
|
|
|
|
|
|
|
|
|
|
|
// Center: MultiTexture
|
2014-06-09 10:58:28 +08:00
|
|
|
_sprite = Sprite::createWithTexture(left->getTexture());
|
2014-05-16 07:45:46 +08:00
|
|
|
addChild(_sprite);
|
|
|
|
_sprite->setPosition(Vec2(s.width/2, s.height/2));
|
2014-05-14 03:55:34 +08:00
|
|
|
|
|
|
|
auto glprogram = GLProgram::createWithFilenames("Shaders/example_MultiTexture.vsh", "Shaders/example_MultiTexture.fsh");
|
2014-05-14 09:12:58 +08:00
|
|
|
auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
|
2014-05-16 07:45:46 +08:00
|
|
|
_sprite->setGLProgramState(glprogramstate);
|
2014-05-14 03:55:34 +08:00
|
|
|
|
2014-06-09 10:58:28 +08:00
|
|
|
glprogramstate->setUniformTexture("u_texture1", right->getTexture());
|
2014-05-16 07:52:15 +08:00
|
|
|
glprogramstate->setUniformFloat("u_interpolate",0.5);
|
2014-05-16 07:45:46 +08:00
|
|
|
|
|
|
|
// slider
|
|
|
|
createSliderCtl();
|
2014-06-09 10:58:28 +08:00
|
|
|
|
|
|
|
// menu
|
|
|
|
auto label = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "change");
|
|
|
|
auto mi = MenuItemLabel::create(label, CC_CALLBACK_1(ShaderMultiTexture::changeTexture, this));
|
|
|
|
auto menu = Menu::create(mi, nullptr);
|
|
|
|
addChild(menu);
|
|
|
|
menu->setPosition(s.width * 7 / 8, s.height / 2);
|
2014-05-16 07:45:46 +08:00
|
|
|
|
2014-05-10 02:48:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-14 03:55:34 +08:00
|
|
|
|
2014-05-10 02:48:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-09 10:58:28 +08:00
|
|
|
void ShaderMultiTexture::changeTexture(Ref*)
|
|
|
|
{
|
|
|
|
static const int textureFilesCount = 3;
|
|
|
|
static const std::string textureFiles[textureFilesCount] = {
|
|
|
|
"Images/grossini.png",
|
|
|
|
"Images/grossinis_sister1.png",
|
|
|
|
"Images/grossinis_sister2.png"
|
|
|
|
};
|
2016-06-29 10:04:11 +08:00
|
|
|
auto texture = Director::getInstance()->getTextureCache()->addImage(textureFiles[_changedTextureId++ % textureFilesCount]);
|
2014-06-09 10:58:28 +08:00
|
|
|
Sprite* right = dynamic_cast<Sprite*>(getChildByTag(rightSpriteTag));
|
2016-06-29 10:04:11 +08:00
|
|
|
right->setTexture(texture);
|
2014-06-09 10:58:28 +08:00
|
|
|
auto programState = _sprite->getGLProgramState();
|
|
|
|
programState->setUniformTexture("u_texture1", right->getTexture());
|
|
|
|
}
|