diff --git a/src/entities/LabelComponent.cpp b/src/entities/LabelComponent.cpp index 4bb544ed..08fa05d4 100644 --- a/src/entities/LabelComponent.cpp +++ b/src/entities/LabelComponent.cpp @@ -9,6 +9,8 @@ #include "entities/Entity.h" #include "common/SpdLogger.h" +#include "scutcheon/osgScutcheon.h" + LabelComponent::LabelComponent(SceneComponent* parent) : SceneComponent(parent) , text_("Label") @@ -112,6 +114,9 @@ void LabelComponent::SetText(const std::string& text) { if (text_ != text) { text_ = text; needUpdate_ = true; + if (textNode_.valid()) { + textNode_->setText(text_); + } } } @@ -132,34 +137,43 @@ void LabelComponent::SetColor(const osg::Vec4& color) { void LabelComponent::SetVisible(bool visible) { if (visible_ != visible) { visible_ = visible; - if (textGeode_.valid()) { - textGeode_->setNodeMask(visible_ ? 0xffffffff : 0x0); + if (billboard_.valid()) { + billboard_->setNodeMask(visible_ ? 0xffffffff : 0x0); } } } void LabelComponent::CreateTextNode() { - if (!textGeode_.valid()) { - textGeode_ = new osg::Geode(); + if (mt_.valid()) { + return; + } + + // 使用布告板来显示文本,确保文本始终面向相机 + if (!billboard_.valid()) { + billboard_ = new osg::Billboard(); + billboard_->setMode(osg::Billboard::POINT_ROT_EYE); + textNode_ = new osgText::Text(); // 设置文本对齐方式为底部居中,这样文本会显示在指定位置的上方 textNode_->setAlignment(osgText::Text::CENTER_BOTTOM); textNode_->setAxisAlignment(osgText::Text::SCREEN); - textNode_->setCharacterSizeMode(osgText::Text::OBJECT_COORDS); - textNode_->setText(text_); - textNode_->setPosition(osg::Vec3(0.0f, 0.0f, 500.0f)); // 设置初始位置在模型上方 + textNode_->setCharacterSizeMode(osgText::Text::SCREEN_COORDS); + textNode_->setText(text_); + textNode_->setPosition(osg::Vec3(0.0f, 0.0f, 0.0f)); // 文本相对于布告板的位置 - textGeode_->addDrawable(textNode_.get()); + // 直接将文本对象添加到布告板,并设置位置偏移 + billboard_->addDrawable(textNode_.get(), osg::Vec3(0.0f, 0.0f, 5.0f)); - osg::StateSet* stateSet = textGeode_->getOrCreateStateSet(); + // 设置渲染状态 + osg::StateSet* stateSet = billboard_->getOrCreateStateSet(); stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); - stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); + stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); - osg::ref_ptr mt = new osg::MatrixTransform(); - mt->addChild(textGeode_); + osg::ref_ptr mt = new osg::MatrixTransform(); + mt->addChild(billboard_); if (!mt_.valid()) { mt_ = mt; } @@ -174,10 +188,7 @@ void LabelComponent::UpdateTextNode() { textNode_->setCharacterSize(fontSize_); textNode_->setColor(color_); - // 设置标签位置在模型上方,增加偏移量使其更明显 - textNode_->setPosition(osg::Vec3(0.0f, 0.0f, 500.0f)); - - SPDLOG_INFO("LabelComponent: Updated text '{}' with size {}", text_, fontSize_); + SPDLOG_INFO("LabelComponent: Updated billboard text '{}' with size {}", text_, fontSize_); } } diff --git a/src/entities/LabelComponent.h b/src/entities/LabelComponent.h index 26891c75..cc56b3bf 100644 --- a/src/entities/LabelComponent.h +++ b/src/entities/LabelComponent.h @@ -4,7 +4,7 @@ #include -#include +#include #include #include @@ -52,6 +52,7 @@ protected: bool visible_; bool needUpdate_; - osg::ref_ptr textGeode_; + //osg::ref_ptr scutcheon_; + osg::ref_ptr billboard_; osg::ref_ptr textNode_; }; \ No newline at end of file diff --git a/src/translations/Dyt_zh_CN.ts b/src/translations/Dyt_zh_CN.ts index 7284eb54..2c2f586f 100644 --- a/src/translations/Dyt_zh_CN.ts +++ b/src/translations/Dyt_zh_CN.ts @@ -694,12 +694,12 @@ OsgWidget - + warning - + open dyt file failed diff --git a/src/viewer/OsgView.cpp b/src/viewer/OsgView.cpp index 0fbb713b..cc5c95ff 100644 --- a/src/viewer/OsgView.cpp +++ b/src/viewer/OsgView.cpp @@ -18,7 +18,6 @@ #include "viewer/KeyMapQtOsg.h" #include "viewer/OsgCameraManipulator.h" #include "viewer/OsgViewUI.h" -#include "scutcheon/osgScutcheon.h" OsgView::OsgView(QObject* parent) noexcept : QObject(parent) { @@ -62,9 +61,6 @@ void OsgView::Initialize(OsgCameraManipulator* cameraManipulator) { root->addChild(viewUI_.get()); } - osg::Group* root = view_->getSceneData()->asGroup(); - osgScutcheonManager::instance()->Init(view_.get(), root); - view_->addEventHandler(new osgViewer::StatsHandler); view_->addEventHandler(new osgGA::StateSetManipulator(view_->getCamera()->getOrCreateStateSet())); initialized_ = true; diff --git a/src/viewer/OsgWidget.cpp b/src/viewer/OsgWidget.cpp index 76afaf47..c2902f79 100644 --- a/src/viewer/OsgWidget.cpp +++ b/src/viewer/OsgWidget.cpp @@ -18,6 +18,7 @@ #include "ui/MainFrame.h" #include "workspace/WorkSpaceManager.h" #include "workspace/WorkSpace.h" +#include "scutcheon/osgScutcheon.h" static void ConfigureView( osgViewer::View* view ) { view->addEventHandler(new osgViewer::StatsHandler()); @@ -77,6 +78,8 @@ void OsgWidget::Initialize() { viewUI_ = new OsgViewUI(view_, 100,100); viewUI_->AddUI(activeScene_->GetOrCreateSceneUI()); + + //osgScutcheonManager::instance()->Init(view_.get(), activeScene_); } void OsgWidget::Uninitialize() {