PuzzleSDK
ShaderTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "ShaderTest.h"
26#include "../testResource.h"
27#include "cocos2d.h"
28#include "renderer/ccShaders.h"
29#include "renderer/backend/Device.h"
30
33
34#define SET_UNIFORM(ps, name, value) do { \
35decltype(value) __v = value; \
36auto __loc = (ps)->getUniformLocation(name); \
37(ps)->setUniform(__loc, &__v, sizeof(__v)); \
38} while(false)
39
40#define SET_TEXTURE(ps, name, idx, value) do { \
41auto * __v = value; \
42auto __loc = (ps)->getUniformLocation(name); \
43(ps)->setTexture(__loc, idx, __v); \
44} while(false)
45
46
47
48ShaderTests::ShaderTests()
49{
61}
62
64//
65// ShaderNode
66//
68enum
69{
70 SIZE_X = 256,
71 SIZE_Y = 256,
72};
73
75:_center(Vec2(0.0f, 0.0f))
76,_resolution(Vec2(0.0f, 0.0f))
77,_time(0.0f)
78{
79}
80
82{
83}
84
85ShaderNode* ShaderNode::shaderNodeWithVertex(const std::string &vert, const std::string& frag)
86{
87 auto node = new (std::nothrow) ShaderNode();
88 node->initWithVertex(vert, frag);
89 node->autorelease();
90
91 return node;
92}
93
94bool ShaderNode::initWithVertex(const std::string &vert, const std::string &frag)
95{
96 _vertFileName = vert;
97 _fragFileName = frag;
98
99 loadShaderVertex(vert, frag);
100
101 _time = 0;
102 _resolution = Vec2(SIZE_X, SIZE_Y);
103
104 scheduleUpdate();
105
106 setContentSize(Size(SIZE_X, SIZE_Y));
107 setAnchorPoint(Vec2(0.5f, 0.5f));
108
109 // init custom command
110 auto layout = _programState->getVertexLayout();
111 layout->setAttribute("a_position", 0, backend::VertexFormat::FLOAT2, 0, false);
112
113 float w = SIZE_X, h = SIZE_Y;
114 Vec2 vertices[6] = { Vec2(0.0f,0.0f), Vec2(w,0.0f), Vec2(w,h), Vec2(0.0f,0.0f), Vec2(0.0f,h), Vec2(w,h) };
115 layout->setLayout(sizeof(Vec2));
116
117 /*
118 * TODO: the Y-coordinate of subclasses are flipped in metal
119 *
120 * keywords: CC_USE_METAL , CC_USE_GL
121 */
122
123 _customCommand.createVertexBuffer(sizeof(Vec2), 6, CustomCommand::BufferUsage::STATIC);
124 _customCommand.updateVertexBuffer(vertices, sizeof(vertices));
125
126 _customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
127
128 return true;
129}
130
131void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &frag)
132{
133 auto fileUtiles = FileUtils::getInstance();
134
135 // frag
136 auto fragmentFilePath = fileUtiles->fullPathForFilename(frag);
137 auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
138
139 // vert
140 std::string vertSource;
141 if (vert.empty()) {
142 vertSource = position_vert;
143 } else {
144 std::string vertexFilePath = fileUtiles->fullPathForFilename(vert);
145 vertSource = fileUtiles->getStringFromFile(vertexFilePath);
146 }
147 auto program = backend::Device::getInstance()->newProgram(vertSource.c_str(), fragSource.c_str());
148 auto programState = new backend::ProgramState(program);
149 setProgramState(programState);
150 CC_SAFE_RELEASE(programState);
151 CC_SAFE_RELEASE(program);
152}
153
154void ShaderNode::update(float dt)
155{
156 _time += dt;
157}
158
159void ShaderNode::setPosition(const Vec2 &newPosition)
160{
161 Node::setPosition(newPosition);
162 auto position = getPosition();
163 auto frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
164 auto visibleSize = Director::getInstance()->getVisibleSize();
165 auto retinaFactor = Director::getInstance()->getOpenGLView()->getRetinaFactor();
166 _center = Vec2(position.x * frameSize.width / visibleSize.width * retinaFactor, position.y * frameSize.height / visibleSize.height * retinaFactor);
167}
168
169void ShaderNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
170{
171 _customCommand.init(_globalZOrder, transform, flags);
172
173 _programState->setUniform(_locResolution, &_resolution, sizeof(_resolution));
174 _programState->setUniform(_locCenter, &_center, sizeof(_center));
175
176
177 auto projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
178 auto finalMatrix = projectionMatrix * transform;
179
180 _programState->setUniform(_locMVP, finalMatrix.m, sizeof(finalMatrix.m));
181
182 float time = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();
183 Vec4 uTime(time / 10.0f, time, time * 2.0f, time * 4.0f);
184 Vec4 sinTime(time / 8.0f, time / 4.0f, time / 2.0f, sinf(time));
185 Vec4 cosTime(time / 8.0f, time / 4.0f, time / 2.0f, cosf(time));
186
187 _programState->setUniform(_locTime, &uTime, sizeof(uTime));
188 _programState->setUniform(_locSinTime, &sinTime, sizeof(sinTime));
189 _programState->setUniform(_locCosTime, &cosTime, sizeof(cosTime));
190
191 renderer->addCommand(&_customCommand);
192 CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 6);
193}
194
196{
197 if(_programState == nullptr)
198 return;
199
200 _locResolution = _programState->getUniformLocation("resolution");
201 _locCenter = _programState->getUniformLocation("center");
202 _locMVP = _programState->getUniformLocation("u_MVPMatrix");
203 _locTime = _programState->getUniformLocation("u_Time");
204 _locSinTime = _programState->getUniformLocation("u_SinTime");
205 _locCosTime = _programState->getUniformLocation("u_CosTime");
206 _locScreenSize = _programState->getUniformLocation("u_screenSize");
207
208 const Vec2& frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
209 float retinaFactor = Director::getInstance()->getOpenGLView()->getRetinaFactor();
210 auto screenSizeInPixels = frameSize * retinaFactor;
211 _programState->setUniform(_locScreenSize, &screenSizeInPixels, sizeof(screenSizeInPixels));
212}
213
215
217{
218}
219
221{
222 if (ShaderTestDemo::init())
223 {
224 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Monjori.fsh");
225
226 auto s = Director::getInstance()->getWinSize();
227 sn->setPosition(Vec2(s.width/2, s.height/2));
228
229 addChild(sn);
230
231 return true;
232 }
233
234 return false;
235}
236
237std::string ShaderMonjori::title() const
238{
239 return "Shader: Frag shader";
240}
241
242std::string ShaderMonjori::subtitle() const
243{
244 return "Monjori plane deformations";
245}
246
247
250{
251}
252
254{
255 if (ShaderTestDemo::init())
256 {
257 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Mandelbrot.fsh");
258
259 auto s = Director::getInstance()->getWinSize();
260 sn->setPosition(Vec2(s.width/2, s.height/2));
261
262 addChild(sn);
263 return true;
264 }
265
266 return false;
267}
268
269std::string ShaderMandelbrot::title() const
270{
271 return "Shader: Frag shader";
272}
273
274std::string ShaderMandelbrot::subtitle() const
275{
276 return "Mandelbrot shader with Zoom";
277}
278
281{
282}
283
285{
286 if (ShaderTestDemo::init())
287 {
288 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Julia.fsh");
289
290 auto s = Director::getInstance()->getWinSize();
291 sn->setPosition(Vec2(s.width/2, s.height/2));
292
293 addChild(sn);
294 return true;
295 }
296
297 return false;
298}
299
300std::string ShaderJulia::title() const
301{
302 return "Shader: Frag shader";
303}
304
305std::string ShaderJulia::subtitle() const
306{
307 return "Julia shader";
308}
309
310
313{
314}
315
317{
318 if (ShaderTestDemo::init())
319 {
320 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Heart.fsh");
321
322 auto s = Director::getInstance()->getWinSize();
323 sn->setPosition(Vec2(s.width/2, s.height/2));
324
325 addChild(sn);
326
327 return true;
328 }
329
330 return false;
331}
332
333std::string ShaderHeart::title() const
334{
335 return "Shader: Frag shader";
336}
337
338std::string ShaderHeart::subtitle() const
339{
340 return "Heart";
341}
342
345{
346}
347
349{
350 if (ShaderTestDemo::init())
351 {
352 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Flower.fsh");
353
354 auto s = Director::getInstance()->getWinSize();
355 sn->setPosition(Vec2(s.width/2, s.height/2));
356
357 addChild(sn);
358
359 return true;
360 }
361
362 return false;
363}
364
365std::string ShaderFlower::title() const
366{
367 return "Shader: Frag shader";
368}
369
370std::string ShaderFlower::subtitle() const
371{
372 return "Flower";
373}
374
377{
378}
379
381{
382 if (ShaderTestDemo::init())
383 {
384 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Plasma.fsh");
385
386 auto s = Director::getInstance()->getWinSize();
387 sn->setPosition(Vec2(s.width/2, s.height/2));
388
389 addChild(sn);
390
391 return true;
392 }
393
394 return false;
395}
396
397std::string ShaderPlasma::title() const
398{
399 return "Shader: Frag shader";
400}
401
402std::string ShaderPlasma::subtitle() const
403{
404 return "Plasma";
405}
406
407// ShaderBlur
408
409class SpriteBlur : public Sprite
410{
411public:
412 ~SpriteBlur();
413 bool initWithTexture(Texture2D* texture, const Rect& rect);
414 void initProgram();
415
416 static SpriteBlur* create(const char *pszFileName);
417 void setBlurRadius(float radius);
418 void setBlurSampleNum(float num);
419
420protected:
423};
424
426{
427}
428
429SpriteBlur* SpriteBlur::create(const char *pszFileName)
430{
431 SpriteBlur* pRet = new (std::nothrow) SpriteBlur();
432 if (pRet)
433 {
434 bool result = pRet->initWithFile("");
435 log("Test call Sprite::initWithFile with bad file name result is : %s", result ? "true" : "false");
436 }
437
438 if (pRet && pRet->initWithFile(pszFileName))
439 {
440 pRet->autorelease();
441 }
442 else
443 {
444 CC_SAFE_DELETE(pRet);
445 }
446
447 return pRet;
448}
449
450bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
451{
452 _blurRadius = 0;
453 if( Sprite::initWithTexture(texture, rect) )
454 {
455#if CC_ENABLE_CACHE_TEXTURE_DATA
456 auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
457 initProgram();
458 });
459
460 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
461#endif
462
463 initProgram();
464
465 return true;
466 }
467
468 return false;
469}
470
472{
473 std::string fragSource = FileUtils::getInstance()->getStringFromFile(
474 FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh"));
475
476 auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.data());
477 auto programState = new backend::ProgramState(program);
478 setProgramState(programState);
479 CC_SAFE_RELEASE(programState);
480 CC_SAFE_RELEASE(program);
481
482 auto size = getTexture()->getContentSizeInPixels();
483
484 SET_UNIFORM(_programState, "resolution", size);
485 SET_UNIFORM(_programState, "blurRadius", _blurRadius);
486 SET_UNIFORM(_programState, "sampleNum", 7.0f);
487 SET_UNIFORM(_programState, "u_PMatrix", Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION));
488}
489
491{
492 _blurRadius = radius;
493 SET_UNIFORM(_programState, "blurRadius", _blurRadius);
494}
495
497{
498 _blurSampleNum = num;
499 SET_UNIFORM(_programState, "sampleNum", _blurSampleNum);
500}
501
502// ShaderBlur
503
505{
506}
507
508std::string ShaderBlur::title() const
509{
510 return "Shader: Frag shader";
511}
512
513std::string ShaderBlur::subtitle() const
514{
515 return "Gaussian blur";
516}
517
519{
520 auto screenSize = Director::getInstance()->getWinSize();
521
522 {
523 ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
524 slider->setAnchorPoint(Vec2(0.5f, 1.0f));
525 slider->setMinimumValue(0.0f);
526 slider->setMaximumValue(25.0f);
527 slider->setScale(0.6f);
528 slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f + 24.0f));
529 slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onRadiusChanged), Control::EventType::VALUE_CHANGED);
530 slider->setValue(2.0f);
531 addChild(slider);
532 _sliderRadiusCtl = slider;
533
534 auto label = Label::createWithTTF("Blur Radius", "fonts/arial.ttf", 12.0f);
535 addChild(label);
536 label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f));
537 }
538
539 {
540 ControlSlider *slider = ControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
541 slider->setAnchorPoint(Vec2(0.5f, 1.0f));
542 slider->setMinimumValue(0.0f);
543 slider->setMaximumValue(11.0f);
544 slider->setScale(0.6f);
545 slider->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 10.0f));
546 slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ShaderBlur::onSampleNumChanged), Control::EventType::VALUE_CHANGED);
547 slider->setValue(7.0f);
548 addChild(slider);
549 _sliderNumCtrl = slider;
550
551 auto label = Label::createWithTTF("Blur Sample Num", "fonts/arial.ttf", 12.0f);
552 addChild(label);
553 label->setPosition(Vec2(screenSize.width / 4.0f, screenSize.height / 3.0f - 34.0f));
554 }
555
556}
557
559{
560 if( ShaderTestDemo::init() )
561 {
562 _blurSprite = SpriteBlur::create("Images/grossini.png");
563 auto sprite = Sprite::create("Images/grossini.png");
564 auto s = Director::getInstance()->getWinSize();
565 _blurSprite->setPosition(Vec2(s.width/3, s.height/2 + 30.0f));
566 sprite->setPosition(Vec2(2*s.width/3, s.height/2 + 30.0f));
567
568 addChild(_blurSprite);
569 addChild(sprite);
570
571 auto label = Label::createWithTTF("Normal Sprite", "fonts/arial.ttf", 12.0f);
572 addChild(label);
573 label->setPosition(Vec2(2*s.width/3, s.height/3.0f));
575
576 return true;
577 }
578
579 return false;
580}
581
582void ShaderBlur::onRadiusChanged(Ref* sender, Control::EventType)
583{
584 ControlSlider* slider = (ControlSlider*)sender;
585 _blurSprite->setBlurRadius(slider->getValue());
586}
587
588void ShaderBlur::onSampleNumChanged(Ref* sender, Control::EventType)
589{
590 ControlSlider* slider = (ControlSlider*)sender;
591 _blurSprite->setBlurSampleNum(slider->getValue());
592}
593
594// ShaderRetroEffect
595
597: _label(nullptr)
598, _accum(0.0f)
599{
600}
601
603{
604 if( ShaderTestDemo::init() ) {
605
606 auto fragStr = FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"));
607 char * fragSource = (char*)fragStr.c_str();
608
609 auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource);
610 auto p = new backend::ProgramState(program);
611 auto director = Director::getInstance();
612 const auto& screenSizeLocation = p->getUniformLocation("u_screenSize");
613 const auto& frameSize = director->getOpenGLView()->getFrameSize();
614 float retinaFactor = director->getOpenGLView()->getRetinaFactor();
615 auto screenSizeInPixels = frameSize * retinaFactor;
616 p->setUniform(screenSizeLocation, &screenSizeInPixels, sizeof(screenSizeInPixels));
617
618 auto s = director->getWinSize();
619
620 _label = Label::createWithBMFont("fonts/west_england-64.fnt","RETRO EFFECT");
621 _label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
622 _label->setProgramState(p);
623 CC_SAFE_RELEASE(p);
624
625 _label->setPosition(Vec2(s.width/2,s.height/2));
626
627 addChild(_label);
628
629 scheduleUpdate();
630 CC_SAFE_RELEASE(program);
631 return true;
632 }
633
634 return false;
635}
636
638{
639 _accum += dt;
640 int letterCount = _label->getStringLength();
641 for (int i = 0; i < letterCount; ++i)
642 {
643 auto sprite = _label->getLetter(i);
644 if (sprite != nullptr)
645 {
646 auto oldPosition = sprite->getPosition();
647 sprite->setPosition(Vec2( oldPosition.x, sinf( _accum * 2 + i/2.0) * 20 ));
648
649 // add fabs() to prevent negative scaling
650 float scaleY = ( sinf( _accum * 2 + i/2.0 + 0.707) );
651
652 sprite->setScaleY(scaleY);
653 }
654 }
655}
656
657std::string ShaderRetroEffect::title() const
658{
659 return "Shader: Retro test";
660}
661
663{
664 return "sin() effect with moving colors";
665}
666
667
669{
670}
671
672std::string ShaderLensFlare::title() const
673{
674 return "ShaderToy Test";
675}
676
677std::string ShaderLensFlare::subtitle() const
678{
679 return "Lens Flare";
680}
681
683{
684 if (ShaderTestDemo::init())
685 {
686 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_LensFlare.fsh");
687
688 auto s = Director::getInstance()->getWinSize();
689 sn->setPosition(Vec2(s.width/2, s.height/2));
690 sn->setContentSize(Size(s.width/2,s.height/2));
691 addChild(sn);
692
693 return true;
694 }
695
696 return false;
697}
698
699//
700// ShaderGlow
701//
703{
704}
705
706std::string ShaderGlow::title() const
707{
708 return "ShaderToy Test";
709}
710
711std::string ShaderGlow::subtitle() const
712{
713 return "Glow";
714}
715
717{
718 if (ShaderTestDemo::init())
719 {
720 auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_Glow.fsh");
721
722 auto s = Director::getInstance()->getWinSize();
723 sn->setPosition(Vec2(s.width/2, s.height/2));
724 sn->setContentSize(Size(s.width/2,s.height/2));
725 addChild(sn);
726
727 return true;
728 }
729
730 return false;
731}
732
733//
734// ShaderMultiTexture
735//
737{
738}
739
740std::string ShaderMultiTexture::title() const
741{
742 return "MultiTexture test";
743}
744
746{
747 return "MultiTexture";
748}
749
751{
752 auto screenSize = Director::getInstance()->getWinSize();
753
754 ui::Slider* slider = ui::Slider::create();
755 slider->loadBarTexture("cocosui/sliderTrack.png");
756 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
757 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
758 slider->setPercent(50);
759
760 slider->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 3.0f));
761 addChild(slider);
762
763 slider->addEventListener([&](Ref* sender, ui::Slider::EventType type) {
764
765 if (type == ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
766 {
767 ui::Slider* slider = dynamic_cast<ui::Slider*>(sender);
768 float p = slider->getPercent() / 100.0f;
769 auto state = _sprite->getProgramState();
770 SET_UNIFORM(state, "u_interpolate", p);
771 }
772 });
773 return slider;
774}
775
777{
778 if (ShaderTestDemo::init())
779 {
780 auto s = Director::getInstance()->getWinSize();
781
782 // Left: normal sprite
783 auto left = Sprite::create("Images/grossinis_sister1.png");
784 addChild(left);
785 left->setPosition(s.width*1/4, s.height/2);
786
787 // Right: normal sprite
788 auto right = Sprite::create("Images/grossinis_sister2.png");
789 addChild(right, 0, rightSpriteTag);
790 right->setPosition(s.width*3/4, s.height/2);
791
792
793 // Center: MultiTexture
794 _sprite = Sprite::createWithTexture(left->getTexture());
795 addChild(_sprite);
796 _sprite->setPosition(Vec2(s.width/2, s.height/2));
797
798 auto * fu = FileUtils::getInstance();
799 auto vertexShader = fu->getStringFromFile("Shaders/example_MultiTexture.vsh");
800 auto fragmentShader = fu->getStringFromFile("Shaders/example_MultiTexture.fsh");
801 auto program = backend::Device::getInstance()->newProgram(vertexShader.c_str(), fragmentShader.c_str());
802 auto programState = new backend::ProgramState(program);
803 _sprite->setProgramState(programState);
804
805 SET_TEXTURE(programState, "u_texture1", 1, right->getTexture()->getBackendTexture());
806 SET_UNIFORM(programState, "u_interpolate",0.5f);
807
808 // slider
810
811 // menu
812 auto label = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "change");
813 auto mi = MenuItemLabel::create(label, CC_CALLBACK_1(ShaderMultiTexture::changeTexture, this));
814 auto menu = Menu::create(mi, nullptr);
815 addChild(menu);
816 menu->setPosition(s.width * 7 / 8, s.height / 2);
817
818 CC_SAFE_RELEASE(programState);
819 CC_SAFE_RELEASE(program);
820 return true;
821 }
822
823 return false;
824}
825
827{
828 static const int textureFilesCount = 3;
829 static const std::string textureFiles[textureFilesCount] = {
830 "Images/grossini.png",
831 "Images/grossinis_sister1.png",
832 "Images/grossinis_sister2.png"
833 };
834 auto texture = Director::getInstance()->getTextureCache()->addImage(textureFiles[_changedTextureId++ % textureFilesCount]);
835 Sprite* right = dynamic_cast<Sprite*>(getChildByTag(rightSpriteTag));
836 right->setTexture(texture);
837 auto programState = _sprite->getProgramState();
838 SET_TEXTURE(programState, "u_texture1", 1, right->getTexture()->getBackendTexture());
839}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
@ SIZE_Y
Definition: ShaderTest.cpp:71
@ SIZE_X
Definition: ShaderTest.cpp:70
USING_NS_CC_EXT
Definition: ShaderTest.cpp:32
USING_NS_CC
Definition: ShaderTest.cpp:31
#define SET_TEXTURE(ps, name, idx, value)
Definition: ShaderTest.cpp:40
#define SET_UNIFORM(ps, name, value)
Definition: ShaderTest.cpp:34
virtual std::string title() const override
Definition: ShaderTest.cpp:508
cocos2d::extension::ControlSlider * _sliderNumCtrl
Definition: ShaderTest.h:121
void onRadiusChanged(cocos2d::Ref *sender, cocos2d::extension::Control::EventType controlEvent)
Definition: ShaderTest.cpp:582
void onSampleNumChanged(cocos2d::Ref *sender, cocos2d::extension::Control::EventType controlEvent)
Definition: ShaderTest.cpp:588
virtual bool init() override
Definition: ShaderTest.cpp:558
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:513
void createSliderCtls()
Definition: ShaderTest.cpp:518
cocos2d::extension::ControlSlider * _sliderRadiusCtl
Definition: ShaderTest.h:120
SpriteBlur * _blurSprite
Definition: ShaderTest.h:119
ShaderFlower()
ShaderFlower
Definition: ShaderTest.cpp:344
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:370
virtual bool init() override
Definition: ShaderTest.cpp:348
virtual std::string title() const override
Definition: ShaderTest.cpp:365
virtual bool init() override
Definition: ShaderTest.cpp:716
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:711
virtual std::string title() const override
Definition: ShaderTest.cpp:706
virtual std::string title() const override
Definition: ShaderTest.cpp:333
ShaderHeart()
ShaderHeart
Definition: ShaderTest.cpp:312
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:338
virtual bool init() override
Definition: ShaderTest.cpp:316
virtual bool init() override
Definition: ShaderTest.cpp:284
ShaderJulia()
ShaderJulia
Definition: ShaderTest.cpp:280
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:305
virtual std::string title() const override
Definition: ShaderTest.cpp:300
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:677
virtual std::string title() const override
Definition: ShaderTest.cpp:672
virtual bool init() override
Definition: ShaderTest.cpp:682
virtual std::string title() const override
Definition: ShaderTest.cpp:269
ShaderMandelbrot()
ShaderMandelbrot
Definition: ShaderTest.cpp:249
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:274
virtual bool init() override
Definition: ShaderTest.cpp:253
ShaderMonjori()
ShaderMonjori
Definition: ShaderTest.cpp:216
virtual std::string title() const override
Definition: ShaderTest.cpp:237
virtual bool init() override
Definition: ShaderTest.cpp:220
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:242
static const int rightSpriteTag
Definition: ShaderTest.h:209
void changeTexture(cocos2d::Ref *)
Definition: ShaderTest.cpp:826
cocos2d::ui::Slider * createSliderCtl()
Definition: ShaderTest.cpp:750
virtual bool init() override
Definition: ShaderTest.cpp:776
virtual std::string title() const override
Definition: ShaderTest.cpp:740
cocos2d::Sprite * _sprite
Definition: ShaderTest.h:216
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:745
cocos2d::Vec2 _resolution
Definition: ShaderTest.h:170
std::string _fragFileName
Definition: ShaderTest.h:173
void loadShaderVertex(const std::string &vert, const std::string &frag)
Definition: ShaderTest.cpp:131
void updateUniforms()
Definition: ShaderTest.cpp:195
virtual void update(float dt) override
Definition: ShaderTest.cpp:154
cocos2d::backend::UniformLocation _locSinTime
Definition: ShaderTest.h:180
float _time
Definition: ShaderTest.h:171
cocos2d::Vec2 _center
Definition: ShaderTest.h:169
cocos2d::backend::UniformLocation _locScreenSize
Definition: ShaderTest.h:182
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override
Definition: ShaderTest.cpp:169
cocos2d::backend::UniformLocation _locCosTime
Definition: ShaderTest.h:181
virtual void setPosition(const cocos2d::Vec2 &newPosition) override
Definition: ShaderTest.cpp:159
cocos2d::backend::UniformLocation _locTime
Definition: ShaderTest.h:179
bool initWithVertex(const std::string &vert, const std::string &frag)
Definition: ShaderTest.cpp:94
cocos2d::backend::UniformLocation _locMVP
Definition: ShaderTest.h:178
virtual void setProgramState(cocos2d::backend::ProgramState *programState) override
Definition: ShaderTest.h:155
cocos2d::backend::UniformLocation _locResolution
Definition: ShaderTest.h:176
std::string _vertFileName
Definition: ShaderTest.h:172
cocos2d::CustomCommand _customCommand
Definition: ShaderTest.h:174
cocos2d::backend::UniformLocation _locCenter
Definition: ShaderTest.h:177
static ShaderNode * shaderNodeWithVertex(const std::string &vert, const std::string &frag)
Definition: ShaderTest.cpp:85
virtual std::string title() const override
Definition: ShaderTest.cpp:397
virtual bool init() override
Definition: ShaderTest.cpp:380
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:402
ShaderPlasma()
ShaderPlasma
Definition: ShaderTest.cpp:376
cocos2d::Label * _label
Definition: ShaderTest.h:134
virtual void update(float dt) override
Definition: ShaderTest.cpp:637
virtual std::string title() const override
Definition: ShaderTest.cpp:657
virtual bool init() override
Definition: ShaderTest.cpp:602
virtual std::string subtitle() const override
Definition: ShaderTest.cpp:662
void setBlurRadius(float radius)
Definition: ShaderTest.cpp:490
static SpriteBlur * create(const char *pszFileName)
Definition: ShaderTest.cpp:429
void initProgram()
Definition: ShaderTest.cpp:471
bool initWithTexture(Texture2D *texture, const Rect &rect)
Definition: ShaderTest.cpp:450
void setBlurSampleNum(float num)
Definition: ShaderTest.cpp:496
float _blurRadius
Definition: ShaderTest.cpp:421
float _blurSampleNum
Definition: ShaderTest.cpp:422