#include "effects/ConeWave.h" #include #include #include #include #include #include #include #include #include #include #include #include #include class WaveSurfaceCallback : public osg::NodeCallback { public: WaveSurfaceCallback() : timeElapsed(0.0f) {} virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { timeElapsed += (nv->getFrameStamp()->getSimulationTime() - SimulationTime); // 获取并更新geometry的顶点 osg::Geode* geode = dynamic_cast(node); if (geode) { osg::Geometry* geometry = dynamic_cast(geode->getDrawable(0)); if (geometry) { osg::Vec3Array* vertices = dynamic_cast(geometry->getVertexArray()); if (vertices) { // 更新顶点位置来模拟波动 float amplitude = 2.0f; float frequency = 1.0f; for (unsigned int i = 0; i < vertices->size(); ++i) { osg::Vec3f& vertex = (*vertices)[i]; vertex.z() = amplitude * sin(frequency * (vertex.x() + timeElapsed)); } geometry->setVertexArray(vertices); geometry->dirtyBound(); // 更新几何体边界 } } } // 继续遍历场景图 traverse(node, nv); SimulationTime = nv->getFrameStamp()->getSimulationTime(); } private: float timeElapsed; double SimulationTime; }; // 雷达扫描波时间更新回调 class RadarWaveTimeCallback : public osg::NodeCallback { public: RadarWaveTimeCallback(osg::ref_ptr waveTimeUniform) : waveTimeUniform_(waveTimeUniform), startTime_(0.0) {} virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { if (nv->getFrameStamp()) { double currentTime = nv->getFrameStamp()->getSimulationTime(); if (startTime_ == 0.0) { startTime_ = currentTime; } // 计算经过的时间 float elapsedTime = static_cast(currentTime - startTime_); // 更新waveTime uniform if (waveTimeUniform_.valid()) { waveTimeUniform_->set(elapsedTime); } } // 继续遍历场景图 traverse(node, nv); } private: osg::ref_ptr waveTimeUniform_; double startTime_; }; ConeWave::ConeWave() { osgEarth::Registry::shaderGenerator().run(this); currentTime_ = 0.0; // 确保成员变量有合理的默认值 height_ = 1000.0f; radius_ = 50.0f; waveRadius_ = 100.0f; waveSpeed_ = 20.0f; waveCount_ = 3; baseColor_ = osg::Vec4(0.0f, 0.8f, 1.0f, 1.0f); waveColor_ = osg::Vec4(1.0f, 0.5f, 1.0f, 0.8f); // 初始化透明度值 ringBrightAlpha_ = 0.8f; ringDarkAlpha_ = 0.3f; coneAlpha_ = 0.7f; } ConeWave::~ConeWave(void) { } void ConeWave::Render(double dt) { // 时间更新现在由RadarWaveTimeCallback回调处理 // 这里可以处理其他需要更新的属性 } void ConeWave::InitGeode() { CreateRadarScanWave(); getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); setCullingActive(true); } void ConeWave::Destory() { OnDestroy(); } void ConeWave::SetHeight(float height) { height_ = height; if (cone_) { cone_->setHeight(height_); } //coneDrawable_->build(); } void ConeWave::SetRadius(float radius) { radius_ = radius; if (cone_) { cone_->setRadius(radius); } // coneDrawable_->build(); } void ConeWave::SetBaseColor(const osg::Vec4& color) { baseColor_ = color; //if (baseColorUniform_) { // baseColorUniform_->set(color); //} } // 雷达扫描波效果相关方法实现 void ConeWave::SetWaveRadius(float radius) { waveRadius_ = radius; if (waveRadiusUniform_.valid()) { waveRadiusUniform_->set(radius); } // 更新Cone的半径以匹配扫描波半径 if (cone_.valid()) { cone_->setRadius(radius); // 强制更新ShapeDrawable if (coneDrawable_.valid()) { coneDrawable_->dirtyDisplayList(); coneDrawable_->dirtyBound(); } } } void ConeWave::SetWaveSpeed(float speed) { waveSpeed_ = speed; if (waveSpeedUniform_.valid()) { waveSpeedUniform_->set(speed); } } void ConeWave::SetWaveCount(int count) { waveCount_ = count; if (waveCountUniform_.valid()) { waveCountUniform_->set(static_cast(count)); } } void ConeWave::SetWaveColor(const osg::Vec4& color) { waveColor_ = color; if (waveColorUniform_.valid()) { waveColorUniform_->set(color); } } // 透明度控制方法实现 void ConeWave::SetRingBrightAlpha(float alpha) { ringBrightAlpha_ = alpha; if (ringBrightAlphaUniform_.valid()) { ringBrightAlphaUniform_->set(alpha); } } void ConeWave::SetRingDarkAlpha(float alpha) { ringDarkAlpha_ = alpha; if (ringDarkAlphaUniform_.valid()) { ringDarkAlphaUniform_->set(alpha); } } void ConeWave::SetConeAlpha(float alpha) { coneAlpha_ = alpha; // 更新锥形的基础颜色透明度 if (coneDrawable_.valid()) { osg::Vec4 currentColor = coneDrawable_->getColor(); currentColor.a() = alpha; coneDrawable_->setColor(currentColor); } } void ConeWave::CreateRadarScanWave() { // 使用OSG内置的Cone几何体来创建雷达扫描区域 cone_ = new osg::Cone(osg::Vec3(0, 0, 0), radius_, height_); // 设置细分参数,创建开口锥形 osg::TessellationHints* tesselate = new osg::TessellationHints; tesselate->setCreateBottom(false); // 不创建底面,保持开口 tesselate->setCreateTop(false); // 不创建顶面 tesselate->setCreateBackFace(false); // 不创建背面 tesselate->setDetailRatio(2.0f); // 增加细分精度 // 创建可绘制对象 coneDrawable_ = new osg::ShapeDrawable(cone_, tesselate); coneDrawable_->setColor(osg::Vec4(0.0f, 0.8f, 1.0f, coneAlpha_)); // 使用coneAlpha_设置基础颜色 // 添加到几何节点 addDrawable(coneDrawable_); // 设置渲染状态 osg::StateSet* ss = coneDrawable_->getOrCreateStateSet(); ss->setRenderBinDetails(120, "RenderBin"); ss->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); // 关闭面剔除以确保可见 ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF); // 设置混合模式 osg::ref_ptr bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); ss->setAttributeAndModes(bf, osg::StateAttribute::ON); ss->setMode(GL_BLEND, osg::StateAttribute::ON); ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); ss->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK)); // 创建雷达扫描波着色器 CreateRadarShader(); // 添加时间更新回调 setUpdateCallback(new RadarWaveTimeCallback(waveTimeUniform_)); } void ConeWave::CreateRadarShader() { // 顶点着色器 - 使用简单的基于高度的条纹效果 static const char* vertexShaderSource = "varying vec3 pos;\n" "void main()\n" "{\n" " pos.x = gl_Vertex.x;\n" " pos.y = gl_Vertex.y;\n" " pos.z = gl_Vertex.z;\n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" "}\n"; // 片段着色器 - 可控透明度的同心圆波纹效果 static const char* fragmentShaderSource = "uniform float height;\n" "uniform vec4 baseColor;\n" "uniform vec4 waveColor;\n" "uniform float waveTime;\n" "uniform float ringBrightAlpha;\n" "uniform float ringDarkAlpha;\n" "uniform float waveSpeed;\n" "varying vec3 pos;\n" "void main()\n" "{\n" " float h = abs(pos.z) / max(height, 1.0);\n" " float radialDist = sqrt(pos.x * pos.x + pos.y * pos.y);\n" " float wavePhase = radialDist * 3.2 - waveTime * waveSpeed;\n" " float ripple = sin(wavePhase);\n" " if (ripple > 0.3)\n" " {\n" " gl_FragColor = vec4(waveColor.rgb, ringBrightAlpha);\n" " }\n" " else\n" " {\n" " gl_FragColor = vec4(baseColor.rgb, ringDarkAlpha);\n" " }\n" "}\n"; // 创建着色器 osg::ref_ptr vertexShader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource); osg::ref_ptr fragmentShader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); osg::ref_ptr program = new osg::Program; program->addShader(vertexShader); program->addShader(fragmentShader); // 获取drawable的状态集并设置着色器程序 osg::StateSet* stateSet = coneDrawable_->getOrCreateStateSet(); stateSet->setAttributeAndModes(program, osg::StateAttribute::ON); // 创建uniform变量 waveTimeUniform_ = new osg::Uniform("waveTime", 0.0f); baseColorUniform_ = new osg::Uniform("baseColor", baseColor_); // 绿色雷达色调 waveColorUniform_ = new osg::Uniform("waveColor", waveColor_); levelHeightUniform_ = new osg::Uniform("height", height_ > 0 ? height_ : 100.0f); // 确保高度不为0 waveSpeedUniform_ = new osg::Uniform("waveSpeed", waveSpeed_ > 0 ? waveSpeed_ : 1.0f); // 创建透明度控制uniform变量 ringBrightAlphaUniform_ = new osg::Uniform("ringBrightAlpha", ringBrightAlpha_); ringDarkAlphaUniform_ = new osg::Uniform("ringDarkAlpha", ringDarkAlpha_); stateSet->addUniform(waveTimeUniform_); stateSet->addUniform(baseColorUniform_); stateSet->addUniform(waveColorUniform_); stateSet->addUniform(levelHeightUniform_); stateSet->addUniform(waveSpeedUniform_); stateSet->addUniform(ringBrightAlphaUniform_); stateSet->addUniform(ringDarkAlphaUniform_); }