axmol/tests/cpp-tests/Classes/Scene3DTest/Scene3DTest.cpp

486 lines
18 KiB
C++
Raw Normal View History

2015-05-07 16:22:30 +08:00
#include "Scene3DTest.h"
2015-05-08 11:46:27 +08:00
#include "ui/CocosGUI.h"
#include <spine/spine-cocos2dx.h>
2015-05-07 16:22:30 +08:00
#include "../testResource.h"
2015-05-14 14:36:45 +08:00
#include "../TerrainTest/TerrainTest.h"
2015-05-07 16:22:30 +08:00
USING_NS_CC;
using namespace spine;
2015-05-08 11:46:27 +08:00
2015-05-07 16:22:30 +08:00
////////////////////////////////////////////////////////////////////////////////
// Implements Scene3DTestScene
class Scene3DTestScene : public TerrainWalkThru
2015-05-07 16:22:30 +08:00
{
public:
CREATE_FUNC(Scene3DTestScene);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
cocos2d::EventListenerCustom* _backToForegroundListener;
#endif
2015-05-07 16:22:30 +08:00
private:
Scene3DTestScene();
virtual ~Scene3DTestScene();
bool init() override;
void create3DWorld();
void createUI();
void createPlayerDlg();
void createDetailDlg();
2015-05-11 14:41:50 +08:00
void createDescDlg();
2015-05-07 16:22:30 +08:00
2015-05-08 11:46:27 +08:00
std::vector<Camera *> _gameCameras;
Node* _playerDlg;
Node* _detailDlg;
2015-05-11 14:41:50 +08:00
Node* _descDlg;
2015-05-08 11:46:27 +08:00
TextureCube* _textureCube;
Skybox* _skyBox;
2015-05-07 16:22:30 +08:00
};
2015-05-07 16:52:37 +08:00
2015-05-11 14:41:50 +08:00
enum GAME_LAYER {
LAYER_SKYBOX = 0,
LAYER_GAME,
LAYER_UI,
LAYER_ACTOR,
LAYER_ZOOM,
LAYER_OSD,
LAYER_COUNT,
2015-05-08 11:46:27 +08:00
};
2015-05-11 14:41:50 +08:00
static CameraFlag s_CF[LAYER_COUNT] = {
2015-05-08 11:46:27 +08:00
CameraFlag::DEFAULT,
CameraFlag::USER1,
CameraFlag::USER2,
CameraFlag::USER3,
CameraFlag::USER4,
2015-05-11 14:41:50 +08:00
CameraFlag::USER5,
2015-05-08 11:46:27 +08:00
};
2015-05-11 14:41:50 +08:00
static unsigned short s_CM[LAYER_COUNT] = {
2015-05-08 11:46:27 +08:00
(unsigned short)s_CF[0],
(unsigned short)s_CF[1],
(unsigned short)s_CF[2],
(unsigned short)s_CF[3],
(unsigned short)s_CF[4],
2015-05-11 14:41:50 +08:00
(unsigned short)s_CF[5],
2015-05-08 11:46:27 +08:00
};
2015-05-07 16:22:30 +08:00
Scene3DTestScene::Scene3DTestScene()
: _playerDlg(nullptr)
, _detailDlg(nullptr)
2015-05-11 14:41:50 +08:00
, _descDlg(nullptr)
2015-05-07 16:22:30 +08:00
{
}
Scene3DTestScene::~Scene3DTestScene()
{
}
bool Scene3DTestScene::init()
{
bool ret = false;
do
{
CC_BREAK_IF(false == TerrainWalkThru::init());
2015-05-07 16:22:30 +08:00
Director::getInstance()->setDisplayStats(false);
2015-05-08 11:46:27 +08:00
// prepare for camera creation, we need create three custom cameras
2015-05-11 14:41:50 +08:00
_gameCameras.resize(LAYER_COUNT);
2015-05-08 11:46:27 +08:00
2015-05-11 14:41:50 +08:00
Camera *ca = nullptr;
auto visibleSize = Director::getInstance()->getVisibleSize();
// first, create a camera to look the skybox
ca = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 10, 1000);
ca->setPosition3D(Vec3(0.f, 0.f, 50.f));
ca->setCameraFlag(s_CF[LAYER_SKYBOX]);
ca->setDepth(LAYER_SKYBOX);
_gameCameras[LAYER_SKYBOX] = ca;
this->addChild(ca);
// second, create a camera to look the 3D game scene
// it has been create in TerrainWalkThru::init
2015-05-11 14:41:50 +08:00
ca = this->_camera;
ca->setCameraFlag(s_CF[LAYER_GAME]);
ca->setDepth(LAYER_GAME);
_gameCameras[LAYER_GAME] = ca;
2015-05-08 11:46:27 +08:00
2015-05-11 14:41:50 +08:00
// third, use the default camera to look 2D base UI layer
2015-05-08 11:46:27 +08:00
ca =this->getDefaultCamera();
2015-05-11 14:41:50 +08:00
ca->setCameraFlag(s_CF[LAYER_UI]);
ca->setDepth(LAYER_UI);
_gameCameras[LAYER_UI] = ca;
2015-05-08 11:46:27 +08:00
2015-05-11 14:41:50 +08:00
// forth, create a camera to look the 3D model in dialogs
2015-05-08 11:46:27 +08:00
ca = Camera::create();
2015-05-11 14:41:50 +08:00
ca->setCameraFlag(s_CF[LAYER_ACTOR]);
ca->setDepth(LAYER_ACTOR);
_gameCameras[LAYER_ACTOR] = ca;
2015-05-08 11:46:27 +08:00
this->addChild(ca);
2015-05-11 14:41:50 +08:00
// fifth, create a camera to look the UI element over on the 3D models
2015-05-08 11:46:27 +08:00
ca = Camera::create();
2015-05-11 14:41:50 +08:00
ca->setCameraFlag(s_CF[LAYER_ZOOM]);
ca->setDepth(LAYER_ZOOM);
_gameCameras[LAYER_ZOOM] = ca;
2015-05-08 11:46:27 +08:00
this->addChild(ca);
2015-05-11 14:41:50 +08:00
// sixth, create a camera to look the OSD UI
ca = Camera::create();
ca->setCameraFlag(s_CF[LAYER_OSD]);
ca->setDepth(LAYER_OSD);
_gameCameras[LAYER_OSD] = ca;
this->addChild(ca);
2015-05-08 11:46:27 +08:00
// create all object in game
2015-05-07 16:22:30 +08:00
create3DWorld();
createUI();
createPlayerDlg();
createDetailDlg();
2015-05-11 14:41:50 +08:00
createDescDlg();
2015-05-08 11:46:27 +08:00
_playerDlg->setVisible(false);
_detailDlg->setVisible(false);
//_terrain->setVisible(false);
2015-05-07 16:22:30 +08:00
ret = true;
} while (0);
return ret;
}
void Scene3DTestScene::create3DWorld()
{
// terrain and player has been create in TerrainWalkThru::init
// we only need override there camera mask
2015-05-11 14:41:50 +08:00
_terrain->setCameraMask(s_CM[LAYER_GAME]);
_player->setCameraMask(s_CM[LAYER_GAME]);
_player->setScale(_player->getScale() * 1.5);
2015-05-08 11:46:27 +08:00
2015-05-11 14:41:50 +08:00
// add two Sprite3D monster, one is transparent
auto monster = Sprite3D::create("Sprite3DTest/orc.c3b");
monster->setRotation3D(Vec3(0,180,0));
monster->setPosition3D(_player->getPosition3D() + Vec3(50, -10, 0));
monster->setCameraMask(s_CM[LAYER_GAME]);
monster->setOpacity(128);
this->addChild(monster);
monster = Sprite3D::create("Sprite3DTest/orc.c3b");
monster->setRotation3D(Vec3(0,180,0));
monster->setPosition3D(_player->getPosition3D() + Vec3(-50, -5, 0));
monster->setCameraMask(s_CM[LAYER_GAME]);
this->addChild(monster);
// add a particle 3d above player
auto rootps = PUParticleSystem3D::create("Particle3D/scripts/blackHole.pu", "Particle3D/materials/pu_mediapack_01.material");
2015-05-11 14:41:50 +08:00
rootps->setScale(2);
rootps->setPosition3D(Vec3(0, 150, 0));
rootps->setCameraMask(s_CM[LAYER_GAME]);
auto moveby = MoveBy::create(2.0f, Vec2(50.0f, 0.0f));
auto moveby1 = MoveBy::create(2.0f, Vec2(-50.0f, 0.0f));
rootps->runAction(RepeatForever::create(Sequence::create(moveby, moveby1, nullptr)));
rootps->startParticleSystem();
_player->addChild(rootps, 0);
// then, create skybox
//create and set our custom shader
auto shader = GLProgram::createWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag");
auto state = GLProgramState::create(shader);
// create the second texture for cylinder
_textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg",
"Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg",
"Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
_textureCube->retain();
//set texture parameters
Texture2D::TexParams tRepeatParams;
tRepeatParams.magFilter = GL_LINEAR;
tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
_textureCube->setTexParameters(tRepeatParams);
2015-05-08 11:46:27 +08:00
// pass the texture sampler to our custom shader
state->setUniformTexture("u_cubeTex", _textureCube);
2015-05-08 11:46:27 +08:00
// add skybox
_skyBox = Skybox::create();
_skyBox->retain();
2015-05-11 14:41:50 +08:00
_skyBox->setCameraMask(s_CM[LAYER_SKYBOX]);
_skyBox->setTexture(_textureCube);
_skyBox->setScale(700.f);
this->addChild(_skyBox);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
2015-05-11 14:41:50 +08:00
auto state = _skyBox->getGLProgramState();
auto glProgram = state->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag");
glProgram->link();
glProgram->updateUniforms();
_textureCube->reloadTexture();
Texture2D::TexParams tRepeatParams;
tRepeatParams.magFilter = GL_NEAREST;
tRepeatParams.minFilter = GL_NEAREST;
tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
_textureCube->setTexParameters(tRepeatParams);
state->setUniformTexture("u_cubeTex", _textureCube);
_skyBox->reload();
_skyBox->setTexture(_textureCube);
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
#endif
2015-05-07 16:22:30 +08:00
}
void Scene3DTestScene::createUI()
{
2015-05-11 14:41:50 +08:00
// add player button
auto showLeftDlgItem = MenuItemImage::create("Images/Pea.png", "Images/Pea.png", [this](Ref* sender){
this->_playerDlg->setVisible(!this->_playerDlg->isVisible());
2015-05-07 16:22:30 +08:00
});
2015-05-08 11:46:27 +08:00
showLeftDlgItem->setPosition(VisibleRect::left().x + 30, VisibleRect::top().y - 30);
2015-05-07 16:22:30 +08:00
2015-05-11 14:41:50 +08:00
// add discription button
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto descItem = MenuItemLabel::create(Label::createWithTTF(ttfConfig, "Description"),
[this](Ref* sender)
{
this->_descDlg->setVisible(!this->_descDlg->isVisible());
});
descItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::top().y - 25));
// add back button
auto backItem = MenuItemLabel::create(Label::createWithTTF(ttfConfig, "Back"),
[this](Ref* sender){
this->getTestSuite()->backsUpOneLevel();
});
backItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));
MenuItemImage *moveCameraLeft = nullptr, *moveCameraRight = nullptr;
// auto uiCamera = _gameCameras[LAYER_UI];
// moveCameraLeft = MenuItemImage::create(s_pathClose, s_pathClose, [uiCamera](Ref* sender){
// uiCamera->setPositionX(uiCamera->getPositionX() - 10);
// });
2015-05-11 14:41:50 +08:00
// moveCameraLeft->setPosition(VisibleRect::left().x + 30, VisibleRect::left().y);
//
2015-05-11 14:41:50 +08:00
// moveCameraRight = MenuItemImage::create(s_pathClose, s_pathClose, [uiCamera](Ref* sender){
// uiCamera->setPositionX(uiCamera->getPositionX() + 10);
// });
2015-05-11 14:41:50 +08:00
// moveCameraRight->setPosition(VisibleRect::right().x - 30, VisibleRect::right().y);
2015-05-07 18:43:07 +08:00
2015-05-08 11:46:27 +08:00
auto menu = Menu::create(showLeftDlgItem,
2015-05-11 14:41:50 +08:00
descItem,
backItem,
2015-05-08 11:46:27 +08:00
moveCameraLeft,
moveCameraRight,
nullptr);
2015-05-07 16:22:30 +08:00
menu->setPosition(Vec2::ZERO);
2015-05-11 14:41:50 +08:00
menu->setCameraMask(s_CM[LAYER_UI], true);
2015-05-07 16:52:37 +08:00
this->addChild(menu);
2015-05-07 16:22:30 +08:00
}
void Scene3DTestScene::createPlayerDlg()
2015-05-07 16:22:30 +08:00
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
Size dlgSize(190, 240);
Vec2 pos = VisibleRect::center();
float margin = 10;
2015-05-11 14:41:50 +08:00
// first, create dialog ui part, include background, title and buttons
_playerDlg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
_playerDlg->setContentSize(dlgSize);
_playerDlg->setAnchorPoint(Vec2(1, 0.5));
pos.y -= margin;
pos.x -= margin;
_playerDlg->setPosition(pos);
2015-05-07 16:22:30 +08:00
2015-05-07 18:43:07 +08:00
// title
auto title = Label::createWithTTF("Player Dialog","fonts/arial.ttf",16);
title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
_playerDlg->addChild(title);
2015-05-07 16:22:30 +08:00
// player background
Size bgSize(110, 180);
Vec2 bgPos(margin, dlgSize.height / 2 - margin);
auto playerBg = ui::Scale9Sprite::createWithSpriteFrameName("item_bg.png");
playerBg->setContentSize(bgSize);
playerBg->setAnchorPoint(Vec2(0, 0.5));
playerBg->setPosition(bgPos);
_playerDlg->addChild(playerBg);
2015-05-07 18:43:07 +08:00
// item background and item
Size itemSize(48, 48);
Vec2 itemAnchor(0, 1);
Vec2 itemPos(bgPos.x + bgSize.width + margin, bgPos.y + bgSize.height / 2);
auto itemBg = ui::Scale9Sprite::createWithSpriteFrameName("item_bg.png");
itemBg->setContentSize(itemSize);
itemBg->setAnchorPoint(itemAnchor);
itemBg->setPosition(itemPos);
_playerDlg->addChild(itemBg);
auto item = ui::Button::create("crystal.png", "", "", ui::TextureResType::PLIST);
item->setScale(1.5);
item->setAnchorPoint(itemAnchor);
item->setPosition(itemPos);
item->addClickEventListener([this](Ref* sender){
this->_detailDlg->setVisible(!this->_detailDlg->isVisible());
});
_playerDlg->addChild(item);
// after add ui element, add player dialog to scene
2015-05-11 14:41:50 +08:00
_playerDlg->setCameraMask(s_CM[LAYER_UI]);
this->addChild(_playerDlg);
// second, add 3d actor, which on dialog layer
2015-05-07 16:22:30 +08:00
std::string filename = "Sprite3DTest/girl.c3b";
auto girl = Sprite3D::create(filename);
girl->setScale(0.5);
girl->setPosition(bgSize.width / 2, margin * 2);
2015-05-11 14:41:50 +08:00
girl->setCameraMask(s_CM[LAYER_ACTOR]);
playerBg->addChild(girl);
2015-05-07 16:52:37 +08:00
// third, add zoom in/out button, which is 2d ui element and over 3d actor
ui::Button* zoomIn = ui::Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
zoomIn->setScale(0.5);
zoomIn->setAnchorPoint(Vec2(1, 1));
zoomIn->setPosition(Vec2(bgSize.width / 2 - margin / 2, bgSize.height - margin));
zoomIn->addClickEventListener([girl](Ref* sender){
2015-05-07 18:43:07 +08:00
girl->setScale(girl->getScale() * 2);
});
zoomIn->setTitleText("Zoom In");
2015-05-11 14:41:50 +08:00
zoomIn->setCameraMask(s_CM[LAYER_ZOOM]);
playerBg->addChild(zoomIn);
ui::Button* zoomOut = ui::Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
zoomOut->setScale(0.5);
zoomOut->setAnchorPoint(Vec2(0, 1));
zoomOut->setPosition(Vec2(bgSize.width / 2 + margin / 2, bgSize.height - margin));
zoomOut->addClickEventListener([girl](Ref* sender){
2015-05-07 18:43:07 +08:00
girl->setScale(girl->getScale() / 2);
});
zoomOut->setTitleText("Zoom Out");
2015-05-11 14:41:50 +08:00
zoomOut->setCameraMask(s_CM[LAYER_ZOOM]);
playerBg->addChild(zoomOut);
}
2015-05-07 18:43:07 +08:00
void Scene3DTestScene::createDetailDlg()
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
Size dlgSize(190, 240);
Vec2 pos = VisibleRect::center();
float margin = 10;
2015-05-11 14:41:50 +08:00
// create dialog
_detailDlg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
_detailDlg->setContentSize(dlgSize);
_detailDlg->setAnchorPoint(Vec2(0, 0.5));
_detailDlg->setOpacity(224);
pos.y -= margin;
pos.x += margin;
_detailDlg->setPosition(pos);
// title
auto title = Label::createWithTTF("Detail Dialog","fonts/arial.ttf",16);
title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
_detailDlg->addChild(title);
// add a spine ffd animation on it
auto skeletonNode = SkeletonAnimation::createWithFile("spine/goblins-ffd.json", "spine/goblins-ffd.atlas", 1.5f);
skeletonNode->setAnimation(0, "walk", true);
skeletonNode->setSkin("goblin");
skeletonNode->setScale(0.25);
Size windowSize = Director::getInstance()->getWinSize();
skeletonNode->setPosition(Vec2(dlgSize.width / 2, 20));
_detailDlg->addChild(skeletonNode);
scheduleUpdate();
EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [skeletonNode] (Touch* touch, Event* event) -> bool {
if (!skeletonNode->getDebugBonesEnabled())
skeletonNode->setDebugBonesEnabled(true);
else if (skeletonNode->getTimeScale() == 1)
skeletonNode->setTimeScale(0.3f);
else
{
skeletonNode->setTimeScale(1);
skeletonNode->setDebugBonesEnabled(false);
}
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2015-05-07 18:43:07 +08:00
2015-05-11 14:41:50 +08:00
// after add ui element, add dialog to scene
_detailDlg->setCameraMask(s_CM[LAYER_UI]);
this->addChild(_detailDlg);
2015-05-07 16:22:30 +08:00
}
2015-05-11 14:41:50 +08:00
void Scene3DTestScene::createDescDlg()
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
Size dlgSize(440, 240);
Vec2 pos = VisibleRect::center();
float margin = 10;
// create dialog
_descDlg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
_descDlg->setContentSize(dlgSize);
_descDlg->setOpacity(224);
_descDlg->setPosition(pos);
// title
auto title = Label::createWithTTF("Description Dialog","fonts/arial.ttf",16);
title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
_descDlg->addChild(title);
// add a label to retain description text
Size textSize(400, 220);
auto desc =
"Scene 3D test for 2D and 3D mix rendering.\n"
"\n"
"- Game world composite with terrain, skybox and 3D objects.\n"
"- UI composite with 2D nodes.\n"
"- Click the icon at the topleft conner, will show a player dialog which "
"there is a 3D sprite on it.\n"
"- There are two button to zoom the player model, which should keep above "
"on 3D model.\n"
" - This description dialog should above all other elements.\n"
"\n"
"Click \"Description\" button to hide this dialog.\n";
auto text = Label::createWithSystemFont(desc, "Helvetica", 12, textSize);
text->setAnchorPoint(Vec2(0, 1));
text->setPosition(margin, dlgSize.height - (20 + margin));
_descDlg->addChild(text);
// after add ui element, add dialog to scene
_descDlg->setCameraMask(s_CM[LAYER_OSD]);
this->addChild(_descDlg);
}
2015-05-07 16:22:30 +08:00
////////////////////////////////////////////////////////////////////////////////
// Implements Scene3DTests
Scene3DTests::Scene3DTests()
{
ADD_TEST_CASE(Scene3DTestScene);
}