// 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; }
// 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; }