void Sprite3D::createAsync(const std::string &modelPath, const std::string &texturePath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam) { Sprite3D *sprite = new (std::nothrow) Sprite3D(); if (sprite->loadFromCache(modelPath)) { sprite->autorelease(); if (!texturePath.empty()) sprite->setTexture(texturePath); callback(sprite, callbackparam); return; } sprite->_asyncLoadParam.afterLoadCallback = callback; sprite->_asyncLoadParam.texPath = texturePath; sprite->_asyncLoadParam.modlePath = modelPath; sprite->_asyncLoadParam.callbackParam = callbackparam; sprite->_asyncLoadParam.materialdatas = new (std::nothrow) MaterialDatas(); sprite->_asyncLoadParam.meshdatas = new (std::nothrow) MeshDatas(); sprite->_asyncLoadParam.nodeDatas = new (std::nothrow) NodeDatas(); AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, CC_CALLBACK_1(Sprite3D::afterAsyncLoad, sprite), (void*)(&sprite->_asyncLoadParam), [sprite]() { sprite->_asyncLoadParam.result = sprite->loadFromFile(sprite->_asyncLoadParam.modlePath, sprite->_asyncLoadParam.nodeDatas, sprite->_asyncLoadParam.meshdatas, sprite->_asyncLoadParam.materialdatas); }); }
void CoinAction::step(float time) { if(_target) { _target->setRotation3D(Vec3(90,angle,180)); angle+=time*140; _target->setPosition3D (_target->getPosition3D()+Vec3(0,0,100*time)); if(_target->getPositionZ()>-40 && _target->getPositionZ()<10 )// enter the front { Sprite3D * sprite = dynamic_cast<Sprite3D * >(_target); OBB obb(sprite->getAABB()); Vec3 vertices[8]; auto player_aabb= player->getPlayer()->getAABB(); auto dist =sprite->getPosition3D().distance(player->getPlayer()->getPosition3D()); if(dist<5 && !is_triggered) { is_triggered =~is_triggered; sprite->setColor(Color3B(255,0,0)); Effect::CoinEffect(render_node); auto a = (GameScene * )this->render_node; a->earnGold(); CCLOG("get Money !!!"); return ; } } if(_target->getPositionZ()>=10) { _target->removeFromParent(); _target=nullptr; } } }
bool LoadingLayer::init() { if(!LayerColor::init()) return false; //Set the background color LoadingLayer::setColor(Color3B::WHITE); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Vec2 centerPoint = Utils::Utils::getCentralPoint(visibleSize, origin); //Load a planet sprite to show while the game loads Sprite3D* sprPlanet = Sprite3D::create("3d/meshes/MarsPlanet.obj", "3d/textures/MarsMap.jpg"); sprPlanet->setPosition(centerPoint); Utils::Utils::setScale3D(10, sprPlanet); sprPlanet->runAction(RepeatForever::create(RotateBy::create(3,Vec3(0,360,0)))); //Load the sprite with "LOADING..." string while the game loads Text* loadingText = Text::create("LOADING...", DEFAULT_FONT, 34); loadingText->setPosition(Vec2(sprPlanet->getPositionX(), SCREEN_FRAME_OFFSET)); //Add elements as layer children addChild(sprPlanet); addChild(loadingText); return true; }
//! called before the action start. It will also set the target. void Animate3D::startWithTarget(Node *target) { Sprite3D* sprite = dynamic_cast<Sprite3D*>(target); CCASSERT(sprite && sprite->getSkeleton() && _animation, "Animate3D apply to Sprite3D only"); ActionInterval::startWithTarget(target); _boneCurves.clear(); auto skin = sprite->getSkeleton(); bool hasCurve = false; for (int i = 0; i < skin->getBoneCount(); i++) { auto bone = skin->getBoneByIndex(static_cast<unsigned int>(i)); auto curve = _animation->getBoneCurveByName(bone->getName()); if (curve) { _boneCurves[bone] = curve; hasCurve = true; } } if (!hasCurve) { CCLOG("warning: no animation finde for the skeleton"); } auto runningAction = s_runningAnimates.find(sprite); if (runningAction != s_runningAnimates.end()) { //make the running action fade out auto action = (*runningAction).second; if (action != this) { if (_transTime < 0.001f) { s_runningAnimates[sprite] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } else { s_fadeOutAnimates[sprite] = action; action->_state = Animate3D::Animate3DState::FadeOut; action->_accTransTime = 0.0f; action->_weight = 1.0f; action->_lastTime = 0.f; s_fadeInAnimates[sprite] = this; _accTransTime = 0.0f; _state = Animate3D::Animate3DState::FadeIn; _weight = 0.f; _lastTime = 0.f; } } } else { s_runningAnimates[sprite] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } }
void PUParticle3DModelRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem ) { if (!_isVisible) return; if (_spriteList.empty()) { for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i) { Sprite3D *sprite = Sprite3D::create(_modelFile); if (sprite == nullptr) { CCLOG("failed to load file %s", _modelFile.c_str()); continue; } sprite->setTexture(_texFile); sprite->setBlendFunc(particleSystem->getBlendFunc()); sprite->setCullFaceEnabled(false); sprite->retain(); _spriteList.push_back(sprite); } if (!_spriteList.empty()) { const AABB &aabb = _spriteList[0]->getAABB(); Vec3 corners[8]; aabb.getCorners(corners); _spriteSize = corners[3] - corners[6]; } else { _isVisible = false; return; } } const ParticlePool& particlePool = particleSystem->getParticlePool(); ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList(); Mat4 mat; Mat4 rotMat; Mat4 sclMat; Quaternion q; transform.decompose(nullptr, &q, nullptr); unsigned int index = 0; for (auto iter : activeParticleList) { auto particle = static_cast<PUParticle3D *>(iter); Mat4::createRotation(q * particle->orientation, &rotMat); sclMat.m[0] = particle->width / _spriteSize.x; sclMat.m[5] = particle->height / _spriteSize.y; sclMat.m[10] = particle->depth / _spriteSize.z; mat = rotMat * sclMat; mat.m[12] = particle->position.x; mat.m[13] = particle->position.y; mat.m[14] = particle->position.z; if (_spriteList[index]->getCameraMask() != particleSystem->getCameraMask()) _spriteList[index]->setCameraMask(particleSystem->getCameraMask()); _spriteList[index]->setColor(Color3B(particle->color.x * 255, particle->color.y * 255, particle->color.z * 255)); _spriteList[index]->setOpacity(particle->color.w * 255); _spriteList[index]->visit(renderer, mat, Node::FLAGS_DIRTY_MASK); ++index; } }
Sprite3D *MySprite3DFactory::create(cocos2d::MySpriteType type) { Sprite3D *sp = Sprite3D::create(m_ModelFilesOfType[type]); if(sp) { sp->setAnchorPoint(Vec2(0, 1)); sp->runAction(getAction(type)); } return sp; }
void MainScene::update( float delta ) { Size visibleSize = Director::getInstance()->getVisibleSize(); /* if( _inputLayer->isKeyboradPushDown( KEY_CODE::KEY_ENTER ) == true ) { log( "enter push" ); } if( _inputLayer->isKeyboradPushDown( KEY_CODE::KEY_ESCAPE ) == true ) { log( "escape push" ); } */ Sprite3D* sprite = FileLoadManager::getInstance()->getSprite3D( _modelArray.size() ); if( sprite != nullptr ) { sprite->setPosition( visibleSize.width * 0.5f, visibleSize.height * 0.5f ); sprite->setScale( 300.0f ); addChild( sprite ); _modelArray.pushBack( sprite ); } if( _selectModel != nullptr ) { _selectModel->setScale( _selectModel->getScale() + _inputLayer->getMouseScroll() * 2.0f ); _selectModel->setPosition( CCPoint( _inputLayer->getMousePosition().x, _inputLayer->getMousePosition().y - (_selectModel->getContentSize().height * _selectModel->getScale()) * 0.5f )); } if( _inputLayer->isMousePush( InputLayer::MouseFlags::LEFT_PUSH_FlAG ) == true && _selectModel == nullptr) { for( int i = 0; i < (int)_modelArray.size(); i++ ) { auto rect = _modelArray.at( i )->getBoundingBox(); if( rect.containsPoint( _inputLayer->getMousePosition() ) == true ) { log( "Model Push" ); _selectModel = _modelArray.at( i ); } } } else { _selectModel = nullptr; } }
AABB Sprite3D::getAABBRecursively() { AABB aabb; const auto children = getChildren(); for (const auto iter: children) { Sprite3D* child = dynamic_cast<Sprite3D*>(iter); if(child) { aabb.merge(child->getAABBRecursively()); } } aabb.merge(getAABB()); return aabb; }
Node* Sprite3DReader::createNodeWithFlatBuffers(const flatbuffers::Table *sprite3DOptions) { auto options = (Sprite3DOptions*)sprite3DOptions; auto fileData = options->fileData(); std::string path = fileData->path()->c_str(); Sprite3D* ret = Sprite3D::create(); if(FileUtils::getInstance()->isFileExist(path)) { ret->initWithFile(path); } setPropsWithFlatBuffers(ret, sprite3DOptions); return ret; }
void GameRenderer::renderAgent(const Agent * _agent) const { dl::Point3D pos = _agent->getPosition(); //Draw the guy glColor3f(1.0, 1.0, 1.0); glEnable(GL_TEXTURE_2D); m_agentTexture.bind(); glTranslatef(pos.x, pos.y, pos.z); Sprite3D sprite; if((dl::Vector2D)_agent->getVelocity() != dl::Vector2D(0, 0)) sprite.setStepping(m_stepping); if(_agent->getDirection().i > 0) if(_agent->getDirection().j > 0) sprite.setOrientation(Sprite3D::RIGHT_BACK); else sprite.setOrientation(Sprite3D::RIGHT_FRONT); else if(_agent->getDirection().j > 0) sprite.setOrientation(Sprite3D::LEFT_BACK); else sprite.setOrientation(Sprite3D::LEFT_FRONT); sprite.render(); glTranslatef(-pos.x, -pos.y, -pos.z); glDisable(GL_TEXTURE_2D); }
void WTest3D::showSprite3D() { //the assets are from the OpenVR demo //get the visible size. Size size = Director::getInstance()->getVisibleSize(); float zeye = Director::getInstance()->getZEye(); 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); _camera = Camera::createPerspective(60, size.width / size.height, 100, 3000);// zeye + size.height / 2.0f); _camera->setCameraFlag(CameraFlag::USER1); //_camera->setPosition3D(Vec3(0, 25, 15)); _camera->setPosition3D(eye + Vec3(0, 60, 10)); //_camera->lookAt(center, up); //_camera->setRotation3D(Vec3(-35, 0, 0)); //auto LightMapScene = Sprite3D::create("Sprite3DTest/boss1.obj"); //LightMapScene->setTexture("Sprite3DTest/boss.png"); //auto LightMapScene = Sprite3D::create("Sprite3DTest/aaaa.c3b"); addChild(_camera); setCameraMask(2); std::string fileName = "Sprite3DTest/orc.c3b"; //std::string fileName = "Sprite3DTest/cccc.c3b"; for (int i = 0; i < 14; ++i) { auto LightMapScene = Sprite3D::create(fileName); LightMapScene->setScale(8); addChild(LightMapScene, 100 + i, 100 + i); LightMapScene->setRotation3D(Vec3(0, 180, 0)); auto sc = LightMapScene->getScale(); auto sz = LightMapScene->getContentSize(); auto curY = _terrain->getHeight(Vec2(250 + i*(sc*sz.width - 150), 10)); LightMapScene->setPosition3D(Vec3(250 + i*(sc*sz.width - 150), curY, 10)); } for (int i = 0; i < 14; ++i) { auto LightMapScene = Sprite3D::create(fileName); LightMapScene->setScale(8); addChild(LightMapScene, 200 + i, 200 + i); LightMapScene->setRotation3D(Vec3(0, 0, 0)); auto sc = LightMapScene->getScale(); auto sz = LightMapScene->getContentSize(); auto curY = _terrain->getHeight(Vec2(250 + i*(sc*sz.width - 150), -1500)); LightMapScene->setPosition3D(Vec3(250 + i*(sc*sz.width - 150), curY, -1500)); } for (int i = 0; i < 14; ++i) { auto LightMapScene = Sprite3D::create(fileName); LightMapScene->setScale(8); addChild(LightMapScene, 300 + i, 300 + i); LightMapScene->setRotation3D(Vec3(0, 90, 0)); auto sc = LightMapScene->getScale(); auto sz = LightMapScene->getContentSize(); auto curY = _terrain->getHeight(Vec2(20, -400 - i * 80)); LightMapScene->setPosition3D(Vec3(20, curY, -400 - i * 80)); } for (int i = 0; i < 14; ++i) { auto LightMapScene = Sprite3D::create(fileName); LightMapScene->setScale(8); addChild(LightMapScene, 400 + i, 400 + i); LightMapScene->setRotation3D(Vec3(0, 270, 0)); auto sc = LightMapScene->getScale(); auto sz = LightMapScene->getContentSize(); auto curY = _terrain->getHeight(Vec2(1200, -400 - i * 80)); LightMapScene->setPosition3D(Vec3(1200, curY, -400 - i * 80)); } //for (int i = 0; i < 10; ++i) //{ // auto LightMapScene = Sprite3D::create(fileName); // LightMapScene->setScale(8); // addChild(LightMapScene, i, i); // LightMapScene->setRotation3D(Vec3(90, 180, 0)); // auto sc = LightMapScene->getScale(); // auto sz = LightMapScene->getContentSize(); // CCLOG("getContentSize().width = %f, getContentSize().height = %f", // getContentSize().width, getContentSize().height); // LightMapScene->setPosition3D(Vec3(200 + i*(sc*sz.width - 150), 100, -300)); //} Sprite3D* temp = (Sprite3D*)getChildByTag(100 + 13); CCLOG("getPositionX() = %f, getPositionY() = %f, getPositionZ = %f", temp->getPositionX(), temp->getPositionY(), temp->getPositionZ()); CCLOG("getScaleX() = %f, getScaleX() = %f, getScaleZ = %f", temp->getScaleX(), temp->getScaleY(), temp->getScaleZ()); CCLOG("getContentSize().width = %f, getContentSize().height = %f", temp->getContentSize().width, temp->getContentSize().height); CCLOG("getAnchorPoint().x = %f, getAnchorPoint().y = %f", temp->getAnchorPoint().x, temp->getAnchorPoint().y); //actions temp->runAction(Sequence::create( DelayTime::create(2), CCMoveBy::create(0.5, Vec3(0, 100, 0)), CCMoveTo::create(1, Vec3(500, _terrain->getHeight(500, -500), -500)), nullptr)); temp->runAction(Sequence::create( DelayTime::create(2.5), CCRotateBy::create(1, Vec3(90, 0, 0)), nullptr)); // actions _camera->runAction(Sequence::create( DelayTime::create(5), MoveBy::create(3, Vec3(0, 0, -1300)), CallFunc::create([=](){ for (int i = 0; i < 14; ++i) { Sprite3D* temp = (Sprite3D*)getChildByTag(200 + i); temp->runAction(Sequence::create( RotateTo::create(0.5, Vec3(90, 0, 0)), nullptr)); } }), nullptr)); }
//! called before the action start. It will also set the target. void Animate3D::startWithTarget(Node *target) { bool needReMap = (_target != target); ActionInterval::startWithTarget(target); if (needReMap) { _boneCurves.clear(); _nodeCurves.clear(); bool hasCurve = false; Sprite3D* sprite = dynamic_cast<Sprite3D*>(target); if(sprite) { if (_animation) { const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves(); for (const auto& iter: boneCurves) { const std::string& boneName = iter.first; auto skin = sprite->getSkeleton(); if(skin) { auto bone = skin->getBoneByName(boneName); if (bone) { auto curve = _animation->getBoneCurveByName(boneName); _boneCurves[bone] = curve; hasCurve = true; } else { Node* node = nullptr; if (target->getName() == boneName) node = target; else node = findChildByNameRecursively(target, boneName); if (node) { auto curve = _animation->getBoneCurveByName(boneName); if (curve) { _nodeCurves[node] = curve; hasCurve = true; } } } } } } } else { const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves(); for (const auto& iter: boneCurves) { const std::string& boneName = iter.first; Node* node = nullptr; if (target->getName() == boneName) node = target; else node = findChildByNameRecursively(target, boneName); if (node) { auto curve = _animation->getBoneCurveByName(boneName); if (curve) { _nodeCurves[node] = curve; hasCurve = true; } } } } if (!hasCurve) { CCLOG("warning: no animation found for the skeleton"); } } auto runningAction = s_runningAnimates.find(target); if (runningAction != s_runningAnimates.end()) { //make the running action fade out auto action = (*runningAction).second; if (action != this) { if (_transTime < 0.001f) { s_runningAnimates[target] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } else { s_fadeOutAnimates[target] = action; action->_state = Animate3D::Animate3DState::FadeOut; action->_accTransTime = 0.0f; action->_weight = 1.0f; action->_lastTime = 0.f; s_runningAnimates.erase(target); s_fadeInAnimates[target] = this; _accTransTime = 0.0f; _state = Animate3D::Animate3DState::FadeIn; _weight = 0.f; _lastTime = 0.f; } } } else { auto it = s_fadeInAnimates.find(target); if (it != s_fadeInAnimates.end()) { s_fadeInAnimates.erase(it); } s_runningAnimates[target] = this; _state = Animate3D::Animate3DState::Running; _weight = 1.0f; } }
// on "init" you need to initialize your instance bool Sprite3DLesson5::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(Sprite3DLesson5::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DLesson5::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); ///////////////////////////// std::string filename = "res/Sprite3D/girl.c3b"; auto sprite = Sprite3D::create(filename); sprite->setPosition(visibleSize.width/2 + origin.x, visibleSize.height/3 + origin.y); addChild(sprite); auto animation = Animation3D::create(filename); auto animate = Animate3D::create(animation); sprite->runAction(RepeatForever::create(animate)); auto lfoot = Sprite::create("res/Sprite3D/circle.png"); auto rfoot = Sprite::create("res/Sprite3D/circle.png"); _lfoot = Sprite3D::create(); _lfoot->addChild(lfoot); _lfoot->setRotation3D(Vec3(90, 0, 0)); _rfoot = Sprite3D::create(); _rfoot->addChild(rfoot); _rfoot->setRotation3D(Vec3(90, 0, 0)); addChild(_lfoot); addChild(_rfoot); _lfoot->setScale(0.3f); _rfoot->setScale(0.3f); _lfoot->setVisible(false); _rfoot->setVisible(false); _sprite = sprite; ValueMap valuemap0; valuemap0["footname"] = Value("Bip001 R Toe0"); valuemap0["lfoot"] = Value(false); animate->setKeyFrameUserInfo(10, valuemap0); valuemap0["footname"] = Value("Bip001 L Toe0"); valuemap0["lfoot"] = Value(true); animate->setKeyFrameUserInfo(26, valuemap0); auto listener2 = EventListenerCustom::create(Animate3DDisplayedNotification, [&](EventCustom* event) { auto info = (Animate3D::Animate3DDisplayedEventInfo*)event->getUserData(); auto footname = info->userInfo->at("footname").asString(); bool lfoot = info->userInfo->at("lfoot").asBool(); auto mat = _sprite->getNodeToWorldTransform() * _sprite->getSkeleton()->getBoneByName(footname)->getWorldMat(); Sprite3D* foot = lfoot ? _lfoot : _rfoot; foot->setPosition3D(Vec3(mat.m[12], mat.m[13], mat.m[14])); foot->setVisible(true); cocos2d::log("frame %d", info->frame); }); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener2, -1); scheduleUpdate(); return true; }