2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +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 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +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 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +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_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
using namespace cocos2d::ui;
|
|
|
|
|
|
|
|
BillBoardTests::BillBoardTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(BillBoardRotationTest);
|
|
|
|
ADD_TEST_CASE(BillBoardTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Billboard Rotation Test
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
BillBoardRotationTest::BillBoardRotationTest()
|
|
|
|
{
|
|
|
|
auto root = Sprite3D::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
root->setPositionNormalized(Vec2(0.5f, 0.25f));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(root);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto model = Sprite3D::create("Sprite3DTest/orc.c3b");
|
|
|
|
model->setScale(5);
|
2021-12-28 21:27:32 +08:00
|
|
|
model->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
root->addChild(model);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto bill = BillBoard::create();
|
|
|
|
bill->setPosition(0, 120);
|
|
|
|
root->addChild(bill);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sp = Sprite::create("Images/SpookyPeas.png");
|
|
|
|
sp->setScale(2);
|
|
|
|
bill->addChild(sp);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto lbl = Label::create();
|
|
|
|
lbl->setPosition(0.0f, 30.0f);
|
|
|
|
lbl->setString("+100");
|
|
|
|
bill->addChild(lbl);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
auto r = RotateBy::create(10, Vec3(0.0f, 360.0f, 0.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto rp = RepeatForever::create(r);
|
|
|
|
root->runAction(rp);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
auto jump = JumpBy::create(1, Vec2(0.0f, 0.0f), 30, 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto scale = ScaleBy::create(2.f, 2.f, 2.f, 0.1f);
|
2021-12-28 21:27:32 +08:00
|
|
|
auto seq = Sequence::create(jump, scale, NULL);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto rot = RotateBy::create(2, Vec3(-90.0f, 0.0f, 0.0f));
|
2021-12-28 21:27:32 +08:00
|
|
|
auto act = Spawn::create(seq, rot, NULL);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto scale2 = scale->reverse();
|
2021-12-28 21:27:32 +08:00
|
|
|
auto rot2 = rot->reverse();
|
|
|
|
auto act2 = Spawn::create(scale2, rot2, NULL);
|
|
|
|
|
|
|
|
auto seq2 = Sequence::create(act, act2, NULL);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto repeat = RepeatForever::create(seq2);
|
|
|
|
model->runAction(repeat);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
BillBoardRotationTest::~BillBoardRotationTest() {}
|
2019-11-23 20:27:39 +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 21:27:32 +08:00
|
|
|
BillBoardTest::BillBoardTest() : _camera(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
// Create touch listener
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(BillBoardTest::onTouchesMoved, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
auto layer3D = Layer::create();
|
|
|
|
addChild(layer3D, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
_layerBillBoard = layer3D;
|
2021-12-28 21:27:32 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_camera == nullptr)
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 500);
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_layerBillBoard->addChild(_camera);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
// Create rotating billboards
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string imgs[3] = {"Images/Icon.png", "Images/r2.png"};
|
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
Layer* layer = Layer::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto billboard = BillBoard::create(imgs[(unsigned int)(CCRANDOM_0_1() * 1 + 0.5f)]);
|
|
|
|
billboard->setScale(0.5f);
|
2021-12-28 21:27:32 +08:00
|
|
|
billboard->setPosition3D(Vec3(0.0f, 0.0f, CCRANDOM_MINUS1_1() * 150.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
billboard->setOpacity(static_cast<uint8_t>(CCRANDOM_0_1() * 128 + 128));
|
|
|
|
_billboards.push_back(billboard);
|
|
|
|
layer->addChild(billboard);
|
|
|
|
_layerBillBoard->addChild(layer);
|
2021-12-28 21:27:32 +08:00
|
|
|
layer->runAction(RepeatForever::create(RotateBy::create(CCRANDOM_0_1() * 10, Vec3(0.0f, 45.0f, 0.0f))));
|
2019-11-23 20:27:39 +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.push_back(billboard);
|
|
|
|
_billboards.push_back(billboard2);
|
|
|
|
|
|
|
|
auto sprite3d = Sprite3D::create("Sprite3DTest/orc.c3t");
|
|
|
|
sprite3d->setScale(2.0f);
|
|
|
|
sprite3d->addChild(billboard);
|
2021-12-28 21:27:32 +08:00
|
|
|
sprite3d->runAction(RepeatForever::create(RotateBy::create(10.0f, Vec3(0.0f, 360.0f, 0.0f))));
|
2019-11-23 20:27:39 +08:00
|
|
|
_layerBillBoard->addChild(sprite3d);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +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-11-23 20:27:39 +08:00
|
|
|
_camera->setPosition3D(Vec3(0.0f, 100.0f, 230.0f));
|
2021-12-28 21:27:32 +08:00
|
|
|
_camera->lookAt(Vec3(0, 0, 0), Vec3(0.0f, 1.0f, 0.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 16);
|
2021-12-28 21:27:32 +08:00
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "rotate+");
|
|
|
|
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(BillBoardTest::rotateCameraCallback, this, 10));
|
|
|
|
auto label2 = Label::createWithTTF(ttfConfig, "rotate-");
|
|
|
|
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(BillBoardTest::rotateCameraCallback, this, -10));
|
|
|
|
auto menu = Menu::create(menuItem1, menuItem2, nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
2021-12-28 21:27:32 +08:00
|
|
|
menuItem1->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 160));
|
|
|
|
menuItem2->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 190));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(menu, 0);
|
|
|
|
_layerBillBoard->setCameraMask(2);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
label1 = Label::createWithTTF(ttfConfig, "Point Oriented");
|
|
|
|
menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(BillBoardTest::menuCallback_orientedPoint, this));
|
|
|
|
label2 = Label::createWithTTF(ttfConfig, "Plane Oriented");
|
|
|
|
menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(BillBoardTest::menuCallback_orientedPlane, this));
|
|
|
|
menuItem1->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 100));
|
|
|
|
menuItem2->setPosition(Vec2(s.width - 80, VisibleRect::top().y - 130));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
menu = Menu::create(menuItem1, menuItem2, nullptr);
|
2021-12-28 21:27:32 +08:00
|
|
|
menu->setPosition(Vec2(0, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(menu, 10);
|
|
|
|
menuCallback_orientedPoint(nullptr);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
schedule(CC_SCHEDULE_SELECTOR(BillBoardTest::update));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BillBoardTest::menuCallback_orientedPoint(Ref* sender)
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
for (auto& billboard : _billboards)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
billboard->setMode(BillBoard::Mode::VIEW_POINT_ORIENTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BillBoardTest::menuCallback_orientedPlane(Ref* sender)
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
for (auto& billboard : _billboards)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
billboard->setMode(BillBoard::Mode::VIEW_PLANE_ORIENTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BillBoardTest::~BillBoardTest()
|
|
|
|
{
|
|
|
|
if (_camera)
|
|
|
|
{
|
|
|
|
_camera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string BillBoardTest::title() const
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
auto billboard = BillBoard::create(imgs[(unsigned int)(CCRANDOM_0_1() * 1 + 0.5f)]);
|
|
|
|
billboard->setScale(0.5f);
|
|
|
|
billboard->setPosition3D(Vec3(p.x, p.y, -150.0f + 30 * i));
|
|
|
|
billboard->setOpacity(static_cast<uint8_t>(CCRANDOM_0_1() * 128 + 128));
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_layerBillBoard->addChild(billboard);
|
|
|
|
_billboards.push_back(billboard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void BillBoardTest::addNewAniBillBoardWithCoords(Vec3 p)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 10; ++i)
|
|
|
|
{
|
|
|
|
auto billboardAni = BillBoard::create("Images/grossini.png");
|
|
|
|
billboardAni->setScale(0.5f);
|
2021-12-28 21:27:32 +08:00
|
|
|
billboardAni->setPosition3D(Vec3(p.x, p.y, -150.0f + 30 * i));
|
2019-11-23 20:27:39 +08:00
|
|
|
_layerBillBoard->addChild(billboardAni);
|
|
|
|
|
|
|
|
auto animation = Animation::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
for (int i = 1; i < 15; i++)
|
2019-11-23 20:27:39 +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);
|
|
|
|
billboardAni->runAction(RepeatForever::create(action));
|
|
|
|
billboardAni->setOpacity(static_cast<uint8_t>(CCRANDOM_0_1() * 128 + 128));
|
|
|
|
_billboards.push_back(billboardAni);
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 21:27:32 +08:00
|
|
|
void BillBoardTest::update(float dt) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void BillBoardTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
if (touches.size() == 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto PreviousLocation = touch->getPreviousLocation();
|
2021-12-28 21:27:32 +08:00
|
|
|
Point newPos = PreviousLocation - location;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
2021-12-28 21:27:32 +08:00
|
|
|
cameraDir.y = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
2021-12-28 21:27:32 +08:00
|
|
|
cameraRightDir.y = 0;
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
cameraPos += cameraDir * newPos.y * 0.5;
|
|
|
|
cameraPos += cameraRightDir * newPos.x * 0.5;
|
|
|
|
_camera->setPosition3D(cameraPos);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void BillBoardTest::rotateCameraCallback(Ref* sender, float value)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
Vec3 rotation3D = _camera->getRotation3D();
|
|
|
|
rotation3D.y += value;
|
2019-11-23 20:27:39 +08:00
|
|
|
_camera->setRotation3D(rotation3D);
|
|
|
|
}
|