axmol/tests/cpp-tests/Source/BillBoardTest/BillBoardTest.cpp

305 lines
11 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
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
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
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
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"
USING_NS_AX;
USING_NS_AX_EXT;
using namespace ax::ui;
BillBoardTests::BillBoardTests()
{
ADD_TEST_CASE(BillBoardRotationTest);
ADD_TEST_CASE(BillBoardTest);
}
//------------------------------------------------------------------
//
// Billboard Rotation Test
//
//------------------------------------------------------------------
BillBoardRotationTest::BillBoardRotationTest()
{
auto root = MeshRenderer::create();
2021-12-28 16:06:23 +08:00
root->setPositionNormalized(Vec2(0.5f, 0.25f));
addChild(root);
2021-12-28 16:06:23 +08:00
auto model = MeshRenderer::create("MeshRendererTest/orc.c3b");
model->setScale(5);
2021-12-28 16:06:23 +08:00
model->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
root->addChild(model);
2021-12-28 16:06:23 +08:00
auto bill = BillBoard::create();
bill->setPosition(0, 120);
root->addChild(bill);
2021-12-28 16:06:23 +08:00
auto sp = Sprite::create("Images/SpookyPeas.png");
sp->setScale(2);
bill->addChild(sp);
2021-12-28 16:06:23 +08:00
auto lbl = Label::create();
lbl->setPosition(0.0f, 30.0f);
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);
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);
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);
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);
auto repeat = RepeatForever::create(seq2);
model->runAction(repeat);
}
2021-12-28 16:06:23 +08:00
BillBoardRotationTest::~BillBoardRotationTest() {}
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)
{
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);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2021-12-28 16:06:23 +08:00
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layerBillBoard = layer3D;
2021-12-28 16:06:23 +08:00
auto s = Director::getInstance()->getWinSize();
if (_camera == nullptr)
{
2021-12-28 16:06:23 +08:00
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 500);
_camera->setCameraFlag(CameraFlag::USER1);
_layerBillBoard->addChild(_camera);
}
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));
_billboards.emplace_back(billboard);
2014-10-14 15:08:36 +08:00
layer->addChild(billboard);
_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);
_billboards.emplace_back(billboard);
_billboards.emplace_back(billboard2);
2014-10-14 15:08:36 +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));
_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));
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);
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));
addChild(menu, 0);
_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));
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));
}
void BillBoardTest::menuCallback_orientedPoint(Ref* sender)
{
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);
}
}
void BillBoardTest::menuCallback_orientedPlane(Ref* sender)
{
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);
}
}
BillBoardTest::~BillBoardTest()
{
if (_camera)
{
_camera = nullptr;
}
}
std::string BillBoardTest::title() const
{
2015-01-12 14:20:41 +08:00
return "BillBoard Test";
}
std::string BillBoardTest::subtitle() const
{
return "";
}
void BillBoardTest::addNewBillBoardWithCoords(Vec3 p)
{
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)]);
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
_layerBillBoard->addChild(billboard);
_billboards.emplace_back(billboard);
}
}
void BillBoardTest::addNewAniBillBoardWithCoords(Vec3 p)
{
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));
_layerBillBoard->addChild(billboardAni);
auto animation = Animation::create();
2021-12-28 16:06:23 +08:00
for (int i = 1; i < 15; i++)
{
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));
_billboards.emplace_back(billboardAni);
}
}
2021-12-28 16:06:23 +08:00
void BillBoardTest::update(float dt) {}
void BillBoardTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
2021-12-28 16:06:23 +08:00
if (touches.size() == 1)
{
2021-12-28 16:06:23 +08:00
auto touch = touches[0];
auto location = touch->getLocation();
auto PreviousLocation = touch->getPreviousLocation();
2021-12-28 16:06:23 +08:00
Point newPos = PreviousLocation - location;
Vec3 cameraDir;
Vec3 cameraRightDir;
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
cameraDir.normalize();
2021-12-28 16:06:23 +08:00
cameraDir.y = 0;
_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);
}
}
2021-12-28 16:06:23 +08:00
void BillBoardTest::rotateCameraCallback(Ref* sender, float value)
{
2021-12-28 16:06:23 +08:00
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y += value;
_camera->setRotation3D(rotation3D);
}