2014-09-04 16:27:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-09-04 16:27:18 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-09-04 16:27:18 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-09-04 16:27:18 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "BillBoardTest.h"
|
|
|
|
#include "3d/CCBillBoard.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
USING_NS_AX_EXT;
|
2022-08-29 20:51:22 +08:00
|
|
|
using namespace ax::ui;
|
2015-04-09 08:37:30 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
BillBoardTests::BillBoardTests()
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(BillBoardRotationTest);
|
|
|
|
ADD_TEST_CASE(BillBoardTest);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 16:04:26 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Billboard Rotation Test
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
BillBoardRotationTest::BillBoardRotationTest()
|
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto root = MeshRenderer::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
root->setPositionNormalized(Vec2(0.5f, 0.25f));
|
2015-01-16 16:04:26 +08:00
|
|
|
addChild(root);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto model = MeshRenderer::create("MeshRendererTest/orc.c3b");
|
2015-01-16 16:04:26 +08:00
|
|
|
model->setScale(5);
|
2021-12-28 16:06:23 +08:00
|
|
|
model->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
2015-01-16 16:04:26 +08:00
|
|
|
root->addChild(model);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-01-16 16:04:26 +08:00
|
|
|
auto bill = BillBoard::create();
|
|
|
|
bill->setPosition(0, 120);
|
|
|
|
root->addChild(bill);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-01-16 16:04:26 +08:00
|
|
|
auto sp = Sprite::create("Images/SpookyPeas.png");
|
|
|
|
sp->setScale(2);
|
|
|
|
bill->addChild(sp);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-01-16 16:04:26 +08:00
|
|
|
auto lbl = Label::create();
|
2019-09-24 11:31:35 +08:00
|
|
|
lbl->setPosition(0.0f, 30.0f);
|
2015-01-16 16:04:26 +08:00
|
|
|
lbl->setString("+100");
|
|
|
|
bill->addChild(lbl);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto r = RotateBy::create(10, Vec3(0.0f, 360.0f, 0.0f));
|
2015-01-16 16:14:24 +08:00
|
|
|
auto rp = RepeatForever::create(r);
|
|
|
|
root->runAction(rp);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto jump = JumpBy::create(1, Vec2(0.0f, 0.0f), 30, 1);
|
2015-06-25 23:15:39 +08:00
|
|
|
auto scale = ScaleBy::create(2.f, 2.f, 2.f, 0.1f);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto seq = Sequence::create(jump, scale, NULL);
|
|
|
|
|
2019-09-24 11:31:35 +08:00
|
|
|
auto rot = RotateBy::create(2, Vec3(-90.0f, 0.0f, 0.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
auto act = Spawn::create(seq, rot, NULL);
|
|
|
|
|
2015-01-16 16:04:26 +08:00
|
|
|
auto scale2 = scale->reverse();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto rot2 = rot->reverse();
|
|
|
|
auto act2 = Spawn::create(scale2, rot2, NULL);
|
|
|
|
|
|
|
|
auto seq2 = Sequence::create(act, act2, NULL);
|
2015-01-16 16:04:26 +08:00
|
|
|
auto repeat = RepeatForever::create(seq2);
|
|
|
|
model->runAction(repeat);
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
BillBoardRotationTest::~BillBoardRotationTest() {}
|
2015-01-16 16:04:26 +08:00
|
|
|
|
|
|
|
std::string BillBoardRotationTest::title() const
|
|
|
|
{
|
|
|
|
return "Rotation Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BillBoardRotationTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "All the sprites should still facing camera";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Billboard Rendering Test
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2021-12-28 16:06:23 +08:00
|
|
|
BillBoardTest::BillBoardTest() : _camera(nullptr)
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
// Create touch listener
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(BillBoardTest::onTouchesMoved, this);
|
2015-04-09 15:08:49 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto layer3D = Layer::create();
|
|
|
|
addChild(layer3D, 0);
|
2017-03-22 09:21:34 +08:00
|
|
|
_layerBillBoard = layer3D;
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-09-04 16:27:18 +08:00
|
|
|
if (_camera == nullptr)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 500);
|
2014-09-04 16:27:18 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
2017-03-22 09:21:34 +08:00
|
|
|
_layerBillBoard->addChild(_camera);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// Create rotating billboards
|
2014-10-14 15:08:36 +08:00
|
|
|
std::string imgs[3] = {"Images/Icon.png", "Images/r2.png"};
|
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
Layer* layer = Layer::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
auto billboard = BillBoard::create(imgs[(unsigned int)(AXRANDOM_0_1() * 1 + 0.5f)]);
|
2014-10-14 15:08:36 +08:00
|
|
|
billboard->setScale(0.5f);
|
2022-07-16 10:43:05 +08:00
|
|
|
billboard->setPosition3D(Vec3(0.0f, 0.0f, rand_minus1_1() * 150.0f));
|
|
|
|
billboard->setOpacity(static_cast<uint8_t>(AXRANDOM_0_1() * 128 + 128));
|
2022-08-08 13:18:33 +08:00
|
|
|
_billboards.emplace_back(billboard);
|
2014-10-14 15:08:36 +08:00
|
|
|
layer->addChild(billboard);
|
2017-03-22 09:21:34 +08:00
|
|
|
_layerBillBoard->addChild(layer);
|
2022-07-16 10:43:05 +08:00
|
|
|
layer->runAction(RepeatForever::create(RotateBy::create(AXRANDOM_0_1() * 10, Vec3(0.0f, 45.0f, 0.0f))));
|
2014-10-14 15:08:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto billboard = BillBoard::create("Images/Icon.png");
|
|
|
|
billboard->setScale(0.2f);
|
|
|
|
billboard->setPosition3D(Vec3(0.0f, 30.0f, 0.0f));
|
|
|
|
|
|
|
|
auto billboard2 = BillBoard::create("Images/r2.png");
|
|
|
|
billboard2->setPosition3D(Vec3(0.0f, 0.0f, 100.0f));
|
|
|
|
billboard->addChild(billboard2);
|
2022-08-08 13:18:33 +08:00
|
|
|
_billboards.emplace_back(billboard);
|
|
|
|
_billboards.emplace_back(billboard2);
|
2014-10-14 15:08:36 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("MeshRendererTest/orc.c3t");
|
|
|
|
mesh->setScale(2.0f);
|
|
|
|
mesh->addChild(billboard);
|
|
|
|
mesh->runAction(RepeatForever::create(RotateBy::create(10.0f, Vec3(0.0f, 360.0f, 0.0f))));
|
|
|
|
_layerBillBoard->addChild(mesh);
|
2014-10-14 15:08:36 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
addNewBillBoardWithCoords(Vec3(20.0f, 5.0f, 0.0f));
|
|
|
|
addNewBillBoardWithCoords(Vec3(60.0f, 5.0f, 0.0f));
|
|
|
|
addNewBillBoardWithCoords(Vec3(100.0f, 5.0f, 0.0f));
|
|
|
|
addNewBillBoardWithCoords(Vec3(140.0f, 5.0f, 0.0f));
|
|
|
|
addNewBillBoardWithCoords(Vec3(180.0f, 5.0f, 0.0f));
|
|
|
|
|
|
|
|
addNewAniBillBoardWithCoords(Vec3(-20.0f, 0.0f, 0.0f));
|
|
|
|
addNewAniBillBoardWithCoords(Vec3(-60.0f, 0.0f, 0.0f));
|
|
|
|
addNewAniBillBoardWithCoords(Vec3(-100.0f, 0.0f, 0.0f));
|
|
|
|
addNewAniBillBoardWithCoords(Vec3(-140.0f, 0.0f, 0.0f));
|
|
|
|
addNewAniBillBoardWithCoords(Vec3(-180.0f, 0.0f, 0.0f));
|
|
|
|
|
2019-09-24 11:31:35 +08:00
|
|
|
_camera->setPosition3D(Vec3(0.0f, 100.0f, 230.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
_camera->lookAt(Vec3(0, 0, 0), Vec3(0.0f, 1.0f, 0.0f));
|
2014-09-04 16:27:18 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 16);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "rotate+");
|
2022-07-15 19:17:01 +08:00
|
|
|
auto menuItem1 = MenuItemLabel::create(label1, AX_CALLBACK_1(BillBoardTest::rotateCameraCallback, this, 10));
|
2021-12-28 16:06:23 +08:00
|
|
|
auto label2 = Label::createWithTTF(ttfConfig, "rotate-");
|
2022-07-15 19:17:01 +08:00
|
|
|
auto menuItem2 = MenuItemLabel::create(label2, AX_CALLBACK_1(BillBoardTest::rotateCameraCallback, this, -10));
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = Menu::create(menuItem1, menuItem2, nullptr);
|
2014-09-04 16:27:18 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem1->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 160));
|
|
|
|
menuItem2->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 190));
|
2014-09-04 16:27:18 +08:00
|
|
|
addChild(menu, 0);
|
2017-03-22 09:21:34 +08:00
|
|
|
_layerBillBoard->setCameraMask(2);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
label1 = Label::createWithTTF(ttfConfig, "Point Oriented");
|
2022-07-15 19:17:01 +08:00
|
|
|
menuItem1 = MenuItemLabel::create(label1, AX_CALLBACK_1(BillBoardTest::menuCallback_orientedPoint, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
label2 = Label::createWithTTF(ttfConfig, "Plane Oriented");
|
2022-07-15 19:17:01 +08:00
|
|
|
menuItem2 = MenuItemLabel::create(label2, AX_CALLBACK_1(BillBoardTest::menuCallback_orientedPlane, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem1->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 100));
|
|
|
|
menuItem2->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 130));
|
|
|
|
|
2014-10-09 09:55:33 +08:00
|
|
|
menu = Menu::create(menuItem1, menuItem2, nullptr);
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(Vec2(0, 0));
|
2014-09-04 16:27:18 +08:00
|
|
|
this->addChild(menu, 10);
|
|
|
|
menuCallback_orientedPoint(nullptr);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(BillBoardTest::update));
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BillBoardTest::menuCallback_orientedPoint(Ref* sender)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& billboard : _billboards)
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
2014-09-05 14:59:14 +08:00
|
|
|
billboard->setMode(BillBoard::Mode::VIEW_POINT_ORIENTED);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BillBoardTest::menuCallback_orientedPlane(Ref* sender)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& billboard : _billboards)
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
2014-09-05 14:59:14 +08:00
|
|
|
billboard->setMode(BillBoard::Mode::VIEW_PLANE_ORIENTED);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BillBoardTest::~BillBoardTest()
|
|
|
|
{
|
|
|
|
if (_camera)
|
|
|
|
{
|
|
|
|
_camera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string BillBoardTest::title() const
|
|
|
|
{
|
2015-01-12 14:20:41 +08:00
|
|
|
return "BillBoard Test";
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
std::string BillBoardTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2017-03-22 09:21:34 +08:00
|
|
|
void BillBoardTest::addNewBillBoardWithCoords(Vec3 p)
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
|
|
|
std::string imgs[3] = {"Images/Icon.png", "Images/r2.png"};
|
|
|
|
for (unsigned int i = 0; i < 10; ++i)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
auto billboard = BillBoard::create(imgs[(unsigned int)(AXRANDOM_0_1() * 1 + 0.5f)]);
|
2017-03-22 09:21:34 +08:00
|
|
|
billboard->setScale(0.5f);
|
|
|
|
billboard->setPosition3D(Vec3(p.x, p.y, -150.0f + 30 * i));
|
2022-07-16 10:43:05 +08:00
|
|
|
billboard->setOpacity(static_cast<uint8_t>(AXRANDOM_0_1() * 128 + 128));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-03-22 09:21:34 +08:00
|
|
|
_layerBillBoard->addChild(billboard);
|
2022-08-08 13:18:33 +08:00
|
|
|
_billboards.emplace_back(billboard);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 09:21:34 +08:00
|
|
|
void BillBoardTest::addNewAniBillBoardWithCoords(Vec3 p)
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 10; ++i)
|
|
|
|
{
|
2015-01-10 06:06:21 +08:00
|
|
|
auto billboardAni = BillBoard::create("Images/grossini.png");
|
|
|
|
billboardAni->setScale(0.5f);
|
2021-12-28 16:06:23 +08:00
|
|
|
billboardAni->setPosition3D(Vec3(p.x, p.y, -150.0f + 30 * i));
|
2017-03-22 09:21:34 +08:00
|
|
|
_layerBillBoard->addChild(billboardAni);
|
2014-09-04 16:27:18 +08:00
|
|
|
|
|
|
|
auto animation = Animation::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 1; i < 15; i++)
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
|
|
|
char szName1[100] = {0};
|
|
|
|
sprintf(szName1, "Images/grossini_dance_%02d.png", i);
|
|
|
|
animation->addSpriteFrameWithFile(szName1);
|
|
|
|
}
|
|
|
|
// should last 2.8 seconds. And there are 14 frames.
|
|
|
|
animation->setDelayPerUnit(2.8f / 14.0f);
|
|
|
|
animation->setRestoreOriginalFrame(true);
|
|
|
|
|
|
|
|
auto action = Animate::create(animation);
|
2015-01-10 06:06:21 +08:00
|
|
|
billboardAni->runAction(RepeatForever::create(action));
|
2022-07-16 10:43:05 +08:00
|
|
|
billboardAni->setOpacity(static_cast<uint8_t>(AXRANDOM_0_1() * 128 + 128));
|
2022-08-08 13:18:33 +08:00
|
|
|
_billboards.emplace_back(billboardAni);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
void BillBoardTest::update(float dt) {}
|
2015-04-03 14:31:03 +08:00
|
|
|
|
2014-09-04 16:27:18 +08:00
|
|
|
void BillBoardTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (touches.size() == 1)
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
2014-09-04 16:27:18 +08:00
|
|
|
auto PreviousLocation = touch->getPreviousLocation();
|
2021-12-28 16:06:23 +08:00
|
|
|
Point newPos = PreviousLocation - location;
|
2014-09-04 16:27:18 +08:00
|
|
|
|
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
cameraDir.y = 0;
|
2014-09-04 16:27:18 +08:00
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
cameraRightDir.y = 0;
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
cameraPos += cameraDir * newPos.y * 0.5;
|
|
|
|
cameraPos += cameraRightDir * newPos.x * 0.5;
|
|
|
|
_camera->setPosition3D(cameraPos);
|
2014-09-04 16:27:18 +08:00
|
|
|
}
|
2015-04-09 15:08:49 +08:00
|
|
|
}
|
2015-04-03 14:31:03 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void BillBoardTest::rotateCameraCallback(Ref* sender, float value)
|
2014-09-04 16:27:18 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
Vec3 rotation3D = _camera->getRotation3D();
|
|
|
|
rotation3D.y += value;
|
2014-09-04 16:27:18 +08:00
|
|
|
_camera->setRotation3D(rotation3D);
|
|
|
|
}
|