axmol/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.cpp

1550 lines
53 KiB
C++
Raw Normal View History

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.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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 "Camera3DTest.h"
#include "testResource.h"
#include "ui/UISlider.h"
#include "platform/CCFileUtils.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/Device.h"
USING_NS_AX;
2019-11-23 20:27:39 +08:00
#define SET_UNIFORM(name, addr, size) \
do \
{ \
auto _loc_ = _programState1->getUniformLocation(name); \
_programState1->setUniform(_loc_, (addr), (size)); \
_loc_ = _programState2->getUniformLocation(name); \
_programState2->setUniform(_loc_, (addr), (size)); \
} while (false)
2019-11-23 20:27:39 +08:00
enum
{
IDC_NEXT = 100,
IDC_BACK,
IDC_RESTART
};
Camera3DTests::Camera3DTests()
{
ADD_TEST_CASE(CameraRotationTest);
ADD_TEST_CASE(Camera3DTestDemo);
ADD_TEST_CASE(CameraCullingDemo);
ADD_TEST_CASE(FogTestDemo);
ADD_TEST_CASE(CameraArcBallDemo);
// ADD_TEST_CASE(CameraFrameBufferTest); //TODO render target
2019-11-23 20:27:39 +08:00
ADD_TEST_CASE(BackgroundColorBrushTest);
}
//------------------------------------------------------------------
//
// Camera Rotation Test
//
//------------------------------------------------------------------
CameraRotationTest::CameraRotationTest()
{
auto s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
_camControlNode = Node::create();
_camControlNode->setPositionNormalized(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
addChild(_camControlNode);
_camNode = Node::create();
_camNode->setPositionZ(Camera::getDefaultCamera()->getPosition3D().z);
_camControlNode->addChild(_camNode);
auto mesh = MeshRenderer::create();
mesh->setPosition(s.width / 2, s.height / 2);
addChild(mesh);
2019-11-23 20:27:39 +08:00
auto lship = Label::create();
lship->setString("Ship");
lship->setPosition(0, 20);
mesh->addChild(lship);
// Billboards
// Yellow is at the back
2019-11-23 20:27:39 +08:00
bill1 = BillBoard::create("Images/Icon.png");
bill1->setPosition3D(Vec3(50.0f, 10.0f, -10.0f));
bill1->setColor(Color3B::YELLOW);
bill1->setScale(0.6f);
mesh->addChild(bill1);
2019-11-23 20:27:39 +08:00
l1 = Label::create();
l1->setPosition(Vec2(0.0f, -10.0f));
2019-11-23 20:27:39 +08:00
l1->setString("Billboard1");
l1->setColor(Color3B::WHITE);
l1->setScale(3);
bill1->addChild(l1);
auto p1 = ParticleSystemQuad::create("Particles/SmallSun.plist");
p1->setPosition(30.0f, 80.0f);
2019-11-23 20:27:39 +08:00
bill1->addChild(p1);
2019-11-23 20:27:39 +08:00
bill2 = BillBoard::create("Images/Icon.png");
bill2->setPosition3D(Vec3(-50.0f, -10.0f, 10.0f));
bill2->setScale(0.6f);
mesh->addChild(bill2);
2019-11-23 20:27:39 +08:00
l2 = Label::create();
l2->setString("Billboard2");
l2->setPosition(Vec2(0.0f, -10.0f));
2019-11-23 20:27:39 +08:00
l2->setColor(Color3B::WHITE);
l2->setScale(3);
bill2->addChild(l2);
2019-11-23 20:27:39 +08:00
auto p2 = ParticleSystemQuad::create("Particles/SmallSun.plist");
p2->setPosition(30, 80);
2019-11-23 20:27:39 +08:00
bill2->addChild(p2);
// 3D models
auto model = MeshRenderer::create("MeshRendererTest/boss1.obj");
2019-11-23 20:27:39 +08:00
model->setScale(4);
model->setTexture("MeshRendererTest/boss.png");
model->setPosition3D(Vec3(s.width / 2, s.height / 2, 0));
2019-11-23 20:27:39 +08:00
addChild(model);
// Listener
_lis = EventListenerTouchOneByOne::create();
_lis->onTouchBegan = [](Touch* t, Event* e) { return true; };
2019-11-23 20:27:39 +08:00
_lis->onTouchMoved = [this](Touch* t, Event* e) {
float dx = t->getDelta().x;
Vec3 rot = _camControlNode->getRotation3D();
rot.y += dx;
_camControlNode->setRotation3D(rot);
Vec3 worldPos;
_camNode->getNodeToWorldTransform().getTranslation(&worldPos);
Camera::getDefaultCamera()->setPosition3D(worldPos);
Camera::getDefaultCamera()->lookAt(_camControlNode->getPosition3D());
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_lis, this);
2022-07-16 10:43:05 +08:00
schedule(AX_SCHEDULE_SELECTOR(CameraRotationTest::update));
2019-11-23 20:27:39 +08:00
}
CameraRotationTest::~CameraRotationTest()
{
Director::getInstance()->getEventDispatcher()->removeEventListener(_lis);
}
std::string CameraRotationTest::title() const
{
return "Camera Rotation Test";
}
std::string CameraRotationTest::subtitle() const
{
return "Slide to rotate";
}
void CameraRotationTest::onEnter()
{
CameraBaseTest::onEnter();
}
void CameraRotationTest::onExit()
{
CameraBaseTest::onExit();
}
void CameraRotationTest::update(float dt) {}
2019-11-23 20:27:39 +08:00
//------------------------------------------------------------------
//
// Camera3DTestDemo
//
//------------------------------------------------------------------
Camera3DTestDemo::Camera3DTestDemo()
: _cameraType(CameraType::Free)
, _incRot(nullptr)
, _decRot(nullptr)
, _camera(nullptr)
, _bZoomOut(false)
, _bZoomIn(false)
, _bRotateLeft(false)
, _bRotateRight(false)
{}
Camera3DTestDemo::~Camera3DTestDemo() {}
void Camera3DTestDemo::reachEndCallBack() {}
2019-11-23 20:27:39 +08:00
std::string Camera3DTestDemo::title() const
{
return "Testing Camera";
}
void Camera3DTestDemo::scaleCameraCallback(Ref* sender, float value)
2019-11-23 20:27:39 +08:00
{
if (_camera && _cameraType != CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 cameraPos = _camera->getPosition3D();
cameraPos += cameraPos.getNormalized() * value;
2019-11-23 20:27:39 +08:00
_camera->setPosition3D(cameraPos);
}
}
void Camera3DTestDemo::rotateCameraCallback(Ref* sender, float value)
2019-11-23 20:27:39 +08:00
{
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y += value;
2019-11-23 20:27:39 +08:00
_camera->setRotation3D(rotation3D);
}
}
void Camera3DTestDemo::SwitchViewCallback(Ref* sender, CameraType cameraType)
{
if (_cameraType == cameraType)
2019-11-23 20:27:39 +08:00
{
return;
2019-11-23 20:27:39 +08:00
}
_cameraType = cameraType;
if (_cameraType == CameraType::Free)
2019-11-23 20:27:39 +08:00
{
_camera->setPosition3D(Vec3(0, 130, 130) + _mesh->getPosition3D());
_camera->lookAt(_mesh->getPosition3D());
2019-11-23 20:27:39 +08:00
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
}
else if (_cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 newFaceDir;
_mesh->getWorldToNodeTransform().getForwardVector(&newFaceDir);
2019-11-23 20:27:39 +08:00
newFaceDir.normalize();
_camera->setPosition3D(Vec3(0, 35, 0) + _mesh->getPosition3D());
_camera->lookAt(_mesh->getPosition3D() + newFaceDir * 50);
2019-11-23 20:27:39 +08:00
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::GRAY);
_ZoomOutlabel->setColor(Color3B::GRAY);
}
else if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
{
_camera->setPosition3D(Vec3(0, 130, 130) + _mesh->getPosition3D());
_camera->lookAt(_mesh->getPosition3D());
2019-11-23 20:27:39 +08:00
_RotateRightlabel->setColor(Color3B::GRAY);
_RotateLeftlabel->setColor(Color3B::GRAY);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
}
}
void Camera3DTestDemo::onEnter()
{
CameraBaseTest::onEnter();
_mesh = nullptr;
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
2022-07-16 10:43:05 +08:00
listener->onTouchesBegan = AX_CALLBACK_2(Camera3DTestDemo::onTouchesBegan, this);
listener->onTouchesMoved = AX_CALLBACK_2(Camera3DTestDemo::onTouchesMoved, this);
listener->onTouchesEnded = AX_CALLBACK_2(Camera3DTestDemo::onTouchesEnded, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
_curState = State_None;
addNewSpriteWithCoords(Vec3(0, 0, 0), "MeshRendererTest/girl.c3b", true, 0.2f, true);
2019-11-23 20:27:39 +08:00
TTFConfig ttfConfig("fonts/arial.ttf", 20);
2019-11-23 20:27:39 +08:00
auto containerForLabel1 = Node::create();
_ZoomOutlabel = Label::createWithTTF(ttfConfig, "zoom out");
_ZoomOutlabel->setPosition(s.width - 50, VisibleRect::top().y - 20);
2019-11-23 20:27:39 +08:00
containerForLabel1->addChild(_ZoomOutlabel);
addChild(containerForLabel1, 10);
2019-11-23 20:27:39 +08:00
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
2022-07-16 10:43:05 +08:00
listener1->onTouchBegan = AX_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOut, this);
listener1->onTouchEnded = AX_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOutEnd, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _ZoomOutlabel);
2019-11-23 20:27:39 +08:00
auto containerForLabel2 = Node::create();
_ZoomInlabel = Label::createWithTTF(ttfConfig, "zoom in");
_ZoomInlabel->setPosition(s.width - 50, VisibleRect::top().y - 70);
2019-11-23 20:27:39 +08:00
containerForLabel2->addChild(_ZoomInlabel);
addChild(containerForLabel2, 10);
2019-11-23 20:27:39 +08:00
auto listener2 = EventListenerTouchOneByOne::create();
listener2->setSwallowTouches(true);
2022-07-16 10:43:05 +08:00
listener2->onTouchBegan = AX_CALLBACK_2(Camera3DTestDemo::onTouchesZoomIn, this);
listener2->onTouchEnded = AX_CALLBACK_2(Camera3DTestDemo::onTouchesZoomInEnd, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, _ZoomInlabel);
2019-11-23 20:27:39 +08:00
auto containerForLabel3 = Node::create();
_RotateLeftlabel = Label::createWithTTF(ttfConfig, "rotate left");
_RotateLeftlabel->setPosition(s.width - 50, VisibleRect::top().y - 120);
2019-11-23 20:27:39 +08:00
containerForLabel3->addChild(_RotateLeftlabel);
addChild(containerForLabel3, 10);
2019-11-23 20:27:39 +08:00
auto listener3 = EventListenerTouchOneByOne::create();
listener3->setSwallowTouches(true);
2022-07-16 10:43:05 +08:00
listener3->onTouchBegan = AX_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeft, this);
listener3->onTouchEnded = AX_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeftEnd, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener3, _RotateLeftlabel);
2019-11-23 20:27:39 +08:00
auto containerForLabel4 = Node::create();
_RotateRightlabel = Label::createWithTTF(ttfConfig, "rotate right");
_RotateRightlabel->setPosition(s.width - 50, VisibleRect::top().y - 170);
2019-11-23 20:27:39 +08:00
containerForLabel4->addChild(_RotateRightlabel);
addChild(containerForLabel4, 10);
2019-11-23 20:27:39 +08:00
auto listener4 = EventListenerTouchOneByOne::create();
listener4->setSwallowTouches(true);
2022-07-16 10:43:05 +08:00
listener4->onTouchBegan = AX_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRight, this);
listener4->onTouchEnded = AX_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRightEnd, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener4, _RotateRightlabel);
auto label1 = Label::createWithTTF(ttfConfig, "free ");
auto menuItem1 =
2022-07-16 10:43:05 +08:00
MenuItemLabel::create(label1, AX_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback, this, CameraType::Free));
auto label2 = Label::createWithTTF(ttfConfig, "third person");
auto menuItem2 = MenuItemLabel::create(
2022-07-16 10:43:05 +08:00
label2, AX_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback, this, CameraType::ThirdPerson));
auto label3 = Label::createWithTTF(ttfConfig, "first person");
auto menuItem3 = MenuItemLabel::create(
2022-07-16 10:43:05 +08:00
label3, AX_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback, this, CameraType::FirstPerson));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
2019-11-23 20:27:39 +08:00
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x + 100, VisibleRect::top().y - 50);
menuItem2->setPosition(VisibleRect::left().x + 100, VisibleRect::top().y - 100);
menuItem3->setPosition(VisibleRect::left().x + 100, VisibleRect::top().y - 150);
2019-11-23 20:27:39 +08:00
addChild(menu, 0);
2022-07-16 10:43:05 +08:00
schedule(AX_SCHEDULE_SELECTOR(Camera3DTestDemo::updateCamera), 0.0f);
2019-11-23 20:27:39 +08:00
if (_camera == nullptr)
{
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2019-11-23 20:27:39 +08:00
_camera->setCameraFlag(CameraFlag::USER1);
_layer3D->addChild(_camera);
}
SwitchViewCallback(this, CameraType::ThirdPerson);
DrawNode3D* line = DrawNode3D::create();
// draw x
for (int j = -20; j <= 20; j++)
2019-11-23 20:27:39 +08:00
{
line->drawLine(Vec3(-100.0f, 0.0f, 5.0f * j), Vec3(100.0f, 0.0f, 5.0f * j), Color4F(1, 0, 0, 1));
2019-11-23 20:27:39 +08:00
}
// draw z
for (int j = -20; j <= 20; j++)
2019-11-23 20:27:39 +08:00
{
line->drawLine(Vec3(5.0f * j, 0.0f, -100.0f), Vec3(5.0f * j, 0.0f, 100.0f), Color4F(0, 0, 1, 1));
2019-11-23 20:27:39 +08:00
}
// draw y
line->drawLine(Vec3(0.0f, -50.0f, 0.0f), Vec3(0, 0, 0), Color4F(0, 0.5, 0, 1));
line->drawLine(Vec3(0, 0, 0), Vec3(0, 50.0f, 0), Color4F(0, 1, 0, 1));
2019-11-23 20:27:39 +08:00
_layer3D->addChild(line);
_layer3D->setCameraMask(2);
}
void Camera3DTestDemo::onExit()
{
CameraBaseTest::onExit();
if (_camera)
{
_camera = nullptr;
}
}
void Camera3DTestDemo::addNewSpriteWithCoords(Vec3 p,
std::string fileName,
bool playAnimation,
float scale,
bool bindCamera)
2019-11-23 20:27:39 +08:00
{
auto mesh = MeshRenderer::create(fileName);
_layer3D->addChild(mesh);
float globalZOrder = mesh->getGlobalZOrder();
mesh->setPosition3D(Vec3(p.x, p.y, p.z));
mesh->setGlobalZOrder(globalZOrder);
if (playAnimation)
2019-11-23 20:27:39 +08:00
{
auto animation = Animation3D::create(fileName, "Take 001");
2019-11-23 20:27:39 +08:00
if (animation)
{
auto animate = Animate3D::create(animation);
mesh->runAction(RepeatForever::create(animate));
2019-11-23 20:27:39 +08:00
}
}
if (bindCamera)
2019-11-23 20:27:39 +08:00
{
_mesh = mesh;
2019-11-23 20:27:39 +08:00
}
mesh->setScale(scale);
2019-11-23 20:27:39 +08:00
}
void Camera3DTestDemo::onTouchesBegan(const std::vector<Touch*>& touches, axis::Event* event) {}
void Camera3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, axis::Event* event)
2019-11-23 20:27:39 +08:00
{
if (touches.size() == 1)
2019-11-23 20:27:39 +08:00
{
auto touch = touches[0];
2019-11-23 20:27:39 +08:00
auto location = touch->getLocation();
Point newPos = touch->getPreviousLocation() - location;
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 cameraDir;
Vec3 cameraRightDir;
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
cameraDir.normalize();
cameraDir.y = 0;
2019-11-23 20:27:39 +08:00
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
cameraRightDir.normalize();
cameraRightDir.y = 0;
Vec3 cameraPos = _camera->getPosition3D();
cameraPos += cameraDir * newPos.y * 0.1f;
cameraPos += cameraRightDir * newPos.x * 0.1f;
2019-11-23 20:27:39 +08:00
_camera->setPosition3D(cameraPos);
if (_mesh && _cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
_mesh->setPosition3D(Vec3(_camera->getPositionX(), 0, _camera->getPositionZ()));
_targetPos = _mesh->getPosition3D();
2019-11-23 20:27:39 +08:00
}
}
}
}
void Camera3DTestDemo::move3D(float elapsedTime)
{
if (_mesh)
2019-11-23 20:27:39 +08:00
{
Vec3 curPos = _mesh->getPosition3D();
2019-11-23 20:27:39 +08:00
Vec3 newFaceDir = _targetPos - curPos;
newFaceDir.y = 0.0f;
2019-11-23 20:27:39 +08:00
newFaceDir.normalize();
Vec3 offset = newFaceDir * 25.0f * elapsedTime;
curPos += offset;
_mesh->setPosition3D(curPos);
if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 cameraPos = _camera->getPosition3D();
cameraPos.x += offset.x;
cameraPos.z += offset.z;
2019-11-23 20:27:39 +08:00
_camera->setPosition3D(cameraPos);
}
}
}
void Camera3DTestDemo::updateState(float elapsedTime)
{
if (_mesh)
2019-11-23 20:27:39 +08:00
{
Vec3 curPos = _mesh->getPosition3D();
2019-11-23 20:27:39 +08:00
Vec3 curFaceDir;
_mesh->getNodeToWorldTransform().getForwardVector(&curFaceDir);
curFaceDir = -curFaceDir;
2019-11-23 20:27:39 +08:00
curFaceDir.normalize();
Vec3 newFaceDir = _targetPos - curPos;
newFaceDir.y = 0.0f;
2019-11-23 20:27:39 +08:00
newFaceDir.normalize();
float cosAngle = std::fabs(Vec3::dot(curFaceDir, newFaceDir) - 1.0f);
float dist = curPos.distanceSquared(_targetPos);
if (dist <= 4.0f)
2019-11-23 20:27:39 +08:00
{
if (cosAngle <= 0.01f)
2019-11-23 20:27:39 +08:00
_curState = State_Idle;
else
_curState = State_Rotate;
}
else
{
if (cosAngle > 0.01f)
2019-11-23 20:27:39 +08:00
_curState = State_Rotate | State_Move;
else
_curState = State_Move;
}
}
}
void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, axis::Event* event)
2019-11-23 20:27:39 +08:00
{
for (auto& item : touches)
2019-11-23 20:27:39 +08:00
{
auto touch = item;
2019-11-23 20:27:39 +08:00
auto location = touch->getLocationInView();
if (_camera)
2019-11-23 20:27:39 +08:00
{
if (_mesh && _cameraType == CameraType::ThirdPerson && _bZoomOut == false && _bZoomIn == false &&
_bRotateLeft == false && _bRotateRight == false)
2019-11-23 20:27:39 +08:00
{
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
2019-11-23 20:27:39 +08:00
auto size = Director::getInstance()->getWinSize();
nearP = _camera->unproject(nearP);
farP = _camera->unproject(farP);
2019-11-23 20:27:39 +08:00
Vec3 dir(farP - nearP);
float dist = 0.0f;
float ndd = Vec3::dot(Vec3(0, 1, 0), dir);
if (ndd == 0)
dist = 0.0f;
float ndo = Vec3::dot(Vec3(0, 1, 0), nearP);
dist = (0 - ndo) / ndd;
Vec3 p = nearP + dist * dir;
if (p.x > 100)
2019-11-23 20:27:39 +08:00
p.x = 100;
if (p.x < -100)
2019-11-23 20:27:39 +08:00
p.x = -100;
if (p.z > 100)
2019-11-23 20:27:39 +08:00
p.z = 100;
if (p.z < -100)
2019-11-23 20:27:39 +08:00
p.z = -100;
_targetPos = p;
2019-11-23 20:27:39 +08:00
}
}
}
}
void onTouchesCancelled(const std::vector<Touch*>& touches, axis::Event* event) {}
2019-11-23 20:27:39 +08:00
void Camera3DTestDemo::updateCamera(float fDelta)
{
if (_mesh)
2019-11-23 20:27:39 +08:00
{
if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
{
updateState(fDelta);
if (isState(_curState, State_Move))
2019-11-23 20:27:39 +08:00
{
move3D(fDelta);
if (isState(_curState, State_Rotate))
2019-11-23 20:27:39 +08:00
{
Vec3 curPos = _mesh->getPosition3D();
2019-11-23 20:27:39 +08:00
Vec3 newFaceDir = _targetPos - curPos;
newFaceDir.y = 0;
2019-11-23 20:27:39 +08:00
newFaceDir.normalize();
Vec3 up;
_mesh->getNodeToWorldTransform().getUpVector(&up);
2019-11-23 20:27:39 +08:00
up.normalize();
Vec3 right;
Vec3::cross(-newFaceDir, up, &right);
2019-11-23 20:27:39 +08:00
right.normalize();
Vec3 pos = Vec3(0, 0, 0);
2019-11-23 20:27:39 +08:00
Mat4 mat;
mat.m[0] = right.x;
mat.m[1] = right.y;
mat.m[2] = right.z;
mat.m[3] = 0.0f;
2019-11-23 20:27:39 +08:00
mat.m[4] = up.x;
mat.m[5] = up.y;
mat.m[6] = up.z;
mat.m[7] = 0.0f;
2019-11-23 20:27:39 +08:00
mat.m[8] = newFaceDir.x;
mat.m[9] = newFaceDir.y;
mat.m[10] = newFaceDir.z;
mat.m[11] = 0.0f;
2019-11-23 20:27:39 +08:00
mat.m[12] = pos.x;
mat.m[13] = pos.y;
mat.m[14] = pos.z;
mat.m[15] = 1.0f;
_mesh->setAdditionalTransform(&mat);
2019-11-23 20:27:39 +08:00
}
}
}
if (_bZoomOut == true)
2019-11-23 20:27:39 +08:00
{
if (_camera)
2019-11-23 20:27:39 +08:00
{
if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 lookDir = _camera->getPosition3D() - _mesh->getPosition3D();
2019-11-23 20:27:39 +08:00
Vec3 cameraPos = _camera->getPosition3D();
if (lookDir.length() <= 300)
2019-11-23 20:27:39 +08:00
{
cameraPos += lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
else if (_cameraType == CameraType::Free)
2019-11-23 20:27:39 +08:00
{
Vec3 cameraPos = _camera->getPosition3D();
if (cameraPos.length() <= 300)
2019-11-23 20:27:39 +08:00
{
cameraPos += cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
if (_bZoomIn == true)
2019-11-23 20:27:39 +08:00
{
if (_camera)
2019-11-23 20:27:39 +08:00
{
if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 lookDir = _camera->getPosition3D() - _mesh->getPosition3D();
2019-11-23 20:27:39 +08:00
Vec3 cameraPos = _camera->getPosition3D();
if (lookDir.length() >= 50)
2019-11-23 20:27:39 +08:00
{
cameraPos -= lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
else if (_cameraType == CameraType::Free)
2019-11-23 20:27:39 +08:00
{
Vec3 cameraPos = _camera->getPosition3D();
if (cameraPos.length() >= 50)
2019-11-23 20:27:39 +08:00
{
cameraPos -= cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
if (_bRotateLeft == true)
2019-11-23 20:27:39 +08:00
{
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y += 1;
2019-11-23 20:27:39 +08:00
_camera->setRotation3D(rotation3D);
}
}
if (_bRotateRight == true)
2019-11-23 20:27:39 +08:00
{
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y -= 1;
2019-11-23 20:27:39 +08:00
_camera->setRotation3D(rotation3D);
}
}
}
}
bool Camera3DTestDemo::onTouchesCommon(Touch* touch, Event* event, bool* touchProperty)
{
auto target = static_cast<Label*>(event->getCurrentTarget());
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
if (rect.containsPoint(locationInNode))
{
*touchProperty = true;
return true;
}
return false;
}
bool Camera3DTestDemo::isState(unsigned int state, unsigned int bit) const
2019-11-23 20:27:39 +08:00
{
return (state & bit) == bit;
}
bool Camera3DTestDemo::onTouchesZoomOut(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bZoomOut);
}
void Camera3DTestDemo::onTouchesZoomOutEnd(Touch* touch, Event* event)
{
_bZoomOut = false;
}
bool Camera3DTestDemo::onTouchesZoomIn(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bZoomIn);
}
void Camera3DTestDemo::onTouchesZoomInEnd(Touch* touch, Event* event)
{
_bZoomIn = false;
}
bool Camera3DTestDemo::onTouchesRotateLeft(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bRotateLeft);
}
void Camera3DTestDemo::onTouchesRotateLeftEnd(Touch* touch, Event* event)
{
_bRotateLeft = false;
}
bool Camera3DTestDemo::onTouchesRotateRight(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bRotateRight);
}
void Camera3DTestDemo::onTouchesRotateRightEnd(Touch* touch, Event* event)
{
_bRotateRight = false;
}
////////////////////////////////////////////////////////////
// CameraCullingDemo
CameraCullingDemo::CameraCullingDemo()
: _layer3D(nullptr)
, _cameraType(CameraType::FirstPerson)
, _cameraFirst(nullptr)
, _cameraThird(nullptr)
, _moveAction(nullptr)
, _drawAABB(nullptr)
, _drawFrustum(nullptr)
, _row(3)
{}
CameraCullingDemo::~CameraCullingDemo() {}
2019-11-23 20:27:39 +08:00
std::string CameraCullingDemo::title() const
{
return "Camera Frustum Clipping";
}
void CameraCullingDemo::onEnter()
{
CameraBaseTest::onEnter();
2022-07-16 10:43:05 +08:00
schedule(AX_SCHEDULE_SELECTOR(CameraCullingDemo::update), 0.0f);
2019-11-23 20:27:39 +08:00
auto s = Director::getInstance()->getWinSize();
/*auto listener = EventListenerTouchAllAtOnce::create();
2022-07-16 10:43:05 +08:00
listener->onTouchesBegan = AX_CALLBACK_2(Camera3DTestDemo::onTouchesBegan, this);
listener->onTouchesMoved = AX_CALLBACK_2(Camera3DTestDemo::onTouchesMoved, this);
listener->onTouchesEnded = AX_CALLBACK_2(Camera3DTestDemo::onTouchesEnded, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);*/
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
2019-11-23 20:27:39 +08:00
// switch camera
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(20);
2022-07-16 10:43:05 +08:00
auto menuItem1 = MenuItemFont::create("Switch Camera", AX_CALLBACK_1(CameraCullingDemo::switchViewCallback, this));
menuItem1->setColor(Color3B(0, 200, 20));
auto menu = Menu::create(menuItem1, NULL);
2019-11-23 20:27:39 +08:00
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y - 70);
2019-11-23 20:27:39 +08:00
addChild(menu, 1);
2019-11-23 20:27:39 +08:00
// + -
MenuItemFont::setFontSize(40);
2022-07-16 10:43:05 +08:00
auto decrease = MenuItemFont::create(" - ", AX_CALLBACK_1(CameraCullingDemo::delMeshCallback, this));
decrease->setColor(Color3B(0, 200, 20));
2022-07-16 10:43:05 +08:00
auto increase = MenuItemFont::create(" + ", AX_CALLBACK_1(CameraCullingDemo::addMeshCallback, this));
increase->setColor(Color3B(0, 200, 20));
2019-11-23 20:27:39 +08:00
menu = Menu::create(decrease, increase, nullptr);
menu->alignItemsHorizontally();
menu->setPosition(Vec2(s.width - 60, VisibleRect::top().y - 70));
2019-11-23 20:27:39 +08:00
addChild(menu, 1);
2019-11-23 20:27:39 +08:00
TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
_labelMeshCount = Label::createWithTTF(ttfCount, "0 sprits");
_labelMeshCount->setColor(Color3B(0, 200, 20));
_labelMeshCount->setPosition(Vec2(s.width / 2, VisibleRect::top().y - 70));
addChild(_labelMeshCount);
2019-11-23 20:27:39 +08:00
// aabb drawNode3D
_drawAABB = DrawNode3D::create();
_drawAABB->setCameraMask((unsigned short)CameraFlag::USER1);
2019-11-23 20:27:39 +08:00
addChild(_drawAABB);
2019-11-23 20:27:39 +08:00
// frustum drawNode3D
_drawFrustum = DrawNode3D::create();
_drawFrustum->setCameraMask((unsigned short)CameraFlag::USER1);
2019-11-23 20:27:39 +08:00
addChild(_drawFrustum);
2019-11-23 20:27:39 +08:00
// set camera
switchViewCallback(this);
2019-11-23 20:27:39 +08:00
// add sprite
addMeshCallback(nullptr);
2019-11-23 20:27:39 +08:00
}
void CameraCullingDemo::onExit()
{
CameraBaseTest::onExit();
if (_cameraFirst)
{
_cameraFirst = nullptr;
}
if (_cameraThird)
{
_cameraThird = nullptr;
}
}
void CameraCullingDemo::update(float dt)
{
_drawAABB->clear();
if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
drawCameraFrustum();
Vector<Node*>& children = _layer3D->getChildren();
Vec3 corners[8];
for (const auto& iter : children)
2019-11-23 20:27:39 +08:00
{
const AABB& aabb = static_cast<MeshRenderer*>(iter)->getAABB();
2019-11-23 20:27:39 +08:00
if (_cameraFirst->isVisibleInFrustum(&aabb))
{
aabb.getCorners(corners);
_drawAABB->drawCube(corners, Color4F(0, 1, 0, 1));
}
}
}
void CameraCullingDemo::reachEndCallBack()
{
_cameraFirst->stopActionByTag(100);
auto inverse = MoveTo::create(4.f, Vec2(-_cameraFirst->getPositionX(), 0.0f));
inverse->retain();
2019-11-23 20:27:39 +08:00
_moveAction->release();
_moveAction = inverse;
auto rot = RotateBy::create(1.f, Vec3(0.f, 180.f, 0.f));
auto seq = Sequence::create(rot, _moveAction,
2022-07-16 10:43:05 +08:00
CallFunc::create(AX_CALLBACK_0(CameraCullingDemo::reachEndCallBack, this)), nullptr);
2019-11-23 20:27:39 +08:00
seq->setTag(100);
_cameraFirst->runAction(seq);
}
void CameraCullingDemo::switchViewCallback(Ref* sender)
{
auto s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
if (_cameraFirst == nullptr)
{
_cameraFirst = Camera::createPerspective(30.0f, (float)s.width / s.height, 10.0f, 200.0f);
2019-11-23 20:27:39 +08:00
_cameraFirst->setCameraFlag(CameraFlag::USER8);
_cameraFirst->setPosition3D(Vec3(-100.0f, 0.0f, 0.0f));
_cameraFirst->lookAt(Vec3(1000.0f, 0.0f, 0.0f));
2019-11-23 20:27:39 +08:00
_moveAction = MoveTo::create(4.f, Vec2(-_cameraFirst->getPositionX(), 0.0f));
_moveAction->retain();
auto seq = Sequence::create(
2022-07-16 10:43:05 +08:00
_moveAction, CallFunc::create(AX_CALLBACK_0(CameraCullingDemo::reachEndCallBack, this)), nullptr);
2019-11-23 20:27:39 +08:00
seq->setTag(100);
_cameraFirst->runAction(seq);
addChild(_cameraFirst);
}
2019-11-23 20:27:39 +08:00
if (_cameraThird == nullptr)
{
_cameraThird = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2019-11-23 20:27:39 +08:00
_cameraThird->setCameraFlag(CameraFlag::USER8);
_cameraThird->setPosition3D(Vec3(0.0f, 130.0f, 130.0f));
_cameraThird->lookAt(Vec3(0, 0, 0));
2019-11-23 20:27:39 +08:00
addChild(_cameraThird);
}
if (_cameraType == CameraType::FirstPerson)
2019-11-23 20:27:39 +08:00
{
_cameraType = CameraType::ThirdPerson;
_cameraThird->setCameraFlag(CameraFlag::USER1);
_cameraFirst->setCameraFlag(CameraFlag::USER8);
}
else if (_cameraType == CameraType::ThirdPerson)
2019-11-23 20:27:39 +08:00
{
_cameraType = CameraType::FirstPerson;
_cameraFirst->setCameraFlag(CameraFlag::USER1);
_cameraThird->setCameraFlag(CameraFlag::USER8);
_drawFrustum->clear();
}
}
void CameraCullingDemo::addMeshCallback(Ref* sender)
2019-11-23 20:27:39 +08:00
{
_layer3D->removeAllChildren();
_objects.clear();
_drawAABB->clear();
2019-11-23 20:27:39 +08:00
++_row;
for (int x = -_row; x < _row; x++)
{
for (int z = -_row; z < _row; z++)
{
auto sprite = MeshRenderer::create("MeshRendererTest/orc.c3b");
2019-11-23 20:27:39 +08:00
sprite->setPosition3D(Vec3(x * 30.0f, 0.0f, z * 30.0f));
sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
_objects.emplace_back(sprite);
2019-11-23 20:27:39 +08:00
_layer3D->addChild(sprite);
}
}
2019-11-23 20:27:39 +08:00
// set layer mask.
_layer3D->setCameraMask((unsigned short)CameraFlag::USER1);
2019-11-23 20:27:39 +08:00
// update sprite number
char szText[16];
sprintf(szText, "%d sprits", static_cast<int32_t>(_layer3D->getChildrenCount()));
_labelMeshCount->setString(szText);
2019-11-23 20:27:39 +08:00
}
void CameraCullingDemo::delMeshCallback(Ref* sender)
2019-11-23 20:27:39 +08:00
{
if (_row == 0)
return;
2019-11-23 20:27:39 +08:00
_layer3D->removeAllChildren();
_objects.clear();
2019-11-23 20:27:39 +08:00
--_row;
for (int x = -_row; x < _row; x++)
{
for (int z = -_row; z < _row; z++)
{
auto sprite = MeshRenderer::create("MeshRendererTest/orc.c3b");
2019-11-23 20:27:39 +08:00
sprite->setPosition3D(Vec3(x * 30.0f, 0.0f, z * 30.0f));
_objects.emplace_back(sprite);
2019-11-23 20:27:39 +08:00
_layer3D->addChild(sprite);
}
}
2019-11-23 20:27:39 +08:00
// set layer mask.
_layer3D->setCameraMask((unsigned short)CameraFlag::USER1);
2019-11-23 20:27:39 +08:00
// update sprite number
char szText[16];
sprintf(szText, "%l sprits", static_cast<int32_t>(_layer3D->getChildrenCount()));
_labelMeshCount->setString(szText);
2019-11-23 20:27:39 +08:00
}
void CameraCullingDemo::drawCameraFrustum()
{
_drawFrustum->clear();
auto size = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
Color4F color(1.f, 1.f, 0.f, 1);
2019-11-23 20:27:39 +08:00
// top-left
Vec3 tl_0, tl_1;
Vec3 src(0, 0, 0);
2019-11-23 20:27:39 +08:00
tl_0 = _cameraFirst->unproject(src);
src = Vec3(0, 0, 1);
2019-11-23 20:27:39 +08:00
tl_1 = _cameraFirst->unproject(src);
2019-11-23 20:27:39 +08:00
// top-right
Vec3 tr_0, tr_1;
src = Vec3(size.width, 0, 0);
2019-11-23 20:27:39 +08:00
tr_0 = _cameraFirst->unproject(src);
src = Vec3(size.width, 0, 1);
2019-11-23 20:27:39 +08:00
tr_1 = _cameraFirst->unproject(src);
2019-11-23 20:27:39 +08:00
// bottom-left
Vec3 bl_0, bl_1;
src = Vec3(0, size.height, 0);
2019-11-23 20:27:39 +08:00
bl_0 = _cameraFirst->unproject(src);
src = Vec3(0, size.height, 1);
2019-11-23 20:27:39 +08:00
bl_1 = _cameraFirst->unproject(src);
2019-11-23 20:27:39 +08:00
// bottom-right
Vec3 br_0, br_1;
src = Vec3(size.width, size.height, 0);
2019-11-23 20:27:39 +08:00
br_0 = _cameraFirst->unproject(src);
src = Vec3(size.width, size.height, 1);
2019-11-23 20:27:39 +08:00
br_1 = _cameraFirst->unproject(src);
2019-11-23 20:27:39 +08:00
_drawFrustum->drawLine(tl_0, tl_1, color);
_drawFrustum->drawLine(tr_0, tr_1, color);
_drawFrustum->drawLine(bl_0, bl_1, color);
_drawFrustum->drawLine(br_0, br_1, color);
2019-11-23 20:27:39 +08:00
_drawFrustum->drawLine(tl_0, tr_0, color);
_drawFrustum->drawLine(tr_0, br_0, color);
_drawFrustum->drawLine(br_0, bl_0, color);
_drawFrustum->drawLine(bl_0, tl_0, color);
2019-11-23 20:27:39 +08:00
_drawFrustum->drawLine(tl_1, tr_1, color);
_drawFrustum->drawLine(tr_1, br_1, color);
_drawFrustum->drawLine(br_1, bl_1, color);
_drawFrustum->drawLine(bl_1, tl_1, color);
}
////////////////////////////////////////////////////////////
// CameraArcBallDemo
CameraArcBallDemo::CameraArcBallDemo()
: CameraBaseTest()
, _layer3D(nullptr)
, _cameraType(CameraType::Free)
, _camera(nullptr)
, _drawGrid(nullptr)
, _radius(1.0f)
, _distanceZ(50.0f)
, _operate(OperateCamType::RotateCamera)
, _center(Vec3(0, 0, 0))
, _target(0)
, _mesh1(nullptr)
, _mesh2(nullptr)
{}
CameraArcBallDemo::~CameraArcBallDemo() {}
2019-11-23 20:27:39 +08:00
std::string CameraArcBallDemo::title() const
{
return "Camera ArcBall Moving";
}
void CameraArcBallDemo::onEnter()
{
CameraBaseTest::onEnter();
_rotationQuat.set(0.0f, 0.0f, 0.0f, 1.0f);
2022-07-16 10:43:05 +08:00
schedule(AX_SCHEDULE_SELECTOR(CameraArcBallDemo::update), 0.0f);
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
2022-07-16 10:43:05 +08:00
listener->onTouchesMoved = AX_CALLBACK_2(CameraArcBallDemo::onTouchsMoved, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// switch camera
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(20);
auto menuItem1 =
2022-07-16 10:43:05 +08:00
MenuItemFont::create("Switch Operation", AX_CALLBACK_1(CameraArcBallDemo::switchOperateCallback, this));
menuItem1->setColor(Color3B(0, 200, 20));
auto menuItem2 =
2022-07-16 10:43:05 +08:00
MenuItemFont::create("Switch Target", AX_CALLBACK_1(CameraArcBallDemo::switchTargetCallback, this));
menuItem2->setColor(Color3B(0, 200, 20));
auto menu = Menu::create(menuItem1, menuItem2, NULL);
2019-11-23 20:27:39 +08:00
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y - 70);
menuItem2->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y - 100);
2019-11-23 20:27:39 +08:00
addChild(menu, 1);
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
2019-11-23 20:27:39 +08:00
if (_camera == nullptr)
{
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2019-11-23 20:27:39 +08:00
_camera->setCameraFlag(CameraFlag::USER1);
_camera->setPosition3D(Vec3(0.0f, 10.0f, 50.0f));
_camera->lookAt(Vec3(0, 0, 0), Vec3(0.0f, 1.0f, 0.0f));
_camera->retain();
_layer3D->addChild(_camera);
}
_mesh1 = MeshRenderer::create("MeshRendererTest/orc.c3b");
_mesh1->setScale(0.5);
_mesh1->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
_mesh1->setPosition3D(Vec3(0, 0, 0));
_layer3D->addChild(_mesh1);
2019-11-23 20:27:39 +08:00
_mesh2 = MeshRenderer::create("MeshRendererTest/boss.c3b");
_mesh2->setScale(0.6f);
_mesh2->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
_mesh2->setPosition3D(Vec3(20.0f, 0.0f, 0.0f));
_layer3D->addChild(_mesh2);
2019-11-23 20:27:39 +08:00
_drawGrid = DrawNode3D::create();
2019-11-23 20:27:39 +08:00
// draw x
for (int j = -20; j <= 20; j++)
2019-11-23 20:27:39 +08:00
{
_drawGrid->drawLine(Vec3(-100.0f, 0, 5.0f * j), Vec3(100.0f, 0, 5.0f * j), Color4F(1, 0, 0, 1));
2019-11-23 20:27:39 +08:00
}
// draw z
for (int j = -20; j <= 20; j++)
2019-11-23 20:27:39 +08:00
{
_drawGrid->drawLine(Vec3(5.0f * j, 0, -100.0f), Vec3(5.0f * j, 0, 100.0f), Color4F(0, 0, 1, 1));
2019-11-23 20:27:39 +08:00
}
// draw y
_drawGrid->drawLine(Vec3(0, 0, 0), Vec3(0, 50.0f, 0), Color4F(0, 1, 0, 1));
2019-11-23 20:27:39 +08:00
_layer3D->addChild(_drawGrid);
_layer3D->setCameraMask(2);
updateCameraTransform();
}
void CameraArcBallDemo::onExit()
{
CameraBaseTest::onExit();
if (_camera)
{
_camera = nullptr;
}
}
void CameraArcBallDemo::onTouchsMoved(const std::vector<Touch*>& touchs, Event* event)
2019-11-23 20:27:39 +08:00
{
if (!touchs.empty())
{
if (_operate == OperateCamType::RotateCamera) // arc ball rotate
2019-11-23 20:27:39 +08:00
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 prelocation = touchs[0]->getPreviousLocationInView();
Vec2 location = touchs[0]->getLocationInView();
location.x = 2.0f * (location.x) / (visibleSize.width) - 1.0f;
location.y = 2.0f * (visibleSize.height - location.y) / (visibleSize.height) - 1.0f;
prelocation.x = 2.0f * (prelocation.x) / (visibleSize.width) - 1.0f;
prelocation.y = 2.0f * (visibleSize.height - prelocation.y) / (visibleSize.height) - 1.0f;
2019-11-23 20:27:39 +08:00
Vec3 axes;
float angle;
calculateArcBall(axes, angle, prelocation.x, prelocation.y, location.x,
location.y); // calculate rotation quaternion parameters
Quaternion quat(axes, angle); // get rotation quaternion
2019-11-23 20:27:39 +08:00
_rotationQuat = quat * _rotationQuat;
updateCameraTransform(); // update camera Transform
2019-11-23 20:27:39 +08:00
}
else if (_operate == OperateCamType::MoveCamera) // camera zoom
2019-11-23 20:27:39 +08:00
{
Point newPos = touchs[0]->getPreviousLocation() - touchs[0]->getLocation();
_distanceZ -= newPos.y * 0.1f;
2019-11-23 20:27:39 +08:00
updateCameraTransform();
}
}
}
void CameraArcBallDemo::calculateArcBall(axis::Vec3& axis, float& angle, float p1x, float p1y, float p2x, float p2y)
2019-11-23 20:27:39 +08:00
{
Mat4 rotation_matrix;
Mat4::createRotation(_rotationQuat, &rotation_matrix);
Vec3 uv = rotation_matrix * Vec3(0.0f, 1.0f, 0.0f); // rotation y
Vec3 sv = rotation_matrix * Vec3(1.0f, 0.0f, 0.0f); // rotation x
Vec3 lv = rotation_matrix * Vec3(0.0f, 0.0f, -1.0f); // rotation z
2019-11-23 20:27:39 +08:00
Vec3 p1 = sv * p1x + uv * p1y - lv * projectToSphere(_radius, p1x, p1y); // start point screen transform to 3d
Vec3 p2 = sv * p2x + uv * p2y - lv * projectToSphere(_radius, p2x, p2y); // end point screen transform to 3d
2019-11-23 20:27:39 +08:00
Vec3::cross(p2, p1, &axis); // calculate rotation axis
2019-11-23 20:27:39 +08:00
axis.normalize();
float t = (p2 - p1).length() / (2.0f * _radius);
// clamp -1 to 1
if (t > 1.0)
t = 1.0;
if (t < -1.0)
t = -1.0;
angle = asin(t); // rotation angle
2019-11-23 20:27:39 +08:00
}
/* project an x,y pair onto a sphere of radius r or a
hyperbolic sheet if we are away from the center of the sphere. */
float CameraArcBallDemo::projectToSphere(float r, float x, float y)
2019-11-23 20:27:39 +08:00
{
float d, t, z;
d = sqrt(x * x + y * y);
if (d < r * 0.70710678118654752440) // inside sphere
2019-11-23 20:27:39 +08:00
{
z = sqrt(r * r - d * d);
}
else // on hyperbola
2019-11-23 20:27:39 +08:00
{
t = r / 1.41421356237309504880f;
z = t * t / d;
2019-11-23 20:27:39 +08:00
}
return z;
}
void CameraArcBallDemo::updateCameraTransform()
{
Mat4 trans, rot, center;
Mat4::createTranslation(Vec3(0.0f, 10.0f, _distanceZ), &trans);
Mat4::createRotation(_rotationQuat, &rot);
Mat4::createTranslation(_center, &center);
Mat4 result = center * rot * trans;
_camera->setNodeToParentTransform(result);
}
void CameraArcBallDemo::switchOperateCallback(Ref* sender)
{
if (_operate == OperateCamType::MoveCamera)
2019-11-23 20:27:39 +08:00
{
_operate = OperateCamType::RotateCamera;
}
else if (_operate == OperateCamType::RotateCamera)
2019-11-23 20:27:39 +08:00
{
_operate = OperateCamType::MoveCamera;
}
}
void CameraArcBallDemo::switchTargetCallback(Ref* sender)
{
if (_target == 0)
2019-11-23 20:27:39 +08:00
{
_target = 1;
_center = _mesh2->getPosition3D();
2019-11-23 20:27:39 +08:00
updateCameraTransform();
}
else if (_target == 1)
2019-11-23 20:27:39 +08:00
{
_target = 0;
_center = _mesh1->getPosition3D();
2019-11-23 20:27:39 +08:00
updateCameraTransform();
}
}
void CameraArcBallDemo::update(float dt)
{
// updateCameraTransform();
2019-11-23 20:27:39 +08:00
}
////////////////////////////////////////////////////////////
// FogTestDemo
FogTestDemo::FogTestDemo() : CameraBaseTest() {}
2019-11-23 20:27:39 +08:00
FogTestDemo::~FogTestDemo()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_programState1);
AX_SAFE_RELEASE_NULL(_programState2);
2019-11-23 20:27:39 +08:00
}
std::string FogTestDemo::title() const
{
return "Fog Test Demo";
}
void FogTestDemo::onEnter()
{
CameraBaseTest::onEnter();
2022-07-16 10:43:05 +08:00
schedule(AX_SCHEDULE_SELECTOR(FogTestDemo::update), 0.0f);
Director::getInstance()->setClearColor(Color4F(0.5, 0.5, 0.5, 1));
2019-11-23 20:27:39 +08:00
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
2022-07-16 10:43:05 +08:00
listener->onTouchesMoved = AX_CALLBACK_2(FogTestDemo::onTouchesMoved, this);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// switch fog type
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig, "Linear ");
2022-07-16 10:43:05 +08:00
auto menuItem1 = MenuItemLabel::create(label1, AX_CALLBACK_1(FogTestDemo::switchTypeCallback, this, 0));
auto label2 = Label::createWithTTF(ttfConfig, "Exp");
2022-07-16 10:43:05 +08:00
auto menuItem2 = MenuItemLabel::create(label2, AX_CALLBACK_1(FogTestDemo::switchTypeCallback, this, 1));
auto label3 = Label::createWithTTF(ttfConfig, "Exp2");
2022-07-16 10:43:05 +08:00
auto menuItem3 = MenuItemLabel::create(label3, AX_CALLBACK_1(FogTestDemo::switchTypeCallback, this, 2));
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
2019-11-23 20:27:39 +08:00
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x + 60, VisibleRect::top().y - 50);
menuItem2->setPosition(VisibleRect::left().x + 60, VisibleRect::top().y - 100);
menuItem3->setPosition(VisibleRect::left().x + 60, VisibleRect::top().y - 150);
addChild(menu, 0);
2021-12-31 11:00:35 +08:00
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_programState1);
AX_SAFE_RELEASE_NULL(_programState2);
2019-11-23 20:27:39 +08:00
auto vertexSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.frag");
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(program);
_mesh1 = MeshRenderer::create("MeshRendererTest/teapot.c3b");
_mesh2 = MeshRenderer::create("MeshRendererTest/teapot.c3b");
2019-11-23 20:27:39 +08:00
_mesh1->setProgramState(_programState1);
_mesh2->setProgramState(_programState2);
2019-11-23 20:27:39 +08:00
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogStart = 10;
float fogEnd = 60;
int fogEquation = 0;
2019-11-23 20:27:39 +08:00
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogStart", &fogStart, sizeof(fogStart));
SET_UNIFORM("u_fogEnd", &fogEnd, sizeof(fogEnd));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
2019-11-23 20:27:39 +08:00
_layer3D->addChild(_mesh1);
_mesh1->setPosition3D(Vec3(0, 0, 0));
_mesh1->setScale(2.0f);
_mesh1->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
2019-11-23 20:27:39 +08:00
_layer3D->addChild(_mesh2);
_mesh2->setPosition3D(Vec3(0.0f, 0.0f, -20.0f));
_mesh2->setScale(2.0f);
_mesh2->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
2019-11-23 20:27:39 +08:00
if (_camera == nullptr)
{
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2019-11-23 20:27:39 +08:00
_camera->setCameraFlag(CameraFlag::USER1);
_camera->setPosition3D(Vec3(0.0f, 30.0f, 40.0f));
_camera->lookAt(Vec3(0, 0, 0), Vec3(0.0f, 1.0f, 0.0f));
2019-11-23 20:27:39 +08:00
_layer3D->addChild(_camera);
}
_layer3D->setCameraMask(2);
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) {
Director::getInstance()->setClearColor(Color4F(0.5, 0.5, 0.5, 1));
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_programState1);
AX_SAFE_RELEASE_NULL(_programState2);
auto vertexSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.frag");
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
_mesh1->setProgramState(_programState1);
_mesh2->setProgramState(_programState2);
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(program);
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogStart = 10;
float fogEnd = 60;
int fogEquation = 0;
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogStart", &fogStart, sizeof(fogStart));
SET_UNIFORM("u_fogEnd", &fogEnd, sizeof(fogEnd));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
});
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
void FogTestDemo::switchTypeCallback(Ref* sender, int type)
2019-11-23 20:27:39 +08:00
{
if (type == 0)
2019-11-23 20:27:39 +08:00
{
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogStart = 10;
float fogEnd = 60;
int fogEquation = 0;
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogStart", &fogStart, sizeof(fogStart));
SET_UNIFORM("u_fogEnd", &fogEnd, sizeof(fogEnd));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
2019-11-23 20:27:39 +08:00
}
else if (type == 1)
2019-11-23 20:27:39 +08:00
{
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogDensity = 0.03f;
int fogEquation = 1;
2019-11-23 20:27:39 +08:00
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogDensity", &fogDensity, sizeof(fogDensity));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
2019-11-23 20:27:39 +08:00
}
else if (type == 2)
2019-11-23 20:27:39 +08:00
{
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogDensity = 0.03f;
int fogEquation = 2;
2019-11-23 20:27:39 +08:00
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogDensity", &fogDensity, sizeof(fogDensity));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
2019-11-23 20:27:39 +08:00
}
}
void FogTestDemo::onExit()
{
CameraBaseTest::onExit();
Director::getInstance()->setClearColor(Color4F(0, 0, 0, 1));
2019-11-23 20:27:39 +08:00
if (_camera)
{
_camera = nullptr;
}
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
void FogTestDemo::update(float dt) {}
2019-11-23 20:27:39 +08:00
void FogTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, axis::Event* event)
2019-11-23 20:27:39 +08:00
{
if (touches.size() == 1)
2019-11-23 20:27:39 +08:00
{
Vec2 prelocation = touches[0]->getPreviousLocationInView();
Vec2 location = touches[0]->getLocationInView();
Vec2 newPos = prelocation - location;
if (_cameraType == CameraType::Free)
2019-11-23 20:27:39 +08:00
{
Vec3 cameraDir;
Vec3 cameraRightDir;
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
cameraDir.normalize();
cameraDir.y = 0;
2019-11-23 20:27:39 +08:00
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
cameraRightDir.normalize();
cameraRightDir.y = 0;
Vec3 cameraPos = _camera->getPosition3D();
cameraPos -= cameraDir * newPos.y * 0.1f;
cameraPos += cameraRightDir * newPos.x * 0.1f;
2019-11-23 20:27:39 +08:00
_camera->setPosition3D(cameraPos);
}
}
}
// CameraFrameBufferTest::CameraFrameBufferTest()
2019-11-23 20:27:39 +08:00
//{
//
// }
//
// CameraFrameBufferTest::~CameraFrameBufferTest()
2019-11-23 20:27:39 +08:00
//{
//
// }
//
// std::string CameraFrameBufferTest::title() const
2019-11-23 20:27:39 +08:00
//{
// return "Camera FrameBuffer Object Test";
// }
2019-11-23 20:27:39 +08:00
//
// void CameraFrameBufferTest::onEnter()
2019-11-23 20:27:39 +08:00
//{
// auto sizeInpixels = Director::getInstance()->getWinSizeInPixels();
// auto size = Director::getInstance()->getWinSize();
// auto fboSize = Size(sizeInpixels.width * 1, sizeInpixels.height * 1.5);
// auto fbo = experimental::FrameBuffer::create(1, fboSize.width, fboSize.height);
//
// CameraBaseTest::onEnter();
// //auto sprite = Sprite::createWithTexture(fbo);
// //sprite->setPosition(Vec2(100,100));
// //std::string filename = "MeshRendererTest/girl.c3b";
// //auto sprite = MeshRenderer::create(filename);
// //sprite->setScale(1.0);
// //auto animation = Animation3D::create(filename);
// //if (animation)
// //{
// // auto animate = Animate3D::create(animation);
//
// // sprite->runAction(RepeatForever::create(animate));
// //}
// //sprite->setPosition(Vec2(100,100));
// auto rt = experimental::RenderTarget::create(fboSize.width, fboSize.height);
// auto rtDS = experimental::RenderTargetDepthStencil::create(fboSize.width, fboSize.height);
// fbo->attachRenderTarget(rt);
// fbo->attachDepthStencilTarget(rtDS);
// auto sprite = Sprite::createWithTexture(fbo->getRenderTarget()->getTexture());
// sprite->setScale(0.3f);
// sprite->runAction(RepeatForever::create(RotateBy::create(1, 90)));
// sprite->setPosition(size.width/2, size.height/2);
// addChild(sprite);
//
// auto sprite2 = Sprite::create(s_pathGrossini);
// sprite2->setPosition(Vec2(size.width/5,size.height/5));
// addChild(sprite2);
// sprite2->setCameraMask((unsigned short)CameraFlag::USER1);
// auto move = MoveBy::create(1.0, Vec2(100,100));
// sprite2->runAction(
// RepeatForever::create(
// Sequence::createWithTwoActions(
// move, move->reverse())
// )
// );
//
// auto camera = Camera::create();
// camera->setCameraFlag(CameraFlag::USER1);
// camera->setDepth(-1);
// camera->setFrameBufferObject(fbo);
// fbo->setClearColor(Color4F(1,1,1,1));
// addChild(camera);
// }
BackgroundColorBrushTest::BackgroundColorBrushTest() {}
BackgroundColorBrushTest::~BackgroundColorBrushTest() {}
2019-11-23 20:27:39 +08:00
std::string BackgroundColorBrushTest::title() const
{
return "CameraBackgroundColorBrush Test";
}
std::string BackgroundColorBrushTest::subtitle() const
{
return "right side object colored by CameraBG";
}
void BackgroundColorBrushTest::onEnter()
{
CameraBaseTest::onEnter();
2019-11-23 20:27:39 +08:00
auto s = Director::getInstance()->getWinSize();
2019-11-23 20:27:39 +08:00
{
// 1st Camera
auto camera = Camera::createPerspective(60.0f, (float)s.width / s.height, 1.0f, 1000.0f);
2019-11-23 20:27:39 +08:00
camera->setPosition3D(Vec3(0.0f, 0.0f, 200.0f));
camera->lookAt(Vec3::ZERO);
camera->setDepth(-2);
camera->setCameraFlag(CameraFlag::USER1);
addChild(camera);
2019-11-23 20:27:39 +08:00
// 3D model
auto model = MeshRenderer::create("MeshRendererTest/boss1.obj");
2019-11-23 20:27:39 +08:00
model->setScale(4);
model->setPosition3D(Vec3(20.0f, 0.0f, 0.0f));
model->setTexture("MeshRendererTest/boss.png");
2019-11-23 20:27:39 +08:00
model->setCameraMask(static_cast<unsigned short>(CameraFlag::USER1));
addChild(model);
model->runAction(RepeatForever::create(RotateBy::create(1.f, Vec3(10.0f, 20.0f, 30.0f))));
}
2019-11-23 20:27:39 +08:00
{
auto base = Node::create();
base->setContentSize(s);
base->setCameraMask(static_cast<unsigned short>(CameraFlag::USER2));
addChild(base);
2019-11-23 20:27:39 +08:00
// 2nd Camera
auto camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2019-11-23 20:27:39 +08:00
auto colorBrush = CameraBackgroundBrush::createColorBrush(Color4F(.1f, .1f, 1.f, .5f), 1.f);
camera->setBackgroundBrush(colorBrush);
camera->setPosition3D(Vec3(0.0f, 0.0f, 200.0f));
camera->lookAt(Vec3::ZERO);
camera->setDepth(-1);
camera->setCameraFlag(CameraFlag::USER2);
base->addChild(camera);
2019-11-23 20:27:39 +08:00
// for alpha setting
auto slider = ui::Slider::create();
slider->loadBarTexture("cocosui/sliderTrack.png");
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
slider->setPosition(Vec2(s.width / 2, s.height / 4));
2019-11-23 20:27:39 +08:00
slider->setPercent(50);
slider->addEventListener([slider, colorBrush](Ref*, ui::Slider::EventType) {
colorBrush->setColor(Color4F(.1f, .1f, 1.f, (float)slider->getPercent() / 100.f));
2019-11-23 20:27:39 +08:00
});
addChild(slider);
2019-11-23 20:27:39 +08:00
// 3D model for 2nd camera
auto model = MeshRenderer::create("MeshRendererTest/boss1.obj");
2019-11-23 20:27:39 +08:00
model->setScale(4);
model->setPosition3D(Vec3(-20.0f, 0.0f, 0.0f));
model->setTexture("MeshRendererTest/boss.png");
2019-11-23 20:27:39 +08:00
model->setCameraMask(static_cast<unsigned short>(CameraFlag::USER2));
base->addChild(model);
model->runAction(RepeatForever::create(RotateBy::create(1.f, Vec3(10.0f, 20.0f, 30.0f))));
}
}