bool EC_Mesh::SetMesh(QString meshResourceName, bool clone) { if (!ViewEnabled()) return false; OgreWorldPtr world = world_.lock(); std::string mesh_name = meshResourceName.trimmed().toStdString(); RemoveMesh(); // If placeable is not set yet, set it manually by searching it from the parent entity if (!placeable_) { Entity* entity = ParentEntity(); if (entity) { ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic()); if (placeable) placeable_ = placeable; } } Ogre::SceneManager* sceneMgr = world->OgreSceneManager(); Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone); if (!mesh) return false; try { entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_Mesh_entity"), mesh->getName()); if (!entity_) { LogError("EC_Mesh::SetMesh: Could not set mesh " + mesh_name); return false; } entity_->setRenderingDistance(drawDistance.Get()); entity_->setCastShadows(castShadows.Get()); entity_->setUserAny(Ogre::Any(static_cast<IComponent *>(this))); // Set UserAny also on subentities for(uint i = 0; i < entity_->getNumSubEntities(); ++i) entity_->getSubEntity(i)->setUserAny(entity_->getUserAny()); if (entity_->hasSkeleton()) { Ogre::SkeletonInstance* skel = entity_->getSkeleton(); // Enable cumulative mode on skeletal animations if (skel) skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE); } // Make sure adjustment node is uptodate Transform newTransform = nodeTransformation.Get(); adjustment_node_->setPosition(newTransform.pos); adjustment_node_->setOrientation(newTransform.Orientation()); // Prevent Ogre exception from zero scale adjustment_node_->setScale(Max(newTransform.scale, float3::FromScalar(0.0000001f))); // Force a re-apply of all materials to this new mesh. ApplyMaterial(); } catch(Ogre::Exception& e) { LogError("EC_Mesh::SetMesh: Could not set mesh " + mesh_name + ": " + std::string(e.what())); return false; } AttachEntity(); emit MeshChanged(); return true; }
bool EC_Mesh::SetMeshWithSkeleton(const std::string& mesh_name, const std::string& skeleton_name, bool clone) { if (!ViewEnabled()) return false; OgreWorldPtr world = world_.lock(); Ogre::SkeletonPtr skel = Ogre::SkeletonManager::getSingleton().getByName(AssetAPI::SanitateAssetRef(skeleton_name)); if (skel.isNull()) { LogError("EC_Mesh::SetMeshWithSkeleton: Could not set skeleton " + skeleton_name + " to mesh " + mesh_name + ": not found"); return false; } RemoveMesh(); Ogre::SceneManager* sceneMgr = world->OgreSceneManager(); Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone); if (!mesh) return false; try { mesh->_notifySkeleton(skel); // LogDebug("Set skeleton " + skeleton_name + " to mesh " + mesh_name); } catch(Ogre::Exception& e) { LogError("EC_Mesh::SetMeshWithSkeleton: Could not set skeleton " + skeleton_name + " to mesh " + mesh_name + ": " + std::string(e.what())); return false; } try { entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_Mesh_entwithskel"), mesh->getName()); if (!entity_) { LogError("EC_Mesh::SetMeshWithSkeleton: Could not set mesh " + mesh_name); return false; } entity_->setRenderingDistance(drawDistance.Get()); entity_->setCastShadows(castShadows.Get()); entity_->setUserAny(Ogre::Any(static_cast<IComponent *>(this))); // Set UserAny also on subentities for(uint i = 0; i < entity_->getNumSubEntities(); ++i) entity_->getSubEntity(i)->setUserAny(entity_->getUserAny()); if (entity_->hasSkeleton()) { Ogre::SkeletonInstance* skel = entity_->getSkeleton(); // Enable cumulative mode on skeletal animations if (skel) skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE); } } catch(Ogre::Exception& e) { LogError("EC_Mesh::SetMeshWithSkeleton: Could not set mesh " + mesh_name + ": " + std::string(e.what())); return false; } AttachEntity(); emit MeshChanged(); return true; }
bool EC_Mesh::SetMesh(QString meshResourceName, bool clone) { if (!ViewEnabled()) return false; if (renderer_.expired()) return false; RendererPtr renderer = renderer_.lock(); std::string mesh_name = meshResourceName.trimmed().toStdString(); RemoveMesh(); // If placeable is not set yet, set it manually by searching it from the parent entity if (!placeable_) { Scene::Entity* entity = GetParentEntity(); if (entity) { ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic()); if (placeable) placeable_ = placeable; } } Ogre::SceneManager* scene_mgr = renderer->GetSceneManager(); Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone); if (!mesh) return false; try { entity_ = scene_mgr->createEntity(renderer->GetUniqueObjectName("EC_Mesh_entity"), mesh->getName()); if (!entity_) { LogError("Could not set mesh " + mesh_name); return false; } entity_->setRenderingDistance(drawDistance.Get()); entity_->setCastShadows(castShadows.Get()); entity_->setUserAny(Ogre::Any(GetParentEntity())); // Set UserAny also on subentities for (uint i = 0; i < entity_->getNumSubEntities(); ++i) entity_->getSubEntity(i)->setUserAny(entity_->getUserAny()); if (entity_->hasSkeleton()) { Ogre::SkeletonInstance* skel = entity_->getSkeleton(); // Enable cumulative mode on skeletal animations if (skel) skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE); } // Make sure adjustment node is uptodate if (adjustment_node_) { Transform newTransform = nodeTransformation.Get(); adjustment_node_->setPosition(newTransform.position.x, newTransform.position.y, newTransform.position.z); Quaternion adjust(DEGTORAD * newTransform.rotation.x, DEGTORAD * newTransform.rotation.y, DEGTORAD * newTransform.rotation.z); adjustment_node_->setOrientation(Ogre::Quaternion(adjust.w, adjust.x, adjust.y, adjust.z)); // Prevent Ogre exception from zero scale if (newTransform.scale.x < 0.0000001f) newTransform.scale.x = 0.0000001f; if (newTransform.scale.y < 0.0000001f) newTransform.scale.y = 0.0000001f; if (newTransform.scale.z < 0.0000001f) newTransform.scale.z = 0.0000001f; adjustment_node_->setScale(newTransform.scale.x, newTransform.scale.y, newTransform.scale.z); } // Force a re-apply of all materials to this new mesh. ApplyMaterial(); } catch (Ogre::Exception& e) { LogError("Could not set mesh " + mesh_name + ": " + std::string(e.what())); return false; } AttachEntity(); emit MeshChanged(); return true; }