PuzzleSDK
Sprite3DTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2012 cocos2d-x.org
3 Copyright (c) 2013-2016 Chukong Technologies Inc.
4 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
5
6 http://www.cocos2d-x.org
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 ****************************************************************************/
26
27#include "Sprite3DTest.h"
28#include "DrawNode3D.h"
29#include "2d/CCCameraBackgroundBrush.h"
30#include "3d/CCSprite3DMaterial.h"
31#include "3d/CCMotionStreak3D.h"
32
33#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
34
35#include <algorithm>
36#include "../testResource.h"
37
39
40Sprite3DTests::Sprite3DTests()
41{
45 // // 3DEffect use custom shader which is not supported on WP8/WinRT yet.
73};
74
75//------------------------------------------------------------------
76//
77// SpriteTestDemo
78//
79//------------------------------------------------------------------
80
81std::string Sprite3DTestDemo::title() const
82{
83 return "No title";
84}
85
87{
88 Sprite3DMaterial::releaseCachedMaterial();
89}
90
91//------------------------------------------------------------------
92//
93// Sprite3DForceDepthTest
94//
95//------------------------------------------------------------------
97{
98 auto orc = cocos2d::Sprite3D::create("Sprite3DTest/orc.c3b");
99 orc->setScale(5);
100 orc->setPositionNormalized(Vec2(.5f, .3f));
101 orc->setPositionZ(40);
102 orc->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
103 orc->setGlobalZOrder(-1);
104
105 addChild(orc);
106
107 auto ship = Sprite3D::create("Sprite3DTest/boss1.obj");
108 ship->setScale(5);
109 ship->setTexture("Sprite3DTest/boss.png");
110 ship->setPositionNormalized(Vec2(.5f, .5f));
111 ship->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
112 ship->setForceDepthWrite(true);
113
114 addChild(ship);
115}
116
118{
119 return "Force Depth Write Error Test";
120}
121
123{
124 return "Ship should always appear behind orc";
125}
126
127//------------------------------------------------------------------
128//
129// Sprite3DEmptyTest
130//
131//------------------------------------------------------------------
133{
134 auto s = Sprite3D::create();
135 s->setPositionNormalized(Vec2(.5f, .5f));
136 auto l = Label::create();
137 l->setString("Test");
138 s->addChild(l);
139 addChild(s);
140}
141
142std::string Sprite3DEmptyTest::title() const
143{
144 return "Testing Sprite3D Container";
145}
146
148{
149 return "Sprite3D can act as containers for 2D objects";
150}
151
152//------------------------------------------------------------------
153//
154// Sprite3DBasicTest
155//
156//------------------------------------------------------------------
157
159{
160 auto listener = EventListenerTouchAllAtOnce::create();
161 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DBasicTest::onTouchesEnded, this);
162 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
163
164 auto s = Director::getInstance()->getWinSize();
165 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
166}
167
169{
170 //int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f);
171 //int x = (idx%5) * 85;
172 //int y = (idx/5) * 121;
173
174// //option 1: load a obj that contain the texture in it
175// auto sprite = Sprite3D::create("sprite3dTest/scene01.obj");
176
177 //option 2: load obj and assign the texture
178 auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj");
179 sprite->setScale(3.f);
180 sprite->setTexture("Sprite3DTest/boss.png");
181
182 //
183 //sprite->setEffect(cocos2d::EFFECT_OUTLINE);
184
185 //add to scene
186 addChild(sprite);
187
188 sprite->setPosition(Vec2(p.x, p.y));
189
190 ActionInterval* action;
191 float random = CCRANDOM_0_1();
192
193 if (random < 0.20)
194 action = ScaleBy::create(3, 2);
195 else if (random < 0.40)
196 action = RotateBy::create(3, 360);
197 else if (random < 0.60)
198 action = Blink::create(1, 3);
199 else if (random < 0.8)
200 action = TintBy::create(2, 0, -255, -255);
201 else
202 action = FadeOut::create(2);
203 auto action_back = action->reverse();
204 auto seq = Sequence::create(action, action_back, nullptr);
205
206 sprite->runAction(RepeatForever::create(seq));
207}
208
209void Sprite3DBasicTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
210{
211 for (auto touch : touches)
212 {
213 auto location = touch->getLocation();
214
215 addNewSpriteWithCoords(location);
216 }
217}
218
219std::string Sprite3DBasicTest::title() const
220{
221 return "Testing Sprite3D";
222}
223
225{
226 return "Tap screen to add more sprites";
227}
228
229//------------------------------------------------------------------
230//
231// Sprite3DUVAnimationTest
232//
233//------------------------------------------------------------------
234
236{
237 //the offset use to translating texture
240 Size visibleSize = Director::getInstance()->getVisibleSize();
241
242 //use custom camera
243 auto camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
244 camera->setCameraFlag(CameraFlag::USER1);
245
246 //create cylinder
247 auto cylinder = Sprite3D::create("Sprite3DTest/cylinder.c3b");
248 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/UVAnimation.material");
249 _state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
250 cylinder->setMaterial(mat);
251
252 this->addChild(cylinder);
253 this->setCameraMask(2);
254 this->addChild(camera);
255
256 //adjust cylinder's position & rotation
257 cylinder->setPosition3D(Vec3(0.0f, -15.0f, -50.0f));
258 cylinder->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
259
260 //the callback function update cylinder's texcoord
261 schedule(CC_SCHEDULE_SELECTOR(Sprite3DUVAnimationTest::cylinderUpdate));
262
263#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
264 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
265 [=](EventCustom*)
266 {
267 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/UVAnimation.material");
268
269 cylinder->setMaterial(mat);
270 _state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
271 }
272 );
273 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
274#endif
275}
276
278{
279#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
280 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
281#endif
282}
283
285{
286 return "Testing UV Animation";
287}
288
290{
291 return "";
292}
293
295{
296 //callback function to update cylinder's texcoord
297 static bool fade_in = true;
298 _cylinder_texture_offset += 0.3*dt;
300 if (fade_in)
301 {
302 _shining_duration += 0.5 * dt;
303 if (_shining_duration > 1) fade_in = false;
304 }
305 else
306 {
307 _shining_duration -= 0.5 * dt;
308 if (_shining_duration < 0) fade_in = true;
309 }
310
311 //pass the result to shader
312 auto offsetLoc = _state->getUniformLocation("offset");
313 auto durationLoc = _state->getUniformLocation("duration");
314 _state->setUniform(offsetLoc, &_cylinder_texture_offset, sizeof(_cylinder_texture_offset));
315 _state->setUniform(durationLoc, &_shining_duration, sizeof(_shining_duration));
316}
317
318//------------------------------------------------------------------
319//
320// Sprite3DFakeShadowTest
321//
322//------------------------------------------------------------------
324{
325 Size visibleSize = Director::getInstance()->getVisibleSize();
326
327 auto listener = EventListenerTouchAllAtOnce::create();
328 listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesBegan, this);
329 listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesMoved, this);
330 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesEnded, this);
331 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
332
333 auto layer = Layer::create();
334 addChild(layer, 0);
335 //create Camera
336 _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
337 _camera->setCameraFlag(CameraFlag::USER1);
338 _camera->setPosition3D(Vec3(0.0f, 20.0f, 25.0f));
339 _camera->setRotation3D(Vec3(-60.0f, 0.0f, 0.0f));
340
341 //create a plane
342 _plane = Sprite3D::create("Sprite3DTest/plane.c3t");
343 _plane->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
344
345
346 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/FakeShadow.material");
347 _state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
348 _plane->setMaterial(mat);
349
350
351 auto location = _state->getUniformLocation("u_model_matrix");
352 auto transform = _plane->getNodeToWorldTransform();
353 _state->setUniform(location, transform.m, sizeof(transform.m));
354
355 layer->addChild(_plane);
356
357 //create the orc
358 _orc = Sprite3D::create("Sprite3DTest/orc.c3b");
359 _orc->setScale(0.2f);
360 _orc->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
361 _orc->setPosition3D(Vec3(0.0f, 0.0f, 10.0f));
362 _targetPos = _orc->getPosition3D();
363 location = _state->getUniformLocation("u_target_pos");
364 _state->setUniform(location, &_targetPos, sizeof(_targetPos));
365 layer->addChild(_orc);
366 layer->addChild(_camera);
367 layer->setCameraMask(2);
368
369 schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f);
370
371#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
372 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
373 [this](EventCustom*)
374 {
375 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/FakeShadow.material");
376 _state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
377 _plane->setMaterial(mat);
378 backend::UniformLocation loc = _state->getUniformLocation("u_model_matrix");
379 auto trans = _plane->getNodeToWorldTransform();
380 _state->setUniform(loc, &trans, sizeof(trans));
381 loc = _state->getUniformLocation("u_target_pos");
382 auto pos = _orc->getPosition3D();
383 _state->setUniform(loc, &pos, sizeof(pos));
384 }
385 );
386 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
387#endif
388}
389
391{
392#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
393 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
394#endif
395}
396
398{
399 return "fake shadow effect";
400}
401
403{
404 return "touch the screen to move around";
405}
406
407void Sprite3DFakeShadowTest::Move(cocos2d::Ref* sender, int value)
408{
409 _orc->setPositionX(_orc->getPositionX() + value);
410
411}
412
414{
415 updateState(fDelta);
417 {
418 move3D(fDelta);
420 {
421 Vec3 curPos = _orc->getPosition3D();
422
423 Vec3 newFaceDir = _targetPos - curPos;
424 newFaceDir.y = 0;
425 newFaceDir.normalize();
426 Vec3 up;
427 _orc->getNodeToWorldTransform().getUpVector(&up);
428 up.normalize();
429 Vec3 right;
430 Vec3::cross(-newFaceDir, up, &right);
431 right.normalize();
432 Vec3 pos = Vec3(0, 0, 0);
433 Mat4 mat;
434 mat.m[0] = right.x;
435 mat.m[1] = right.y;
436 mat.m[2] = right.z;
437 mat.m[3] = 0.0f;
438
439 mat.m[4] = up.x;
440 mat.m[5] = up.y;
441 mat.m[6] = up.z;
442 mat.m[7] = 0.0f;
443
444 mat.m[8] = newFaceDir.x;
445 mat.m[9] = newFaceDir.y;
446 mat.m[10] = newFaceDir.z;
447 mat.m[11] = 0.0f;
448
449 mat.m[12] = pos.x;
450 mat.m[13] = pos.y;
451 mat.m[14] = pos.z;
452 mat.m[15] = 1.0f;
453 _orc->setAdditionalTransform(&mat);
454 }
455 }
456}
457
458
459void Sprite3DFakeShadowTest::move3D(float elapsedTime)
460{
461 Vec3 curPos = _orc->getPosition3D();
462 Vec3 newFaceDir = _targetPos - curPos;
463 newFaceDir.y = 0.0f;
464 newFaceDir.normalize();
465 Vec3 offset = newFaceDir * 25.0f * elapsedTime;
466 curPos += offset;
467 _orc->setPosition3D(curPos);
468 offset.x = offset.x;
469 offset.z = offset.z;
470 //pass the newest orc position
471 auto location = _state->getUniformLocation("u_target_pos");
472 auto position = _orc->getPosition3D();
473 _state->setUniform(location, &position, sizeof(position));
474}
475
477{
478 Vec3 curPos = _orc->getPosition3D();
479 Vec3 curFaceDir;
480 _orc->getNodeToWorldTransform().getForwardVector(&curFaceDir);
481 curFaceDir = -curFaceDir;
482 curFaceDir.normalize();
483 Vec3 newFaceDir = _targetPos - curPos;
484 newFaceDir.y = 0.0f;
485 newFaceDir.normalize();
486 float cosAngle = std::fabs(Vec3::dot(curFaceDir, newFaceDir) - 1.0f);
487 float dist = curPos.distanceSquared(_targetPos);
488 if (dist <= 4.0f)
489 {
490 if (cosAngle <= 0.01f)
492 else
494 }
495 else
496 {
497 if (cosAngle > 0.01f)
499 else
501 }
502}
503
504bool Sprite3DFakeShadowTest::isState(unsigned int state, unsigned int bit) const
505{
506 return (state & bit) == bit;
507}
508
509void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
510{
511
512}
513
514void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
515{
516}
517
518void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
519{
520 for (auto &item : touches)
521 {
522 auto touch = item;
523 auto location = touch->getLocationInView();
524 if (_camera)
525 {
526 if (_orc)
527 {
528 Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
529
530 auto size = Director::getInstance()->getWinSize();
531 nearP = _camera->unproject(nearP);
532 farP = _camera->unproject(farP);
533 Vec3 dir(farP - nearP);
534 float dist = 0.0f;
535 float ndd = Vec3::dot(Vec3(0, 1, 0), dir);
536 if (ndd == 0)
537 dist = 0.0f;
538 float ndo = Vec3::dot(Vec3(0, 1, 0), nearP);
539 dist = (0 - ndo) / ndd;
540 Vec3 p = nearP + dist * dir;
541
542 if (p.x > 100)
543 p.x = 100;
544 if (p.x < -100)
545 p.x = -100;
546 if (p.z > 100)
547 p.z = 100;
548 if (p.z < -100)
549 p.z = -100;
550
551 _targetPos = p;
552 }
553 }
554 }
555}
556
557//------------------------------------------------------------------
558//
559// Sprite3DBasicToonShaderTest
560//
561//------------------------------------------------------------------
563{
564 Size visibleSize = Director::getInstance()->getVisibleSize();
565 auto _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
566 _camera->setCameraFlag(CameraFlag::USER1);
567 // create a teapot
568 auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
569 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/BasicToon.material");
570 _state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
571 teapot->setMaterial(mat);
572
573 teapot->setPosition3D(Vec3(0.0f, -5.0f, -20.0f));
574 teapot->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
575 auto rotate_action = RotateBy::create(1.5f, Vec3(0.0f, 30.0f, 0.0f));
576 teapot->runAction(RepeatForever::create(rotate_action));
577 addChild(teapot);
578 addChild(_camera);
579 setCameraMask(2);
580#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
581 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
582 [=](EventCustom*)
583 {
584 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/BasicToon.material");
585 _state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
586 teapot->setMaterial(mat);
587 }
588 );
589 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
590#endif
591}
592
594{
595#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
596 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
597#endif
598}
599
601{
602 return "basic toon shader test";
603}
604
606{
607 return " ";
608}
609
610//------------------------------------------------------------------
611//
612// Sprite3DLightMapTest
613//
614//------------------------------------------------------------------
616{
617 //the assets are from the OpenVR demo
618 //get the visible size.
619 Size visibleSize = Director::getInstance()->getVisibleSize();
620 _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
621 _camera->setCameraFlag(CameraFlag::USER1);
622 _camera->setPosition3D(Vec3(0.0f, 25.0f, 15.0f));
623 _camera->setRotation3D(Vec3(-35.0f, 0.0f, 0.0f));
624 auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b");
625 LightMapScene->setScale(0.1f);
626 addChild(LightMapScene);
627 addChild(_camera);
628 setCameraMask(2);
629
630 //add a point light
631 auto light = PointLight::create(Vec3(35, 75, -20.5), Color3B(255, 255, 255), 150);
632 addChild(light);
633 //set the ambient light
634 auto ambient = AmbientLight::create(Color3B(55, 55, 55));
635 addChild(ambient);
636
637 //create a listener
638 auto listener = EventListenerTouchAllAtOnce::create();
639 listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DLightMapTest::onTouchesMoved, this);
640 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
641}
643{
644
645}
647{
648 return "light map test";
649}
650
652{
653 return "drag the screen to move around";
654}
655
656void Sprite3DLightMapTest::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
657{
658 if (touches.size() == 1)
659 {
660 float delta = Director::getInstance()->getDeltaTime();
661 auto touch = touches[0];
662 auto location = touch->getLocation();
663 auto PreviousLocation = touch->getPreviousLocation();
664 Point newPos = PreviousLocation - location;
665
666 Vec3 cameraDir;
667 Vec3 cameraRightDir;
668 _camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
669 cameraDir.normalize();
670 cameraDir.y = 0;
671 _camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
672 cameraRightDir.normalize();
673 cameraRightDir.y = 0;
674 Vec3 cameraPos = _camera->getPosition3D();
675 cameraPos += cameraDir * newPos.y*delta;
676 cameraPos += cameraRightDir * newPos.x*delta;
677 _camera->setPosition3D(cameraPos);
678 }
679}
680
681//------------------------------------------------------------------
682//
683// Sprite3DHitTest
684//
685//------------------------------------------------------------------
687{
688 auto s = Director::getInstance()->getWinSize();
689
690 auto sprite1 = Sprite3D::create("Sprite3DTest/boss1.obj");
691
692 sprite1->setScale(4.f);
693 sprite1->setTexture("Sprite3DTest/boss.png");
694 sprite1->setPosition(Vec2(s.width / 2, s.height / 2));
695
696 //add to scene
697 addChild(sprite1);
698 sprite1->runAction(RepeatForever::create(RotateBy::create(3.0f, 360.0f)));
699
700 auto sprite2 = Sprite3D::create("Sprite3DTest/boss1.obj");
701
702 sprite2->setScale(4.f);
703 sprite2->setTexture("Sprite3DTest/boss.png");
704 sprite2->setPosition(Vec2(s.width / 2, s.height / 2));
705 sprite2->setAnchorPoint(Vec2(0.5f, 0.5f));
706
707 //add to scene
708 addChild(sprite2);
709 sprite2->runAction(RepeatForever::create(RotateBy::create(3.0f, -360.0f)));
710
711
712 // Make sprite1 touchable
713 auto listener1 = EventListenerTouchOneByOne::create();
714 listener1->setSwallowTouches(true);
715
716 listener1->onTouchBegan = [](Touch* touch, Event* event) {
717 auto target = static_cast<Sprite3D*>(event->getCurrentTarget());
718
719 Rect rect = target->getBoundingBox();
720 if (rect.containsPoint(touch->getLocation()))
721 {
722 log("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
723 target->setOpacity(100);
724 return true;
725 }
726 return false;
727 };
728
729 listener1->onTouchMoved = [](Touch* touch, Event* event) {
730 auto target = static_cast<Sprite3D*>(event->getCurrentTarget());
731 target->setPosition(target->getPosition() + touch->getDelta());
732 };
733
734 listener1->onTouchEnded = [=](Touch* touch, Event* event) {
735 auto target = static_cast<Sprite3D*>(event->getCurrentTarget());
736 log("sprite3d onTouchesEnded.. ");
737 target->setOpacity(255);
738 };
739
740 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
741 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
742
743}
744
745std::string Sprite3DHitTest::title() const
746{
747 return "Testing Sprite3D Touch in 2D";
748}
749
750std::string Sprite3DHitTest::subtitle() const
751{
752 return "Tap Sprite3D and Drag";
753}
754
756{
757 auto s = Director::getInstance()->getWinSize();
758 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
759
760 auto listener = EventListenerTouchAllAtOnce::create();
761 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DEffectTest::onTouchesEnded, this);
762 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
763#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
764 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
765 [this](EventCustom*)
766 {
767 auto material = Sprite3DMaterial::createWithFilename("Sprite3DTest/outline.material");
768 material->setTechnique("outline_noneskinned");
769 for (auto& sprite : _sprites)
770 {
771 sprite->setMaterial(material->clone());
772 }
773 }
774 );
775 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
776#endif
777}
778
780{
781#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
782 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
783#endif
784}
785
786std::string Sprite3DEffectTest::title() const
787{
788 return "Testing Sprite3D";
789}
791{
792 return "Sprite3d with effects";
793}
794
796{
797 //option 2: load obj and assign the texture
798 auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj");
799 auto material = Sprite3DMaterial::createWithFilename("Sprite3DTest/outline.material");
800 material->setTechnique("outline_noneskinned");
801 sprite->setMaterial(material);
802 sprite->setScale(6.f);
803 Director::getInstance()->getTextureCache()->removeUnusedTextures();
804
805 //add to scene
806 addChild(sprite);
807
808 sprite->setPosition(Vec2(p.x, p.y));
809
810 ActionInterval* action;
811 float random = CCRANDOM_0_1();
812
813 if (random < 0.20)
814 action = ScaleBy::create(3, 2);
815 else if (random < 0.40)
816 action = RotateBy::create(3, 360);
817 else if (random < 0.60)
818 action = Blink::create(1, 3);
819 else if (random < 0.8)
820 action = TintBy::create(2, 0, -255, -255);
821 else
822 action = FadeOut::create(2);
823 auto action_back = action->reverse();
824 auto seq = Sequence::create(action, action_back, nullptr);
825
826 sprite->runAction(RepeatForever::create(seq));
827 _sprites.push_back(sprite);
828}
829
830void Sprite3DEffectTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
831{
832 for (auto touch : touches)
833 {
834 auto location = touch->getLocation();
835
836 addNewSpriteWithCoords(location);
837 }
838}
839
841{
842 _paths.push_back("Sprite3DTest/girl.c3b");
843 _paths.push_back("Sprite3DTest/orc.c3b");
844 _paths.push_back("Sprite3DTest/ReskinGirl.c3b");
845 _paths.push_back("Sprite3DTest/axe.c3b");
846
847 TTFConfig ttfConfig("fonts/arial.ttf", 15);
848 auto label1 = Label::createWithTTF(ttfConfig, "AsyncLoad Sprite3D");
849 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite, this));
850
851 auto s = Director::getInstance()->getWinSize();
852 item1->setPosition(s.width * .5f, s.height * .8f);
853
854 auto pMenu1 = Menu::create(item1, nullptr);
855 pMenu1->setPosition(Vec2(0.0f, 0.0f));
856 this->addChild(pMenu1, 10);
857
858 auto node = Node::create();
859 node->setTag(101);
860 this->addChild(node);
861
863}
864
866{
867}
868
870{
871 return "Testing Sprite3D::createAsync";
872}
874{
875 return "";
876}
877
879{
880 //Note that you must stop the tasks before leaving the scene.
881 AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO);
882
883 auto node = getChildByTag(101);
884 node->removeAllChildren(); //remove all loaded sprite
885
886 //remove cache data
887 Sprite3DCache::getInstance()->removeAllSprite3DData();
888 long index = 0;
889 for (const auto& path : _paths) {
890 Sprite3D::createAsync(path, CC_CALLBACK_2(AsyncLoadSprite3DTest::asyncLoad_Callback, this), (void*)index++);
891 }
892}
893
894void AsyncLoadSprite3DTest::asyncLoad_Callback(Sprite3D* sprite, void* param)
895{
896 long index = (long)param;
897 auto node = getChildByTag(101);
898 auto s = Director::getInstance()->getWinSize();
899 float width = s.width / _paths.size();
900 Vec2 point(width * (0.5f + index), s.height / 2.f);
901 sprite->setPosition(point);
902 node->addChild(sprite);
903}
904
905
907{
908 auto listener = EventListenerTouchAllAtOnce::create();
909 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this);
910 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
911
912 // switch animation quality. In fact, you can set the sprite3d out of frustum to Animate3DQuality::QUALITY_NONE, it can save a lot of cpu time
913 MenuItemFont::setFontName("fonts/arial.ttf");
914 MenuItemFont::setFontSize(15);
915 _animateQuality = (int)Animate3DQuality::QUALITY_LOW;
917 _menuItem->setColor(Color3B(0, 200, 20));
918 auto menu = Menu::create(_menuItem, NULL);
919 menu->setPosition(Vec2::ZERO);
920 _menuItem->setPosition(VisibleRect::left().x + 50, VisibleRect::top().y - 70);
921 addChild(menu, 1);
922
923 _sprits.clear();
924
925 auto s = Director::getInstance()->getWinSize();
926 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
927}
929{
930 return "Testing Sprite3D";
931}
933{
934 return "Tap screen to add more sprite3D";
935}
936
938{
939 std::string fileName = "Sprite3DTest/orc.c3b";
940 auto sprite = Sprite3D::create(fileName);
941 sprite->setScale(3);
942 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
943 sprite->setPosition(Vec2(p.x, p.y));
944 addChild(sprite);
945 _sprits.push_back(sprite);
946
947 auto animation = Animation3D::create(fileName);
948 if (animation)
949 {
950 auto animate = Animate3D::create(animation);
951 bool inverse = (std::rand() % 3 == 0);
952
953 int rand2 = std::rand();
954 float speed = 1.0f;
955 if (rand2 % 3 == 1)
956 {
957 speed = animate->getSpeed() + CCRANDOM_0_1();
958 }
959 else if (rand2 % 3 == 2)
960 {
961 speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1();
962 }
963 animate->setSpeed(inverse ? -speed : speed);
964 animate->setTag(110);
965 animate->setQuality((Animate3DQuality)_animateQuality);
966 auto repeat = RepeatForever::create(animate);
967 repeat->setTag(110);
968 sprite->runAction(repeat);
969 }
970}
971
973{
974 if (_animateQuality == (int)Animate3DQuality::QUALITY_NONE)
975 return "None Quality";
976 else if (_animateQuality == (int)Animate3DQuality::QUALITY_LOW)
977 return "Low Quality";
978 else if (_animateQuality == (int)Animate3DQuality::QUALITY_HIGH)
979 return "High Quality";
980
981 return "";
982}
983
985{
987 if (_animateQuality > (int)Animate3DQuality::QUALITY_HIGH)
988 _animateQuality = (int)Animate3DQuality::QUALITY_NONE;
989
991
992 for (auto iter : _sprits)
993 {
994 RepeatForever* repAction = dynamic_cast<RepeatForever*>(iter->getActionByTag(110));
995 Animate3D* animate3D = dynamic_cast<Animate3D*>(repAction->getInnerAction());
996 animate3D->setQuality((Animate3DQuality)_animateQuality);
997 }
998}
999
1000void Sprite3DWithSkinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
1001{
1002 for (auto touch : touches)
1003 {
1004 auto location = touch->getLocation();
1005
1006 addNewSpriteWithCoords(location);
1007 }
1008}
1009
1011{
1012 auto listener = EventListenerTouchAllAtOnce::create();
1013 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinOutlineTest::onTouchesEnded, this);
1014 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
1015
1016 auto s = Director::getInstance()->getWinSize();
1017 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
1018
1019#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
1020 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
1021 [this](EventCustom*)
1022 {
1023 auto material = Sprite3DMaterial::createWithFilename("Sprite3DTest/outline.material");
1024 material->setTechnique("outline_skinned");
1025 for (auto& sprite : _sprites)
1026 {
1027 sprite->setMaterial(material->clone());
1028 }
1029 }
1030 );
1031 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
1032#endif
1033}
1035{
1036#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
1037 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
1038#endif
1039}
1041{
1042 return "Testing Sprite3D for skinned outline";
1043}
1045{
1046 return "Tap screen to add more sprite3D";
1047}
1048
1050{
1051
1052 std::string fileName = "Sprite3DTest/orc.c3b";
1053 auto sprite = Sprite3D::create(fileName);
1054
1055 auto material = Sprite3DMaterial::createWithFilename("Sprite3DTest/outline.material");
1056 material->setTechnique("outline_skinned");
1057 sprite->setMaterial(material);
1058
1059 sprite->setScale(3);
1060 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
1061 addChild(sprite);
1062 sprite->setPosition(Vec2(p.x, p.y));
1063
1064 auto animation = Animation3D::create(fileName);
1065 if (animation)
1066 {
1067 auto animate = Animate3D::create(animation);
1068 bool inverse = (std::rand() % 3 == 0);
1069
1070 int rand2 = std::rand();
1071 float speed = 1.0f;
1072 if (rand2 % 3 == 1)
1073 {
1074 speed = animate->getSpeed() + CCRANDOM_0_1();
1075 }
1076 else if (rand2 % 3 == 2)
1077 {
1078 speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1();
1079 }
1080 animate->setSpeed(inverse ? -speed : speed);
1081
1082 sprite->runAction(RepeatForever::create(animate));
1083 }
1084}
1085
1086void Sprite3DWithSkinOutlineTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
1087{
1088 for (auto touch : touches)
1089 {
1090 auto location = touch->getLocation();
1091
1092 addNewSpriteWithCoords(location);
1093 }
1094}
1095
1097 : _sprite(nullptr)
1098 , _swim(nullptr)
1099 , _hurt(nullptr)
1100 , _elapseTransTime(0.f)
1101 , _moveAction(nullptr)
1102{
1103 addSprite3D();
1104
1105 auto listener = EventListenerTouchAllAtOnce::create();
1106 listener->onTouchesEnded = CC_CALLBACK_2(Animate3DTest::onTouchesEnded, this);
1107 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
1108
1109 scheduleUpdate();
1110}
1111
1113{
1114 CC_SAFE_RELEASE(_moveAction);
1115 CC_SAFE_RELEASE(_hurt);
1116 CC_SAFE_RELEASE(_swim);
1117}
1118
1119std::string Animate3DTest::title() const
1120{
1121 return "Testing Animate3D";
1122}
1123
1124std::string Animate3DTest::subtitle() const
1125{
1126 return "Touch to beat the tortoise";
1127}
1128
1130{
1132 {
1133 _elapseTransTime += dt;
1134
1135 if (_elapseTransTime >= Animate3D::getTransitionTime())
1136 {
1137 _sprite->stopAction(_hurt);
1139 }
1140 }
1141 else if (_state == State::SWIMMING_TO_HURT)
1142 {
1143 _elapseTransTime += dt;
1144 if (_elapseTransTime >= Animate3D::getTransitionTime())
1145 {
1146 _sprite->stopAction(_swim);
1148 }
1149 }
1150}
1151
1153{
1154 std::string fileName = "Sprite3DTest/tortoise.c3b";
1155 auto sprite = Sprite3D::create(fileName);
1156 sprite->setScale(0.1f);
1157 auto s = Director::getInstance()->getWinSize();
1158 sprite->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
1159 addChild(sprite);
1160 _sprite = sprite;
1161 auto animation = Animation3D::create(fileName);
1162 if (animation)
1163 {
1164 auto animate = Animate3D::create(animation, 0.f, 1.933f);
1165 _swim = RepeatForever::create(animate);
1166 sprite->runAction(_swim);
1167
1168 _swim->retain();
1169 _hurt = Animate3D::create(animation, 1.933f, 2.8f);
1170 _hurt->retain();
1172 }
1173
1174 _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
1175 _moveAction->retain();
1176 auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr);
1177 seq->setTag(100);
1178 sprite->runAction(seq);
1179}
1180
1182{
1183 auto s = Director::getInstance()->getWinSize();
1184 _sprite->stopActionByTag(100);
1185 auto inverse = MoveTo::create(4.f, Vec2(s.width - _sprite->getPositionX(), s.height / 2.f));
1186 inverse->retain();
1187 _moveAction->release();
1188 _moveAction = inverse;
1189 auto rot = RotateBy::create(1.f, Vec3(0.f, 180.f, 0.f));
1190 auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr);
1191 seq->setTag(100);
1192 _sprite->runAction(seq);
1193}
1194
1196{
1197 //rerun swim action
1198 _sprite->runAction(_swim);
1200 _elapseTransTime = 0.0f;
1201}
1202
1203void Animate3DTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
1204{
1205 for (auto touch : touches)
1206 {
1207 auto location = touch->getLocation();
1208
1209 if (_sprite)
1210 {
1211 float len = (_sprite->getPosition() - location).length();
1212 if (len < 40)
1213 {
1214 //hurt the tortoise
1215 if (_state == State::SWIMMING)
1216 {
1217 _elapseTransTime = 0.0f;
1219 _sprite->stopAction(_hurt);
1220 _sprite->runAction(_hurt);
1221 auto delay = DelayTime::create(_hurt->getDuration() - Animate3D::getTransitionTime());
1222 auto seq = Sequence::create(delay, CallFunc::create(CC_CALLBACK_0(Animate3DTest::renewCallBack, this)), nullptr);
1223 seq->setTag(101);
1224 _sprite->runAction(seq);
1225 }
1226 return;
1227 }
1228 }
1229 }
1230}
1231
1233 : _hasWeapon(false)
1234 , _sprite(nullptr)
1235{
1236 auto s = Director::getInstance()->getWinSize();
1237 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
1238
1239 auto listener = EventListenerTouchAllAtOnce::create();
1240 listener->onTouchesEnded = CC_CALLBACK_2(AttachmentTest::onTouchesEnded, this);
1241 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
1242}
1243std::string AttachmentTest::title() const
1244{
1245 return "Testing Sprite3D Attachment";
1246}
1247std::string AttachmentTest::subtitle() const
1248{
1249 return "touch to switch weapon";
1250}
1251
1253{
1254 std::string fileName = "Sprite3DTest/orc.c3b";
1255 auto sprite = Sprite3D::create(fileName);
1256 sprite->setScale(5);
1257 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
1258 addChild(sprite);
1259 sprite->setPosition(Vec2(p.x, p.y));
1260
1261 //test attach
1262 auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
1263 sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
1264
1265 auto animation = Animation3D::create(fileName);
1266 if (animation)
1267 {
1268 auto animate = Animate3D::create(animation);
1269
1270 sprite->runAction(RepeatForever::create(animate));
1271 }
1272 _sprite = sprite;
1273 _hasWeapon = true;
1274}
1275
1276void AttachmentTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
1277{
1278 if (_hasWeapon)
1279 {
1280 _sprite->removeAllAttachNode();
1281 }
1282 else
1283 {
1284 auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
1285 _sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
1286 }
1288}
1289
1291 : _sprite(nullptr)
1292{
1293 auto s = Director::getInstance()->getWinSize();
1294 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
1295
1296 auto listener = EventListenerTouchAllAtOnce::create();
1297 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this);
1298 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
1299 TTFConfig ttfConfig("fonts/arial.ttf", 20);
1300 auto label1 = Label::createWithTTF(ttfConfig, "Hair");
1301 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin, this));
1302 auto label2 = Label::createWithTTF(ttfConfig, "Glasses");
1303 auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin, this));
1304 auto label3 = Label::createWithTTF(ttfConfig, "Coat");
1305 auto item3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin, this));
1306 auto label4 = Label::createWithTTF(ttfConfig, "Pants");
1307 auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin, this));
1308 auto label5 = Label::createWithTTF(ttfConfig, "Shoes");
1309 auto item5 = MenuItemLabel::create(label5, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin, this));
1310 item1->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 4));
1311 item2->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 5));
1312 item3->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 6));
1313 item4->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 7));
1314 item5->setPosition(Vec2(VisibleRect::left().x + 50, VisibleRect::bottom().y + item1->getContentSize().height * 8));
1315 item1->setUserData((void*)SkinType::HAIR);
1316 item2->setUserData((void*)SkinType::GLASSES);
1317 item3->setUserData((void*)SkinType::UPPER_BODY);
1318 item4->setUserData((void*)SkinType::PANTS);
1319 item5->setUserData((void*)SkinType::SHOES);
1320 auto pMenu1 = Menu::create(item1, item2, item3, item4, item5, nullptr);
1321 pMenu1->setPosition(Vec2(0.0f, 0.0f));
1322 this->addChild(pMenu1, 10);
1323
1324}
1326{
1327 long index = (long)(((MenuItemLabel*)sender)->getUserData());
1328 if (index < (int)SkinType::MAX_TYPE)
1329 {
1330 _curSkin[index] = (_curSkin[index] + 1) % _skins[index].size();
1331 applyCurSkin();
1332 }
1333}
1334
1335std::string Sprite3DReskinTest::title() const
1336{
1337 return "Testing Sprite3D Reskin";
1338}
1340{
1341 return "";
1342}
1343
1345{
1346 std::string fileName = "Sprite3DTest/ReskinGirl.c3b";
1347 auto sprite = Sprite3D::create(fileName);
1348 sprite->setScale(4);
1349 sprite->setRotation3D(Vec3(0.0f, 0.0f, 0.0f));
1350 addChild(sprite);
1351 sprite->setPosition(Vec2(p.x, p.y - 60));
1352 auto animation = Animation3D::create(fileName);
1353 if (animation)
1354 {
1355 auto animate = Animate3D::create(animation);
1356
1357 sprite->runAction(RepeatForever::create(animate));
1358 }
1359 _sprite = sprite;
1360
1361 auto& body = _skins[(int)SkinType::UPPER_BODY];
1362 body.push_back("Girl_UpperBody01");
1363 body.push_back("Girl_UpperBody02");
1364
1365 auto& pants = _skins[(int)SkinType::PANTS];
1366 pants.push_back("Girl_LowerBody01");
1367 pants.push_back("Girl_LowerBody02");
1368
1369 auto& shoes = _skins[(int)SkinType::SHOES];
1370 shoes.push_back("Girl_Shoes01");
1371 shoes.push_back("Girl_Shoes02");
1372
1373 auto& hair = _skins[(int)SkinType::HAIR];
1374 hair.push_back("Girl_Hair01");
1375 hair.push_back("Girl_Hair02");
1376
1377 auto& face = _skins[(int)SkinType::FACE];
1378 face.push_back("Girl_Face01");
1379 face.push_back("Girl_Face02");
1380
1381 auto& hand = _skins[(int)SkinType::HAND];
1382 hand.push_back("Girl_Hand01");
1383 hand.push_back("Girl_Hand02");
1384
1385 auto& glasses = _skins[(int)SkinType::GLASSES];
1386 glasses.push_back("");
1387 glasses.push_back("Girl_Glasses01");
1388
1389 memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int));
1390
1391 applyCurSkin();
1392}
1393
1394void Sprite3DReskinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
1395{
1396}
1397
1399{
1400 for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) {
1401 auto mesh = _sprite->getMeshByIndex(static_cast<int>(i));
1402 bool isVisible = false;
1403 for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) {
1404 if (mesh->getName() == _skins[j].at(_curSkin[j]))
1405 {
1406 isVisible = true;
1407 break;
1408 }
1409 }
1410 _sprite->getMeshByIndex(static_cast<int>(i))->setVisible(isVisible);
1411 }
1412}
1413
1415{
1416 auto listener = EventListenerTouchAllAtOnce::create();
1417 listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DWithOBBPerformanceTest::onTouchesBegan, this);
1418 listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithOBBPerformanceTest::onTouchesEnded, this);
1419 listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DWithOBBPerformanceTest::onTouchesMoved, this);
1420 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
1421 auto s = Director::getInstance()->getWinSize();
1422 initDrawBox();
1423
1424 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
1425 MenuItemFont::setFontName("fonts/arial.ttf");
1426 MenuItemFont::setFontSize(65);
1427 auto decrease = MenuItemFont::create(" - ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::delOBBCallback, this));
1428 decrease->setColor(Color3B(0, 200, 20));
1429 auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::addOBBCallback, this));
1430 increase->setColor(Color3B(0, 200, 20));
1431
1432 auto menu = Menu::create(decrease, increase, nullptr);
1433 menu->alignItemsHorizontally();
1434 menu->setPosition(Vec2(s.width / 2, s.height - 65));
1435 addChild(menu, 1);
1436
1437 TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
1438 _labelCubeCount = Label::createWithTTF(ttfCount, "0 cubes");
1439 _labelCubeCount->setColor(Color3B(0, 200, 20));
1440 _labelCubeCount->setPosition(Vec2(s.width / 2, s.height - 90));
1441 addChild(_labelCubeCount);
1442 _hasCollider = false;
1443 addOBBCallback(nullptr);
1444 scheduleUpdate();
1445}
1447{
1448 return "OBB Collision Performance Test";
1449}
1451{
1452 return "";
1453}
1455{
1456 Vec3 extents = Vec3(10, 10, 10);
1457 AABB aabb(-extents, extents);
1458 auto obb = OBB(aabb);
1459 obb._center = Vec3(p.x, p.y, 0);
1460 _obb.push_back(obb);
1461}
1462
1463void Sprite3DWithOBBPerformanceTest::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
1464{
1465 for (const auto& touch : touches)
1466 {
1467 auto location = touch->getLocationInView();
1468 auto obbSize = _obb.size();
1469 if (obbSize)
1470 {
1471 _intersetList.clear();
1472 Ray ray;
1473 calculateRayByLocationInView(&ray, location);
1474 for (decltype(obbSize) i = 0; i < obbSize; i++)
1475 {
1476 if (ray.intersects(_obb[i]))
1477 {
1478 _intersetList.insert((int)i);
1479 return;
1480 }
1481 }
1482 }
1483 }
1484}
1485
1486void Sprite3DWithOBBPerformanceTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
1487{
1488
1489}
1490
1491void Sprite3DWithOBBPerformanceTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
1492{
1493 for (const auto& touch : touches)
1494 {
1495 auto location = touch->getLocation();
1496 auto obbSize = _obb.size();
1497
1498 for (decltype(obbSize) i = 0; i < obbSize; i++)
1499 {
1500 if (_intersetList.find((int)i) != _intersetList.end())
1501 _obb[i]._center = Vec3(location.x, location.y, 0);
1502 }
1503 }
1504}
1505
1507{
1508 char szText[16];
1509 sprintf(szText, "%lu cubes", static_cast<unsigned long>(_obb.size()));
1510 _labelCubeCount->setString(szText);
1511
1512 if (_drawDebug)
1513 {
1514 _drawDebug->clear();
1515
1516 Mat4 mat = _sprite->getNodeToWorldTransform();
1517
1518 _obbt = _obbtOri;
1519 _obbt.transform(mat);
1520
1521 Vec3 corners[8] = {};
1522 _obbt.getCorners(corners);
1523 _drawDebug->drawCube(corners, Color4F(0, 0, 1, 1));
1524 }
1525 if (_obb.size() > 0)
1526 {
1527 _drawOBB->clear();
1528 auto obbSize = _obb.size();
1529 for (decltype(obbSize) i = 0; i < obbSize; i++)
1530 {
1531 Vec3 corners[8] = {};
1532 _obb[i].getCorners(corners);
1533 _drawOBB->drawCube(corners, _obbt.intersects(_obb[i]) ? Color4F(1, 0, 0, 1) : Color4F(0, 1, 0, 1));
1534 }
1535 }
1536}
1537
1539{
1541 addChild(_drawOBB);
1542}
1543
1545{
1546 std::string fileName = "Sprite3DTest/tortoise.c3b";
1547 auto sprite = Sprite3D::create(fileName);
1548 AABB aabb = sprite->getAABB();
1549 _obbt = OBB(aabb);
1550 _obbtOri = _obbt;
1551
1552 sprite->setScale(0.1f);
1553 auto s = Director::getInstance()->getWinSize();
1554 sprite->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
1555 addChild(sprite);
1556 _sprite = sprite;
1557 auto animation = Animation3D::create(fileName);
1558 if (animation)
1559 {
1560 auto animate = Animate3D::create(animation, 0.f, 1.933f);
1561 sprite->runAction(RepeatForever::create(animate));
1562 }
1563
1564 _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
1565 _moveAction->retain();
1566 auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr);
1567 seq->setTag(100);
1568 sprite->runAction(seq);
1569
1570
1571
1573 addChild(_drawDebug);
1574}
1575
1577{
1578 auto s = Director::getInstance()->getWinSize();
1579 _sprite->stopActionByTag(100);
1580 auto inverse = MoveTo::create(4.f, Vec2(s.width - _sprite->getPositionX(), s.height / 2.f));
1581 inverse->retain();
1582 _moveAction->release();
1583 _moveAction = inverse;
1584 auto rot = RotateBy::create(1.0f, Vec3(0.f, 180.f, 0.f));
1585 auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr);
1586 seq->setTag(100);
1587 _sprite->runAction(seq);
1588}
1589
1591{
1592 addOBBWithCount(10);
1593}
1594
1596{
1597 for (int i = 0; i < value; i++)
1598 {
1599 Vec2 randompos = Vec2(CCRANDOM_0_1() * Director::getInstance()->getWinSize().width, CCRANDOM_0_1() * Director::getInstance()->getWinSize().height);
1600 Vec3 extents = Vec3(10, 10, 10);
1601 AABB aabb(-extents, extents);
1602 auto obb = OBB(aabb);
1603 obb._center = Vec3(randompos.x, randompos.y, 0);
1604 _obb.push_back(obb);
1605 }
1606}
1607
1609{
1610 delOBBWithCount(10);
1611}
1612
1614{
1615 if (_obb.size() >= 10)
1616 {
1617 _obb.erase(_obb.begin(), _obb.begin() + value);
1618 _drawOBB->clear();
1619 }
1620 else
1621 return;
1622}
1623void Sprite3DWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst)
1624{
1625 assert(dst);
1626
1627 assert(viewport->width != 0.0f && viewport->height != 0.0f);
1628 Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f);
1629
1630 screen.x = screen.x * 2.0f - 1.0f;
1631 screen.y = screen.y * 2.0f - 1.0f;
1632 screen.z = screen.z * 2.0f - 1.0f;
1633
1634 viewProjection.getInversed().transformVector(screen, &screen);
1635
1636 if (screen.w != 0.0f)
1637 {
1638 screen.x /= screen.w;
1639 screen.y /= screen.w;
1640 screen.z /= screen.w;
1641 }
1642
1643 dst->set(screen.x, screen.y, screen.z);
1644}
1645
1647{
1648 auto dir = Director::getInstance();
1649 auto view = dir->getWinSize();
1650 auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
1651
1652 Vec3 src = Vec3(location.x, location.y, -1);
1653 Vec3 nearPoint;
1654 unproject(mat, &view, &src, &nearPoint);
1655
1656 src = Vec3(location.x, location.y, 1);
1657 Vec3 farPoint;
1658 unproject(mat, &view, &src, &farPoint);
1659
1660 Vec3 direction;
1661 Vec3::subtract(farPoint, nearPoint, &direction);
1662 direction.normalize();
1663
1664 ray->_origin = nearPoint;
1665 ray->_direction = direction;
1666}
1667
1669 : _sprite(nullptr)
1670 , _mirrorSprite(nullptr)
1671{
1672 auto s = Director::getInstance()->getWinSize();
1673 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
1674}
1675std::string Sprite3DMirrorTest::title() const
1676{
1677 return "Sprite3D Mirror Test";
1678}
1680{
1681 return "";
1682}
1683
1685{
1686 std::string fileName = "Sprite3DTest/orc.c3b";
1687 auto sprite = Sprite3D::create(fileName);
1688 sprite->setScale(5);
1689 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
1690 addChild(sprite);
1691 sprite->setPosition(Vec2(p.x - 80, p.y));
1692
1693 //test attach
1694 auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
1695 sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
1696
1697 auto animation = Animation3D::create(fileName);
1698 if (animation)
1699 {
1700 auto animate = Animate3D::create(animation);
1701
1702 sprite->runAction(RepeatForever::create(animate));
1703 }
1704 _sprite = sprite;
1705 _hasWeapon = true;
1706
1707 //create mirror Sprite3D
1708 sprite = Sprite3D::create(fileName);
1709 sprite->setScale(5);
1710 sprite->setScaleX(-5);
1711 sprite->setCullFace(CullFaceSide::FRONT);
1712 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
1713 addChild(sprite);
1714 sprite->setPosition(Vec2(p.x + 80, p.y));
1715
1716 //test attach
1717 sp = Sprite3D::create("Sprite3DTest/axe.c3b");
1718 sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
1719
1720 animation = Animation3D::create(fileName);
1721 if (animation)
1722 {
1723 auto animate = Animate3D::create(animation);
1724
1725 sprite->runAction(RepeatForever::create(animate));
1726 }
1727 _mirrorSprite = sprite;
1728}
1729
1731 : _arcSpeed(CC_DEGREES_TO_RADIANS(90))
1732 , _radius(100.f)
1733 , _accAngle(0.f)
1734{
1735 auto s = Director::getInstance()->getWinSize();
1736 addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f));
1737 scheduleUpdate();
1738}
1739std::string QuaternionTest::title() const
1740{
1741 return "Test Rotation With Quaternion";
1742}
1743std::string QuaternionTest::subtitle() const
1744{
1745 return "";
1746}
1747
1749{
1750 std::string fileName = "Sprite3DTest/tortoise.c3b";
1751 auto sprite = Sprite3D::create(fileName);
1752 sprite->setScale(0.1f);
1753 auto s = Director::getInstance()->getWinSize();
1754 sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle)));
1755 addChild(sprite);
1756 _sprite = sprite;
1757 auto animation = Animation3D::create(fileName);
1758 if (animation)
1759 {
1760 auto animate = Animate3D::create(animation, 0.f, 1.933f);
1761 sprite->runAction(RepeatForever::create(animate));
1762 }
1763}
1764
1765void QuaternionTest::update(float delta)
1766{
1767 _accAngle += delta * _arcSpeed;
1768 const float pi = (float)M_PI;
1769 if (_accAngle >= 2 * pi)
1770 _accAngle -= 2 * pi;
1771
1772 auto s = Director::getInstance()->getWinSize();
1773 _sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle)));
1774
1775 Quaternion quat;
1776 Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat);
1777 _sprite->setRotationQuat(quat);
1778}
1779
1781 : _caseIdx(0)
1782{
1783 auto s = Director::getInstance()->getWinSize();
1784
1785 _useCaseTitles[0] = "transparent 3d sprite and 2d sprite";
1786 _useCaseTitles[1] = "ui - 3d - ui";
1787
1788 auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png",
1789 [&](Ref *sender) {
1790 _caseIdx--;
1791 if (_caseIdx < 0)
1793 this->switchCase();
1794 });
1795
1796 auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png",
1797 [&](Ref *sender) {
1798 _caseIdx++;
1799 if (_caseIdx >= (int)USECASE::MAX_CASE_NUM)
1800 _caseIdx = 0;
1801 this->switchCase();
1802 });
1803
1804 auto menu = Menu::create(itemPrev, itemNext, nullptr);
1805 menu->alignItemsHorizontally();
1806 menu->setScale(0.5);
1807 menu->setAnchorPoint(Vec2(0.0f, 0.0f));
1808 menu->setPosition(Vec2(s.width / 2, 70.0f));
1809
1810 _label = Label::create();
1811 _label->setPosition(s.width * 0.5f, s.height * 0.8f);
1812 addChild(_label);
1813
1814 addChild(menu);
1815
1816 //setup camera
1817 auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
1818 camera->setCameraFlag(CameraFlag::USER1);
1819 camera->setPosition3D(Vec3(0.f, 30.f, 100.f));
1820 camera->lookAt(Vec3(0.f, 0.f, 0.f));
1821 addChild(camera);
1822
1823 switchCase();
1824}
1825
1826std::string UseCaseSprite3D::title() const
1827{
1828 return "Use Case For 2D + 3D";
1829}
1830
1831std::string UseCaseSprite3D::subtitle() const
1832{
1833 return "";
1834}
1835
1837{
1838 removeChildByTag(101);
1839
1840 auto s = Director::getInstance()->getWinSize();
1841 _label->setString(_useCaseTitles[_caseIdx]);
1842 if (_caseIdx == 0) // use case 1, 3d transparent sprite + 2d sprite
1843 {
1844 std::string filename = "Sprite3DTest/girl.c3b";
1845 auto sprite = Sprite3D::create(filename);
1846 sprite->setScale(0.15f);
1847 auto animation = Animation3D::create(filename);
1848 if (animation)
1849 {
1850 auto animate = Animate3D::create(animation);
1851 sprite->runAction(RepeatForever::create(animate));
1852 }
1853
1854 auto circleBack = Sprite3D::create();
1855 auto circle = Sprite::create("Sprite3DTest/circle.png");
1856 circleBack->setScale(0.5f);
1857 circleBack->addChild(circle);
1858 circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f))));
1859
1860 circleBack->setRotation3D(Vec3(-90.0f, -90.0f, 0.0f));
1861
1862 auto pos = sprite->getPosition3D();
1863 circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1));
1864
1865 sprite->setOpacity(250);
1866 sprite->setCameraMask(2);
1867 circleBack->setCameraMask(2);
1868 sprite->setTag(3);
1869 circleBack->setTag(2);
1870
1871 auto node = Node::create();
1872 node->addChild(sprite);
1873 node->addChild(circleBack);
1874 node->setTag(101);
1875 addChild(node);
1876
1877 scheduleUpdate();
1878 update(0.f);
1879 }
1880 else if (_caseIdx == 1) // use case 2, ui - 3d - ui, last ui should on the top
1881 {
1882 auto layer = LayerColor::create(Color4B(0, 0, 100, 255), s.width / 2.f, s.height / 2.f);
1883 layer->setPosition(s.width * 0.25f, s.height * 0.25f);
1884 layer->setGlobalZOrder(-1);
1885 addChild(layer);
1886
1887 std::string filename = "Sprite3DTest/girl.c3b";
1888 auto sprite = Sprite3D::create(filename);
1889 sprite->setScale(0.5f);
1890 auto animation = Animation3D::create(filename);
1891 if (animation)
1892 {
1893 auto animate = Animate3D::create(animation);
1894 sprite->runAction(RepeatForever::create(animate));
1895 }
1896 sprite->setPosition(s.width * 0.25f, s.height * 0.125f);
1897 layer->addChild(sprite);
1898
1899 TTFConfig ttfConfig("fonts/arial.ttf", 15);
1900 auto label1 = Label::createWithTTF(ttfConfig, "Message");
1901 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message, this));
1902 auto label2 = Label::createWithTTF(ttfConfig, "Message");
1903 auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message, this));
1904
1905 item1->setPosition(Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height));
1906 item2->setPosition(Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f));
1907
1908 auto pMenu1 = Menu::create(item1, item2, nullptr);
1909 pMenu1->setPosition(Vec2(0.0f, 0.0f));
1910 layer->addChild(pMenu1);
1911
1912 layer->setTag(101);
1913 }
1914}
1915
1917{
1918 auto layer = getChildByTag(101);
1919 auto message = layer->getChildByTag(102); // message layer
1920 if (message)
1921 layer->removeChild(message);
1922 else
1923 {
1924 // create a new message layer on the top
1925 auto s = layer->getContentSize();
1926 auto messagelayer = LayerColor::create(Color4B(100, 100, 0, 255));
1927 messagelayer->setContentSize(Size(s.width * 0.5f, s.height * 0.5f));
1928 messagelayer->setPosition(Vec2(s.width * 0.25f, s.height * 0.25f));
1929 auto label = Label::create();
1930 label->setString("This Message Layer \n Should Be On Top");
1931 label->setPosition(Vec2(s.width * 0.25f, s.height * 0.25f));
1932 messagelayer->addChild(label);
1933 messagelayer->setTag(102);
1934 layer->addChild(messagelayer);
1935 }
1936}
1937
1939{
1940 if (_caseIdx == 0)
1941 {
1942 static float accAngle = 0.f;
1943 accAngle += delta * CC_DEGREES_TO_RADIANS(60);
1944
1945 float radius = 30.f;
1946 float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius;
1947
1948 auto node = getChildByTag(101);
1949 auto sprite3d = node->getChildByTag(3);
1950 auto circle = node->getChildByTag(2);
1951
1952 sprite3d->setPositionX(x);
1953 sprite3d->setPositionZ(z);
1954 circle->setPositionX(x);
1955 circle->setPositionZ(z);
1956 }
1957}
1958
1960// Node Frame Animation
1962 :_vectorIndex(0)
1963{
1964 auto s = Director::getInstance()->getWinSize();
1965
1966 auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png",
1967 [&](Ref *sender) {
1968 _sprites[_vectorIndex]->setVisible(false);
1969
1970 int tIndex = _vectorIndex - 1;
1971 if (tIndex < 0)
1972 _vectorIndex = (int)_sprites.size() - 1;
1973 else
1974 _vectorIndex--;
1975
1976 _sprites[_vectorIndex]->setVisible(true);
1977 });
1978
1979 auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png",
1980 [&](Ref *sender) {
1981 _sprites[_vectorIndex]->setVisible(false);
1982
1983 int tIndex = _vectorIndex + 1;
1984 if (tIndex >= static_cast<int>(_sprites.size()))
1985 _vectorIndex = 0;
1986 else
1987 _vectorIndex++;
1988
1989 _sprites[_vectorIndex]->setVisible(true);
1990 });
1991
1992 auto menu = Menu::create(itemPrev, itemNext, nullptr);
1993 menu->alignItemsHorizontally();
1994 menu->setScale(0.5f);
1995 menu->setAnchorPoint(Vec2(0.0f, 0.0f));
1996 menu->setPosition(Vec2(s.width / 2, 70.0f));
1997 addChild(menu);
1998
1999 addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f));
2000}
2001std::string NodeAnimationTest::title() const
2002{
2003 return "Node Animation Test";
2004}
2006{
2007 return "Jumping animation";
2008}
2009
2011{
2012 auto s = Director::getInstance()->getWinSize();
2013
2014 // add jumping ball
2015 std::string fileName = "Sprite3DTest/ball.c3b";
2016 auto sprite = Sprite3D::create(fileName);
2017 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
2018 sprite->setScale(3);
2019 sprite->setPosition(Vec2(s.width / 2.f, s.height / 3.f));
2020 sprite->setTexture("Sprite3DTest/teapot.png");
2021
2022 auto light1 = PointLight::create(Vec3(s.width * 0.2f, s.height * 0.8f, 100.0f), Color3B(200, 200, 200), 10000.0f);
2023 addChild(light1);
2024 auto light2 = AmbientLight::create(Color3B(100, 100, 100));
2025 addChild(light2);
2026
2027 auto animation = Animation3D::create(fileName);
2028 if (animation)
2029 {
2030 auto animate = Animate3D::create(animation);
2031 auto act = RepeatForever::create(animate);
2032 act->setTag(0);
2033 sprite->runAction(act);
2034 }
2035 addChild(sprite);
2036 _sprites.push_back(sprite);
2037
2038 // add jumping orc
2039 fileName = "Sprite3DTest/orc_jump.c3t";
2040 sprite = Sprite3D::create(fileName);
2041 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
2042 sprite->setScale(3);
2043 sprite->setPosition(Vec2(s.width / 2.f, s.height / 3.f));
2044 sprite->setVisible(false);
2045 animation = Animation3D::create(fileName);
2046 if (animation)
2047 {
2048 auto animate = Animate3D::create(animation);
2049 auto act = RepeatForever::create(animate);
2050 act->setTag(0);
2051 sprite->runAction(act);
2052 }
2053 addChild(sprite);
2054 _sprites.push_back(sprite);
2055}
2056
2058 _textureCube(nullptr),
2059 _skyBox(nullptr),
2060 _teapot(nullptr)
2061{
2062 auto s = Director::getInstance()->getWinSize();
2063 addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
2064}
2065
2067{
2068#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2069 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
2070#endif
2071
2072 _teapot->release();
2073 _skyBox->release();
2074 _textureCube->release();
2075}
2076
2078{
2079 return "CubeMap & Skybox Test";
2080}
2081
2083{
2084 return "";
2085}
2086
2088{
2089 Size visibleSize = Director::getInstance()->getVisibleSize();
2090 _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 10, 1000);
2091 _camera->setPosition3D(Vec3(0.f, 0.f, 50.f));
2092 _camera->setCameraFlag(CameraFlag::USER1);
2093
2094 auto listener = EventListenerTouchAllAtOnce::create();
2095 listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DCubeMapTest::onTouchesMoved, this);
2096 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2097
2098 // create a teapot
2099 _teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
2100 _teapot->retain();
2101
2102 // create the second texture for cylinder
2103 _textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg",
2104 "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg",
2105 "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
2106
2107 _textureCube->retain();
2108
2109 //set texture parameters
2110 Texture2D::TexParams tRepeatParams;
2111 tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;
2112 tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
2113 tRepeatParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
2114 tRepeatParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
2115 _textureCube->setTexParameters(tRepeatParams);
2116
2117
2118 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/CubeMap.material");
2119 auto state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
2120 _teapot->setMaterial(mat);
2121
2122 // pass the texture sampler to our custom shader
2123 auto cubeTexLoc = state->getUniformLocation("u_cubeTex");
2124 state->setTexture(cubeTexLoc, 0, _textureCube->getBackendTexture());
2125
2126 _teapot->setPosition3D(Vec3(0.0f, -5.0f, 0.0f));
2127 _teapot->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
2128
2129 auto rotate_action = RotateBy::create(1.5, Vec3(0.0f, 30.0f, 0.0f));
2130 _teapot->runAction(RepeatForever::create(rotate_action));
2131
2132 addChild(_teapot);
2133
2134 {
2135 // config skybox
2136 _skyBox = Skybox::create();
2137 _skyBox->retain();
2138
2139 _skyBox->setTexture(_textureCube);
2140 addChild(_skyBox);
2141 }
2142
2143 addChild(_camera);
2144 setCameraMask(2);
2145#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2146 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
2147 [this](EventCustom*)
2148 {
2149 CC_SAFE_RELEASE(_textureCube);
2150 _textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg",
2151 "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg",
2152 "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
2153
2154 _textureCube->retain();
2155 //set texture parameters
2156 Texture2D::TexParams tRepeatParams;
2157 tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;
2158 tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
2159 tRepeatParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
2160 tRepeatParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
2161 _textureCube->setTexParameters(tRepeatParams);
2162
2163 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/CubeMap.material");
2164 auto state = mat->getTechniqueByIndex(0)->getPassByIndex(0)->getProgramState();
2165 _teapot->setMaterial(mat);
2166 _skyBox->setTexture(_textureCube);
2167 // pass the texture sampler to our custom shader
2168 auto cubeTexLoc = state->getUniformLocation("u_cubeTex");
2169 state->setTexture(cubeTexLoc, 0, _textureCube->getBackendTexture());
2170 }
2171 );
2172 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
2173#endif
2174}
2175
2176void Sprite3DCubeMapTest::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
2177{
2178 if (touches.size())
2179 {
2180 auto touch = touches[0];
2181 auto delta = touch->getDelta();
2182
2183 static float _angle = 0.f;
2184 _angle -= CC_DEGREES_TO_RADIANS(delta.x);
2185 _camera->setPosition3D(Vec3(50.0f * sinf(_angle), 0.0f, 50.0f * cosf(_angle)));
2186 _camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
2187 }
2188}
2189
2191{
2193
2194 auto s = Director::getInstance()->getWinSize();
2195 auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj");
2196 sprite->setScale(3.f);
2197 sprite->setTexture("Sprite3DTest/boss.png");
2198 addChild(sprite);
2199 sprite->setPosition(Vec2(s.width / 2, s.height / 2));
2200 _sprite = sprite;
2201
2202 TTFConfig ttfConfig("fonts/arial.ttf", 15);
2203 auto label1 = Label::createWithTTF(ttfConfig, "switch shader");
2204 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Issue9767::menuCallback_SwitchShader, this));
2205
2206 item1->setPosition(Vec2(s.width * 0.9f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height));
2207
2208 auto pMenu1 = Menu::create(item1, nullptr);
2209 pMenu1->setPosition(Vec2(0.0f, 0.0f));
2210 addChild(pMenu1);
2211}
2212
2214{
2215}
2216
2218{
2219 CC_SAFE_RELEASE_NULL(_programState);
2221 {
2223 auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_3D);
2224 _programState = new backend::ProgramState(program);
2225 }
2226 else
2227 {
2229 auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_3D);
2230 _programState = new backend::ProgramState(program);
2231 }
2232 _sprite->setProgramState(_programState);
2233}
2234
2235std::string Issue9767::title() const
2236{
2237 return "Issue9767: test setGLProgram";
2238}
2239
2240std::string Issue9767::subtitle() const
2241{
2242 return "";
2243}
2244
2246{
2247 auto size = Director::getInstance()->getWinSize();
2248 auto stencil = Sprite::create("Images/close.png");
2249 auto clipSprite3D = ClippingNode::create();
2250 clipSprite3D->setStencil(stencil);
2251 this->addChild(clipSprite3D);
2252 clipSprite3D->setScale(3.0f);
2253
2254 auto sprite3D = Sprite3D::create("Sprite3DTest/orc.c3b");
2255 sprite3D->setScale(1.0f);
2256 sprite3D->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
2257 clipSprite3D->addChild(sprite3D);//5
2258
2259 clipSprite3D->setPosition(Vec2(size.width / 2, size.height / 2));
2260
2261 auto seq = Sequence::create(ScaleTo::create(2.f, 3), ScaleTo::create(2.f, 0.5f), NULL);
2262 sprite3D->runAction(RepeatForever::create(seq));
2263 auto animation = Animation3D::create("Sprite3DTest/orc.c3b");
2264 auto animate = Animate3D::create(animation);
2265 sprite3D->runAction(RepeatForever::create(animate));
2266 sprite3D->setForce2DQueue(true);
2267}
2268
2270{
2271
2272}
2273
2275{
2276 return "Sprite3D Clipping Test";
2277}
2278
2280{
2281 return "";
2282}
2283
2285{
2286 FileUtils::getInstance()->addSearchPath("Particle3D/materials");
2287 FileUtils::getInstance()->addSearchPath("Particle3D/scripts");
2288
2289 auto s = Director::getInstance()->getWinSize();
2290 _sprite3d = Sprite3D::create("Sprite3DTest/ReskinGirl.c3b");
2291 _sprite3d->setPosition(Vec2(s.width / 2.0f, s.height / 3.0f));
2292 _sprite3d->setScale(3.0f);
2293 _sprite3d->setRotation3D(Vec3(0.0f, 90.0f, 0.0f));
2294 this->addChild(_sprite3d);
2295
2296 _sprite3d->getMeshByName("Girl_UpperBody02")->setVisible(false);
2297 _sprite3d->getMeshByName("Girl_LowerBody02")->setVisible(false);
2298 _sprite3d->getMeshByName("Girl_Shoes02")->setVisible(false);
2299 _sprite3d->getMeshByName("Girl_Hair02")->setVisible(false);
2300
2301 auto rootps = PUParticleSystem3D::create("explosionSystem.pu");
2302 rootps->stopParticleSystem();
2303 rootps->setScale(4.0f);
2304 this->addChild(rootps, 0, 100);
2305
2306 auto animation = Animation3D::create("Sprite3DTest/ReskinGirl.c3b");
2307 if (animation)
2308 {
2309 auto animate = Animate3D::create(animation);
2310 _sprite3d->runAction(RepeatForever::create(animate));
2311
2312 ValueMap valuemap0;
2313 animate->setKeyFrameUserInfo(275, valuemap0);
2314
2315 auto listener = EventListenerCustom::create(Animate3DDisplayedNotification, [&](EventCustom* event)
2316 {
2317 auto info = (Animate3D::Animate3DDisplayedEventInfo*)event->getUserData();
2318 auto node = getChildByTag(100);
2319 if (node)
2320 {
2321 auto mat = _sprite3d->getNodeToWorldTransform() * _sprite3d->getSkeleton()->getBoneByName("Bip01 R Hand")->getWorldMat();
2322 node->setPosition3D(Vec3(mat.m[12] + 100, mat.m[13], mat.m[14]));
2323 ((PUParticleSystem3D*)node)->startParticleSystem();
2324 }
2325
2326
2327 cocos2d::log("frame %d", info->frame);
2328 });
2329 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, -1);
2330 }
2331}
2332
2334{
2335}
2336
2338{
2339 return "Testing Animate3D Callback";
2340}
2341
2343{
2344 return "";
2345}
2346
2347
2349{
2350 auto s = Director::getInstance()->getWinSize();
2351
2352 auto sprite = Sprite3D::create("Sprite3DTest/box_VertexCol.c3t");
2353 sprite->setPosition(Vec2(0.0f, 0.0f));
2354 sprite->setScale(1.0f);
2355 sprite->setCameraMask(2);
2356 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/VertexColor.material");
2357 sprite->setMaterial(mat);
2358 sprite->runAction(RepeatForever::create(RotateBy::create(1.0f, Vec3(10.0f, 50.0f, 10.0f))));
2359
2360 this->addChild(sprite);
2361
2362 //setup camera
2363 auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
2364 camera->setCameraFlag(CameraFlag::USER1);
2365 camera->setPosition3D(Vec3(0.0f, 0.0f, 10.f));
2366 camera->lookAt(Vec3(0.f, 0.f, 0.f));
2367 addChild(camera);
2368
2369#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2370 _backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND,
2371 [=](EventCustom*)
2372 {
2373 auto mat = Sprite3DMaterial::createWithFilename("Sprite3DTest/VertexColor.material");
2374 sprite->setMaterial(mat);
2375 }
2376 );
2377 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
2378#endif
2379}
2380
2382{
2383#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2384 Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
2385#endif
2386}
2387
2389{
2390 return "Testing Vertex Color";
2391}
2392
2394{
2395 return "";
2396}
2397
2399{
2400 TTFConfig ttfConfig("fonts/arial.ttf", 20);
2401 auto label1 = Label::createWithTTF(ttfConfig,"Clear Mode");
2402 auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(CameraBackgroundClearTest::switch_CameraClearMode,this) );
2403
2404 item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) );
2405
2406 auto pMenu1 = Menu::create(item1, nullptr);
2407 pMenu1->setPosition(Vec2(0.0f,0.0f));
2408 this->addChild(pMenu1, 10);
2409
2410 //setup camera
2411 auto s = Director::getInstance()->getWinSize();
2412 _camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
2413 _camera->setCameraFlag(CameraFlag::USER1);
2414 _camera->setPosition3D(Vec3(0.f, 30.f, 100.f));
2415 _camera->lookAt(Vec3(0.f, 0.f, 0.f));
2416 addChild(_camera);
2417
2418 auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
2419 addChild(sprite);
2420 sprite->setCameraMask(2);
2421
2422 _label = Label::createWithTTF(ttfConfig, "Depth Clear Brush");
2423 addChild(_label);
2424 _label->setPosition(s.width / 2.f , VisibleRect::top().y * 0.8f);
2425}
2426
2428{
2429 auto brush = _camera->getBackgroundBrush();
2430 CameraBackgroundBrush::BrushType type = CameraBackgroundBrush::BrushType::NONE;
2431 if (!brush)
2432 {
2433 CCLOG("No brash found!");
2434 }
2435 else
2436 {
2437 type = brush->getBrushType();
2438 }
2439
2440 if (type == CameraBackgroundBrush::BrushType::NONE)
2441 {
2442 _camera->setBackgroundBrush(CameraBackgroundBrush::createDepthBrush(1.f));
2443 _label->setString("Depth Clear Brush");
2444 // Test brush valid when set by user scene setting
2445 CCLOG("Background brush valid status is : %s", _camera->isBrushValid() ? "true" : "false");
2446 }
2447 else if (type == CameraBackgroundBrush::BrushType::DEPTH)
2448 {
2449 _camera->setBackgroundBrush(CameraBackgroundBrush::createColorBrush(Color4F(1.f, 0.f, 0.f, 1.f), 1.f));
2450 _label->setString("Color Clear Brush");
2451 }
2452 else if (type == CameraBackgroundBrush::BrushType::COLOR)
2453 {
2454 _camera->setBackgroundBrush(CameraBackgroundBrush::createSkyboxBrush("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg","Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg","Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg"));
2455 _label->setString("Skybox Clear Brush");
2456 }
2457 else if (type == CameraBackgroundBrush::BrushType::SKYBOX)
2458 {
2459 _camera->setBackgroundBrush(CameraBackgroundBrush::createNoneBrush());
2460 _label->setString("None Clear Brush");
2461 }
2462}
2463
2465{
2466 return "Camera Background Clear Brush";
2467}
2468
2470{
2471 return "";
2472}
2473
2475{
2476 auto s = Director::getInstance()->getWinSize();
2477
2478 auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
2479 camera->setCameraFlag(CameraFlag::USER1);
2480 camera->setPosition3D(Vec3(0.f, 50.f, 200.f));
2481 camera->lookAt(Vec3(0.f, 0.f, 0.f));
2482 addChild(camera);
2483
2484 auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
2485 sprite->setPosition(20.f, 0.f);
2486 addChild(sprite);
2487
2488 auto streak = MotionStreak3D::create(1.0f, 1.0f, 5.f, Color3B(255, 255, 0), "Images/Icon.png");
2489 addChild(streak);
2490
2491 setCameraMask(2);
2492
2493 _sprite = sprite;
2494 _streak = streak;
2495 scheduleUpdate();
2496}
2497std::string MotionStreak3DTest::title() const
2498{
2499 return "MotionStreak3D Test";
2500}
2502{
2503 return "";
2504}
2505
2507{
2508 static float t = 0;
2509 t += delta;
2510 float angle = t * M_PI;
2511 float r = 20.f;
2512
2513 _sprite->setPosition3D(Vec3(r * cosf(angle), 0, r * sinf(angle)));
2514 _streak->setPosition3D(_sprite->getPosition3D());
2515 _streak->setSweepAxis(Vec3(cosf(angle), 0, sinf(angle)));
2516}
2517
2519{
2520 auto s = Director::getInstance()->getWinSize();
2521
2522 {
2523 auto sprite = Sprite3D::create("Sprite3DTest/sphere.c3b");
2524 sprite->setPosition(Vec2(-30.0f, 0.0f));
2525 sprite->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
2526 sprite->setScale(2.0);
2527 sprite->setCameraMask(2);
2528 sprite->setTexture("Sprite3DTest/brickwork-texture.jpg");
2529 addChild(sprite);
2530 }
2531
2532 int maxAttributes = Configuration::getInstance()->getMaxAttributes();
2533 CCASSERT(maxAttributes > 8, "attributes supported must be greater than 8");
2534 if (maxAttributes > 8)
2535 {
2536 auto sprite = Sprite3D::create("Sprite3DTest/sphere_bumped.c3b");
2537 sprite->setPosition(Vec2(30.0f, 0.0f));
2538 sprite->setRotation3D(Vec3(90.0f, 0.0f, 0.0f));
2539 sprite->setScale(20.0);
2540 sprite->setCameraMask(2);
2541 sprite->setTexture("Sprite3DTest/brickwork-texture.jpg");
2542 addChild(sprite);
2543 }
2544
2545 //setup camera
2546 auto camera = Camera::createPerspective(60.0, s.width / s.height, 1.0f, 1000.f);
2547 camera->setCameraFlag(CameraFlag::USER1);
2548 camera->setPosition3D(Vec3(0.f, 0.f, 100.f));
2549 camera->lookAt(Vec3(0.f, 0.f, 0.f));
2550 addChild(camera);
2551
2552 PointLight* light = PointLight::create(Vec3(0.0, 0.0, 0.0), Color3B(255, 255, 255), 1000);
2553 light->setTag(100);
2554 addChild(light);
2555
2556 scheduleUpdate();
2557}
2558
2560{
2561 static float angle = 0.0f;
2562 static bool reverseDir = false;
2563 static float radius = 100.0f;
2564
2565 auto light = static_cast<PointLight*>(getChildByTag(100));
2566 light->setPosition3D(Vec3(radius * cos(angle), 0.0f, radius * sin(angle)));
2567 if (reverseDir){
2568 angle -= 0.01f;
2569 if (angle < 0.0)
2570 reverseDir = false;
2571 }
2572 else{
2573 angle += 0.01f;
2574 if (3.14159 < angle)
2575 reverseDir = true;
2576 }
2577}
2578
2580{
2581
2582}
2583
2585{
2586 return "NormalMapping Test";
2587}
2588
2590{
2591 return "";
2592}
2593
2594
2595
2596
2598{
2599 auto s = Director::getInstance()->getWinSize();
2600
2601 auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
2602 camera->setCameraFlag(CameraFlag::USER1);
2603 camera->setPosition3D(Vec3(0.f, 50.f, 200.f));
2604 camera->lookAt(Vec3(0.f, 0.f, 0.f));
2605 addChild(camera);
2606
2607 _sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
2608 _sprite->setPosition(20.f, 0.f);
2609 _sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
2610 _meshTex = _sprite->getMesh()->getTexture();
2611 _texFile = _meshTex->getPath();
2612 addChild(_sprite);
2613
2614 setCameraMask(2);
2615
2616 //auto listener = EventListenerTouchAllAtOnce::create();
2618 //_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2619
2620 TTFConfig ttfConfig("fonts/arial.ttf", 20);
2621
2622 auto label1 = Label::createWithTTF(ttfConfig, "Print Mesh Name");
2623 auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DPropertyTest::printMeshName, this));
2624 auto label2 = Label::createWithTTF(ttfConfig, "Remove Used Texture");
2625 auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DPropertyTest::removeUsedTexture, this));
2626 auto label3 = Label::createWithTTF(ttfConfig, "Reset");
2627 auto item3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Sprite3DPropertyTest::resetTexture, this));
2628
2629 item1->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 4));
2630 item2->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 5));
2631 item3->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 6));
2632
2633 auto pMenu1 = Menu::create(item1, item2, item3,nullptr);
2634 pMenu1->setPosition(Vec2(0.0f, 0.0f));
2635 this->addChild(pMenu1, 10);
2636
2637 scheduleUpdate();
2638}
2640{
2641 return "Sprite3DPropertyTest Test";
2642}
2644{
2645 return "";
2646}
2647
2649{
2650
2651}
2652void Sprite3DPropertyTest::printMeshName(cocos2d::Ref* sender)
2653{
2654 CCLOG("MeshName Begin");
2655 Vector<Mesh*> meshes =_sprite->getMeshes();
2656 for(Mesh* mesh : meshes)
2657 {
2658 log("MeshName: %s ", mesh->getName().c_str());
2659 }
2660 CCLOG("MeshName End");
2661}
2663{
2664 if (_meshTex != nullptr)
2665 {
2666 Director::getInstance()->getTextureCache()->removeTexture(_meshTex);
2667 this->refreshSpriteRender();
2668 }
2669}
2670
2671void Sprite3DPropertyTest::resetTexture(cocos2d::Ref* sender)
2672{
2673 if (_meshTex != nullptr)
2674 {
2675 _meshTex = Director::getInstance()->getTextureCache()->addImage(_texFile);
2676 this->refreshSpriteRender();
2677 }
2678}
2679
2681{
2682 Vector<Mesh*> meshes = _sprite->getMeshes();
2683 for (Mesh* mesh : meshes)
2684 {
2685 std::string file = mesh->getTextureFileName();
2686 Texture2D* cacheTex = Director::getInstance()->getTextureCache()->getTextureForKey(file);
2687 if (cacheTex == nullptr)
2688 {
2689 unsigned char data[] = { 255, 0, 0, 255 };//1*1 red picture
2690 Image * image = new (std::nothrow) Image();
2691 image->initWithRawData(data, sizeof(data), 1, 1, sizeof(unsigned char));
2692 cacheTex = Director::getInstance()->getTextureCache()->addImage(image, "/dummyTexture");
2693 image->release();
2694 }
2695 mesh->setTexture(cacheTex, cocos2d::NTextureData::Usage::Diffuse, false);
2696 }
2697}
2698
2699//
2700// Issue16155Test
2701//
2703{
2704 auto s = Director::getInstance()->getWinSize();
2705
2706 auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
2707
2708 int rcBefore = sprite->getMeshByIndex(0)->getTexture()->getReferenceCount();
2709 addChild(sprite);
2710 removeChild(sprite);
2711
2712 cocos2d::log("Issue 16155: Ref count:%d. Run this test again. RC should be the same", rcBefore);
2713}
2714
2715std::string Issue16155Test::title() const
2716{
2717 return "Issue16155 Test";
2718}
2719std::string Issue16155Test::subtitle() const
2720{
2721 return "Should not leak texture. See console";
2722}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
cocos2d::Sprite3D * _sprite3d
Definition: Sprite3DTest.h:530
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Action * _swim
Definition: Sprite3DTest.h:288
virtual void update(float dt) override
virtual std::string title() const override
cocos2d::MoveTo * _moveAction
Definition: Sprite3DTest.h:295
virtual std::string subtitle() const override
float _elapseTransTime
Definition: Sprite3DTest.h:291
void reachEndCallBack()
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:286
cocos2d::Animate3D * _hurt
Definition: Sprite3DTest.h:289
virtual ~AsyncLoadSprite3DTest()
std::vector< std::string > _paths
Definition: Sprite3DTest.h:214
void menuCallback_asyncLoadSprite(cocos2d::Ref *sender)
virtual std::string title() const override
virtual std::string subtitle() const override
void asyncLoad_Callback(cocos2d::Sprite3D *sprite, void *param)
void addNewSpriteWithCoords(cocos2d::Vec2 p)
virtual std::string subtitle() const override
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string title() const override
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:312
cocos2d::Label * _label
Definition: Sprite3DTest.h:544
void switch_CameraClearMode(cocos2d::Ref *sender)
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::Camera * _camera
Definition: Sprite3DTest.h:543
static cocos2d::DrawNode3D * create()
Definition: DrawNode3D.cpp:42
virtual std::string title() const override
virtual std::string subtitle() const override
issue 9767 setGLProgram
Definition: Sprite3DTest.h:489
virtual std::string subtitle() const override
void menuCallback_SwitchShader(cocos2d::Ref *sender)
ShaderType _shaderType
Definition: Sprite3DTest.h:505
virtual std::string title() const override
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:506
virtual std::string subtitle() const override
cocos2d::MotionStreak3D * _streak
Definition: Sprite3DTest.h:574
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:573
virtual std::string title() const override
virtual void update(float delta) override
virtual std::string subtitle() const override
virtual std::string title() const override
void addNewSpriteWithCoords(cocos2d::Vec2 p)
std::vector< cocos2d::Sprite3D * > _sprites
Definition: Sprite3DTest.h:453
virtual void update(float delta) override
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:409
virtual std::string title() const override
void addNewSpriteWithCoords(cocos2d::Vec2 p)
virtual std::string subtitle() const override
virtual std::string title() const override
void addNewSpriteWithCoords(cocos2d::Vec2 p)
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:165
cocos2d::backend::ProgramState * _state
Definition: Sprite3DTest.h:162
Clipping Sprite3D
Definition: Sprite3DTest.h:511
virtual std::string title() const override
virtual std::string subtitle() const override
void addNewSpriteWithCoords(cocos2d::Vec2)
virtual std::string subtitle() const override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string title() const override
cocos2d::TextureCube * _textureCube
Definition: Sprite3DTest.h:477
cocos2d::Sprite3D * _teapot
Definition: Sprite3DTest.h:479
cocos2d::Skybox * _skyBox
Definition: Sprite3DTest.h:478
cocos2d::Camera * _camera
Definition: Sprite3DTest.h:480
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:483
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:196
void addNewSpriteWithCoords(cocos2d::Vec2 p)
virtual std::string subtitle() const override
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string title() const override
virtual ~Sprite3DEffectTest()
std::vector< cocos2d::Sprite3D * > _sprites
Definition: Sprite3DTest.h:193
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:135
bool isState(unsigned int state, unsigned int bit) const
void move3D(float elapsedTime)
virtual std::string title() const override
void Move(cocos2d::Ref *sender, int value)
cocos2d::Sprite3D * _plane
Definition: Sprite3DTest.h:130
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::Sprite3D * _orc
Definition: Sprite3DTest.h:131
void updateState(float elapsedTime)
unsigned int _curState
Definition: Sprite3DTest.h:129
cocos2d::Camera * _camera
Definition: Sprite3DTest.h:127
virtual std::string subtitle() const override
cocos2d::backend::ProgramState * _state
Definition: Sprite3DTest.h:132
virtual ~Sprite3DFakeShadowTest()
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void updateCamera(float fDelta)
cocos2d::Vec3 _targetPos
Definition: Sprite3DTest.h:128
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
cocos2d::Camera * _camera
Definition: Sprite3DTest.h:149
virtual std::string title() const override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual ~Sprite3DLightMapTest()
void addNewSpriteWithCoords(cocos2d::Vec2 p)
cocos2d::Sprite3D * _mirrorSprite
Definition: Sprite3DTest.h:394
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:393
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void update(float dt) override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
std::string _texFile
Definition: Sprite3DTest.h:606
virtual std::string title() const override
void resetTexture(cocos2d::Ref *sender)
void printMeshName(cocos2d::Ref *sender)
cocos2d::Texture2D * _meshTex
Definition: Sprite3DTest.h:605
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:604
void removeUsedTexture(cocos2d::Ref *sender)
virtual void update(float delta) override
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:343
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
std::vector< std::string > _skins[(int) SkinType::MAX_TYPE]
Definition: Sprite3DTest.h:341
virtual std::string title() const override
void menuCallback_reSkin(cocos2d::Ref *sender)
void addNewSpriteWithCoords(cocos2d::Vec2 p)
int _curSkin[(int) SkinType::MAX_TYPE]
Definition: Sprite3DTest.h:342
virtual std::string subtitle() const override
virtual std::string title() const override
virtual ~Sprite3DTestDemo()
cocos2d::backend::ProgramState * _state
Definition: Sprite3DTest.h:96
virtual std::string subtitle() const override
virtual std::string title() const override
virtual ~Sprite3DUVAnimationTest()
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:99
void cylinderUpdate(float dt)
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:559
virtual std::string title() const override
virtual std::string subtitle() const override
void calculateRayByLocationInView(cocos2d::Ray *ray, const cocos2d::Vec2 &location)
std::vector< cocos2d::OBB > _obb
Definition: Sprite3DTest.h:365
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void unproject(const cocos2d::Mat4 &viewProjection, const cocos2d::Size *viewport, cocos2d::Vec3 *src, cocos2d::Vec3 *dst)
cocos2d::DrawNode3D * _drawDebug
Definition: Sprite3DTest.h:371
virtual std::string title() const override
cocos2d::MoveTo * _moveAction
Definition: Sprite3DTest.h:368
void delOBBCallback(cocos2d::Ref *sender)
void addNewOBBWithCoords(cocos2d::Vec2 p)
virtual std::string subtitle() const override
cocos2d::Sprite3D * _sprite
Definition: Sprite3DTest.h:364
cocos2d::DrawNode3D * _drawOBB
Definition: Sprite3DTest.h:366
cocos2d::Label * _labelCubeCount
Definition: Sprite3DTest.h:367
void addOBBCallback(cocos2d::Ref *sender)
void addNewSpriteWithCoords(cocos2d::Vec2 p)
virtual void update(float dt) override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string title() const override
cocos2d::EventListenerCustom * _backToForegroundListener
Definition: Sprite3DTest.h:254
virtual std::string subtitle() const override
void addNewSpriteWithCoords(cocos2d::Vec2 p)
std::vector< cocos2d::Sprite3D * > _sprites
Definition: Sprite3DTest.h:251
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
std::vector< cocos2d::Sprite3D * > _sprits
Definition: Sprite3DTest.h:232
virtual std::string title() const override
void switchAnimationQualityCallback(cocos2d::Ref *sender)
std::string getAnimationQualityMessage() const
void addNewSpriteWithCoords(cocos2d::Vec2 p)
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::MenuItemFont * _menuItem
Definition: Sprite3DTest.h:234
virtual std::string subtitle() const override
cocos2d::Label * _label
Definition: Sprite3DTest.h:437
virtual std::string subtitle() const override
virtual std::string title() const override
std::string _useCaseTitles[(int) USECASE::MAX_CASE_NUM]
Definition: Sprite3DTest.h:439
virtual void update(float delta) override
void menuCallback_Message(cocos2d::Ref *sender)
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45