void CreditsScene::initCreditsThings() { { auto node = PixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setPosition3D({30.2,-26,0}); node->configSopx("sopx/welco/back.png.sopx"); _hub->addChild(node); node->setScale(0.4); _pxBack = node; } { auto node = PixelTextNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(0.27); node->setPosition3D({30.4,-26,0}); node->configText("back",1); node->configMixColor({1.f, 200.f/255.f, 51.f/255.f,1.f}); _hub->addChild(node); } { auto _image = RoadPlane::create(); _image->configImage("images/credits.png"); _image->configBlend(true); _image->setScale(0.1); _image->setCameraMask(_mainCamera->getCameraMask()); _image->runAction(MoveBy::create(30, Vec3{0,40,0})); _hub->addChild(_image); } }
void BattleFieldUI::touchButtonInit() {//init touch buttons auto _setBtn = Sprite::createWithSpriteFrameName("UI-1136-640_06.png"); _setBtn->setPosition3D(Vec3(VisibleSize.width - 50, VisibleSize.height - 50, 3)); _setBtn->setScale(0.8); _setBtn->setGlobalZOrder(10); addChild(_setBtn, 3); auto _chest = Sprite::createWithSpriteFrameName("chest.png"); _chest->setPosition3D(Vec3(VisibleSize.width - 100, VisibleSize.height - 50, 3)); _chest->setScale(0.8); _chest->setGlobalZOrder(10); addChild(_chest, 3); auto _coin = Sprite::createWithSpriteFrameName("coins.png"); _coin->setPosition3D(Vec3(VisibleSize.width - 300, VisibleSize.height - 50, 3)); _coin->setScaleX(0.8); _coin->setScaleY(0.8); _coin->setGlobalZOrder(10); addChild(_coin, 3); auto _chestAmount = Sprite::createWithSpriteFrameName("UI-1.png"); _chestAmount->setPosition3D(Vec3(VisibleSize.width - 170, VisibleSize.height - 50, 3)); _chestAmount->setScaleX(0.8); _chestAmount->setScaleY(0.7); _chestAmount->setGlobalZOrder(9); addChild(_chestAmount, 2); auto _coinAmount = Sprite::createWithSpriteFrameName("UI-1.png"); _coinAmount->setPosition3D(Vec3(VisibleSize.width - 370, VisibleSize.height - 50, 3)); _coinAmount->setScaleX(0.8); _coinAmount->setScaleY(0.7); _coinAmount->setGlobalZOrder(9); addChild(_coinAmount, 2); }
bool CreditsScene::init() { assert(TRBaseScene::init()); auto layer = Layer::create(); this->addChild(layer); auto size = Director::getInstance()->getVisibleSize(); auto camera = Camera::createPerspective(60, size.width/size.height, 0.1, 1000000); camera->setCameraFlag(CameraFlag::USER1); layer->addChild(camera); camera->setCameraMask((unsigned short)CameraFlag::USER1); camera->setPosition3D({0,0,50}); camera->lookAt({0,0,0}, {0,1,0}); _mainLayer = layer; _mainCamera = camera; auto hub = Node::create(); hub->setPosition3D({0,0,0}); hub->setCameraMask(_mainCamera->getCameraMask()); _mainLayer->addChild(hub); _hub = hub; initCreditsThings(); initTouchThings(); return true; }
bool Camera::initDefault() { auto size = Director::getInstance()->getWinSize(); //create default camera auto projection = Director::getInstance()->getProjection(); switch (projection) { case Director::Projection::_2D: { initOrthographic(size.width, size.height, -1024, 1024); setPosition3D(Vec3(0.0f, 0.0f, 0.0f)); setRotation3D(Vec3(0.f, 0.f, 0.f)); break; } case Director::Projection::_3D: { float zeye = Director::getInstance()->getZEye(); initPerspective(60, (GLfloat)size.width / size.height, 10, zeye + size.height / 2.0f); Vec3 eye(size.width/2, size.height/2.0f, zeye), center(size.width/2, size.height/2, 0.0f), up(0.0f, 1.0f, 0.0f); setPosition3D(eye); lookAt(center, up); break; } default: CCLOG("unrecognized projection"); break; } return true; }
void WindBar::initWindThings() { { auto node = PlanePixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(1); node->setPosition3D({0,0,0}); auto pixelData = loadScatPixelFile("hunters/sopx/wind_pillar.png.sopx"); node->configBatch(PlanePixelBatchTuple{Vec3{0,0,0}, 1, &pixelData}, true); _hubNode->addChild(node); _pxBg = node; } { auto node = PlanePixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(0.8); node->setPosition3D({0,2,0}); auto pixelData = loadScatPixelFile("hunters/sopx/wind_strenth.png.sopx"); node->configBatch(PlanePixelBatchTuple{Vec3{0,0,0}, 1, &pixelData}, false); _hubNode->addChild(node); _pxStrenth = node; _pxStrenth->setVisible(false); } { auto node = PlanePixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(0.8); node->setPosition3D({0,0.5,0}); auto pixelData = loadScatPixelFile("hunters/sopx/wind_direction.png.sopx"); node->configBatch(PlanePixelBatchTuple{Vec3{0,0,0}, 1, &pixelData}, false); _hubNode->addChild(node); _pxDirection = node; _pxDirection->setVisible(false); } { auto node = PixelTextNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(0.6f,0.9f); node->setPosition3D({1.5,25,0.5}); node->configText("wind",1); node->configMixColor({49.f/255.f, 36.f/255.f, 33.f/255.f,1.f}); _hubNode->addChild(node); _ptWind = node; } { auto node = PixelTextNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(0.8f,0.9f); node->setPosition3D({2,20,0.5}); node->configText("0",1); node->configMixColor({49.f/255.f, 36.f/255.f, 33.f/255.f,1.f}); _hubNode->addChild(node); _ptStrenth = node; } }
void BattleFieldUI::showVictoryUI() {//show the victory UI when win //diable AI //color layer auto layer = LayerColor::create(Color4B(10, 10, 10, 150)); layer->ignoreAnchorPointForPosition(false); layer->setPosition3D(Vec3(VisibleSize.width*0.5, VisibleSize.height*0.5, 0)); //add victory auto victory = Sprite::createWithSpriteFrameName("victory.png"); victory->setPosition3D(Vec3(VisibleSize.width*0.5, VisibleSize.height*0.5, 3)); victory->setScale(0.1); layer->addChild(victory, 1); layer->setGlobalZOrder(100); victory->setGlobalZOrder(100); //victory runaction auto action = EaseElasticOut::create(ScaleTo::create(1.5, 1)); victory->runAction(action); auto listener = EventListenerTouchOneByOne::create(); //touch event listener->onTouchBegan = [](Touch*, Event*) { return true; }; listener->onTouchEnded = [this](Touch*, Event*) { //clear GlobalVaribals HeroPool.clear(); DragonPool.clear(); SlimePool.clear(); RatPool.clear(); BossPool.clear(); PigletPool.clear(); HeroManager.clear(); MonsterManager.clear(); GameMaster::reset(); //stop schedule currentLayer->unscheduleUpdate(); Director::getInstance()->getScheduler()->unschedule("gameController", currentLayer); //stop sound experimental::AudioEngine::stop(AUDIO_ID.BATTLEFIELDBGM); //replace scene Director::getInstance()->replaceScene(ChooseRoleScene::createScene()); }; victory->setCameraMask(2); auto eventDispatcher = layer->getEventDispatcher(); eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer); addChild(layer); }
void HelloWorld::resetScene() { memset(_elements, 0, sizeof(_elements)); _elementContainer->removeAllChildren(); _touchElements[0] = _touchElements[1] = TOUCH_NONE; std::vector<std::pair<int, int> > emptySpace; for (int i = 1; i < MAX_CAPACITY_NUM_IN_LINE - 1; ++i) for (int j = 1; j < MAX_CAPACITY_NUM_IN_LINE - 1; ++j) emptySpace.push_back(std::pair<int, int>(i, j)); while (!emptySpace.empty()) { std::string tex = images[(unsigned int)(CCRANDOM_0_1() * (IMAGE_NUM - 1))]; //first element { unsigned int index = (unsigned int)(CCRANDOM_0_1() * (emptySpace.size() - 1)); auto location = emptySpace.at(index); auto sprite = Sprite3D::create("models/box.c3b"); auto size = sprite->getBoundingBox().size; _elementSize = size; sprite->setTexture(tex); sprite->setName(tex); sprite->setPosition3D(Vec3((-(MAX_ELEMENT_NUM_IN_LINE / 2 - size.width / 2.0f) + location.first - 1) * size.width, 0.0f, (-(MAX_ELEMENT_NUM_IN_LINE / 2 - size.height / 2.0f) + location.second - 1) * size.height)); _elementContainer->addChild(sprite); _elements[location.first * MAX_CAPACITY_NUM_IN_LINE + location.second] = sprite; emptySpace.erase(emptySpace.begin() + index); } //second element { unsigned int index = (unsigned int)(CCRANDOM_0_1() * (emptySpace.size() - 1)); auto location = emptySpace.at(index); auto sprite = Sprite3D::create("models/box.c3b"); auto size = sprite->getBoundingBox().size; _elementSize = size; sprite->setTexture(tex); sprite->setName(tex); sprite->setPosition3D(Vec3((-(MAX_ELEMENT_NUM_IN_LINE / 2 - size.width / 2.0f) + location.first - 1) * size.width, 0.0f, (-(MAX_ELEMENT_NUM_IN_LINE / 2 - size.height / 2.0f) + location.second - 1) * size.height)); _elementContainer->addChild(sprite); _elements[location.first * MAX_CAPACITY_NUM_IN_LINE + location.second] = sprite; emptySpace.erase(emptySpace.begin() + index); } } //auto sprite = Sprite3D::create("D:/Develop/cocos2d-x/tests/cpp-tests/Resources/Sprite3DTest/orc.c3b"); //sprite->setPosition3D(Vec3(0.0f, 1.0f, 0.0f)); //sprite->setScale(0.1f); //_elementContainer->addChild(sprite); _elementContainer->setCameraMask((unsigned short)CameraFlag::USER1); }
void PowerBar::initPowerThings() { { auto node = PixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(1.2,1); node->setPosition3D({0,0,0}); node->configSopx("hunters/sopx/strenth_rect.png.sopx"); node->configBlend(true); _hubNode->addChild(node); _pxRect = node; } { auto node = PixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(1.2,1); node->setPosition3D({0,0,0}); node->configSopx("hunters/sopx/strenth_content.png.sopx"); node->configBlend(true); _hubNode->addChild(node); _pxContent = node; } { auto node = PixelTextNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(1.1f,2.f); node->setPosition3D({-12,8,0}); node->configText("00#",2.0); node->configMixColor({0.f/255.f, 183.f/255.f, 233.f/255.f,1.f}); _hubNode->addChild(node); _ptAngle = node; } { auto node = PixelNode::create(); node->setCameraMask(_mainCamera->getCameraMask()); node->setScale(0.15); node->setPosition3D({0,0,-10}); node->configSopx("hunters/sopx/power_arrow.png.sopx"); node->configBlend(true); _mainCamera->addChild(node); _pxArrow = node; node->setVisible(false); } }
void Physics3DTestDemo::shootBox( const cocos2d::Vec3 &des ) { Physics3DRigidBodyDes rbDes; Vec3 linearVel = des - _camera->getPosition3D(); linearVel.normalize(); linearVel *= 100.0f; rbDes.originalTransform.translate(_camera->getPosition3D()); rbDes.mass = 1.f; rbDes.shape = Physics3DShape::createBox(Vec3(0.5f, 0.5f, 0.5f)); auto sprite = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes); sprite->setTexture("Images/Icon.png"); auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj()); rigidBody->setLinearFactor(Vec3::ONE); rigidBody->setLinearVelocity(linearVel); rigidBody->setAngularVelocity(Vec3::ZERO); rigidBody->setCcdMotionThreshold(0.5f); rigidBody->setCcdSweptSphereRadius(0.4f); this->addChild(sprite); sprite->setPosition3D(_camera->getPosition3D()); sprite->setScale(0.5f); sprite->syncNodeToPhysics(); //optimize, only sync node to physics sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); //sync node to physics sprite->setCameraMask((unsigned short)CameraFlag::USER1); }
void BillBoardTest::addNewAniBillBoradWithCoords(Vec3 p) { for (unsigned int i = 0; i < 10; ++i) { auto billboardAni = BillBoard::create("Images/grossini.png"); billboardAni->setScale(0.5f); billboardAni->setPosition3D(Vec3(p.x, p.y, -150.0f + 30 * i)); _layerBillBorad->addChild(billboardAni); auto animation = Animation::create(); for( int i=1; i<15; i++) { char szName1[100] = {0}; sprintf(szName1, "Images/grossini_dance_%02d.png", i); animation->addSpriteFrameWithFile(szName1); } // should last 2.8 seconds. And there are 14 frames. animation->setDelayPerUnit(2.8f / 14.0f); animation->setRestoreOriginalFrame(true); auto action = Animate::create(animation); billboardAni->runAction(RepeatForever::create(action)); billboardAni->setOpacity(CCRANDOM_0_1() * 128 + 128); _billboards.push_back(billboardAni); } }
void CameraCullingDemo::delSpriteCallback(Ref* sender) { if (_row == 0) return; _layer3D->removeAllChildren(); _objects.clear(); --_row; for (int x = -_row; x < _row; x++) { for (int z = -_row; z < _row; z++) { auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b"); sprite->setPosition3D(Vec3(x * 30, 0, z * 30)); _objects.push_back(sprite); _layer3D->addChild(sprite); } } // set layer mask. _layer3D->setCameraMask((unsigned short) CameraFlag::USER1); // update sprite number char szText[16]; sprintf(szText,"%ld sprits",_layer3D->getChildrenCount()); _labelSprite3DCount->setString(szText); }
Camera* HMDScene::setupCamera(float ratio, bool left) { auto s = Director::getInstance()->getWinSize(); auto sizeInpixels = Director::getInstance()->getWinSizeInPixels(); auto fboSize = Size(sizeInpixels.width * 1.f, sizeInpixels.height * 1.f); auto fbo = experimental::FrameBuffer::create(1, fboSize.width, fboSize.height); 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->setPosition((left ? s.width * 0.25f : s.width * 0.75f) , s.height * 0.5f); sprite->setRotation3D(Vec3(0.0f, 180.0f, 180.0f)); sprite->setScale(0.5f, 1.0f); Scene::addChild(sprite); auto cam = Camera::createPerspective(60, ratio, 0.01f, 100.0f); cam->setPosition3D(Vec3(0.0f, 0.0f, 0.0f)); cam->setCameraFlag(left ? CameraFlag::USER7 : CameraFlag::USER8); cam->setDepth(-1); cam->setName( (left ? "HMD-Cam-L" : "HMD-Cam-R") ); cam->setFrameBufferObject(fbo); // useful for debugging viewport stuff //fbo->setClearColor(Color4F( (left ? 0.25f : 0.0f) , (left ? 0.0f : 0.25f), 0, 1)); return cam; }
//Throwing Object void HelloWorld::shootBoxfunc(const cocos2d::Vec3 &des) { //Vec3 linearVel = des -camera->getPosition3D(); //linearVel.normalize(); //linearVel *= 100.0f; Physics3DRigidBodyDes rbDes; rbDes.originalTransform.translate(car_cabine->getPosition3D()); rbDes.mass = 1.f; rbDes.shape = Physics3DShape::createSphere(0.25); auto sprite = PhysicsSprite3D::create("box.c3t", &rbDes); sprite->setTexture("Gun.png"); auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj()); rigidBody->setLinearFactor(Vec3::ONE); rigidBody->setLinearVelocity(des*15); rigidBody->setAngularVelocity(Vec3::ZERO); rigidBody->setCcdMotionThreshold(0.5f); rigidBody->setCcdSweptSphereRadius(0.4f); sprite->setPosition3D(Vec3(car_cabine->getPosition3D().x, car_cabine->getPosition3D().y+4, car_cabine->getPosition3D().z)); sprite->setScale(0.5f); sprite->syncNodeToPhysics(); sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); sprite->setCameraMask((unsigned short)CameraFlag::USER2); this->addChild(sprite,1); }
void MapSceneController::linkRoads(void) { //目前角色移动只有前后方向操作,所以暂时只需要根据z坐标进行实际逻辑实现 auto pPlayer = _pPlayer; auto index = _vpRoadSprites.size(); for (auto it = _vpRoadSprites.begin(); it < _vpRoadSprites.end(); ++it) { auto pos = (*it)->getAABB().getCenter(); pos.set(pos.x, pos.y, pPlayer->getCurSprite()->getPositionZ()); if ((*it)->getAABB().containPoint(pos)) { index = _vpRoadSprites.getIndex((*it)); break; } } if (index >= _vpRoadSprites.size()/2) { auto size = _vpRoadSprites.size(); for (decltype(index) i = 0; i < index; ++i) { auto pSprite = _vpRoadSprites.at(0); pSprite->retain(); pSprite->setPosition3D(pSprite->getPosition3D() + Vec3(0, 0, -(ROAD_SPRITE_MODEL_WIDTH * static_cast<float>(size)))); _vpRoadSprites.eraseObject(pSprite); _vpRoadSprites.pushBack(pSprite); pSprite->release(); } } }
bool HelpMenuFrame::init(MenuFrameProtocal *controller) { assert(MenuFrame::init(controller)); setPosition3D({0, -650, 0}); float x_diff = 0; float width = 250; float height = 40; float y_now = 210; float y_step = 55; _btnRate = MenuButton::create("help_rate", width, height, [](){ CocosDenshion::SimpleAudioEngine::getInstance()->iosGotoRate(); }, _controller); _btnRate->setPosition3D({x_diff, y_now, 0}); this->addChild(_btnRate); y_now -= y_step; _btnCredits = MenuButton::create("help_credits", width, height, [this](){_controller->move2frame(MENU_CREDIT);}, _controller); _btnCredits->setPosition3D({x_diff, y_now, 0}); this->addChild(_btnCredits); y_now -= y_step; _buttons.push_back(_btnRate); _buttons.push_back(_btnCredits); _lbHelpInfo = Label::createWithTTF(TRLocale::s()["help_info"], uic::font_en, 36); _lbHelpInfo->setScale(0.5); this->addChild(_lbHelpInfo); _lbHelpInfo->setPosition3D({0,0,0}); return true; }
Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() { //the offset use to translating texture _cylinder_texture_offset = 0; _shining_duraion = 0; Size visibleSize = Director::getInstance()->getVisibleSize(); //use custom camera auto camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200); camera->setCameraFlag(CameraFlag::USER1); //create cylinder auto cylinder = Sprite3D::create("Sprite3DTest/cylinder.c3b"); //create and set our custom shader auto shader =GLProgram::createWithFilenames("Sprite3DTest/cylinder.vert","Sprite3DTest/cylinder.frag"); _state = GLProgramState::create(shader); cylinder->setGLProgramState(_state); _state->setUniformFloat("offset",_cylinder_texture_offset); _state->setUniformFloat("duration",_shining_duraion); //pass mesh's attribute to shader long offset = 0; auto attributeCount = cylinder->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, cylinder->getMesh()->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } //create the second texture for cylinder auto shining_texture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/caustics.png"); Texture2D::TexParams tRepeatParams;//set texture parameters tRepeatParams.magFilter = GL_NEAREST; tRepeatParams.minFilter = GL_NEAREST; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; shining_texture->setTexParameters(tRepeatParams); //pass the texture sampler to our custom shader _state->setUniformTexture("caustics",shining_texture); this->addChild(cylinder); this->setCameraMask(2); this->addChild(camera); //adjust cylinder's position & rotation cylinder->setPosition3D(Vec3(0,-15,-50)); cylinder->setRotation3D(Vec3(-90,0,0)); //the callback function update cylinder's texcoord schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate)); }
void BattleScene::createBackground() {//create the 3d world auto background3D = Sprite3D::create("model/scene/changing.c3b"); background3D->setScale(2.65); background3D->setPosition3D(Vec3(-2300, -1000, 0)); background3D->setRotation3D(Vec3(90, 0, 0)); background3D->setCameraMask(2); addChild(background3D); }
PointLight* PointLight::create(const Vec3 &position, const Color3B &color, float range) { auto light = new (std::nothrow) PointLight(); light->setPosition3D(position); light->setColor(color); light->_range = range; light->autorelease(); return light; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } //the offset use to translating texture this->cylinder_texture_offset = 0; this->shining_duraion = 0; Size visibleSize = Director::getInstance()->getVisibleSize(); //use custom camera auto camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200); camera->setCameraFlag(CameraFlag::USER1); auto cylinder = Sprite3D::create("cylinder.c3b"); auto shader =GLProgram::createWithFilenames("cylinder.vert","cylinder.frag"); state = GLProgramState::create(shader); cylinder->setGLProgramState(state); //set attrib to shader long offset = 0; auto attributeCount = cylinder->getMesh()->getMeshVertexAttribCount(); for (auto k = 0; k < attributeCount; k++) { auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(k); state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, cylinder->getMesh()->getVertexSizeInBytes(), (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } auto shining_texture = Director::getInstance()->getTextureCache()->addImage("caustics.png"); Texture2D::TexParams tRepeatParams; tRepeatParams.magFilter = GL_LINEAR_MIPMAP_LINEAR; tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; shining_texture->setTexParameters(tRepeatParams); state->setUniformTexture("caustics",shining_texture); cylinder->setPosition3D(Vec3(0,-15,-50)); cylinder->setRotation3D(Vec3(-90,0,0)); this->addChild(cylinder); this->setCameraMask(2); this->addChild(camera); schedule(schedule_selector(HelloWorld::cylinderUpdate)); return true; }
void HelloWorld::update(float delta) { if (count % 240 == 0) { /** エフェクトファイルを読み込みます。 You read an effect file. 您讀取效果文件。 您读取效果文件。 */ auto effect = efk::Effect::create("Laser01.efk"); if (effect != nullptr) { /** エミッターを生成し、パラメーターを設定してレイヤーに追加します。 You generate an emitter, set parameters and add it to the layer. 您會生成一個發射極,並通過將參數添加到該層。 您会生成一个发射极,并通过将参数添加到该层。 */ auto emitter = efk::EffectEmitter::create(manager); emitter->setEffect(effect); emitter->setPlayOnEnter(true); emitter->setPosition3D(Vec3(0, 0, 0)); emitter->setRotation3D(Vec3(0, 90, 0)); emitter->setScale(3); emitter->setCameraMask(2); this->addChild(emitter); // No need (because it uses autorelease after 1.41) //effect->release(); } } /** 毎フレーム、マネージャーを更新します。 You update the manager every frame. 您將更新每一幀,經理。 您将更新每一帧,经理。 */ manager->update(); manager->setCameraMatrix(camera->getViewMatrix()); manager->setProjectionMatrix(camera->getProjectionMatrix()); count++; }
Sprite3D* Sprite3D::createSprite3DNode(NodeData* nodedata,ModelData* modeldata,const MaterialDatas& materialdatas) { auto sprite = new (std::nothrow) Sprite3D(); if (sprite) { sprite->setName(nodedata->id); auto mesh = Mesh::create(nodedata->id, getMeshIndexData(modeldata->subMeshId)); if (modeldata->matrialId == "" && materialdatas.materials.size()) { const NTextureData* textureData = materialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse); if (!textureData->filename.empty()) mesh->setTexture(textureData->filename); } else { const NMaterialData* materialData=materialdatas.getMaterialData(modeldata->matrialId); if(materialData) { const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse); if(textureData && !textureData->filename.empty()) { auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename); if(tex) { Texture2D::TexParams texParams; texParams.minFilter = GL_LINEAR; texParams.magFilter = GL_LINEAR; texParams.wrapS = textureData->wrapS; texParams.wrapT = textureData->wrapT; tex->setTexParameters(texParams); mesh->setTexture(tex); mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr); } } } } // set locale transform Vec3 pos; Quaternion qua; Vec3 scale; nodedata->transform.decompose(&scale, &qua, &pos); sprite->setPosition3D(pos); sprite->setRotationQuat(qua); sprite->setScaleX(scale.x); sprite->setScaleY(scale.y); sprite->setScaleZ(scale.z); sprite->addMesh(mesh); sprite->autorelease(); sprite->genGLProgramState(); } return sprite; }
bool Physics3DCollisionCallbackDemo::init() { if (!Physics3DTestDemo::init()) return false; { Physics3DRigidBodyDes rbDes; float scale = 2.0f; std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b"); for (auto& it : trianglesList) { it *= scale; } rbDes.mass = 0.0f; rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3); auto rigidBody = Physics3DRigidBody::create(&rbDes); auto component = Physics3DComponent::create(rigidBody); auto sprite = Sprite3D::create("Sprite3DTest/boss.c3b"); sprite->addComponent(component); sprite->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f)); sprite->setScale(scale); sprite->setCameraMask((unsigned short)CameraFlag::USER1); this->addChild(sprite); //preload // rigidBody->setCollisionCallback([=](const Physics3DCollisionInfo &ci){ if (!ci.collisionPointList.empty()){ if (ci.objA->getMask() != 0){ auto ps = PUParticleSystem3D::create("Particle3D/scripts/mp_hit_04.pu"); ps->setPosition3D(ci.collisionPointList[0].worldPositionOnB); ps->setScale(0.05f); ps->startParticleSystem(); ps->setCameraMask(2); this->addChild(ps); ps->runAction(Sequence::create(DelayTime::create(1.0f), CallFunc::create([=](){ ps->removeFromParent(); }), nullptr)); ci.objA->setMask(0); } } //CCLOG("------------BoxB Collision Info------------"); //CCLOG("Collision Point Num: %d", ci.collisionPointList.size()); //for (auto &iter : ci.collisionPointList){ // CCLOG("Collision Position On A: (%.2f, %.2f, %.2f)", iter.worldPositionOnA.x, iter.worldPositionOnA.y, iter.worldPositionOnA.z); // CCLOG("Collision Position On B: (%.2f, %.2f, %.2f)", iter.worldPositionOnB.x, iter.worldPositionOnB.y, iter.worldPositionOnB.z); // CCLOG("Collision Normal On B: (%.2f, %.2f, %.2f)", iter.worldNormalOnB.x, iter.worldNormalOnB.y, iter.worldNormalOnB.z); //} //CCLOG("------------BoxB Collision Info------------"); }); } physicsScene->setPhysics3DDebugCamera(_camera); return true; }
//------------------------------------------------------------------ // // CSUserCameraTest // //------------------------------------------------------------------ CSUserCameraTest::CSUserCameraTest() { auto node = CSLoader::createNode("UserCamera.csb"); auto sprite3D = Sprite3D::create("dragon/dragon.c3b"); sprite3D->setPosition3D(Vec3(100, 95, 80)); sprite3D->setCameraMask((unsigned int)CameraFlag::USER1); addChild(node); addChild(sprite3D); }
SpotLight* SpotLight::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range) { auto light = new (std::nothrow) SpotLight(); light->setRotationFromDirection(direction); light->setPosition3D(position); light->setColor(color); light->setInnerAngle(innerAngle); light->setOuterAngle(outerAngle); light->_range = range; light->autorelease(); return light; }
bool BasicPhysics3DDemo::init() { if (!Physics3DTestDemo::init()) return false; //create floor Physics3DRigidBodyDes rbDes; rbDes.mass = 0.0f; rbDes.shape = Physics3DShape::createBox(Vec3(60.0f, 1.0f, 60.0f)); auto floor = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes); floor->setTexture("Sprite3DTest/plane.png"); floor->setScaleX(60); floor->setScaleZ(60); this->addChild(floor); floor->setCameraMask((unsigned short)CameraFlag::USER1); floor->syncNodeToPhysics(); //static object sync is not needed floor->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE); //create several boxes using PhysicsSprite3D rbDes.mass = 1.f; rbDes.shape = Physics3DShape::createBox(Vec3(0.8f, 0.8f, 0.8f)); float start_x = START_POS_X - ARRAY_SIZE_X/2; float start_y = START_POS_Y; float start_z = START_POS_Z - ARRAY_SIZE_Z/2; for (int k=0;k<ARRAY_SIZE_Y;k++) { for (int i=0;i<ARRAY_SIZE_X;i++) { for(int j = 0;j<ARRAY_SIZE_Z;j++) { float x = 1.0*i + start_x; float y = 5.0+1.0*k + start_y; float z = 1.0*j + start_z; auto sprite = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes); sprite->setTexture("Images/CyanSquare.png"); sprite->setPosition3D(Vec3(x, y, z)); sprite->syncNodeToPhysics(); sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); sprite->setCameraMask((unsigned short)CameraFlag::USER1); sprite->setScale(0.8f); this->addChild(sprite); } } } physicsScene->setPhysics3DDebugCamera(_camera); return true; }
void MapSceneController::preGenerate(void){ //为什么是5,没想好 这里刚好遇到一个问题,double转化为float导致截断问题 //todo for (int i = 0; i < 5; ++i) { auto pRoadSprite = Sprite3D::create("model/scene.c3b"); pRoadSprite->setScale(0.2f); pRoadSprite->setRotation3D(Vec3(0, 90, 0)); pRoadSprite->setPosition3D(Vec3(0, -5, -(ROAD_SPRITE_MODEL_WIDTH * i))); _pGameLayer->addChild(pRoadSprite, -100); _vpRoadSprites.pushBack(pRoadSprite); } };
bool GlassDemoScene::init() { assert(TRBaseScene::init()); _layer = Layer::create(); this->addChild(_layer); auto size = Director::getInstance()->getVisibleSize(); auto _formCamera = Camera::createPerspective(60, size.width/size.height, 0.01, 1000); _formCamera->setPosition3D({0,0,0}); _formCamera->lookAt(Vec3{0,0,-20},Vec3{0,1,0}); _formCamera->setCameraFlag(CameraFlag::USER1); _layer->addChild(_formCamera); _formCamera->setCameraMask((unsigned short)CameraFlag::USER1); _camera = _formCamera; _glass = DynamicGlass::create(1.2,1.2,10,10); _glass->setCameraMask(_camera->getCameraMask()); _layer->addChild(_glass); _glass->setPosition3D({0,0,0}); this->addCommonBtn({0.1f,0.95f}, "back", [](){ Director::getInstance()->popScene(); }); this->addCommonBtn({0.1f,0.9f}, "hit", [this](){ _glass->hit(1.0); }); this->addCommonBtn({0.1f,0.85f}, "reset", [this](){ _glass->reset(); }); ///acce _acceEffect = AcceEffectSpace::create(); _acceEffect->setCameraMask(_camera->getCameraMask()); _acceEffect->setPosition3D({0,0,0}); _layer->addChild(_acceEffect); this->addCommonBtn({0.9f, 0.8f}, "per +", [this](){_percent += 0.1;}); this->addCommonBtn({0.9f, 0.75f}, "per -", [this](){_percent -= 0.1;}); scheduleUpdate(); return true; }
void BillBoardTest::addNewBillBoradWithCoords(Vec3 p) { std::string imgs[3] = {"Images/Icon.png", "Images/r2.png"}; for (unsigned int i = 0; i < 10; ++i) { auto billborad = BillBoard::create(imgs[(unsigned int)(CCRANDOM_0_1() * 1 + 0.5)]); billborad->setScale(0.5f); billborad->setPosition3D(Vec3(p.x, p.y, -150.0f + 30 * i)); billborad->setOpacity(CCRANDOM_0_1() * 128 + 128); _layerBillBorad->addChild(billborad); _billboards.push_back(billborad); } }
// on "init" you need to initialize your instance bool Lession25::init() { if ( !Layer::init() ) { return false; } auto winSize = Director::getInstance()->getWinSize(); FileUtils::getInstance()->addSearchPath(FileUtils::getInstance()->getSearchPaths().at(0) + "Lession25/"); ////µÃµ½ÆÁÄ»³ß´ç auto s = Director::getInstance()->getWinSize(); //´´½¨3DÉãÏñ»ú auto _camera = cocos2d::Camera::createPerspective(60, (GLfloat)s.width / s.height, 1, 1000); //ÉèÖÃÉãÏñ»úÑÛ¾¦Î»Öà _camera->setPosition3D(Vec3(0, 200, 200)); //ÉèÖÃÉãÏñ»ú¹Û²ìµãλÖà _camera->lookAt(Vec3(0, 0, 0)); _camera->setCameraFlag(CameraFlag::USER1); addChild(_camera); //´´½¨¾²Ì¬Ä£ÐÍ auto spriteStatic3D = Sprite3D::create("water.c3t"); _sprite3D = spriteStatic3D; spriteStatic3D->setCameraMask(2); spriteStatic3D->setScale(2); spriteStatic3D->setPosition3D(Vec3(0, 0, 0)); spriteStatic3D->setRotation3D(Vec3(-90, 0, 0)); addChild(spriteStatic3D); GLProgramState* state = GLProgramState::create(GLProgram::createWithFilenames("Lession25.vert", "Lession25.frag")); state->applyUniforms(); spriteStatic3D->setGLProgramState(state); return true; }
void City_layer::create_human1(Vec3 pos,float scale) { auto sprite = Charecter1::create(); sprite->setScale(scale); this->addChild(sprite); sprite->setPosition3D(pos); sprite->setCameraMask(2); vecObj.pushBack(sprite); // // Vec3 corners[8]={}; // AABB aabbsp1=sprite->getAABB(); // aabbsp1.getCorners(corners); // _aabb.push_back(aabbsp1); // node1->drawCube(corners, Color4F(0,0,1,1)); }