From 52c20bd29781b1d4963864a2d526ca3be03c24ec Mon Sep 17 00:00:00 2001 From: brige Date: Tue, 24 Jun 2025 23:40:11 +0800 Subject: [PATCH] modify effects conwav alple --- src/effects/ConeWave.cpp | 59 ++++++++++++++++++++++++++++++++-------- src/effects/ConeWave.h | 20 ++++++++++++++ 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/effects/ConeWave.cpp b/src/effects/ConeWave.cpp index 28bd4dfe..7ae6c279 100644 --- a/src/effects/ConeWave.cpp +++ b/src/effects/ConeWave.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include class WaveSurfaceCallback : public osg::NodeCallback { @@ -96,6 +97,11 @@ ConeWave::ConeWave() { levelHeight_ = 100.0f; 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; } @@ -114,7 +120,7 @@ void ConeWave::InitGeode() { getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); - setCullingActive(false); + setCullingActive(true); } void ConeWave::Destory() { @@ -196,6 +202,31 @@ void ConeWave::SetWaveColor(const osg::Vec4& 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, height_/2), radius_, height_); @@ -209,7 +240,7 @@ void ConeWave::CreateRadarScanWave() { // 创建可绘制对象 coneDrawable_ = new osg::ShapeDrawable(cone_, tesselate); - coneDrawable_->setColor(osg::Vec4(0.0f, 0.8f, 1.0f, 0.7f)); // 设置基础颜色 + coneDrawable_->setColor(osg::Vec4(0.0f, 0.8f, 1.0f, coneAlpha_)); // 使用coneAlpha_设置基础颜色 // 添加到几何节点 addDrawable(coneDrawable_); @@ -225,6 +256,7 @@ void ConeWave::CreateRadarScanWave() { 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(); @@ -245,29 +277,28 @@ void ConeWave::CreateRadarShader() { " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" "}\n"; - // 片段着色器 - 明显的同心圆波纹效果 + // 片段着色器 - 可控透明度的同心圆波纹效果 static const char* fragmentShaderSource = "uniform float num;\n" "uniform float height;\n" "uniform vec4 baseColor;\n" "uniform float waveTime;\n" + "uniform float ringBrightAlpha;\n" + "uniform float ringDarkAlpha;\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 * 0.3 - waveTime * 1.5;\n" + " float wavePhase = radialDist * 3.2 - waveTime * 30.0;\n" " float ripple = sin(wavePhase);\n" - " float heightWave = sin(h * num * 6.28 + waveTime * 1.0);\n" - " if (ripple > 0.5)\n" + " if (ripple > 0.3)\n" " {\n" - " float intensity = (heightWave + 1.0) * 0.4 + 0.6;\n" - " gl_FragColor = vec4(baseColor.rgb, intensity);\n" + " gl_FragColor = vec4(baseColor.rgb, ringBrightAlpha);\n" " }\n" " else\n" " {\n" - " float intensity = (heightWave + 1.0) * 0.1 + 0.1;\n" - " gl_FragColor = vec4(baseColor.rgb, intensity);\n" + " gl_FragColor = vec4(baseColor.rgb, ringDarkAlpha);\n" " }\n" "}\n"; /* @@ -324,11 +355,17 @@ void ConeWave::CreateRadarShader() { levelCountUniform_ = new osg::Uniform("num", 5.0f); // 层数 levelHeightUniform_ = new osg::Uniform("height", height_ > 0 ? height_ : 100.0f); // 确保高度不为0 + // 创建透明度控制uniform变量 + ringBrightAlphaUniform_ = new osg::Uniform("ringBrightAlpha", ringBrightAlpha_); + ringDarkAlphaUniform_ = new osg::Uniform("ringDarkAlpha", ringDarkAlpha_); + stateSet->addUniform(waveTimeUniform_); stateSet->addUniform(baseColorUniform_); - stateSet->addUniform(waveColorUniform_); + stateSet->addUniform(waveColorUniform_); stateSet->addUniform(levelCountUniform_); stateSet->addUniform(levelHeightUniform_); + stateSet->addUniform(ringBrightAlphaUniform_); + stateSet->addUniform(ringDarkAlphaUniform_); } void ConeWave::CreateTexturedCone(osg::Geode* geode) { diff --git a/src/effects/ConeWave.h b/src/effects/ConeWave.h index 87609bbe..c34c6bcf 100644 --- a/src/effects/ConeWave.h +++ b/src/effects/ConeWave.h @@ -60,6 +60,16 @@ public: return levelHeight_; } + // 透明度控制方法 + void SetRingBrightAlpha(float alpha); + float GetRingBrightAlpha() const { return ringBrightAlpha_; } + + void SetRingDarkAlpha(float alpha); + float GetRingDarkAlpha() const { return ringDarkAlpha_; } + + void SetConeAlpha(float alpha); + float GetConeAlpha() const { return coneAlpha_; } + protected: virtual void CreateTexturedCone(osg::Geode* geode); @@ -82,12 +92,22 @@ private: osg::ref_ptr waveCountUniform_; osg::ref_ptr waveColorUniform_; + // 透明度控制uniform变量 + osg::ref_ptr ringBrightAlphaUniform_; + osg::ref_ptr ringDarkAlphaUniform_; + osg::ref_ptr coneAlphaUniform_; + float waveRadius_{ 100.0f }; // 扫描波最大半径 float waveSpeed_{ 20.0f }; // 扫描波速度 int waveCount_{ 30 }; // 同时存在的波纹数量 osg::Vec4 baseColor_{ 1.0f, 0.2f, 0.5f, 1.f }; osg::Vec4 waveColor_{ 0.0f, 0.5f, 1.0f, 0.8f }; // 扫描波颜色(蓝色) double currentTime_{ 0.0 }; // 当前时间 + + // 透明度值 + float ringBrightAlpha_{ 0.8f }; // 亮环透明度 + float ringDarkAlpha_{ 0.3f }; // 暗环透明度 + float coneAlpha_{ 0.7f }; // 锥形透明度 }; /*