// on "init" you need to initialize your instance bool UVAnim::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } InitUI(); _angle = 0.0f; _meshUVRunTime = 0.0f; auto s = Director::getInstance()->getWinSize(); auto layer3D = Layer::create(); addChild(layer3D, 0); _camera = cocos2d::Camera::createPerspective(60, (GLfloat)s.width / s.height, 1, 1000); _camera->setPosition3D(Vec3(0, 50, 50)); _camera->lookAt(Vec3(0, 0, 0)); _camera->setCameraFlag(CameraFlag::USER1); layer3D->addChild(_camera); layer3D->setCameraMask(2); DrawNode3D* line = DrawNode3D::create(); for (int j = -20; j <= 20; j++) { line->drawLine(Vec3(-100, 0, 5 * j), Vec3(100, 0, 5 * j), Color4F(1, 0, 0, 1)); } //draw z for (int j = -20; j <= 20; j++) { line->drawLine(Vec3(5 * j, 0, -100), Vec3(5 * j, 0, 100), Color4F(0, 0, 1, 1)); } line->setCameraMask(2); layer3D->addChild(line); auto sprite3D = Sprite3D::create("UVAnim/tortoise.c3b"); _sprite3D = sprite3D; auto shader = GLProgram::createWithFilenames("UVAnim/qiu.vert", "UVAnim/qiu.frag"); _state = GLProgramState::create(shader); sprite3D->setGLProgramState(_state); sprite3D->setCameraMask(2); sprite3D->setScale(0.05); sprite3D->setPosition3D(Vec3(0, 0, 0)); sprite3D->setRotation3D(Vec3(-90, 0, 0)); layer3D->addChild(sprite3D); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(UVAnim::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(UVAnim::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(UVAnim::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; }
DrawNode3D* DrawNode3D::create() { DrawNode3D* ret = new (std::nothrow) DrawNode3D(); if (ret && ret->init()) { ret->autorelease(); } else { CC_SAFE_DELETE(ret); } return ret; }
void Camera3DTestDemo::onEnter() { CameraBaseTest::onEnter(); _sprite3D=nullptr; 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); auto layer3D=Layer::create(); addChild(layer3D,0); _layer3D=layer3D; _curState=State_None; addNewSpriteWithCoords( Vec3(0,0,0),"Sprite3DTest/girl.c3b",true,0.2f,true); TTFConfig ttfConfig("fonts/arial.ttf", 20); auto containerForLabel1 = Node::create(); _ZoomOutlabel = Label::createWithTTF(ttfConfig,"zoom out"); _ZoomOutlabel->setPosition(s.width-50, VisibleRect::top().y-30); containerForLabel1->addChild(_ZoomOutlabel); addChild(containerForLabel1, 10); auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); listener1->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOut, this); listener1->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOutEnd, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _ZoomOutlabel); auto containerForLabel2 = Node::create(); _ZoomInlabel = Label::createWithTTF(ttfConfig,"zoom in"); _ZoomInlabel->setPosition(s.width-50, VisibleRect::top().y-100); containerForLabel2->addChild(_ZoomInlabel); addChild(containerForLabel2, 10); auto listener2 = EventListenerTouchOneByOne::create(); listener2->setSwallowTouches(true); listener2->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomIn, this); listener2->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomInEnd, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, _ZoomInlabel); auto containerForLabel3 = Node::create(); _RotateLeftlabel = Label::createWithTTF(ttfConfig,"rotate left"); _RotateLeftlabel->setPosition(s.width-50, VisibleRect::top().y-170); containerForLabel3->addChild(_RotateLeftlabel); addChild(containerForLabel3, 10); auto listener3 = EventListenerTouchOneByOne::create(); listener3->setSwallowTouches(true); listener3->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeft, this); listener3->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeftEnd, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener3, _RotateLeftlabel); auto containerForLabel4 = Node::create(); _RotateRightlabel = Label::createWithTTF(ttfConfig,"rotate right"); _RotateRightlabel->setPosition(s.width-50, VisibleRect::top().y-240); containerForLabel4->addChild(_RotateRightlabel); addChild(containerForLabel4, 10); auto listener4 = EventListenerTouchOneByOne::create(); listener4->setSwallowTouches(true); listener4->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRight, this); listener4->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRightEnd, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener4, _RotateRightlabel); 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)); auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr); menu->setPosition(Vec2::ZERO); menuItem1->setPosition(VisibleRect::left().x+100, VisibleRect::top().y-50); menuItem2->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -100); menuItem3->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -150); addChild(menu, 0); schedule(CC_SCHEDULE_SELECTOR(Camera3DTestDemo::updateCamera), 0.0f); if (_camera == nullptr) { _camera=Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000); _camera->setCameraFlag(CameraFlag::USER1); _layer3D->addChild(_camera); } SwitchViewCallback(this,CameraType::ThirdPerson); DrawNode3D* line =DrawNode3D::create(); //draw x for( int j =-20; j<=20 ;j++) { line->drawLine(Vec3(-100, 0, 5*j),Vec3(100,0,5*j),Color4F(1,0,0,1)); } //draw z for( int j =-20; j<=20 ;j++) { line->drawLine(Vec3(5*j, 0, -100),Vec3(5*j,0,100),Color4F(0,0,1,1)); } //draw y line->drawLine(Vec3(0, -50, 0),Vec3(0,0,0),Color4F(0,0.5,0,1)); line->drawLine(Vec3(0, 0, 0),Vec3(0,50,0),Color4F(0,1,0,1)); _layer3D->addChild(line); _layer3D->setCameraMask(2); }
// on "init" you need to initialize your instance bool UVEquiReload::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } InitUI(); _angle = 0.0f; ////得到屏幕尺寸 auto s = Director::getInstance()->getWinSize(); //创建3D层 auto layer3D = Layer::create(); addChild(layer3D, 0); //创建3D摄像机 _camera = cocos2d::Camera::createPerspective(60, (GLfloat)s.width / s.height, 1, 1000); //设置摄像机眼睛位置 _camera->setPosition3D(Vec3(0, 100, 100)); //设置摄像机观察点位置 _camera->lookAt(Vec3(0, 0, 0)); _camera->setCameraFlag(CameraFlag::USER1); layer3D->addChild(_camera); layer3D->setCameraMask(2); //创建3D网格 DrawNode3D* line = DrawNode3D::create(); //draw x for (int j = -20; j <= 20; j++) { line->drawLine(Vec3(-100, 0, 5 * j), Vec3(100, 0, 5 * j), Color4F(1, 0, 0, 1)); } //draw z for (int j = -20; j <= 20; j++) { line->drawLine(Vec3(5 * j, 0, -100), Vec3(5 * j, 0, 100), Color4F(0, 0, 1, 1)); } line->setCameraMask(2); layer3D->addChild(line); //添加触摸事件 auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(UVEquiReload::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(UVEquiReload::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(UVEquiReload::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //创建3D换装动画模型 _spriteMeshReskin = nullptr; Vec2 origin = Director::getInstance()->getVisibleOrigin(); Vec2 size = Director::getInstance()->getVisibleSize(); std::string fileName = "UVEquiReload/ReskinGirl.c3b"; auto sprite = Sprite3D::create(fileName); sprite->setScale(0.8); sprite->setRotation3D(Vec3(0, 0, 0)); sprite->setPosition3D(Vec3(0, 0, 0)); sprite->setCameraMask(2); layer3D->addChild(sprite); auto animation = Animation3D::create(fileName); if (animation){ auto animate = Animate3D::create(animation); sprite->runAction(RepeatForever::create(animate)); } _spriteMeshReskin = sprite; auto& body = _skins[(int)SkinType::UPPER_BODY]; body.push_back("Girl_UpperBody01"); body.push_back("Girl_UpperBody02"); auto& pants = _skins[(int)SkinType::PANTS]; pants.push_back("Girl_LowerBody01"); pants.push_back("Girl_LowerBody02"); auto& shoes = _skins[(int)SkinType::SHOES]; shoes.push_back("Girl_Shoes01"); shoes.push_back("Girl_Shoes02"); auto& hair = _skins[(int)SkinType::HAIR]; hair.push_back("Girl_Hair01"); hair.push_back("Girl_Hair02"); auto& face = _skins[(int)SkinType::FACE]; face.push_back("Girl_Face01"); face.push_back("Girl_Face02"); auto& hand = _skins[(int)SkinType::HAND]; hand.push_back("Girl_Hand01"); hand.push_back("Girl_Hand02"); auto& glasses = _skins[(int)SkinType::GLASSES]; glasses.push_back(""); glasses.push_back("Girl_Glasses01"); memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int)); applyCurSkin(); return true; }
bool ToonScene::init() { bool bRet = false; do { // CC_BREAK_IF(!Layer::init()) if (!Layer::init()) { break; } FileUtils::getInstance()->addSearchPath("res/toonshader"); this->InitUI(); auto s = Director::getInstance()->getWinSize(); auto layer3D = Layer::create(); addChild(layer3D, 0); m_pCamera = Camera::createPerspective(60, (GLfloat)s.width / s.height, 1, 1000); m_pCamera->setPosition3D(Vec3(0, 60, 50)); m_pCamera->lookAt(Vec3(0, 20, 0)); m_pCamera->setCameraFlag(CameraFlag::USER1); layer3D->addChild(m_pCamera); layer3D->setCameraMask(2); DrawNode3D* line = DrawNode3D::create(); //draw x for (int j = -20; j <= 20; j++) { line->drawLine(Vec3(-100, 0, 5 * j), Vec3(100, 0, 5 * j), Color4F(1, 0, 0, 1)); } //draw z for (int j = -20; j <= 20; j++) { line->drawLine(Vec3(5 * j, 0, -100), Vec3(5 * j, 0, 100), Color4F(0, 0, 1, 1)); } line->setCameraMask(2); layer3D->addChild(line); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(ToonScene::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(ToonScene::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(ToonScene::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto spriteStaticMesh = Sprite3D::create("mod_niutou.c3b"); m_pSprite3D = spriteStaticMesh; spriteStaticMesh->setCameraMask(2); spriteStaticMesh->setScale(0.2); spriteStaticMesh->setPosition3D(Vec3(0, 0, 0)); spriteStaticMesh->setRotation3D(Vec3(-90, 0, 0)); layer3D->addChild(spriteStaticMesh); // auto shader = GLProgram::createWithFilenames("Mesh.vert", "Mesh.frag"); // m_pState = GLProgramState::create(shader); // spriteStaticMesh->setGLProgramState(m_pState); bRet = true; } while (0); return bRet; }