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

1550 lines
53 KiB
C++
Raw Normal View History

2014-08-07 15:23:31 +08:00
/****************************************************************************
2014-10-16 17:38:42 +08:00
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2014-10-16 17:38:42 +08:00
2022-02-24 18:45:25 +08:00
https://adxeproject.github.io/
2014-10-16 17:38:42 +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.
****************************************************************************/
2014-08-07 15:23:31 +08:00
#include "Camera3DTest.h"
2015-05-13 14:24:53 +08:00
#include "testResource.h"
#include "ui/UISlider.h"
2021-12-28 16:06:23 +08:00
#include "platform/CCFileUtils.h"
#include "renderer/backend/Device.h"
2014-08-07 15:23:31 +08:00
USING_NS_CC;
2021-12-28 16:06:23 +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-03-13 15:06:30 +08:00
2014-08-07 15:23:31 +08:00
enum
{
IDC_NEXT = 100,
IDC_BACK,
IDC_RESTART
};
Camera3DTests::Camera3DTests()
2014-08-07 15:23:31 +08:00
{
ADD_TEST_CASE(CameraRotationTest);
ADD_TEST_CASE(Camera3DTestDemo);
ADD_TEST_CASE(CameraCullingDemo);
ADD_TEST_CASE(FogTestDemo);
ADD_TEST_CASE(CameraArcBallDemo);
2021-12-28 16:06:23 +08:00
// ADD_TEST_CASE(CameraFrameBufferTest); //TODO render target
ADD_TEST_CASE(BackgroundColorBrushTest);
}
2014-08-07 15:23:31 +08:00
//------------------------------------------------------------------
//
// Camera Rotation Test
2014-08-07 15:23:31 +08:00
//
//------------------------------------------------------------------
CameraRotationTest::CameraRotationTest()
{
auto s = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
_camControlNode = Node::create();
2021-12-28 16:06:23 +08:00
_camControlNode->setPositionNormalized(Vec2(0.5f, 0.5f));
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);
2021-12-28 16:06:23 +08:00
2015-01-15 08:23:35 +08:00
auto lship = Label::create();
lship->setString("Ship");
lship->setPosition(0, 20);
mesh->addChild(lship);
2021-12-28 16:06:23 +08:00
// Billboards
// Yellow is at the back
2015-01-12 14:20:41 +08:00
bill1 = BillBoard::create("Images/Icon.png");
bill1->setPosition3D(Vec3(50.0f, 10.0f, -10.0f));
2015-01-12 14:20:41 +08:00
bill1->setColor(Color3B::YELLOW);
bill1->setScale(0.6f);
mesh->addChild(bill1);
2021-12-28 16:06:23 +08:00
2015-01-12 14:20:41 +08:00
l1 = Label::create();
2021-12-28 16:06:23 +08:00
l1->setPosition(Vec2(0.0f, -10.0f));
l1->setString("Billboard1");
2015-01-12 14:20:41 +08:00
l1->setColor(Color3B::WHITE);
l1->setScale(3);
bill1->addChild(l1);
2015-07-15 12:04:48 +08:00
auto p1 = ParticleSystemQuad::create("Particles/SmallSun.plist");
2021-12-28 16:06:23 +08:00
p1->setPosition(30.0f, 80.0f);
2015-01-16 06:00:49 +08:00
bill1->addChild(p1);
2021-12-28 16:06:23 +08:00
2015-01-12 14:20:41 +08:00
bill2 = BillBoard::create("Images/Icon.png");
bill2->setPosition3D(Vec3(-50.0f, -10.0f, 10.0f));
bill2->setScale(0.6f);
mesh->addChild(bill2);
2021-12-28 16:06:23 +08:00
2015-01-12 14:20:41 +08:00
l2 = Label::create();
l2->setString("Billboard2");
2021-12-28 16:06:23 +08:00
l2->setPosition(Vec2(0.0f, -10.0f));
2015-01-12 14:20:41 +08:00
l2->setColor(Color3B::WHITE);
l2->setScale(3);
bill2->addChild(l2);
2021-12-28 16:06:23 +08:00
2015-07-15 12:04:48 +08:00
auto p2 = ParticleSystemQuad::create("Particles/SmallSun.plist");
2021-12-28 16:06:23 +08:00
p2->setPosition(30, 80);
2015-01-16 06:00:49 +08:00
bill2->addChild(p2);
2021-12-28 16:06:23 +08:00
// 3D models
auto model = MeshRenderer::create("MeshRendererTest/boss1.obj");
model->setScale(4);
model->setTexture("MeshRendererTest/boss.png");
2021-12-28 16:06:23 +08:00
model->setPosition3D(Vec3(s.width / 2, s.height / 2, 0));
addChild(model);
2021-12-28 16:06:23 +08:00
// Listener
_lis = EventListenerTouchOneByOne::create();
_lis->onTouchBegan = [](Touch* t, Event* e) { return true; };
_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);
2021-12-28 16:06:23 +08:00
2019-03-13 15:06:30 +08:00
schedule(CC_SCHEDULE_SELECTOR(CameraRotationTest::update));
}
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();
}
2021-12-28 16:06:23 +08:00
void CameraRotationTest::update(float dt) {}
2014-08-07 15:23:31 +08:00
//------------------------------------------------------------------
//
// Camera3DTestDemo
//
//------------------------------------------------------------------
Camera3DTestDemo::Camera3DTestDemo()
2021-12-28 16:06:23 +08:00
: _cameraType(CameraType::Free)
, _incRot(nullptr)
, _decRot(nullptr)
, _camera(nullptr)
, _bZoomOut(false)
, _bZoomIn(false)
, _bRotateLeft(false)
, _bRotateRight(false)
{}
Camera3DTestDemo::~Camera3DTestDemo() {}
void Camera3DTestDemo::reachEndCallBack() {}
2014-08-07 15:23:31 +08:00
std::string Camera3DTestDemo::title() const
{
return "Testing Camera";
}
2021-12-28 16:06:23 +08:00
void Camera3DTestDemo::scaleCameraCallback(Ref* sender, float value)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
if (_camera && _cameraType != CameraType::FirstPerson)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
Vec3 cameraPos = _camera->getPosition3D();
cameraPos += cameraPos.getNormalized() * value;
2014-08-07 15:23:31 +08:00
_camera->setPosition3D(cameraPos);
}
2014-10-16 17:31:18 +08:00
}
2021-12-28 16:06:23 +08:00
void Camera3DTestDemo::rotateCameraCallback(Ref* sender, float value)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y += value;
2014-08-07 15:23:31 +08:00
_camera->setRotation3D(rotation3D);
}
}
void Camera3DTestDemo::SwitchViewCallback(Ref* sender, CameraType cameraType)
{
2021-12-28 16:06:23 +08:00
if (_cameraType == cameraType)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
return;
2014-08-07 15:23:31 +08:00
}
_cameraType = cameraType;
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::Free)
2014-08-07 15:23:31 +08:00
{
_camera->setPosition3D(Vec3(0, 130, 130) + _mesh->getPosition3D());
_camera->lookAt(_mesh->getPosition3D());
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
2014-08-07 15:23:31 +08:00
}
2021-12-28 16:06:23 +08:00
else if (_cameraType == CameraType::FirstPerson)
2014-08-07 15:23:31 +08:00
{
Vec3 newFaceDir;
_mesh->getWorldToNodeTransform().getForwardVector(&newFaceDir);
newFaceDir.normalize();
_camera->setPosition3D(Vec3(0, 35, 0) + _mesh->getPosition3D());
_camera->lookAt(_mesh->getPosition3D() + newFaceDir * 50);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::GRAY);
_ZoomOutlabel->setColor(Color3B::GRAY);
2014-08-07 15:23:31 +08:00
}
2021-12-28 16:06:23 +08:00
else if (_cameraType == CameraType::ThirdPerson)
2014-08-07 15:23:31 +08:00
{
_camera->setPosition3D(Vec3(0, 130, 130) + _mesh->getPosition3D());
_camera->lookAt(_mesh->getPosition3D());
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_RotateRightlabel->setColor(Color3B::GRAY);
_RotateLeftlabel->setColor(Color3B::GRAY);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
2014-08-07 15:23:31 +08:00
}
}
void Camera3DTestDemo::onEnter()
{
CameraBaseTest::onEnter();
_mesh = nullptr;
2021-12-28 16:06:23 +08:00
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
2014-08-07 15:23:31 +08:00
listener->onTouchesBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(Camera3DTestDemo::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2021-12-28 16:06:23 +08:00
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
_curState = State_None;
addNewSpriteWithCoords(Vec3(0, 0, 0), "MeshRendererTest/girl.c3b", true, 0.2f, true);
2014-08-07 15:23:31 +08:00
TTFConfig ttfConfig("fonts/arial.ttf", 20);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto containerForLabel1 = Node::create();
2021-12-28 16:06:23 +08:00
_ZoomOutlabel = Label::createWithTTF(ttfConfig, "zoom out");
_ZoomOutlabel->setPosition(s.width - 50, VisibleRect::top().y - 20);
2014-10-16 17:31:18 +08:00
containerForLabel1->addChild(_ZoomOutlabel);
addChild(containerForLabel1, 10);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
listener1->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOut, this);
listener1->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOutEnd, this);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _ZoomOutlabel);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto containerForLabel2 = Node::create();
2021-12-28 16:06:23 +08:00
_ZoomInlabel = Label::createWithTTF(ttfConfig, "zoom in");
_ZoomInlabel->setPosition(s.width - 50, VisibleRect::top().y - 70);
2014-10-16 17:31:18 +08:00
containerForLabel2->addChild(_ZoomInlabel);
addChild(containerForLabel2, 10);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto listener2 = EventListenerTouchOneByOne::create();
listener2->setSwallowTouches(true);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
listener2->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomIn, this);
listener2->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomInEnd, this);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, _ZoomInlabel);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto containerForLabel3 = Node::create();
2021-12-28 16:06:23 +08:00
_RotateLeftlabel = Label::createWithTTF(ttfConfig, "rotate left");
_RotateLeftlabel->setPosition(s.width - 50, VisibleRect::top().y - 120);
2014-10-16 17:31:18 +08:00
containerForLabel3->addChild(_RotateLeftlabel);
addChild(containerForLabel3, 10);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto listener3 = EventListenerTouchOneByOne::create();
listener3->setSwallowTouches(true);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
listener3->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeft, this);
listener3->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeftEnd, this);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener3, _RotateLeftlabel);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto containerForLabel4 = Node::create();
2021-12-28 16:06:23 +08:00
_RotateRightlabel = Label::createWithTTF(ttfConfig, "rotate right");
_RotateRightlabel->setPosition(s.width - 50, VisibleRect::top().y - 170);
2014-10-16 17:31:18 +08:00
containerForLabel4->addChild(_RotateRightlabel);
addChild(containerForLabel4, 10);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
auto listener4 = EventListenerTouchOneByOne::create();
listener4->setSwallowTouches(true);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
listener4->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRight, this);
listener4->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRightEnd, this);
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener4, _RotateRightlabel);
2021-12-28 16:06:23 +08:00
auto label1 = Label::createWithTTF(ttfConfig, "free ");
auto menuItem1 =
MenuItemLabel::create(label1, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback, this, CameraType::Free));
auto label2 = Label::createWithTTF(ttfConfig, "third person");
auto menuItem2 = MenuItemLabel::create(
label2, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback, this, CameraType::ThirdPerson));
auto label3 = Label::createWithTTF(ttfConfig, "first person");
auto menuItem3 = MenuItemLabel::create(
label3, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback, this, CameraType::FirstPerson));
2014-10-16 17:31:18 +08:00
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
2021-12-28 16:06:23 +08:00
2014-08-07 15:23:31 +08:00
menu->setPosition(Vec2::ZERO);
2021-12-28 16:06:23 +08:00
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);
2014-08-07 15:23:31 +08:00
addChild(menu, 0);
schedule(CC_SCHEDULE_SELECTOR(Camera3DTestDemo::updateCamera), 0.0f);
2014-08-07 15:23:31 +08:00
if (_camera == nullptr)
{
2021-12-28 16:06:23 +08:00
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2014-08-07 15:23:31 +08:00
_camera->setCameraFlag(CameraFlag::USER1);
_layer3D->addChild(_camera);
}
2021-12-28 16:06:23 +08:00
SwitchViewCallback(this, CameraType::ThirdPerson);
DrawNode3D* line = DrawNode3D::create();
// draw x
for (int j = -20; j <= 20; j++)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +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));
2014-08-07 15:23:31 +08:00
}
2021-12-28 16:06:23 +08:00
// draw z
for (int j = -20; j <= 20; j++)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +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));
2014-08-07 15:23:31 +08:00
}
2021-12-28 16:06:23 +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));
2014-08-07 15:23:31 +08:00
_layer3D->addChild(line);
2015-01-07 11:47:34 +08:00
2014-08-07 15:23:31 +08:00
_layer3D->setCameraMask(2);
}
void Camera3DTestDemo::onExit()
{
CameraBaseTest::onExit();
2014-08-07 15:23:31 +08:00
if (_camera)
{
_camera = nullptr;
}
}
2021-12-28 16:06:23 +08:00
void Camera3DTestDemo::addNewSpriteWithCoords(Vec3 p,
std::string fileName,
bool playAnimation,
float scale,
bool bindCamera)
2014-08-07 15:23:31 +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);
2021-12-28 16:06:23 +08:00
if (playAnimation)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
auto animation = Animation3D::create(fileName, "Take 001");
2014-08-07 15:23:31 +08:00
if (animation)
{
auto animate = Animate3D::create(animation);
mesh->runAction(RepeatForever::create(animate));
2014-08-07 15:23:31 +08:00
}
}
2021-12-28 16:06:23 +08:00
if (bindCamera)
2014-08-07 15:23:31 +08:00
{
_mesh = mesh;
2014-08-07 15:23:31 +08:00
}
mesh->setScale(scale);
2014-08-07 15:23:31 +08:00
}
2021-12-28 16:06:23 +08:00
void Camera3DTestDemo::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event* event) {}
void Camera3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event* event)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
if (touches.size() == 1)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
auto touch = touches[0];
2014-08-07 15:23:31 +08:00
auto location = touch->getLocation();
2021-12-28 16:06:23 +08:00
Point newPos = touch->getPreviousLocation() - location;
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2014-08-07 15:23:31 +08:00
{
2014-10-16 17:31: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-10-16 17:31: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.1f;
cameraPos += cameraRightDir * newPos.x * 0.1f;
2014-08-07 15:23:31 +08:00
_camera->setPosition3D(cameraPos);
if (_mesh && _cameraType == CameraType::FirstPerson)
2014-08-07 15:23:31 +08:00
{
_mesh->setPosition3D(Vec3(_camera->getPositionX(), 0, _camera->getPositionZ()));
_targetPos = _mesh->getPosition3D();
2014-08-07 15:23:31 +08:00
}
}
}
}
void Camera3DTestDemo::move3D(float elapsedTime)
{
if (_mesh)
2014-08-07 15:23:31 +08:00
{
Vec3 curPos = _mesh->getPosition3D();
2014-08-07 15:23:31 +08:00
Vec3 newFaceDir = _targetPos - curPos;
2021-12-28 16:06:23 +08:00
newFaceDir.y = 0.0f;
2014-08-07 15:23:31 +08:00
newFaceDir.normalize();
Vec3 offset = newFaceDir * 25.0f * elapsedTime;
2021-12-28 16:06:23 +08:00
curPos += offset;
_mesh->setPosition3D(curPos);
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::ThirdPerson)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
Vec3 cameraPos = _camera->getPosition3D();
cameraPos.x += offset.x;
cameraPos.z += offset.z;
2014-08-07 15:23:31 +08:00
_camera->setPosition3D(cameraPos);
}
}
}
void Camera3DTestDemo::updateState(float elapsedTime)
{
if (_mesh)
2014-08-07 15:23:31 +08:00
{
Vec3 curPos = _mesh->getPosition3D();
2014-08-07 15:23:31 +08:00
Vec3 curFaceDir;
_mesh->getNodeToWorldTransform().getForwardVector(&curFaceDir);
2021-12-28 16:06:23 +08:00
curFaceDir = -curFaceDir;
2014-08-07 15:23:31 +08:00
curFaceDir.normalize();
Vec3 newFaceDir = _targetPos - curPos;
2021-12-28 16:06:23 +08:00
newFaceDir.y = 0.0f;
2014-08-07 15:23:31 +08:00
newFaceDir.normalize();
2021-12-28 16:06:23 +08:00
float cosAngle = std::fabs(Vec3::dot(curFaceDir, newFaceDir) - 1.0f);
float dist = curPos.distanceSquared(_targetPos);
if (dist <= 4.0f)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
if (cosAngle <= 0.01f)
2014-08-07 15:23:31 +08:00
_curState = State_Idle;
else
_curState = State_Rotate;
}
else
{
2021-12-28 16:06:23 +08:00
if (cosAngle > 0.01f)
2014-08-07 15:23:31 +08:00
_curState = State_Rotate | State_Move;
else
_curState = State_Move;
}
}
}
2021-12-28 16:06:23 +08:00
void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event* event)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
for (auto& item : touches)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
auto touch = item;
2014-08-07 15:23:31 +08:00
auto location = touch->getLocationInView();
2021-12-28 16:06:23 +08:00
if (_camera)
2014-08-07 15:23:31 +08:00
{
if (_mesh && _cameraType == CameraType::ThirdPerson && _bZoomOut == false && _bZoomIn == false &&
2021-12-28 16:06:23 +08:00
_bRotateLeft == false && _bRotateRight == false)
2014-08-07 15:23:31 +08:00
{
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
2021-12-28 16:06:23 +08:00
2014-08-07 15:23:31 +08:00
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
nearP = _camera->unproject(nearP);
farP = _camera->unproject(farP);
2014-08-07 15:23:31 +08:00
Vec3 dir(farP - nearP);
2021-12-28 16:06:23 +08:00
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)
2014-10-16 17:31:18 +08:00
p.x = 100;
2021-12-28 16:06:23 +08:00
if (p.x < -100)
2014-10-16 17:31:18 +08:00
p.x = -100;
2021-12-28 16:06:23 +08:00
if (p.z > 100)
2014-10-16 17:31:18 +08:00
p.z = 100;
2021-12-28 16:06:23 +08:00
if (p.z < -100)
2014-10-16 17:31:18 +08:00
p.z = -100;
2021-12-28 16:06:23 +08:00
_targetPos = p;
2014-08-07 15:23:31 +08:00
}
}
}
}
2021-12-28 16:06:23 +08:00
void onTouchesCancelled(const std::vector<Touch*>& touches, cocos2d::Event* event) {}
2014-08-07 15:23:31 +08:00
void Camera3DTestDemo::updateCamera(float fDelta)
{
if (_mesh)
2014-08-07 15:23:31 +08:00
{
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::ThirdPerson)
2014-08-07 15:23:31 +08:00
{
updateState(fDelta);
2021-12-28 16:06:23 +08:00
if (isState(_curState, State_Move))
2014-08-07 15:23:31 +08:00
{
move3D(fDelta);
2021-12-28 16:06:23 +08:00
if (isState(_curState, State_Rotate))
2014-08-07 15:23:31 +08:00
{
Vec3 curPos = _mesh->getPosition3D();
2021-12-28 16:06:23 +08:00
2014-08-07 15:23:31 +08:00
Vec3 newFaceDir = _targetPos - curPos;
2021-12-28 16:06:23 +08:00
newFaceDir.y = 0;
2014-08-07 15:23:31 +08:00
newFaceDir.normalize();
Vec3 up;
_mesh->getNodeToWorldTransform().getUpVector(&up);
2014-08-07 15:23:31 +08:00
up.normalize();
Vec3 right;
2021-12-28 16:06:23 +08:00
Vec3::cross(-newFaceDir, up, &right);
2014-08-07 15:23:31 +08:00
right.normalize();
2021-12-28 16:06:23 +08:00
Vec3 pos = Vec3(0, 0, 0);
2014-08-07 15:23:31 +08:00
Mat4 mat;
mat.m[0] = right.x;
mat.m[1] = right.y;
mat.m[2] = right.z;
mat.m[3] = 0.0f;
2021-12-28 16:06:23 +08:00
2014-08-07 15:23:31 +08:00
mat.m[4] = up.x;
mat.m[5] = up.y;
mat.m[6] = up.z;
mat.m[7] = 0.0f;
2021-12-28 16:06:23 +08:00
2014-08-07 15:23:31 +08:00
mat.m[8] = newFaceDir.x;
mat.m[9] = newFaceDir.y;
mat.m[10] = newFaceDir.z;
mat.m[11] = 0.0f;
2021-12-28 16:06:23 +08:00
2014-08-07 15:23:31 +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);
2014-08-07 15:23:31 +08:00
}
}
}
2021-12-28 16:06:23 +08:00
if (_bZoomOut == true)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
if (_camera)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::ThirdPerson)
2014-10-16 17:31:18 +08:00
{
Vec3 lookDir = _camera->getPosition3D() - _mesh->getPosition3D();
2014-10-16 17:31:18 +08:00
Vec3 cameraPos = _camera->getPosition3D();
2021-12-28 16:06:23 +08:00
if (lookDir.length() <= 300)
2014-10-16 17:31:18 +08:00
{
cameraPos += lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
2021-12-28 16:06:23 +08:00
else if (_cameraType == CameraType::Free)
2014-10-16 17:31:18 +08:00
{
Vec3 cameraPos = _camera->getPosition3D();
2021-12-28 16:06:23 +08:00
if (cameraPos.length() <= 300)
2014-10-16 17:31:18 +08:00
{
cameraPos += cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
2021-12-28 16:06:23 +08:00
if (_bZoomIn == true)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
if (_camera)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::ThirdPerson)
2014-10-16 17:31:18 +08:00
{
Vec3 lookDir = _camera->getPosition3D() - _mesh->getPosition3D();
2014-10-16 17:31:18 +08:00
Vec3 cameraPos = _camera->getPosition3D();
2021-12-28 16:06:23 +08:00
if (lookDir.length() >= 50)
2014-10-16 17:31:18 +08:00
{
cameraPos -= lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
2021-12-28 16:06:23 +08:00
else if (_cameraType == CameraType::Free)
2014-10-16 17:31:18 +08:00
{
Vec3 cameraPos = _camera->getPosition3D();
2021-12-28 16:06:23 +08:00
if (cameraPos.length() >= 50)
2014-10-16 17:31:18 +08:00
{
cameraPos -= cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
2021-12-28 16:06:23 +08:00
if (_bRotateLeft == true)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y += 1;
2014-10-16 17:31:18 +08:00
_camera->setRotation3D(rotation3D);
}
}
2021-12-28 16:06:23 +08:00
if (_bRotateRight == true)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::Free || _cameraType == CameraType::FirstPerson)
2014-10-16 17:31:18 +08:00
{
2021-12-28 16:06:23 +08:00
Vec3 rotation3D = _camera->getRotation3D();
rotation3D.y -= 1;
2014-10-16 17:31:18 +08:00
_camera->setRotation3D(rotation3D);
}
}
2014-08-07 15:23:31 +08:00
}
}
bool Camera3DTestDemo::onTouchesCommon(Touch* touch, Event* event, bool* touchProperty)
2014-10-16 17:31:18 +08:00
{
auto target = static_cast<Label*>(event->getCurrentTarget());
2021-12-28 16:06:23 +08:00
2014-10-16 17:31:18 +08:00
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
2021-12-28 16:06:23 +08:00
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2014-10-16 17:31:18 +08:00
if (rect.containsPoint(locationInNode))
{
*touchProperty = true;
2014-10-16 17:31:18 +08:00
return true;
}
return false;
}
2021-12-28 16:06:23 +08:00
bool Camera3DTestDemo::isState(unsigned int state, unsigned int bit) const
{
return (state & bit) == bit;
}
bool Camera3DTestDemo::onTouchesZoomOut(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bZoomOut);
}
2014-10-16 17:31:18 +08:00
void Camera3DTestDemo::onTouchesZoomOutEnd(Touch* touch, Event* event)
{
_bZoomOut = false;
}
bool Camera3DTestDemo::onTouchesZoomIn(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bZoomIn);
2014-10-16 17:31:18 +08:00
}
void Camera3DTestDemo::onTouchesZoomInEnd(Touch* touch, Event* event)
{
_bZoomIn = false;
}
bool Camera3DTestDemo::onTouchesRotateLeft(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bRotateLeft);
2014-10-16 17:31:18 +08:00
}
void Camera3DTestDemo::onTouchesRotateLeftEnd(Touch* touch, Event* event)
{
_bRotateLeft = false;
}
bool Camera3DTestDemo::onTouchesRotateRight(Touch* touch, Event* event)
{
return Camera3DTestDemo::onTouchesCommon(touch, event, &_bRotateRight);
2014-10-16 17:31:18 +08:00
}
void Camera3DTestDemo::onTouchesRotateRightEnd(Touch* touch, Event* event)
{
_bRotateRight = false;
}
2014-12-16 16:34:35 +08:00
////////////////////////////////////////////////////////////
// CameraCullingDemo
CameraCullingDemo::CameraCullingDemo()
2021-12-28 16:06:23 +08:00
: _layer3D(nullptr)
, _cameraType(CameraType::FirstPerson)
, _cameraFirst(nullptr)
, _cameraThird(nullptr)
, _moveAction(nullptr)
, _drawAABB(nullptr)
, _drawFrustum(nullptr)
, _row(3)
{}
CameraCullingDemo::~CameraCullingDemo() {}
2014-12-16 16:34:35 +08:00
std::string CameraCullingDemo::title() const
2014-12-16 16:34:35 +08:00
{
return "Camera Frustum Clipping";
2014-12-16 16:34:35 +08:00
}
void CameraCullingDemo::onEnter()
2014-12-16 16:34:35 +08:00
{
CameraBaseTest::onEnter();
2019-03-13 15:06:30 +08:00
schedule(CC_SCHEDULE_SELECTOR(CameraCullingDemo::update), 0.0f);
2021-12-28 16:06:23 +08:00
2014-12-16 16:34:35 +08:00
auto s = Director::getInstance()->getWinSize();
/*auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(Camera3DTestDemo::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);*/
2021-12-28 16:06:23 +08:00
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
// switch camera
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(20);
2021-12-28 16:06:23 +08:00
auto menuItem1 = MenuItemFont::create("Switch Camera", CC_CALLBACK_1(CameraCullingDemo::switchViewCallback, this));
menuItem1->setColor(Color3B(0, 200, 20));
auto menu = Menu::create(menuItem1, NULL);
2014-12-16 16:34:35 +08:00
menu->setPosition(Vec2::ZERO);
2021-12-28 16:06:23 +08:00
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y - 70);
addChild(menu, 1);
2021-12-28 16:06:23 +08:00
// + -
MenuItemFont::setFontSize(40);
auto decrease = MenuItemFont::create(" - ", CC_CALLBACK_1(CameraCullingDemo::delMeshCallback, this));
2021-12-28 16:06:23 +08:00
decrease->setColor(Color3B(0, 200, 20));
auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(CameraCullingDemo::addMeshCallback, this));
2021-12-28 16:06:23 +08:00
increase->setColor(Color3B(0, 200, 20));
menu = Menu::create(decrease, increase, nullptr);
menu->alignItemsHorizontally();
2021-12-28 16:06:23 +08:00
menu->setPosition(Vec2(s.width - 60, VisibleRect::top().y - 70));
addChild(menu, 1);
2021-12-28 16:06:23 +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);
2021-12-28 16:06:23 +08:00
// aabb drawNode3D
_drawAABB = DrawNode3D::create();
2021-12-28 16:06:23 +08:00
_drawAABB->setCameraMask((unsigned short)CameraFlag::USER1);
addChild(_drawAABB);
2021-12-28 16:06:23 +08:00
// frustum drawNode3D
_drawFrustum = DrawNode3D::create();
2021-12-28 16:06:23 +08:00
_drawFrustum->setCameraMask((unsigned short)CameraFlag::USER1);
addChild(_drawFrustum);
2021-12-28 16:06:23 +08:00
// set camera
switchViewCallback(this);
2021-12-28 16:06:23 +08:00
// add sprite
addMeshCallback(nullptr);
}
void CameraCullingDemo::onExit()
{
CameraBaseTest::onExit();
if (_cameraFirst)
2014-12-16 16:34:35 +08:00
{
_cameraFirst = nullptr;
2014-12-16 16:34:35 +08:00
}
if (_cameraThird)
2014-12-16 16:34:35 +08:00
{
_cameraThird = nullptr;
2014-12-16 16:34:35 +08:00
}
}
void CameraCullingDemo::update(float dt)
{
2019-03-13 15:06:30 +08:00
_drawAABB->clear();
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::ThirdPerson)
2019-03-13 15:06:30 +08:00
drawCameraFrustum();
Vector<Node*>& children = _layer3D->getChildren();
Vec3 corners[8];
2021-12-28 16:06:23 +08:00
for (const auto& iter : children)
2019-03-13 15:06:30 +08:00
{
const AABB& aabb = static_cast<MeshRenderer*>(iter)->getAABB();
2019-03-13 15:06:30 +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();
2021-12-28 16:06:23 +08:00
_moveAction->release();
_moveAction = inverse;
2021-12-28 16:06:23 +08:00
auto rot = RotateBy::create(1.f, Vec3(0.f, 180.f, 0.f));
auto seq = Sequence::create(rot, _moveAction,
CallFunc::create(CC_CALLBACK_0(CameraCullingDemo::reachEndCallBack, this)), nullptr);
seq->setTag(100);
_cameraFirst->runAction(seq);
}
void CameraCullingDemo::switchViewCallback(Ref* sender)
{
auto s = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
if (_cameraFirst == nullptr)
2014-12-16 16:34:35 +08:00
{
2021-12-28 16:06:23 +08:00
_cameraFirst = Camera::createPerspective(30.0f, (float)s.width / s.height, 10.0f, 200.0f);
_cameraFirst->setCameraFlag(CameraFlag::USER8);
2021-12-28 16:06:23 +08:00
_cameraFirst->setPosition3D(Vec3(-100.0f, 0.0f, 0.0f));
_cameraFirst->lookAt(Vec3(1000.0f, 0.0f, 0.0f));
_moveAction = MoveTo::create(4.f, Vec2(-_cameraFirst->getPositionX(), 0.0f));
_moveAction->retain();
2021-12-28 16:06:23 +08:00
auto seq = Sequence::create(
_moveAction, CallFunc::create(CC_CALLBACK_0(CameraCullingDemo::reachEndCallBack, this)), nullptr);
seq->setTag(100);
_cameraFirst->runAction(seq);
addChild(_cameraFirst);
2014-12-16 16:34:35 +08:00
}
2021-12-28 16:06:23 +08:00
if (_cameraThird == nullptr)
{
2021-12-28 16:06:23 +08:00
_cameraThird = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
_cameraThird->setCameraFlag(CameraFlag::USER8);
_cameraThird->setPosition3D(Vec3(0.0f, 130.0f, 130.0f));
2021-12-28 16:06:23 +08:00
_cameraThird->lookAt(Vec3(0, 0, 0));
addChild(_cameraThird);
2014-12-16 16:34:35 +08:00
}
2021-12-28 16:06:23 +08:00
if (_cameraType == CameraType::FirstPerson)
2014-12-16 16:34:35 +08:00
{
_cameraType = CameraType::ThirdPerson;
_cameraThird->setCameraFlag(CameraFlag::USER1);
_cameraFirst->setCameraFlag(CameraFlag::USER8);
}
2021-12-28 16:06:23 +08:00
else if (_cameraType == CameraType::ThirdPerson)
{
_cameraType = CameraType::FirstPerson;
_cameraFirst->setCameraFlag(CameraFlag::USER1);
_cameraThird->setCameraFlag(CameraFlag::USER8);
_drawFrustum->clear();
2014-12-16 16:34:35 +08:00
}
}
void CameraCullingDemo::addMeshCallback(Ref* sender)
2014-12-16 16:34:35 +08:00
{
_layer3D->removeAllChildren();
_objects.clear();
_drawAABB->clear();
2021-12-28 16:06:23 +08:00
++_row;
for (int x = -_row; x < _row; x++)
{
for (int z = -_row; z < _row; z++)
{
auto sprite = MeshRenderer::create("MeshRendererTest/orc.c3b");
sprite->setPosition3D(Vec3(x * 30.0f, 0.0f, z * 30.0f));
2021-12-28 16:06:23 +08:00
sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
_objects.push_back(sprite);
_layer3D->addChild(sprite);
}
}
2021-12-28 16:06:23 +08:00
// set layer mask.
2021-12-28 16:06:23 +08:00
_layer3D->setCameraMask((unsigned short)CameraFlag::USER1);
// update sprite number
char szText[16];
2021-12-28 16:06:23 +08:00
sprintf(szText, "%d sprits", static_cast<int32_t>(_layer3D->getChildrenCount()));
_labelMeshCount->setString(szText);
2014-12-16 16:34:35 +08:00
}
void CameraCullingDemo::delMeshCallback(Ref* sender)
2014-12-16 16:34:35 +08:00
{
2021-12-28 16:06:23 +08:00
if (_row == 0)
return;
_layer3D->removeAllChildren();
_objects.clear();
2021-12-28 16:06:23 +08:00
--_row;
for (int x = -_row; x < _row; x++)
{
for (int z = -_row; z < _row; z++)
{
auto sprite = MeshRenderer::create("MeshRendererTest/orc.c3b");
sprite->setPosition3D(Vec3(x * 30.0f, 0.0f, z * 30.0f));
_objects.push_back(sprite);
_layer3D->addChild(sprite);
}
}
2021-12-28 16:06:23 +08:00
// set layer mask.
2021-12-28 16:06:23 +08:00
_layer3D->setCameraMask((unsigned short)CameraFlag::USER1);
// update sprite number
char szText[16];
2021-12-28 16:06:23 +08:00
sprintf(szText, "%l sprits", static_cast<int32_t>(_layer3D->getChildrenCount()));
_labelMeshCount->setString(szText);
2014-12-16 16:34:35 +08:00
}
void CameraCullingDemo::drawCameraFrustum()
{
_drawFrustum->clear();
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
Color4F color(1.f, 1.f, 0.f, 1);
2021-12-28 16:06:23 +08:00
// top-left
2021-12-28 16:06:23 +08:00
Vec3 tl_0, tl_1;
Vec3 src(0, 0, 0);
2015-02-27 10:01:57 +08:00
tl_0 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
src = Vec3(0, 0, 1);
2015-02-27 10:01:57 +08:00
tl_1 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
// top-right
2021-12-28 16:06:23 +08:00
Vec3 tr_0, tr_1;
src = Vec3(size.width, 0, 0);
2015-02-27 10:01:57 +08:00
tr_0 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
src = Vec3(size.width, 0, 1);
2015-02-27 10:01:57 +08:00
tr_1 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
// bottom-left
2021-12-28 16:06:23 +08:00
Vec3 bl_0, bl_1;
src = Vec3(0, size.height, 0);
2015-02-27 10:01:57 +08:00
bl_0 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
src = Vec3(0, size.height, 1);
2015-02-27 10:01:57 +08:00
bl_1 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
// bottom-right
2021-12-28 16:06:23 +08:00
Vec3 br_0, br_1;
src = Vec3(size.width, size.height, 0);
2015-02-27 10:01:57 +08:00
br_0 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +08:00
src = Vec3(size.width, size.height, 1);
2015-02-27 10:01:57 +08:00
br_1 = _cameraFirst->unproject(src);
2021-12-28 16:06:23 +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);
2021-12-28 16:06:23 +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);
2021-12-28 16:06:23 +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);
2014-12-16 16:34:35 +08:00
}
2015-01-07 11:47:34 +08:00
////////////////////////////////////////////////////////////
// CameraArcBallDemo
CameraArcBallDemo::CameraArcBallDemo()
2021-12-28 16:06:23 +08:00
: 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)
2021-12-28 16:06:23 +08:00
{}
CameraArcBallDemo::~CameraArcBallDemo() {}
2015-01-07 11:47:34 +08:00
std::string CameraArcBallDemo::title() const
{
return "Camera ArcBall Moving";
}
void CameraArcBallDemo::onEnter()
{
CameraBaseTest::onEnter();
2015-01-07 11:47:34 +08:00
_rotationQuat.set(0.0f, 0.0f, 0.0f, 1.0f);
2019-03-13 15:06:30 +08:00
schedule(CC_SCHEDULE_SELECTOR(CameraArcBallDemo::update), 0.0f);
2021-12-28 16:06:23 +08:00
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
2015-01-07 11:47:34 +08:00
listener->onTouchesMoved = CC_CALLBACK_2(CameraArcBallDemo::onTouchsMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// switch camera
2015-01-07 11:47:34 +08:00
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(20);
2021-12-28 16:06:23 +08:00
auto menuItem1 =
MenuItemFont::create("Switch Operation", CC_CALLBACK_1(CameraArcBallDemo::switchOperateCallback, this));
menuItem1->setColor(Color3B(0, 200, 20));
auto menuItem2 =
MenuItemFont::create("Switch Target", CC_CALLBACK_1(CameraArcBallDemo::switchTargetCallback, this));
menuItem2->setColor(Color3B(0, 200, 20));
auto menu = Menu::create(menuItem1, menuItem2, NULL);
2015-01-07 11:47:34 +08:00
menu->setPosition(Vec2::ZERO);
2021-12-28 16:06:23 +08:00
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y - 70);
menuItem2->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y - 100);
2015-01-07 11:47:34 +08:00
addChild(menu, 1);
2021-12-28 16:06:23 +08:00
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
2015-01-07 11:47:34 +08:00
if (_camera == nullptr)
{
2021-12-28 16:06:23 +08:00
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2015-01-07 11:47:34 +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));
2015-01-07 11:47:34 +08:00
_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);
2015-01-07 11:47:34 +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);
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
_drawGrid = DrawNode3D::create();
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
// draw x
for (int j = -20; j <= 20; j++)
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
_drawGrid->drawLine(Vec3(-100.0f, 0, 5.0f * j), Vec3(100.0f, 0, 5.0f * j), Color4F(1, 0, 0, 1));
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
// draw z
for (int j = -20; j <= 20; j++)
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
_drawGrid->drawLine(Vec3(5.0f * j, 0, -100.0f), Vec3(5.0f * j, 0, 100.0f), Color4F(0, 0, 1, 1));
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
// draw y
_drawGrid->drawLine(Vec3(0, 0, 0), Vec3(0, 50.0f, 0), Color4F(0, 1, 0, 1));
2015-01-07 11:47:34 +08:00
_layer3D->addChild(_drawGrid);
_layer3D->setCameraMask(2);
updateCameraTransform();
}
void CameraArcBallDemo::onExit()
{
CameraBaseTest::onExit();
2015-01-07 11:47:34 +08:00
if (_camera)
{
_camera = nullptr;
}
}
2021-12-28 16:06:23 +08:00
void CameraArcBallDemo::onTouchsMoved(const std::vector<Touch*>& touchs, Event* event)
2015-01-07 11:47:34 +08:00
{
if (!touchs.empty())
{
2021-12-28 16:06:23 +08:00
if (_operate == OperateCamType::RotateCamera) // arc ball rotate
2015-01-07 11:47:34 +08:00
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 prelocation = touchs[0]->getPreviousLocationInView();
2021-12-28 16:06:23 +08:00
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;
2015-01-07 11:47:34 +08:00
Vec3 axes;
float angle;
2021-12-28 16:06:23 +08:00
calculateArcBall(axes, angle, prelocation.x, prelocation.y, location.x,
location.y); // calculate rotation quaternion parameters
Quaternion quat(axes, angle); // get rotation quaternion
2015-01-07 11:47:34 +08:00
_rotationQuat = quat * _rotationQuat;
2021-12-28 16:06:23 +08:00
updateCameraTransform(); // update camera Transform
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
else if (_operate == OperateCamType::MoveCamera) // camera zoom
2015-01-07 11:47:34 +08:00
{
Point newPos = touchs[0]->getPreviousLocation() - touchs[0]->getLocation();
2021-12-28 16:06:23 +08:00
_distanceZ -= newPos.y * 0.1f;
2015-01-07 11:47:34 +08:00
updateCameraTransform();
}
}
}
2021-12-28 16:06:23 +08:00
void CameraArcBallDemo::calculateArcBall(cocos2d::Vec3& axis, float& angle, float p1x, float p1y, float p2x, float p2y)
2015-01-07 11:47:34 +08:00
{
Mat4 rotation_matrix;
Mat4::createRotation(_rotationQuat, &rotation_matrix);
2021-12-28 16:06:23 +08:00
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
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +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
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
Vec3::cross(p2, p1, &axis); // calculate rotation axis
2015-01-07 11:47:34 +08:00
axis.normalize();
float t = (p2 - p1).length() / (2.0f * _radius);
2021-12-28 16:06:23 +08:00
// clamp -1 to 1
if (t > 1.0)
t = 1.0;
if (t < -1.0)
t = -1.0;
angle = asin(t); // rotation angle
2015-01-07 11:47:34 +08:00
}
2015-01-09 10:23:40 +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. */
2021-12-28 16:06:23 +08:00
float CameraArcBallDemo::projectToSphere(float r, float x, float y)
2015-01-07 11:47:34 +08:00
{
float d, t, z;
2021-12-28 16:06:23 +08:00
d = sqrt(x * x + y * y);
if (d < r * 0.70710678118654752440) // inside sphere
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
z = sqrt(r * r - d * d);
}
else // on hyperbola
2015-01-07 11:47:34 +08:00
{
t = r / 1.41421356237309504880f;
2021-12-28 16:06:23 +08:00
z = t * t / d;
2015-01-07 11:47:34 +08:00
}
return z;
}
void CameraArcBallDemo::updateCameraTransform()
{
Mat4 trans, rot, center;
2015-01-15 15:53:19 +08:00
Mat4::createTranslation(Vec3(0.0f, 10.0f, _distanceZ), &trans);
2015-01-07 11:47:34 +08:00
Mat4::createRotation(_rotationQuat, &rot);
Mat4::createTranslation(_center, &center);
Mat4 result = center * rot * trans;
_camera->setNodeToParentTransform(result);
}
void CameraArcBallDemo::switchOperateCallback(Ref* sender)
{
2021-12-28 16:06:23 +08:00
if (_operate == OperateCamType::MoveCamera)
2015-01-07 11:47:34 +08:00
{
_operate = OperateCamType::RotateCamera;
}
2021-12-28 16:06:23 +08:00
else if (_operate == OperateCamType::RotateCamera)
2015-01-07 11:47:34 +08:00
{
_operate = OperateCamType::MoveCamera;
}
}
void CameraArcBallDemo::switchTargetCallback(Ref* sender)
{
2021-12-28 16:06:23 +08:00
if (_target == 0)
2015-01-07 11:47:34 +08:00
{
_target = 1;
_center = _mesh2->getPosition3D();
2015-01-07 11:47:34 +08:00
updateCameraTransform();
}
2021-12-28 16:06:23 +08:00
else if (_target == 1)
2015-01-07 11:47:34 +08:00
{
_target = 0;
_center = _mesh1->getPosition3D();
2015-01-07 11:47:34 +08:00
updateCameraTransform();
}
}
void CameraArcBallDemo::update(float dt)
{
2021-12-28 16:06:23 +08:00
// updateCameraTransform();
2015-01-07 11:47:34 +08:00
}
////////////////////////////////////////////////////////////
// FogTestDemo
2021-12-28 16:06:23 +08:00
FogTestDemo::FogTestDemo() : CameraBaseTest() {}
FogTestDemo::~FogTestDemo()
2015-01-07 11:47:34 +08:00
{
2019-03-13 15:06:30 +08:00
CC_SAFE_RELEASE_NULL(_programState1);
CC_SAFE_RELEASE_NULL(_programState2);
2015-01-07 11:47:34 +08:00
}
std::string FogTestDemo::title() const
{
return "Fog Test Demo";
}
void FogTestDemo::onEnter()
{
CameraBaseTest::onEnter();
2019-03-13 15:06:30 +08:00
schedule(CC_SCHEDULE_SELECTOR(FogTestDemo::update), 0.0f);
2021-12-28 16:06:23 +08:00
Director::getInstance()->setClearColor(Color4F(0.5, 0.5, 0.5, 1));
2015-01-09 10:23:40 +08:00
2021-12-28 16:06:23 +08:00
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
2015-01-07 11:47:34 +08:00
listener->onTouchesMoved = CC_CALLBACK_2(FogTestDemo::onTouchesMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// switch fog type
2015-01-07 11:47:34 +08:00
TTFConfig ttfConfig("fonts/arial.ttf", 20);
2021-12-28 16:06:23 +08:00
auto label1 = Label::createWithTTF(ttfConfig, "Linear ");
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(FogTestDemo::switchTypeCallback, this, 0));
auto label2 = Label::createWithTTF(ttfConfig, "Exp");
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(FogTestDemo::switchTypeCallback, this, 1));
auto label3 = Label::createWithTTF(ttfConfig, "Exp2");
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(FogTestDemo::switchTypeCallback, this, 2));
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
2015-01-07 11:47:34 +08:00
menu->setPosition(Vec2::ZERO);
2021-12-28 16:06:23 +08:00
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);
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
auto layer3D = Layer::create();
addChild(layer3D, 0);
_layer3D = layer3D;
2015-01-07 11:47:34 +08:00
2019-03-13 15:06:30 +08:00
CC_SAFE_RELEASE_NULL(_programState1);
CC_SAFE_RELEASE_NULL(_programState2);
auto vertexSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.frag");
2021-12-28 16:06:23 +08:00
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
CC_SAFE_RELEASE(program);
2021-12-28 16:06:23 +08:00
_mesh1 = MeshRenderer::create("MeshRendererTest/teapot.c3b");
_mesh2 = MeshRenderer::create("MeshRendererTest/teapot.c3b");
2015-01-07 11:47:34 +08:00
_mesh1->setProgramState(_programState1);
_mesh2->setProgramState(_programState2);
2019-03-13 15:06:30 +08:00
2021-12-28 16:06:23 +08:00
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogStart = 10;
float fogEnd = 60;
int fogEquation = 0;
2019-03-13 15:06:30 +08:00
2021-12-28 16:06:23 +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));
2015-01-07 11:47:34 +08:00
_layer3D->addChild(_mesh1);
_mesh1->setPosition3D(Vec3(0, 0, 0));
_mesh1->setScale(2.0f);
_mesh1->setRotation3D(Vec3(-90.0f, 180.0f, 0.0f));
2015-01-07 11:47:34 +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));
2015-01-07 11:47:34 +08:00
if (_camera == nullptr)
{
2021-12-28 16:06:23 +08:00
_camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
2015-01-07 11:47:34 +08:00
_camera->setCameraFlag(CameraFlag::USER1);
_camera->setPosition3D(Vec3(0.0f, 30.0f, 40.0f));
2021-12-28 16:06:23 +08:00
_camera->lookAt(Vec3(0, 0, 0), Vec3(0.0f, 1.0f, 0.0f));
2015-01-07 11:47:34 +08:00
_layer3D->addChild(_camera);
}
_layer3D->setCameraMask(2);
metal support for cocos2d-x (#19305) * remove deprecated files * remove some deprecated codes * remove more deprecated codes * remove ui deprecated codes * remove more deprecated codes * remove deprecated codes in ccmenuitem * remove more deprecated codes in ui * remove more deprecated codes in ui * remove more deprecated codes in ui * remove more deprecated codes * remove more deprecated codes * remove more deprecated codes * remove vr related codes and ignore some modules * remove allocator * remove some config * 【Feature】add back-end project file * [Feature] add back-end file * add pipeline descriptor and shader cache * [Feature] support sprite for backend * [Feature] remove unneeded code * [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture * [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture * [Feature] remove macro define to .cpp file * [Feature] add log info * [Feature] add PipelineDescriptor for TriangleCommand * [Feature] add PipelineDescriptor object as member of TriangleCommand * [Feature] add getPipelineDescriptor method * add renderbackend * complete pipeline descriptor * [Feature] add viewport in RenderCommand * set viewport when rendrering * [Feature] occur error when using RendererBackend, to be fixed. * a workaround to fix black screen on macOS 10.14 (#19090) * add rendererbackend init function * fix typo * [Feature] modify testFile * [BugFix] modify shader path * [Feature] set default viewport * fix projection * [Feature] modify log info * [BugFix] change viewport data type to int * [BugFix] add BindGroup to PipelienDescriptor * [BugFix] change a_position to vec3 in sprite.vert * [BugFix] set vertexLayout according to V3F_C4B_T2F structure * [Feature] revert a_position to vec4 * [Feature] renderer should not use gl codes directly * [Feature] it's better not use default value parameter * fix depth test setting * rendererbackend -> renderer * clear color and depth at begin * add metal backend * metal support normalized attribute * simplify codes * update external * add render pass desctriptor in pipeline descriptor * fix warnings * fix crash and memeory leak * refactor Texture2D * put pipeline descriptor into render command * simplify codes * [Feature] update Sprite * fix crash when closing app * [Feature] update SpriteBatchNode and TextureAtlas * support render texture(not finish) * [Feature] remove unused code * make tests work on mac * fix download-deps path error * make tests work on iOS * [Feature] support ttf under normal label effect * refactor triangle command processing * let renderer handle more common commands * refactor backend * make render texture work * [Feature] refactor backend for GL * [Feature]Renaming to make it easy to understand * [Feature] change warp mode to CLAMP_TO_EDGE * fix ghost * simplify visit render queue logic * support progress timer without rial mode * support partcile system * Feature/update label (#149) * [BugFix] fix compile error * [Feature] support outline effect in ios * [Feature] add shader file * [BugFix] fix begin and end RenderPass * [Feature] update CustomCommand * [Feature] revert project.pbxproj * [Feature] simplify codes * [BugFix] pack AI88 to RGBA8888 only when outline enable * [Feature] support shadow effect in Label * [Feature] support BMFont * [Feature] support glow effect * [Feature] simplify shader files * LabelAtlas work * handle blend function correctly * support tile map * don't share buffer in metal * alloc buffer size as needed * support more tilemap * Merge branch 'minggo/metal-support' into feature/updateLabel * minggo/metal-support: support tile map handle blend function correctly LabelAtlas work Feature/update label (#149) support partcile system # Conflicts: # cocos/2d/CCLabel.cpp # cocos/2d/CCSprite.cpp # cocos/2d/CCSpriteBatchNode.cpp # cocos/renderer/CCQuadCommand.cpp # cocos/renderer/CCQuadCommand.h * render texture work without saving file * use global viewport * grid3d works * remove grabber * tiled3d works * [BugFix] fix label bug * [Feature] add updateSubData for buffer * [Feature] remove setVertexCount * support depth test * add callback command * [Feature] add UITest * [Feature] update UITest * [Feature] remove unneeded codes * fix custom command issue * fix layer color blend issue * [BugFix] fix iOS compile error * [Feature] remove unneeded codes * [Feature] fix updateVertexBuffer * layerradial works * add draw test back * fix batch issue * fix compiling error * [BugFix] support ETC1 * [BugFix] get the correct pipelineDescriptor * [BugFix] skip draw when backendTexture nullptr * clipping node support * [Feature] add shader files * fix stencil issue in metal * [Feature] update UILayoutTest * [BugFix] skip drawing when vertexCount is zero * refactor renderer * add set global z order for stencil manager commands * fix warnings caused by type * remove viewport in render command * [Feature] fix warnings caused by type * [BugFix] clear vertexCount and indexCount for CustomComand when needed * [Feature] update clear for CustomCommand * ios use metal * fix viewport issue * fix LayerColorGradient crash * [cmake] transport to android and windows (#160) * save point 1 * compile on windows * run on android * revert useless change * android set CC_ENABLE_CACHE_TEXTURE_DATA to 1 * add initGlew * fix android crash * add TODO new-renderer * review update * revert onGLFWWindowPosCallback * fix android compiling error * Impl progress radial (#162) * progresstimer add radial impl * default drawType to element * dec invoke times of createVertexBuffer (#163) * support depth/stencil format for gl backend * simplify progress timer codes * support motionstreak, effect is wrong * fix motionstreak issue * [Feature] update Scissor Test (#161) * [Feature] update Scissor Test * [Feature] update ScissorTest * [Feature] rename function * [Feature] get constant reference if needed * [Feature] show render status (#164) * improve performance * fix depth state * fill error that triangle vertex/index number bigger than buffer * fix compiline error in release mode * fix buffer conflict between CPU and GPU on iOS/macOS * Renderer refactor (#165) * use one vertes/index buffer with opengl * fix error on windows * custom command support index format config * CCLayer: compact vertex data structure * update comment * fix doc * support fast tilemap * pass index format instead * fix some wrong effect * fix render texture error * fix texture per-element size * fix texture format error * BlendFunc type refactor, GLenum -> backend::BlendFactor (#167) * BlendFunc use backend::BlendFactor as inner field * update comments * use int to replace GLenum * update xcode project fiel * rename to GLBlendConst * add ccConstants.h * update xcode project file * update copyright * remove primitive command * remove CCPrimitive.cpp/.h * remove deprecated files * remove unneeded files * remove multiple view support * remove multiple view support * remove the usage of frame buffer in camera * director don't use frame buffer * remove FrameBuffer * remove BatchCommand * add some api reference * add physics2d back * fix crash when close app on mac * improve render texture * fix rendertexture issue * fix rendertexture issue * simplify codes * CMake support for mac & ios (#169) * update cmake * fix compile error * update 3rd libs version * remove CCThread.h/.cpp * remove ccthread * use audio engine to implement simple audio engine * remove unneeded codes * remove deprecated codes * remove winrt macro * remove CC_USE_WIC * set partcile blend function in more elegant way * remove unneeded codes * remove unneeded codes * cmake works on windows * update project setting * improve performance * GLFloat -> float * sync v3 cmake improvements into metal-support (#172) * pick: modern cmake, compile definitions improvement (#19139) * modern cmake, use target_compile_definitions partly * simplify macro define, remove USE_* * modern cmake, macro define * add physics 2d macro define into ccConfig.h * remove USE_CHIPMUNK macro in build.gradle * remove CocosSelectModule.cmake * shrink useless define * simplify compile options config, re-add if necessary * update external for tmp CI test * un-quote target_compile_options value * add "-g" parameter only when debug mode * keep single build type when generator Xcode & VS projecy * update external for tmp CI tes * add static_cast<char>(-1), fix -Wc++11-narrowing * simplify win32 compile define * not modify code, only improve compile options # Conflicts: # .gitignore # cmake/Modules/CocosConfigDepend.cmake # cocos/CMakeLists.txt # external/config.json # tests/cpp-tests/CMakeLists.txt * modern cmake, improve cmake_compiler_flags (#19145) * cmake_compiler_flags * Fix typo * Fix typo2 * Remove chanages from Android.mk * correct lua template cmake build (#19149) * don't add -Wno-deprecated into jsb target * correct lua template cmake build * fix win32 lua template compile error * prevent cmake in-source-build friendly (#19151) * pick: Copy resources to "Resources/" on win32 like in linux configuration * add "/Z7" for cpp-tests on windows * [cmake] fix iOS xcode property setting failed (#19208) * fix iOS xcode property setting failed * use search_depend_libs_recursive at dlls collect * fix typo * [cmake] add find_host_library into iOS toolchain file (#19230) * pick: [lua android] use luajit & template cmake update (#19239) * increase cmake stability , remove tests/CMakeLists.txt (#19261) * cmake win32 Precompiled header (#19273) * Precompiled header * Fix * Precompiled header for cocos * Precompiled header jscocos2d * Fix for COCOS2D_DEBUG is always 1 on Android (#19291) Related #19289 * little build fix, tests cpp-tests works on mac * sync v3 build related codes into metal-support (#173) * strict initialization for std::array * remove proj.win32 project configs * modern cmake, cmake_cleanup_remove_unused_variables (#19146) * Switch travis CI to xenial (#19207) * Switch travis CI to xenial * Remove language: android * Set language: cpp * Fix java problem * Update sdkmanager * Fix sdkmanger * next sdkmanager fix * Remove xenial from android * revert to sdk-tools-{system}-3859397 * Remove linux cmake install * Update before-install.sh * Update .travis.yml * Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212) * Simplify install-deps-linux.sh * Cleanup * pick: install ninja * update cocos2d-console submodule * for metal-support alpha release, we only test cpp * add HelloCpp into project(Cocos2d-x) for tmp test * update extenal metal-support-4 * update uniform setting * [Feature] update BindGroup * [Feature] empty-test * [Feature] cpp-test * [Feature] fix GL compiler error * [Feature] fix GL crash * [Feature] empty-test * [Feature] cpp-tests * [feature] improve frameRate * [feature] fix opengl compile error * [feature] fix opengl compile error * [BugFix] fix compute maxLocation error * [Feature] update setting unifrom * [Feature] fix namespace * [Feature] remove unneeded code * [Bugfix] fix project file * [Feature] update review * [texture2d] impl texture format support (#175) * texture update * update * update texture * commit * compile on windows * ddd * rename * rename methods * no crash * save gl * save * save * rename * move out pixel format convert functions * metal crash * update * update android * support gles compressed texture format * support more compress format * add more conversion methods * ss * save * update conversion methods * add PVRTC format support * reformat * add marco linux * fix GL marcro * pvrtc supported only by ios 8.0+ * remove unused cmake * revert change * refactor Texture2D::initWithData * fix conversion log * refactor Texture2D::initWithData * remove some OpenGL constants for PVRTC * add todo * fix typo * AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174) * review cpp-tests, and fix part issues on start auto test * sync png format fix: Node:Particle3D abnormal texture effects #19204 * fix cpp-tests SpritePolygon crash, wrong png format (#19170) * fix wrong png convert format from sRGB to Gray * erase plist index if all frames was erased * test_A8.png have I8 format, fix it * [CCSpriteCache] allow re-add plist & add testcase (#19175) * allow re-add plist & add testcase * remove comments/rename method/update testcase * fix isSpriteFramesWithFileLoaded & add testcase * remove used variable * remove unused variable * fix double free issues when js/lua-tests exit on iOS (#19236) * disable part cases, AutoTest works without crash on mac * update cocos2dx files json, to test cocos new next * fix spritecache plist parsing issue (#19269) * [linux] Fix FileUtils::getContents with folder (#19157) * fix FileUtils::getContents on linux/mac * use stat.st_mode * simplify * [CCFileUtils] win32 getFileSize (#19176) * win32 getFileSize * fix stat * [cpp test-Android]20:FileUtils/2 change title (#19197) * sync #19200 * sync #19231 * [android lua] improve performance of lua loader (#19234) * [lua] improve performance of lua loader * remove cache fix * Revert "fix spritecache plist parsing issue (#19269)" This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf. * remove win32 project files ref in template.json * add metal framework lnk ref into cpp template * test on iOS, and disable part cases * alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227) * changes AudioCache to use alBufferData instead of alBufferDataStatic (also makes test 19 faster to trigger openal bugs faster) The original problem: CrashIfClientProvidedBogusAudioBufferList https://github.com/cocos2d/cocos2d-x/issues/18948 is not happening anymore, but there's still a not very frequent issue that makes OpenAL crash with a call stack like this. AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList It happes more frequently when the device is "cold", which means after half an hour of not using the device (locked). I could not find the actual source code for iOS OpenAL, so I used the macOS versions: https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html They seem to use CAGuard.h to make sure the dead buffer list has no threading issues. I'm worried because the CAGuard code I found has macos and win32 define but no iOS, so I'm not sure. I guess the iOS version is different and has the guard. I could not find a place in the code that's unprotected by the locks except the InitializeBufferMap() which should not be called more than once from cocos, and there's a workaround in AudioEngine-impl for it. I reduced the occurence of the CleanUpDeadBufferList crash by moving the guard in ~AudioCache to cover the alDeleteBuffers call. * remove hack method "setTimeout" on audio * AutoTest works on iOS * support set ios deployment target for root project * enable all texture2d cases, since Jiang have fixed * add CCTextureUtils to xcode project file (#176) * add leak cases for SpriteFrameCache (#177) * re-add SpriteFrameCache cases * update template file json * Update SpriteFrameCacheTest.cpp * fix compiling error
2019-01-18 15:08:25 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2021-12-28 16:06:23 +08:00
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) {
Director::getInstance()->setClearColor(Color4F(0.5, 0.5, 0.5, 1));
CC_SAFE_RELEASE_NULL(_programState1);
CC_SAFE_RELEASE_NULL(_programState2);
auto vertexSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.frag");
2021-12-28 16:06:23 +08:00
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
_mesh1->setProgramState(_programState1);
_mesh2->setProgramState(_programState2);
2021-12-28 16:06:23 +08:00
CC_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));
});
2015-01-09 10:23:40 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
void FogTestDemo::switchTypeCallback(Ref* sender, int type)
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
if (type == 0)
2015-01-07 11:47:34 +08:00
{
2019-03-13 15:06:30 +08:00
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogStart = 10;
float fogEnd = 60;
int fogEquation = 0;
2021-12-28 16:06:23 +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));
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
else if (type == 1)
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogDensity = 0.03f;
int fogEquation = 1;
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogDensity", &fogDensity, sizeof(fogDensity));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
else if (type == 2)
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogDensity = 0.03f;
int fogEquation = 2;
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
SET_UNIFORM("u_fogColor", &fogColor, sizeof(fogColor));
SET_UNIFORM("u_fogDensity", &fogDensity, sizeof(fogDensity));
SET_UNIFORM("u_fogEquation", &fogEquation, sizeof(fogEquation));
2015-01-07 11:47:34 +08:00
}
}
void FogTestDemo::onExit()
{
CameraBaseTest::onExit();
2021-12-28 16:06:23 +08:00
Director::getInstance()->setClearColor(Color4F(0, 0, 0, 1));
2015-01-07 11:47:34 +08:00
if (_camera)
{
_camera = nullptr;
}
2015-01-09 10:23:40 +08:00
metal support for cocos2d-x (#19305) * remove deprecated files * remove some deprecated codes * remove more deprecated codes * remove ui deprecated codes * remove more deprecated codes * remove deprecated codes in ccmenuitem * remove more deprecated codes in ui * remove more deprecated codes in ui * remove more deprecated codes in ui * remove more deprecated codes * remove more deprecated codes * remove more deprecated codes * remove vr related codes and ignore some modules * remove allocator * remove some config * 【Feature】add back-end project file * [Feature] add back-end file * add pipeline descriptor and shader cache * [Feature] support sprite for backend * [Feature] remove unneeded code * [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture * [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture * [Feature] remove macro define to .cpp file * [Feature] add log info * [Feature] add PipelineDescriptor for TriangleCommand * [Feature] add PipelineDescriptor object as member of TriangleCommand * [Feature] add getPipelineDescriptor method * add renderbackend * complete pipeline descriptor * [Feature] add viewport in RenderCommand * set viewport when rendrering * [Feature] occur error when using RendererBackend, to be fixed. * a workaround to fix black screen on macOS 10.14 (#19090) * add rendererbackend init function * fix typo * [Feature] modify testFile * [BugFix] modify shader path * [Feature] set default viewport * fix projection * [Feature] modify log info * [BugFix] change viewport data type to int * [BugFix] add BindGroup to PipelienDescriptor * [BugFix] change a_position to vec3 in sprite.vert * [BugFix] set vertexLayout according to V3F_C4B_T2F structure * [Feature] revert a_position to vec4 * [Feature] renderer should not use gl codes directly * [Feature] it's better not use default value parameter * fix depth test setting * rendererbackend -> renderer * clear color and depth at begin * add metal backend * metal support normalized attribute * simplify codes * update external * add render pass desctriptor in pipeline descriptor * fix warnings * fix crash and memeory leak * refactor Texture2D * put pipeline descriptor into render command * simplify codes * [Feature] update Sprite * fix crash when closing app * [Feature] update SpriteBatchNode and TextureAtlas * support render texture(not finish) * [Feature] remove unused code * make tests work on mac * fix download-deps path error * make tests work on iOS * [Feature] support ttf under normal label effect * refactor triangle command processing * let renderer handle more common commands * refactor backend * make render texture work * [Feature] refactor backend for GL * [Feature]Renaming to make it easy to understand * [Feature] change warp mode to CLAMP_TO_EDGE * fix ghost * simplify visit render queue logic * support progress timer without rial mode * support partcile system * Feature/update label (#149) * [BugFix] fix compile error * [Feature] support outline effect in ios * [Feature] add shader file * [BugFix] fix begin and end RenderPass * [Feature] update CustomCommand * [Feature] revert project.pbxproj * [Feature] simplify codes * [BugFix] pack AI88 to RGBA8888 only when outline enable * [Feature] support shadow effect in Label * [Feature] support BMFont * [Feature] support glow effect * [Feature] simplify shader files * LabelAtlas work * handle blend function correctly * support tile map * don't share buffer in metal * alloc buffer size as needed * support more tilemap * Merge branch 'minggo/metal-support' into feature/updateLabel * minggo/metal-support: support tile map handle blend function correctly LabelAtlas work Feature/update label (#149) support partcile system # Conflicts: # cocos/2d/CCLabel.cpp # cocos/2d/CCSprite.cpp # cocos/2d/CCSpriteBatchNode.cpp # cocos/renderer/CCQuadCommand.cpp # cocos/renderer/CCQuadCommand.h * render texture work without saving file * use global viewport * grid3d works * remove grabber * tiled3d works * [BugFix] fix label bug * [Feature] add updateSubData for buffer * [Feature] remove setVertexCount * support depth test * add callback command * [Feature] add UITest * [Feature] update UITest * [Feature] remove unneeded codes * fix custom command issue * fix layer color blend issue * [BugFix] fix iOS compile error * [Feature] remove unneeded codes * [Feature] fix updateVertexBuffer * layerradial works * add draw test back * fix batch issue * fix compiling error * [BugFix] support ETC1 * [BugFix] get the correct pipelineDescriptor * [BugFix] skip draw when backendTexture nullptr * clipping node support * [Feature] add shader files * fix stencil issue in metal * [Feature] update UILayoutTest * [BugFix] skip drawing when vertexCount is zero * refactor renderer * add set global z order for stencil manager commands * fix warnings caused by type * remove viewport in render command * [Feature] fix warnings caused by type * [BugFix] clear vertexCount and indexCount for CustomComand when needed * [Feature] update clear for CustomCommand * ios use metal * fix viewport issue * fix LayerColorGradient crash * [cmake] transport to android and windows (#160) * save point 1 * compile on windows * run on android * revert useless change * android set CC_ENABLE_CACHE_TEXTURE_DATA to 1 * add initGlew * fix android crash * add TODO new-renderer * review update * revert onGLFWWindowPosCallback * fix android compiling error * Impl progress radial (#162) * progresstimer add radial impl * default drawType to element * dec invoke times of createVertexBuffer (#163) * support depth/stencil format for gl backend * simplify progress timer codes * support motionstreak, effect is wrong * fix motionstreak issue * [Feature] update Scissor Test (#161) * [Feature] update Scissor Test * [Feature] update ScissorTest * [Feature] rename function * [Feature] get constant reference if needed * [Feature] show render status (#164) * improve performance * fix depth state * fill error that triangle vertex/index number bigger than buffer * fix compiline error in release mode * fix buffer conflict between CPU and GPU on iOS/macOS * Renderer refactor (#165) * use one vertes/index buffer with opengl * fix error on windows * custom command support index format config * CCLayer: compact vertex data structure * update comment * fix doc * support fast tilemap * pass index format instead * fix some wrong effect * fix render texture error * fix texture per-element size * fix texture format error * BlendFunc type refactor, GLenum -> backend::BlendFactor (#167) * BlendFunc use backend::BlendFactor as inner field * update comments * use int to replace GLenum * update xcode project fiel * rename to GLBlendConst * add ccConstants.h * update xcode project file * update copyright * remove primitive command * remove CCPrimitive.cpp/.h * remove deprecated files * remove unneeded files * remove multiple view support * remove multiple view support * remove the usage of frame buffer in camera * director don't use frame buffer * remove FrameBuffer * remove BatchCommand * add some api reference * add physics2d back * fix crash when close app on mac * improve render texture * fix rendertexture issue * fix rendertexture issue * simplify codes * CMake support for mac & ios (#169) * update cmake * fix compile error * update 3rd libs version * remove CCThread.h/.cpp * remove ccthread * use audio engine to implement simple audio engine * remove unneeded codes * remove deprecated codes * remove winrt macro * remove CC_USE_WIC * set partcile blend function in more elegant way * remove unneeded codes * remove unneeded codes * cmake works on windows * update project setting * improve performance * GLFloat -> float * sync v3 cmake improvements into metal-support (#172) * pick: modern cmake, compile definitions improvement (#19139) * modern cmake, use target_compile_definitions partly * simplify macro define, remove USE_* * modern cmake, macro define * add physics 2d macro define into ccConfig.h * remove USE_CHIPMUNK macro in build.gradle * remove CocosSelectModule.cmake * shrink useless define * simplify compile options config, re-add if necessary * update external for tmp CI test * un-quote target_compile_options value * add "-g" parameter only when debug mode * keep single build type when generator Xcode & VS projecy * update external for tmp CI tes * add static_cast<char>(-1), fix -Wc++11-narrowing * simplify win32 compile define * not modify code, only improve compile options # Conflicts: # .gitignore # cmake/Modules/CocosConfigDepend.cmake # cocos/CMakeLists.txt # external/config.json # tests/cpp-tests/CMakeLists.txt * modern cmake, improve cmake_compiler_flags (#19145) * cmake_compiler_flags * Fix typo * Fix typo2 * Remove chanages from Android.mk * correct lua template cmake build (#19149) * don't add -Wno-deprecated into jsb target * correct lua template cmake build * fix win32 lua template compile error * prevent cmake in-source-build friendly (#19151) * pick: Copy resources to "Resources/" on win32 like in linux configuration * add "/Z7" for cpp-tests on windows * [cmake] fix iOS xcode property setting failed (#19208) * fix iOS xcode property setting failed * use search_depend_libs_recursive at dlls collect * fix typo * [cmake] add find_host_library into iOS toolchain file (#19230) * pick: [lua android] use luajit & template cmake update (#19239) * increase cmake stability , remove tests/CMakeLists.txt (#19261) * cmake win32 Precompiled header (#19273) * Precompiled header * Fix * Precompiled header for cocos * Precompiled header jscocos2d * Fix for COCOS2D_DEBUG is always 1 on Android (#19291) Related #19289 * little build fix, tests cpp-tests works on mac * sync v3 build related codes into metal-support (#173) * strict initialization for std::array * remove proj.win32 project configs * modern cmake, cmake_cleanup_remove_unused_variables (#19146) * Switch travis CI to xenial (#19207) * Switch travis CI to xenial * Remove language: android * Set language: cpp * Fix java problem * Update sdkmanager * Fix sdkmanger * next sdkmanager fix * Remove xenial from android * revert to sdk-tools-{system}-3859397 * Remove linux cmake install * Update before-install.sh * Update .travis.yml * Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212) * Simplify install-deps-linux.sh * Cleanup * pick: install ninja * update cocos2d-console submodule * for metal-support alpha release, we only test cpp * add HelloCpp into project(Cocos2d-x) for tmp test * update extenal metal-support-4 * update uniform setting * [Feature] update BindGroup * [Feature] empty-test * [Feature] cpp-test * [Feature] fix GL compiler error * [Feature] fix GL crash * [Feature] empty-test * [Feature] cpp-tests * [feature] improve frameRate * [feature] fix opengl compile error * [feature] fix opengl compile error * [BugFix] fix compute maxLocation error * [Feature] update setting unifrom * [Feature] fix namespace * [Feature] remove unneeded code * [Bugfix] fix project file * [Feature] update review * [texture2d] impl texture format support (#175) * texture update * update * update texture * commit * compile on windows * ddd * rename * rename methods * no crash * save gl * save * save * rename * move out pixel format convert functions * metal crash * update * update android * support gles compressed texture format * support more compress format * add more conversion methods * ss * save * update conversion methods * add PVRTC format support * reformat * add marco linux * fix GL marcro * pvrtc supported only by ios 8.0+ * remove unused cmake * revert change * refactor Texture2D::initWithData * fix conversion log * refactor Texture2D::initWithData * remove some OpenGL constants for PVRTC * add todo * fix typo * AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174) * review cpp-tests, and fix part issues on start auto test * sync png format fix: Node:Particle3D abnormal texture effects #19204 * fix cpp-tests SpritePolygon crash, wrong png format (#19170) * fix wrong png convert format from sRGB to Gray * erase plist index if all frames was erased * test_A8.png have I8 format, fix it * [CCSpriteCache] allow re-add plist & add testcase (#19175) * allow re-add plist & add testcase * remove comments/rename method/update testcase * fix isSpriteFramesWithFileLoaded & add testcase * remove used variable * remove unused variable * fix double free issues when js/lua-tests exit on iOS (#19236) * disable part cases, AutoTest works without crash on mac * update cocos2dx files json, to test cocos new next * fix spritecache plist parsing issue (#19269) * [linux] Fix FileUtils::getContents with folder (#19157) * fix FileUtils::getContents on linux/mac * use stat.st_mode * simplify * [CCFileUtils] win32 getFileSize (#19176) * win32 getFileSize * fix stat * [cpp test-Android]20:FileUtils/2 change title (#19197) * sync #19200 * sync #19231 * [android lua] improve performance of lua loader (#19234) * [lua] improve performance of lua loader * remove cache fix * Revert "fix spritecache plist parsing issue (#19269)" This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf. * remove win32 project files ref in template.json * add metal framework lnk ref into cpp template * test on iOS, and disable part cases * alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227) * changes AudioCache to use alBufferData instead of alBufferDataStatic (also makes test 19 faster to trigger openal bugs faster) The original problem: CrashIfClientProvidedBogusAudioBufferList https://github.com/cocos2d/cocos2d-x/issues/18948 is not happening anymore, but there's still a not very frequent issue that makes OpenAL crash with a call stack like this. AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList It happes more frequently when the device is "cold", which means after half an hour of not using the device (locked). I could not find the actual source code for iOS OpenAL, so I used the macOS versions: https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html They seem to use CAGuard.h to make sure the dead buffer list has no threading issues. I'm worried because the CAGuard code I found has macos and win32 define but no iOS, so I'm not sure. I guess the iOS version is different and has the guard. I could not find a place in the code that's unprotected by the locks except the InitializeBufferMap() which should not be called more than once from cocos, and there's a workaround in AudioEngine-impl for it. I reduced the occurence of the CleanUpDeadBufferList crash by moving the guard in ~AudioCache to cover the alDeleteBuffers call. * remove hack method "setTimeout" on audio * AutoTest works on iOS * support set ios deployment target for root project * enable all texture2d cases, since Jiang have fixed * add CCTextureUtils to xcode project file (#176) * add leak cases for SpriteFrameCache (#177) * re-add SpriteFrameCache cases * update template file json * Update SpriteFrameCacheTest.cpp * fix compiling error
2019-01-18 15:08:25 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2015-01-09 10:23:40 +08:00
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
2015-01-07 11:47:34 +08:00
}
2021-12-28 16:06:23 +08:00
void FogTestDemo::update(float dt) {}
2015-01-07 11:47:34 +08:00
2021-12-28 16:06:23 +08:00
void FogTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event* event)
2015-01-07 11:47:34 +08:00
{
2021-12-28 16:06:23 +08:00
if (touches.size() == 1)
2015-01-07 11:47:34 +08:00
{
2015-01-09 10:23:40 +08:00
Vec2 prelocation = touches[0]->getPreviousLocationInView();
2021-12-28 16:06:23 +08:00
Vec2 location = touches[0]->getLocationInView();
Vec2 newPos = prelocation - location;
if (_cameraType == CameraType::Free)
2015-01-07 11:47:34 +08:00
{
Vec3 cameraDir;
Vec3 cameraRightDir;
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
cameraDir.normalize();
2021-12-28 16:06:23 +08:00
cameraDir.y = 0;
2015-01-07 11:47:34 +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.1f;
cameraPos += cameraRightDir * newPos.x * 0.1f;
2015-01-07 11:47:34 +08:00
_camera->setPosition3D(cameraPos);
}
}
}
2015-05-13 14:24:53 +08:00
2021-12-28 16:06:23 +08:00
// CameraFrameBufferTest::CameraFrameBufferTest()
2019-03-13 15:06:30 +08:00
//{
//
2021-12-28 16:06:23 +08:00
// }
//
// CameraFrameBufferTest::~CameraFrameBufferTest()
2019-03-13 15:06:30 +08:00
//{
//
2021-12-28 16:06:23 +08:00
// }
//
// std::string CameraFrameBufferTest::title() const
2019-03-13 15:06:30 +08:00
//{
2021-12-28 16:06:23 +08:00
// return "Camera FrameBuffer Object Test";
// }
2019-03-13 15:06:30 +08:00
//
2021-12-28 16:06:23 +08:00
// void CameraFrameBufferTest::onEnter()
2019-03-13 15:06:30 +08:00
//{
2021-12-28 16:06:23 +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);
2021-12-28 16:06:23 +08:00
// //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);
// }
2021-12-28 16:06:23 +08:00
BackgroundColorBrushTest::BackgroundColorBrushTest() {}
BackgroundColorBrushTest::~BackgroundColorBrushTest() {}
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();
2021-12-28 16:06:23 +08:00
auto s = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
{
// 1st Camera
2021-12-28 16:06:23 +08:00
auto camera = Camera::createPerspective(60.0f, (float)s.width / s.height, 1.0f, 1000.0f);
camera->setPosition3D(Vec3(0.0f, 0.0f, 200.0f));
camera->lookAt(Vec3::ZERO);
camera->setDepth(-2);
camera->setCameraFlag(CameraFlag::USER1);
addChild(camera);
2021-12-28 16:06:23 +08:00
// 3D model
auto model = MeshRenderer::create("MeshRendererTest/boss1.obj");
model->setScale(4);
model->setPosition3D(Vec3(20.0f, 0.0f, 0.0f));
model->setTexture("MeshRendererTest/boss.png");
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))));
}
2021-12-28 16:06:23 +08:00
{
auto base = Node::create();
base->setContentSize(s);
base->setCameraMask(static_cast<unsigned short>(CameraFlag::USER2));
addChild(base);
2021-12-28 16:06:23 +08:00
// 2nd Camera
2021-12-28 16:06:23 +08:00
auto camera = Camera::createPerspective(60, (float)s.width / s.height, 1, 1000);
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);
2021-12-28 16:06:23 +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");
2021-12-28 16:06:23 +08:00
slider->setPosition(Vec2(s.width / 2, s.height / 4));
slider->setPercent(50);
2021-12-28 16:06:23 +08:00
slider->addEventListener([slider, colorBrush](Ref*, ui::Slider::EventType) {
colorBrush->setColor(Color4F(.1f, .1f, 1.f, (float)slider->getPercent() / 100.f));
});
addChild(slider);
2021-12-28 16:06:23 +08:00
// 3D model for 2nd camera
auto model = MeshRenderer::create("MeshRendererTest/boss1.obj");
model->setScale(4);
model->setPosition3D(Vec3(-20.0f, 0.0f, 0.0f));
model->setTexture("MeshRendererTest/boss.png");
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))));
}
}