如何使osgEarth::ModelLayer的模型在距离超过某个值时隐藏?

How to make osgEarth::ModelLayer's model hide if distance exceeds some value?

本文关键字:隐藏 距离 osgEarth 何使 ModelLayer 模型      更新时间:2023-10-16

我正在尝试显示模型文件中的标签,osgearth_features演示展示了如何做到这一点。它对我来说效果很好,但是一旦到地球的距离超过某个值,我需要标签消失。(我使用的是osgEarth的25ce0e1版本。

我知道有PagedLOD,这将有助于我隐藏osg::Node。但我所拥有的是osgEarth::ModelLayer,我似乎找不到一种明智的方式在MapNode和我的标签之间插入PagedLOD Node

我目前的方法虽然可行,但有些笨拙。这是对原始osgearth osgearth_features.cpp的实验性更改,以完成我需要的操作:

diff --git a/src/applications/osgearth_features/osgearth_features.cpp b/src/applications/osgearth_features/osgearth_features.cpp
index 2bb1ed8..fbdd3da 100644
--- a/src/applications/osgearth_features/osgearth_features.cpp
+++ b/src/applications/osgearth_features/osgearth_features.cpp
@@ -184,6 +184,12 @@ int main(int argc, char** argv)
         geomOptions.styles()->addStyle( labelStyle );
         map->addModelLayer( new ModelLayer("labels", geomOptions) );
+        osg::Group*const modelLayerGroup=mapNode->getModelLayerGroup();
+        const int newNumChildren=modelLayerGroup->getNumChildren();
+        osg::Node*const model=modelLayerGroup->getChild(newNumChildren-1);
+        osg::PagedLOD*const lod=new osg::PagedLOD;
+        modelLayerGroup->replaceChild(model,lod);
+        lod->addChild(model, 0, 1e7);
     }
     if ( !useStencil )

这种节点的替换对我来说似乎太丑陋了。实现目标的更好、"正确"的方法是什么?或者这就是这些事情应该做的方式?

也许你可以编写一个带有自定义文件扩展名的伪加载器,用它来包装你的真实模型文件名(如mymodel.obj.plod(,然后"加载"并将PagedLOD重新转换为osgEarth到图形中,在PageLOD子属性中使用"真实"模型路径名。