PuzzleSDK
ClippingNodeTest.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//
26// ClippingNodeTest
27//
28//
29// by Pierre-David Bélanger
30//
31
32#include "ClippingNodeTest.h"
33#include "../testResource.h"
34#include "renderer/CCRenderer.h"
35#include "renderer/backend/ProgramState.h"
36#include "renderer/ccShaders.h"
37
39
40enum {
46};
47
48ClippingNodeTests::ClippingNodeTests()
49{
66}
67
69
70//@implementation BaseClippingNodeTest
71
73{
74 if (TestCase::init()) {
75
76 auto background = Sprite::create(s_back3);
77 background->setAnchorPoint( Vec2::ZERO );
78 background->setPosition( Vec2::ZERO );
79 this->addChild(background, -1);
80
81 this->setup();
82 return true;
83 }
84 return false;
85}
86
88{
89 Director::getInstance()->getTextureCache()->removeUnusedTextures();
90}
91
92std::string BaseClippingNodeTest::title() const
93{
94 return "Clipping Demo";
95}
96
98{
99
100}
101
102
103// BasicTest
104
105std::string BasicTest::title() const
106{
107 return "Basic Test";
108}
109
110std::string BasicTest::subtitle() const
111{
112 return "";
113}
114
116{
117 auto s = Director::getInstance()->getWinSize();
118
119 auto stencil = this->stencil();
120 stencil->setTag( kTagStencilNode );
121 stencil->setPosition(50, 50);
122
123 auto clipper = this->clipper();
124 clipper->setTag( kTagClipperNode );
125 clipper->setAnchorPoint(Vec2(0.5f, 0.5f));
126 clipper->setPosition(s.width / 2 - 50, s.height / 2 - 50);
127 clipper->setStencil(stencil);
128 this->addChild(clipper);
129
130 auto content = this->content();
131 content->setPosition(50, 50);
132 clipper->addChild(content);
133}
134
136{
137 return RepeatForever::create(RotateBy::create(1.0f, 90.0f));
138}
139
141{
142 auto scale = ScaleBy::create(1.33f, 1.5f);
143 return RepeatForever::create(Sequence::create(scale, scale->reverse(), nullptr));
144}
145
147{
148 auto shape = DrawNode::create();
149 static Vec2 triangle[3];
150 triangle[0] = Vec2(-100, -100);
151 triangle[1] = Vec2(100, -100);
152 triangle[2] = Vec2(0, 100);
153
154 static Color4F green(0, 1, 0, 1);
155 shape->drawPolygon(triangle, 3, green, 0, green);
156 return shape;
157}
158
160{
161 auto grossini = Sprite::create(s_pathGrossini);
162 grossini->setScale( 1.5 );
163 return grossini;
164}
165
167{
168 return nullptr;
169}
170
171ClippingNode* BasicTest::clipper()
172{
173 return ClippingNode::create();
174}
175
177{
178 return nullptr;
179}
180
181
182// ShapeTest
183
184std::string ShapeTest::title() const
185{
186 return "Shape Basic Test";
187}
188
189std::string ShapeTest::subtitle() const
190{
191 return "A DrawNode as stencil and Sprite as content";
192}
193
195{
196 auto node = this->shape();
197 node->runAction(this->actionRotate());
198 return node;
199}
200
202{
203 auto node = this->grossini();
204 node->runAction(this->actionScale());
205 return node;
206}
207
208
209// ShapeInvertedTest
210
211std::string ShapeInvertedTest::title() const
212{
213 return "Shape Inverted Basic Test";
214}
215
217{
218 return "A DrawNode as stencil and Sprite as content, inverted";
219}
220
222{
224 clipper->setInverted(true);
225 return clipper;
226}
227
228// SpriteTest
229
230std::string SpriteTest::title() const
231{
232 return "Sprite Basic Test";
233}
234
235std::string SpriteTest::subtitle() const
236{
237 return "A Sprite as stencil and DrawNode as content";
238}
239
241{
242 auto node = this->grossini();
243 node->runAction(this->actionRotate());
244 return node;
245}
246
247ClippingNode* SpriteTest::clipper()
248{
250 clipper->setAlphaThreshold(0.05f);
251 return clipper;
252}
253
255{
256 auto node = this->shape();
257 node->runAction(this->actionScale());
258 return node;
259}
260
261// SpriteNoAlphaTest
262
263std::string SpriteNoAlphaTest::title() const
264{
265 return "Sprite No Alpha Basic Test";
266}
267
269{
270 return "A Sprite as stencil and DrawNode as content, no alpha";
271}
272
274{
276 clipper->setAlphaThreshold(1);
277 return clipper;
278}
279
280// SpriteInvertedTest
281
282std::string SpriteInvertedTest::title() const
283{
284 return "Sprite Inverted Basic Test";
285}
286
288{
289 return "A Sprite as stencil and DrawNode as content, inverted";
290}
291
293{
295 clipper->setAlphaThreshold(0.05f);
296 clipper->setInverted(true);
297 return clipper;
298}
299
300// NestedTest
301
302std::string NestedTest::title() const
303{
304 return "Nested Test";
305}
306
307std::string NestedTest::subtitle() const
308{
309 return "Nest 9 Clipping Nodes, max is usually 8";
310}
311
313{
314 static int depth = 9;
315
316 Node* parent = this;
317
318 for (int i = 0; i < depth; i++) {
319
320 int size = 225 - i * (225 / (depth * 2));
321
322 auto clipper = ClippingNode::create();
323 clipper->setContentSize(Size(size, size));
324 clipper->setAnchorPoint(Vec2(0.5f, 0.5f));
325 clipper->setPosition(parent->getContentSize().width / 2, parent->getContentSize().height / 2);
326 clipper->setAlphaThreshold(0.05f);
327 clipper->runAction(RepeatForever::create(RotateBy::create(i % 3 ? 1.33f : 1.66f, i % 2 ? 90.0f : -90.0f)));
328 parent->addChild(clipper);
329
330 auto stencil = Sprite::create(s_pathGrossini);
331 stencil->setScale( 2.5f - (i * (2.5f / depth)) );
332 stencil->setAnchorPoint( Vec2(0.5f, 0.5f) );
333 stencil->setPosition(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2);
334 stencil->setVisible(false);
335 stencil->runAction(Sequence::createWithTwoActions(DelayTime::create(i), Show::create()));
336 clipper->setStencil(stencil);
337
338 clipper->addChild(stencil);
339
340 parent = clipper;
341 }
342
343}
344
345// HoleDemo
346
348{
349 CC_SAFE_RELEASE(_outerClipper);
350 CC_SAFE_RELEASE(_holes);
351 CC_SAFE_RELEASE(_holesStencil);
352}
353
354std::string HoleDemo::title() const
355{
356 return "Hole Demo";
357}
358
359std::string HoleDemo::subtitle() const
360{
361 return "Touch/click to poke holes";
362}
363
365{
366 auto target = Sprite::create(s_pathBlock);
367 target->setAnchorPoint(Vec2::ZERO);
368 target->setScale(3);
369
370 _outerClipper = ClippingNode::create();
371 _outerClipper->retain();
372 AffineTransform transform = AffineTransform::IDENTITY;
373 transform = AffineTransformScale(transform, target->getScale(), target->getScale());
374
375 _outerClipper->setContentSize(SizeApplyAffineTransform(target->getContentSize(), transform));
376 _outerClipper->setAnchorPoint( Vec2(0.5f, 0.5f) );
377 _outerClipper->setPosition(Vec2(this->getContentSize()) * 0.5f);
378 _outerClipper->runAction(RepeatForever::create(RotateBy::create(1, 45)));
379
380 _outerClipper->setStencil( target );
381
382 auto holesClipper = ClippingNode::create();
383 holesClipper->setInverted(true);
384 holesClipper->setAlphaThreshold( 0.05f );
385
386 holesClipper->addChild(target);
387
388 _holes = Node::create();
389 _holes->retain();
390
391 holesClipper->addChild(_holes);
392
393 _holesStencil = Node::create();
394 _holesStencil->retain();
395
396 holesClipper->setStencil( _holesStencil);
397
398 _outerClipper->addChild(holesClipper);
399
400 this->addChild(_outerClipper);
401
402 auto listener = EventListenerTouchAllAtOnce::create();
403 listener->onTouchesBegan = CC_CALLBACK_2(HoleDemo::onTouchesBegan, this);
404 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
405}
406
408{
409 float scale = CCRANDOM_0_1() * 0.2 + 0.9;
410 float rotation = CCRANDOM_0_1() * 360;
411
412 auto hole = Sprite::create("Images/hole_effect.png");
413 hole->setPosition( point );
414 hole->setRotation( rotation );
415 hole->setScale( scale );
416
417 _holes->addChild(hole);
418
419 auto holeStencil = Sprite::create("Images/hole_stencil.png");
420 holeStencil->setPosition( point );
421 holeStencil->setRotation( rotation );
422 holeStencil->setScale( scale );
423
424 _holesStencil->addChild(holeStencil);
425
426 _outerClipper->runAction(Sequence::createWithTwoActions(ScaleBy::create(0.05f, 0.95f),
427 ScaleTo::create(0.125f, 1)));
428}
429
430
431void HoleDemo::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
432{
433 Touch *touch = (Touch *)touches[0];
434 Vec2 point = _outerClipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView()));
435 auto rect = Rect(0, 0, _outerClipper->getContentSize().width, _outerClipper->getContentSize().height);
436 if (!rect.containsPoint(point)) return;
437 this->pokeHoleAtPoint(point);
438}
439
440// ScrollViewDemo
441
442std::string ScrollViewDemo::title() const
443{
444 return "Scroll View Demo";
445}
446
447std::string ScrollViewDemo::subtitle() const
448{
449 return "Move/drag to scroll the content";
450}
451
453{
454 auto clipper = ClippingNode::create();
455 clipper->setTag( kTagClipperNode );
456 clipper->setContentSize( Size(200.0f, 200.0f) );
457 clipper->setAnchorPoint( Vec2(0.5f, 0.5f) );
458 clipper->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2);
459 clipper->runAction(RepeatForever::create(RotateBy::create(1, 45)));
460 this->addChild(clipper);
461
462 auto stencil = DrawNode::create();
463 Vec2 rectangle[4];
464 rectangle[0] = Vec2(0.0f, 0.0f);
465 rectangle[1] = Vec2(clipper->getContentSize().width, 0.0f);
466 rectangle[2] = Vec2(clipper->getContentSize().width, clipper->getContentSize().height);
467 rectangle[3] = Vec2(0.0f, clipper->getContentSize().height);
468
469 Color4F white(1, 1, 1, 1);
470 stencil->drawPolygon(rectangle, 4, white, 1, white);
471 clipper->setStencil(stencil);
472
473 auto content = Sprite::create(s_back2);
474 content->setTag( kTagContentNode );
475 content->setAnchorPoint( Vec2(0.5f, 0.5f) );
476 content->setPosition(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2);
477 clipper->addChild(content);
478
479 _scrolling = false;
480
481 auto listener = EventListenerTouchAllAtOnce::create();
482 listener->onTouchesBegan = CC_CALLBACK_2(ScrollViewDemo::onTouchesBegan, this);
483 listener->onTouchesMoved = CC_CALLBACK_2(ScrollViewDemo::onTouchesMoved, this);
484 listener->onTouchesEnded = CC_CALLBACK_2(ScrollViewDemo::onTouchesEnded, this);
485 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
486}
487
488void ScrollViewDemo::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
489{
490 Touch *touch = touches[0];
491 auto clipper = this->getChildByTag(kTagClipperNode);
492 Vec2 point = clipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView()));
493 auto rect = Rect(0, 0, clipper->getContentSize().width, clipper->getContentSize().height);
494 _scrolling = rect.containsPoint(point);
495 _lastPoint = point;
496}
497
498void ScrollViewDemo::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
499{
500 if (!_scrolling) return;
501 Touch *touch = touches[0];
502 auto clipper = this->getChildByTag(kTagClipperNode);
503 auto point = clipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView()));
504 Vec2 diff = point - _lastPoint;
505 auto content = clipper->getChildByTag(kTagContentNode);
506 content->setPosition(content->getPosition() + diff);
507 _lastPoint = point;
508}
509
510void ScrollViewDemo::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
511{
512 if (!_scrolling) return;
513 _scrolling = false;
514}
515
516// RawStencilBufferTests
517
518//#if COCOS2D_DEBUG > 1
519
520static const float _alphaThreshold = 0.05f;
521
522static const int _planeCount = 8;
523static const float _planeColor[][4] = {
524 {0, 0, 0, 0.65f},
525 {0.7f, 0, 0, 0.6f},
526 {0, 0.7f, 0, 0.55f},
527 {0, 0, 0.7f, 0.5f},
528 {0.7f, 0.7f, 0, 0.45f},
529 {0, 0.7f, 0.7f, 0.4f},
530 {0.7f, 0, 0.7f, 0.35f},
531 {0.7f, 0.7f, 0.7f, 0.3f},
532};
533
535{
536}
537
539{
540 return "Raw Stencil Tests";
541}
542
544{
545 return "1:Default";
546}
547
549{
550 for(int i = 0; i < _planeCount; ++i)
551 {
552 Sprite* sprite = Sprite::create(s_pathGrossini);
553 sprite->setAnchorPoint( Vec2(0.5, 0) );
554 sprite->setScale( 2.5f );
555 _sprites.pushBack(sprite);
556
557 Sprite* sprite2 = Sprite::create(s_pathGrossini);
558 sprite2->setAnchorPoint( Vec2(0.5, 0) );
559 sprite2->setScale( 2.5f );
560 _spritesStencil.pushBack(sprite2);
561 }
562
563 initCommands();
564}
565
567{
568 auto renderer = Director::getInstance()->getRenderer();
569 _enableStencilCallback.func = [=](){
570 renderer->setStencilTest(true);
571 };
572 _enableStencilCallback.init(_globalZOrder);
573
574 _disableStencilCallback.func = [=](){
575 renderer->setStencilTest(false);
576 };
577 _disableStencilCallback.init(_globalZOrder);
578
579 auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_UCOLOR);
580 _programState = new (std::nothrow) backend::ProgramState(program);
581 _locColor = _programState->getProgram()->getUniformLocation("u_color");
582 _locMVPMatrix = _programState->getProgram()->getUniformLocation("u_MVPMatrix");
583 const auto& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
584 _programState->setUniform(_locMVPMatrix, projectionMat.m, sizeof(projectionMat.m));
585
586 size_t neededCmdSize = _planeCount * 2;
587 _renderCmds.resize(neededCmdSize);
588 auto winPoint = Vec2(Director::getInstance()->getWinSize());
589 auto planeSize = winPoint * (1.0 / _planeCount);
590 BlendFunc blend;
591 blend.src = backend::BlendFactor::ONE;
592 blend.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
593 for (int i = 0, cmdIndex = 0; i < _planeCount; i++)
594 {
595 auto stencilPoint = planeSize * (_planeCount - i);
596 stencilPoint.x = winPoint.x;
597
598 auto& cmd = _renderCmds[cmdIndex];
599 cmdIndex++;
600 cmd.init(_globalZOrder, blend);
601 cmd.setBeforeCallback( CC_CALLBACK_0(RawStencilBufferTest::onBeforeDrawClip, this, i) );
602 Vec2 vertices[] = {
603 Vec2::ZERO,
604 Vec2(stencilPoint.x, 0.0f),
605 stencilPoint,
606 Vec2(0.0f, stencilPoint.y)
607 };
608 unsigned short indices[] = {0, 2, 1, 0, 3, 2};
609 cmd.createVertexBuffer(sizeof(Vec2), 4, backend::BufferUsage::STATIC);
610 cmd.updateVertexBuffer(vertices, sizeof(vertices));
611 cmd.createIndexBuffer(backend::IndexFormat::U_SHORT, 6, backend::BufferUsage::STATIC);
612 cmd.updateIndexBuffer(indices, sizeof(indices));
613 cmd.getPipelineDescriptor().programState = _programState;
614 auto vertexLayout = _programState->getVertexLayout();
615 auto& attributes = _programState->getProgram()->getActiveAttributes();
616 auto iter = attributes.find("a_position");
617 if (iter != attributes.end())
618 vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
619 vertexLayout->setLayout(sizeof(Vec2));
620
621
622 auto& cmd2 = _renderCmds[cmdIndex];
623 cmdIndex++;
624 cmd2.init(_globalZOrder, blend);
625 cmd2.setBeforeCallback(CC_CALLBACK_0(RawStencilBufferTest::onBeforeDrawSprite, this, i));
626 Vec2 vertices2[] = {
627 Vec2::ZERO,
628 Vec2(winPoint.x, 0.0f),
629 winPoint,
630 Vec2(0.0f, winPoint.y)
631 };
632 cmd2.createVertexBuffer(sizeof(Vec2), 4, backend::BufferUsage::STATIC);
633 cmd2.updateVertexBuffer(vertices2, sizeof(vertices2));
634 cmd2.createIndexBuffer(backend::IndexFormat::U_SHORT, 6, backend::BufferUsage::STATIC);
635 cmd2.updateIndexBuffer(indices, sizeof(indices));
636 cmd2.getPipelineDescriptor().programState = _programState;
637 }
638}
639
640void RawStencilBufferTest::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
641{
642 auto winPoint = Vec2(Director::getInstance()->getWinSize());
643 auto planeSize = winPoint * (1.0 / _planeCount);
644
645 renderer->addCommand(&_enableStencilCallback);
646
647 for (int i = 0, cmdIndex = 0; i < _planeCount; i++)
648 {
649 auto spritePoint = planeSize * i;
650 spritePoint.x += planeSize.x / 2;
651 spritePoint.y = 0;
652 _sprites.at(i)->setPosition( spritePoint );
653 _spritesStencil.at(i)->setPosition( spritePoint );
654
655 renderer->clear(ClearFlag::STENCIL, Color4F::BLACK, 0.f, 0x0, _globalZOrder);
656
657 renderer->addCommand(&_renderCmds[cmdIndex]);
658 cmdIndex++;
659
660 Director* director = Director::getInstance();
661 CCASSERT(nullptr != director, "Director is null when setting matrix stack");
662 director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
663
664 _modelViewTransform = this->transform(transform);
665 _spritesStencil.at(i)->visit(renderer, _modelViewTransform, flags);
666 director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
667
668 renderer->addCommand(&_renderCmds[cmdIndex]);
669 cmdIndex++;
670
671 director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
672 _modelViewTransform = this->transform(transform);
673 _sprites.at(i)->visit(renderer, _modelViewTransform, flags);
674 director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
675 }
676
677 renderer->addCommand(&_disableStencilCallback);
678}
679
681{
682 this->setupStencilForClippingOnPlane(planeIndex);
683 float color[4] = {1.f, 1.f, 1.f, 1.f};
684 _programState->setUniform(_locColor, color, sizeof(color));
685}
686
688{
689 this->setupStencilForDrawingOnPlane(planeIndex);
690 auto& color = _planeColor[planeIndex];
691 _programState->setUniform(_locColor, (void*)color, sizeof(color));
692}
693
695{
696 auto renderer = Director::getInstance()->getRenderer();
697 unsigned int planeMask = 0x1 << plane;
698 renderer->setStencilWriteMask(planeMask);
699 renderer->setStencilCompareFunction(backend::CompareFunction::NEVER, planeMask, planeMask);
700 renderer->setStencilOperation(backend::StencilOperation::REPLACE, backend::StencilOperation::KEEP, backend::StencilOperation::KEEP);
701}
702
704{
705 auto renderer = Director::getInstance()->getRenderer();
706 unsigned int planeMask = 0x1 << plane;
707 renderer->setStencilCompareFunction(backend::CompareFunction::EQUAL, planeMask, planeMask);
708 renderer->setStencilOperation(backend::StencilOperation::KEEP, backend::StencilOperation::KEEP, backend::StencilOperation::KEEP);
709}
710
711//@implementation RawStencilBufferTest2
712
714{
715 return "2:DepthMask:FALSE";
716}
717
719{
721 Director::getInstance()->getRenderer()->setDepthWrite(false);
722}
723
725{
726 Director::getInstance()->getRenderer()->setDepthWrite(true);
728}
729
730//@implementation RawStencilBufferTest3
731
733{
734 return "3:DepthTest:DISABLE,DepthMask:FALSE";
735}
736
738{
740 auto renderer = Director::getInstance()->getRenderer();
741 renderer->setDepthTest(false);
742 renderer->setDepthWrite(false);
743}
744
746{
747 Director::getInstance()->getRenderer()->setDepthWrite(true);
749}
750
752{
754 for(int i = 0; i < _planeCount; ++i)
755 {
756 auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
757 auto programState = new backend::ProgramState(program);
758 programState->setUniform(programState->getUniformLocation("u_alpha_value"), &_alphaThreshold, sizeof(_alphaThreshold));
759 _spritesStencil.at(i)->setProgramState(programState);
760 }
761}
762//@implementation RawStencilBufferTest4
763
765{
766 return "4:DepthMask:FALSE,AlphaTest:ENABLE";
767}
768
770{
772 auto renderer = Director::getInstance()->getRenderer();
773 renderer->setDepthWrite(false);
774}
775
777{
778 Director::getInstance()->getRenderer()->setDepthWrite(true);
780}
781
782//@implementation RawStencilBufferTest5
783
785{
786 return "5:DepthTest:DISABLE,DepthMask:FALSE,AlphaTest:ENABLE";
787}
788
790{
792 auto renderer = Director::getInstance()->getRenderer();
793 renderer->setDepthWrite(false);
794 renderer->setDepthTest(false);
795}
796
798{
799 auto renderer = Director::getInstance()->getRenderer();
800 renderer->setDepthWrite(false);
802}
803
804//@implementation RawStencilBufferTest6
805
807{
808 return "6:ManualClear,AlphaTest:ENABLE";
809}
810
812{
814 Director::getInstance()->getRenderer()->setStencilWriteMask(~0);
815}
816
818{
819 int planeMask = 0x1 << plane;
820 auto renderer = Director::getInstance()->getRenderer();
821 renderer->setStencilCompareFunction(backend::CompareFunction::NEVER, planeMask, planeMask);
822 renderer->setStencilOperation(backend::StencilOperation::REPLACE, backend::StencilOperation::KEEP, backend::StencilOperation::KEEP);
823 renderer->setDepthTest(false);
824 renderer->setDepthWrite(false);
825}
826
828{
829 auto renderer = Director::getInstance()->getRenderer();
830 renderer->setDepthWrite(true);
832}
833
834//#endif // COCOS2D_DEBUG > 1
835
836//ClippingToRenderTextureTest
837
839{
840 return "Clipping to RenderTexture";
841}
842
844{
845 return "Both should look the same";
846}
847
849{
850 auto button = MenuItemFont::create("Reproduce bug", [&](Ref *sender) {
851 std::vector<Node*> nodes;
852 enumerateChildren("remove me [0-9]", [&](Node *node) {
853 nodes.push_back(node);
854 return false;
855 });
856 for (auto node : nodes)
857 {
858 this->removeChild(node);
859 }
860 this->reproduceBug();
861 });
862
863 auto s = Director::getInstance()->getWinSize();
864 // create menu, it's an autorelease object
865 auto menu = Menu::create(button, nullptr);
866 menu->setPosition(Point(s.width/2, s.height/2));
867 this->addChild(menu, 1);
868
870}
871
873{
874 auto director = Director::getInstance();
875 Size visibleSize = director->getVisibleSize();
876 Point origin = director->getVisibleOrigin();
877
878
879 // add "HelloWorld" splash screen"
880 auto sprite = Sprite::create("Images/grossini.png");
881
882 // position the sprite on the center of the screen
883 sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
884
885 // add the sprite as a child to this layer
886 this->addChild(sprite, 0);
887 sprite->setName("remove me 0");
888
889 // container node that will contain the clippingNode
890 auto container = Node::create();
891 this->addChild(container);
892 container->setName("remove me 1");
893
894 auto stencil = DrawNode::create();
895 Point triangle[3];
896 triangle[0] = Point(-50, -50);
897 triangle[1] = Point(50, -50);
898 triangle[2] = Point(0, 50);
899 Color4F green(0, 1, 0, 1);
900 stencil->drawPolygon(triangle, 3, green, 0, green);
901
902 auto clipper = ClippingNode::create();
903 clipper->setAnchorPoint(Point(0.5f, 0.5f));
904 clipper->setPosition( Point(visibleSize.width/2, visibleSize.height/2) );
905 clipper->setStencil(stencil);
906 clipper->setInverted(true);
907 container->addChild(clipper, 1);
908
909
910 auto img = DrawNode::create();
911 triangle[0] = Point(-200, -200);
912 triangle[1] = Point(200, -200);
913 triangle[2] = Point(0, 200);
914 Color4F red(1, 0, 0, 1);
915 img->drawPolygon(triangle, 3, red, 0, red);
916 clipper->addChild(img);
917}
918
920{
921 auto director = Director::getInstance();
922 Size visibleSize = director->getVisibleSize();
923 Point origin = director->getVisibleOrigin();
924
925
926 // add "HelloWorld" splash screen"
927 auto sprite = Sprite::create("Images/grossini.png");
928
929 // position the sprite on the center of the screen
930 sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
931
932 // add the sprite as a child to this layer
933 this->addChild(sprite, 0);
934
935
936 // container node that will contain the clippingNode
937 auto container = Node::create();
938 container->retain();
939
940 auto stencil = DrawNode::create();
941 Point triangle[3];
942 triangle[0] = Point(-50, -50);
943 triangle[1] = Point(50, -50);
944 triangle[2] = Point(0, 50);
945 Color4F green(0, 1, 0, 1);
946 stencil->drawPolygon(triangle, 3, green, 0, green);
947
948 auto clipper = ClippingNode::create();
949 clipper->setAnchorPoint(Point(0.5f, 0.5f));
950 clipper->setPosition( Point(visibleSize.width/2, visibleSize.height/2) );
951 clipper->setStencil(stencil);
952 clipper->setInverted(true);
953 container->addChild(clipper, 1);
954
955
956 auto img = DrawNode::create();
957 triangle[0] = Point(-200, -200);
958 triangle[1] = Point(200, -200);
959 triangle[2] = Point(0, 200);
960 Color4F red(1, 0, 0, 1);
961 img->drawPolygon(triangle, 3, red, 0, red);
962 clipper->addChild(img);
963
964 // container rendered on Texture the size of the screen and because Clipping node use stencil buffer so we need to
965 // create RenderTexture with depthStencil format parameter
966 RenderTexture* rt = RenderTexture::create(visibleSize.width, visibleSize.height, backend::PixelFormat::RGBA8888, PixelFormat::D24S8);
967 rt->setPosition(visibleSize.width/2, visibleSize.height/2);
968 this->addChild(rt);
969
970 rt->begin();
971 container->visit();
972 rt->end();
973}
974
975// ClippingRectangleNodeDemo
976
978{
979 return "ClippingRectangleNode Test";
980}
981
983{
984 return "more effectively";
985}
986
988{
989 auto clipper = ClippingRectangleNode::create();
990 clipper->setClippingRegion(Rect(this->getContentSize().width / 2 - 100, this->getContentSize().height / 2 - 100, 200.0f, 200.0f));
991 clipper->setTag( kTagClipperNode );
992 this->addChild(clipper);
993
994 auto content = Sprite::create(s_back2);
995 content->setTag( kTagContentNode );
996 content->setAnchorPoint( Vec2(0.5f, 0.5f) );
997 content->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2);
998 clipper->addChild(content);
999}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
static const float _alphaThreshold
static const float _planeColor[][4]
@ kTagTitleLabel
@ kTagStencilNode
@ kTagContentNode
@ kTagClipperNode
@ kTagSubtitleLabel
static const int _planeCount
USING_NS_CC
virtual std::string title() const override
virtual bool init() override
virtual std::string title() const override
virtual cocos2d::Sprite * grossini()
virtual cocos2d::DrawNode * shape()
virtual void setup() override
virtual cocos2d::Action * actionScale()
virtual cocos2d::Action * actionRotate()
virtual cocos2d::Node * content()
virtual cocos2d::ClippingNode * clipper()
virtual std::string subtitle() const override
virtual cocos2d::Node * stencil()
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void setup() override
virtual std::string title() const override
virtual void setup() override
virtual std::string subtitle() const override
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual void setup() override
cocos2d::Node * _holesStencil
void pokeHoleAtPoint(cocos2d::Vec2 point)
cocos2d::Node * _holes
virtual std::string title() const override
cocos2d::ClippingNode * _outerClipper
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void setup() override
virtual std::string subtitle() const override
virtual void setupStencilForClippingOnPlane(int plane) override
virtual void setupStencilForDrawingOnPlane(int plane) override
virtual std::string subtitle() const override
virtual void setupStencilForDrawingOnPlane(int plane) override
virtual std::string subtitle() const override
virtual void setupStencilForClippingOnPlane(int plane) override
virtual void setupStencilForClippingOnPlane(int plane) override
virtual void setupStencilForDrawingOnPlane(int plane) override
virtual std::string subtitle() const override
virtual void setupStencilForClippingOnPlane(int plane) override
virtual void setupStencilForDrawingOnPlane(int plane) override
virtual std::string subtitle() const override
virtual void setupStencilForClippingOnPlane(int plane) override
virtual std::string subtitle() const override
virtual void setup() override
virtual void setupStencilForDrawingOnPlane(int plane) override
virtual void setup() override
virtual std::string subtitle() const override
void onBeforeDrawSprite(int planeIndex)
cocos2d::Vector< cocos2d::Sprite * > _sprites
cocos2d::CallbackCommand _disableStencilCallback
cocos2d::backend::UniformLocation _locMVPMatrix
virtual void setupStencilForClippingOnPlane(int plane)
std::vector< cocos2d::CustomCommand > _renderCmds
virtual std::string title() const override
cocos2d::Vector< cocos2d::Sprite * > _spritesStencil
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override
void onBeforeDrawClip(int planeIndex)
cocos2d::CallbackCommand _enableStencilCallback
cocos2d::backend::UniformLocation _locColor
virtual void setup() override
virtual void setupStencilForDrawingOnPlane(int plane)
virtual void setup() override
virtual std::string title() const override
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string subtitle() const override
cocos2d::Vec2 _lastPoint
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string subtitle() const override
virtual cocos2d::ClippingNode * clipper() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual cocos2d::Node * stencil() override
virtual std::string title() const override
virtual cocos2d::Node * content() override
virtual cocos2d::ClippingNode * clipper() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual cocos2d::ClippingNode * clipper() override
virtual cocos2d::Node * content() override
virtual cocos2d::Node * stencil() override
virtual std::string subtitle() const override
virtual cocos2d::ClippingNode * clipper() override
virtual std::string title() const override
static const char s_back3[]
Definition: testResource.h:41
static const char s_pathBlock[]
Definition: testResource.h:37
static const char s_back2[]
Definition: testResource.h:40
static const char s_pathGrossini[]
Definition: testResource.h:28