modify label

This commit is contained in:
brige 2025-06-22 18:18:46 +08:00
parent 36ba2b1009
commit 2e8a12ba34
5 changed files with 35 additions and 24 deletions

View File

@ -9,6 +9,8 @@
#include "entities/Entity.h" #include "entities/Entity.h"
#include "common/SpdLogger.h" #include "common/SpdLogger.h"
#include "scutcheon/osgScutcheon.h"
LabelComponent::LabelComponent(SceneComponent* parent) LabelComponent::LabelComponent(SceneComponent* parent)
: SceneComponent(parent) : SceneComponent(parent)
, text_("Label") , text_("Label")
@ -112,6 +114,9 @@ void LabelComponent::SetText(const std::string& text) {
if (text_ != text) { if (text_ != text) {
text_ = text; text_ = text;
needUpdate_ = true; needUpdate_ = true;
if (textNode_.valid()) {
textNode_->setText(text_);
}
} }
} }
@ -132,34 +137,43 @@ void LabelComponent::SetColor(const osg::Vec4& color) {
void LabelComponent::SetVisible(bool visible) { void LabelComponent::SetVisible(bool visible) {
if (visible_ != visible) { if (visible_ != visible) {
visible_ = visible; visible_ = visible;
if (textGeode_.valid()) { if (billboard_.valid()) {
textGeode_->setNodeMask(visible_ ? 0xffffffff : 0x0); billboard_->setNodeMask(visible_ ? 0xffffffff : 0x0);
} }
} }
} }
void LabelComponent::CreateTextNode() { void LabelComponent::CreateTextNode() {
if (!textGeode_.valid()) { if (mt_.valid()) {
textGeode_ = new osg::Geode(); return;
}
// 使用布告板来显示文本,确保文本始终面向相机
if (!billboard_.valid()) {
billboard_ = new osg::Billboard();
billboard_->setMode(osg::Billboard::POINT_ROT_EYE);
textNode_ = new osgText::Text(); textNode_ = new osgText::Text();
// 设置文本对齐方式为底部居中,这样文本会显示在指定位置的上方 // 设置文本对齐方式为底部居中,这样文本会显示在指定位置的上方
textNode_->setAlignment(osgText::Text::CENTER_BOTTOM); textNode_->setAlignment(osgText::Text::CENTER_BOTTOM);
textNode_->setAxisAlignment(osgText::Text::SCREEN); textNode_->setAxisAlignment(osgText::Text::SCREEN);
textNode_->setCharacterSizeMode(osgText::Text::OBJECT_COORDS); textNode_->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
textNode_->setText(text_); textNode_->setText(text_);
textNode_->setPosition(osg::Vec3(0.0f, 0.0f, 500.0f)); // 设置初始位置在模型上方 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_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->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform(); osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();
mt->addChild(textGeode_); mt->addChild(billboard_);
if (!mt_.valid()) { if (!mt_.valid()) {
mt_ = mt; mt_ = mt;
} }
@ -174,10 +188,7 @@ void LabelComponent::UpdateTextNode() {
textNode_->setCharacterSize(fontSize_); textNode_->setCharacterSize(fontSize_);
textNode_->setColor(color_); textNode_->setColor(color_);
// 设置标签位置在模型上方,增加偏移量使其更明显 SPDLOG_INFO("LabelComponent: Updated billboard text '{}' with size {}", text_, fontSize_);
textNode_->setPosition(osg::Vec3(0.0f, 0.0f, 500.0f));
SPDLOG_INFO("LabelComponent: Updated text '{}' with size {}", text_, fontSize_);
} }
} }

View File

@ -4,7 +4,7 @@
#include <qstring.h> #include <qstring.h>
#include <osg/Geode> #include <osg/Billboard>
#include <osgText/Text> #include <osgText/Text>
#include <string> #include <string>
@ -52,6 +52,7 @@ protected:
bool visible_; bool visible_;
bool needUpdate_; bool needUpdate_;
osg::ref_ptr<osg::Geode> textGeode_; //osg::ref_ptr<class osgScutcheon> scutcheon_;
osg::ref_ptr<osg::Billboard> billboard_;
osg::ref_ptr<osgText::Text> textNode_; osg::ref_ptr<osgText::Text> textNode_;
}; };

View File

@ -694,12 +694,12 @@
<context> <context>
<name>OsgWidget</name> <name>OsgWidget</name>
<message> <message>
<location filename="../viewer/OsgWidget.cpp" line="110"/> <location filename="../viewer/OsgWidget.cpp" line="113"/>
<source>warning</source> <source>warning</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../viewer/OsgWidget.cpp" line="111"/> <location filename="../viewer/OsgWidget.cpp" line="114"/>
<source>open dyt file failed</source> <source>open dyt file failed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>

View File

@ -18,7 +18,6 @@
#include "viewer/KeyMapQtOsg.h" #include "viewer/KeyMapQtOsg.h"
#include "viewer/OsgCameraManipulator.h" #include "viewer/OsgCameraManipulator.h"
#include "viewer/OsgViewUI.h" #include "viewer/OsgViewUI.h"
#include "scutcheon/osgScutcheon.h"
OsgView::OsgView(QObject* parent) noexcept OsgView::OsgView(QObject* parent) noexcept
: QObject(parent) { : QObject(parent) {
@ -62,9 +61,6 @@ void OsgView::Initialize(OsgCameraManipulator* cameraManipulator) {
root->addChild(viewUI_.get()); root->addChild(viewUI_.get());
} }
osg::Group* root = view_->getSceneData()->asGroup();
osgScutcheonManager::instance()->Init(view_.get(), root);
view_->addEventHandler(new osgViewer::StatsHandler); view_->addEventHandler(new osgViewer::StatsHandler);
view_->addEventHandler(new osgGA::StateSetManipulator(view_->getCamera()->getOrCreateStateSet())); view_->addEventHandler(new osgGA::StateSetManipulator(view_->getCamera()->getOrCreateStateSet()));
initialized_ = true; initialized_ = true;

View File

@ -18,6 +18,7 @@
#include "ui/MainFrame.h" #include "ui/MainFrame.h"
#include "workspace/WorkSpaceManager.h" #include "workspace/WorkSpaceManager.h"
#include "workspace/WorkSpace.h" #include "workspace/WorkSpace.h"
#include "scutcheon/osgScutcheon.h"
static void ConfigureView( osgViewer::View* view ) { static void ConfigureView( osgViewer::View* view ) {
view->addEventHandler(new osgViewer::StatsHandler()); view->addEventHandler(new osgViewer::StatsHandler());
@ -77,6 +78,8 @@ void OsgWidget::Initialize() {
viewUI_ = new OsgViewUI(view_, 100,100); viewUI_ = new OsgViewUI(view_, 100,100);
viewUI_->AddUI(activeScene_->GetOrCreateSceneUI()); viewUI_->AddUI(activeScene_->GetOrCreateSceneUI());
//osgScutcheonManager::instance()->Init(view_.get(), activeScene_);
} }
void OsgWidget::Uninitialize() { void OsgWidget::Uninitialize() {