modify conwave

This commit is contained in:
brige 2025-06-24 07:47:18 +08:00
parent d2a724bde6
commit e271e78200

View File

@ -85,6 +85,17 @@ private:
ConeWave::ConeWave() { ConeWave::ConeWave() {
osgEarth::Registry::shaderGenerator().run(this); osgEarth::Registry::shaderGenerator().run(this);
currentTime_ = 0.0; currentTime_ = 0.0;
// 确保成员变量有合理的默认值
height_ = 100.0f;
radius_ = 50.0f;
waveRadius_ = 100.0f;
waveSpeed_ = 20.0f;
waveCount_ = 3;
levelCount_ = 5;
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);
} }
@ -187,17 +198,18 @@ void ConeWave::SetWaveColor(const osg::Vec4& color) {
void ConeWave::CreateRadarScanWave() { void ConeWave::CreateRadarScanWave() {
// 使用OSG内置的Cone几何体来创建雷达扫描区域 // 使用OSG内置的Cone几何体来创建雷达扫描区域
cone_ = new osg::Cone(osg::Vec3(0, 0, 0.), waveRadius_, height_); cone_ = new osg::Cone(osg::Vec3(0, 0, height_/2), radius_, height_);
// 设置细分参数,创建更平滑的圆形底面 // 设置细分参数,创建更平滑的圆形底面
osg::TessellationHints* tesselate = new osg::TessellationHints; osg::TessellationHints* tesselate = new osg::TessellationHints;
tesselate->setCreateBottom(true); // 创建底面作为扫描面 tesselate->setCreateBottom(true); // 创建底面作为扫描面
tesselate->setCreateTop(true); // 创建顶面
tesselate->setCreateBackFace(false); // 不创建背面 tesselate->setCreateBackFace(false); // 不创建背面
tesselate->setDetailRatio(2.0f); // 增加细分精度 tesselate->setDetailRatio(2.0f); // 增加细分精度
// 创建可绘制对象 // 创建可绘制对象
coneDrawable_ = new osg::ShapeDrawable(cone_, tesselate); coneDrawable_ = new osg::ShapeDrawable(cone_, tesselate);
coneDrawable_->setColor(waveColor_); coneDrawable_->setColor(osg::Vec4(0.0f, 0.8f, 1.0f, 0.7f)); // 设置基础颜色
// 添加到几何节点 // 添加到几何节点
addDrawable(coneDrawable_); addDrawable(coneDrawable_);
@ -205,10 +217,11 @@ void ConeWave::CreateRadarScanWave() {
// 设置渲染状态 // 设置渲染状态
osg::StateSet* ss = coneDrawable_->getOrCreateStateSet(); osg::StateSet* ss = coneDrawable_->getOrCreateStateSet();
ss->setRenderBinDetails(120, "RenderBin"); ss->setRenderBinDetails(120, "RenderBin");
ss->setMode(GL_CULL_FACE, osg::StateAttribute::ON); ss->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); // 关闭面剔除以确保可见
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
// 设置混合模式 // 设置混合模式
osg::ref_ptr<osg::BlendFunc> bf = new osg::BlendFunc(); osg::ref_ptr<osg::BlendFunc> bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA);
ss->setAttributeAndModes(bf, osg::StateAttribute::ON); ss->setAttributeAndModes(bf, osg::StateAttribute::ON);
ss->setMode(GL_BLEND, osg::StateAttribute::ON); ss->setMode(GL_BLEND, osg::StateAttribute::ON);
ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
@ -232,49 +245,29 @@ void ConeWave::CreateRadarShader() {
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
"}\n"; "}\n";
// 片段着色器 - 基于高度的波纹效果 // 片段着色器 - 明显的同心圆波纹效果
static const char* fragmentShaderSource = static const char* fragmentShaderSource =
"uniform float num;\n" "uniform float num;\n"
"uniform float height;\n" "uniform float height;\n"
"uniform vec4 baseColor;\n" "uniform vec4 baseColor;\n"
"uniform vec4 waveColor;\n"
"uniform float waveTime;\n" "uniform float waveTime;\n"
"varying vec3 pos;\n" "varying vec3 pos;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" // 基于高度的层次效果\n" " float h = abs(pos.z) / max(height, 1.0);\n"
" float h = abs(pos.z) / height;\n" " float radialDist = sqrt(pos.x * pos.x + pos.y * pos.y);\n"
" float layer = h * num + waveTime;\n" " float wavePhase = radialDist * 0.3 - waveTime * 1.5;\n"
" \n" " float ripple = sin(wavePhase);\n"
" // 简单的波纹效果\n" " float heightWave = sin(h * num * 6.28 + waveTime * 1.0);\n"
" float wave = sin(layer * 6.28);\n" " if (ripple > 0.5)\n"
" float pulse = (wave + 1.0) * 0.5;\n"
" \n"
" // 计算边缘\n"
" float dist = sqrt(pos.x * pos.x + pos.y * pos.y);\n"
" float maxDist = abs(pos.z) * 1.2;\n"
" float edge = dist / maxDist;\n"
" \n"
" // 层次线效果\n"
" float layerLine = sin(h * num * 6.28 + waveTime * 4.0);\n"
" float lineIntensity = (layerLine + 1.0) * 0.5;\n"
" \n"
" // 颜色选择\n"
" if (lineIntensity > 0.7 && edge < 1.0)\n"
" {\n" " {\n"
" // 黄色边框线\n" " float intensity = (heightWave + 1.0) * 0.4 + 0.6;\n"
" gl_FragColor = vec4(1.0, 1.0, 0.0, 0.9);\n" " gl_FragColor = vec4(baseColor.rgb, intensity);\n"
" }\n"
" else if (edge < 1.0)\n"
" {\n"
" // 基础锥形区域\n"
" float intensity = pulse * 0.6 + 0.3;\n"
" gl_FragColor = vec4(baseColor.rgb * intensity, 0.4 * intensity);\n"
" }\n" " }\n"
" else\n" " else\n"
" {\n" " {\n"
" // 超出边界\n" " float intensity = (heightWave + 1.0) * 0.1 + 0.1;\n"
" gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);\n" " gl_FragColor = vec4(baseColor.rgb, intensity);\n"
" }\n" " }\n"
"}\n"; "}\n";
/* /*
@ -326,10 +319,10 @@ void ConeWave::CreateRadarShader() {
// 创建uniform变量 // 创建uniform变量
waveTimeUniform_ = new osg::Uniform("waveTime", 0.0f); waveTimeUniform_ = new osg::Uniform("waveTime", 0.0f);
baseColorUniform_ = new osg::Uniform("baseColor", osg::Vec4(0.8f, 0.2f, 0.2f, 1.0f)); // 红色基 baseColorUniform_ = new osg::Uniform("baseColor", osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); // 绿色雷达色
waveColorUniform_ = new osg::Uniform("waveColor", waveColor_); waveColorUniform_ = new osg::Uniform("waveColor", waveColor_);
levelCountUniform_ = new osg::Uniform("num", 8.0f); // 增加层数 levelCountUniform_ = new osg::Uniform("num", 5.0f); // 层数
levelHeightUniform_ = new osg::Uniform("height", height_); levelHeightUniform_ = new osg::Uniform("height", height_ > 0 ? height_ : 100.0f); // 确保高度不为0
stateSet->addUniform(waveTimeUniform_); stateSet->addUniform(waveTimeUniform_);
stateSet->addUniform(baseColorUniform_); stateSet->addUniform(baseColorUniform_);