From b58690dbf7dfa9accbdfcfb22ab97eeee9062fd2 Mon Sep 17 00:00:00 2001 From: jiegeaiai Date: Sun, 29 Dec 2024 22:33:01 +0800 Subject: [PATCH] modify cone wave --- Source/src/effects/ConeWave.cpp | 104 ++++++++++-------- Source/src/effects/ConeWave.h | 19 +++- Source/src/entities/ConeWaveComponent.cpp | 25 ++++- Source/src/entities/ConeWaveComponent.h | 6 + Source/src/translations/Dyt_zh_CN.ts | 40 ++++--- Source/src/ui/PropertyBrowser.cpp | 3 +- .../ui/PropertyBrowser/qtpropertymanager.cpp | 69 +++++++++++- .../ui/PropertyBrowser/qtpropertymanager.h | 1 + .../PropertyBrowser/qtworkspaceattribute.cpp | 38 +++++++ .../ui/PropertyBrowser/qtworkspaceattribute.h | 6 + Source/workspace/default2.dyt | 8 +- 11 files changed, 244 insertions(+), 75 deletions(-) diff --git a/Source/src/effects/ConeWave.cpp b/Source/src/effects/ConeWave.cpp index c6e444d9..d6a003d0 100644 --- a/Source/src/effects/ConeWave.cpp +++ b/Source/src/effects/ConeWave.cpp @@ -12,8 +12,7 @@ #include -ConeWave::ConeWave(const std::string& path) - : txturePath_(path) { +ConeWave::ConeWave() { } @@ -23,12 +22,7 @@ ConeWave::~ConeWave(void) } void ConeWave::Render(double dt) { - if (timeUniform_) { - //float timeValue; - //timeUniform_->get(timeValue); - //timeValue += dt * 0.1; // ¿ØÖƹö¶¯ËÙ¶È - timeUniform_->set(static_cast(dt * 0.1)); - } + } void ConeWave::InitGeode() { @@ -62,6 +56,20 @@ void ConeWave::SetBaseColor(const osg::Vec4& color) { } } +void ConeWave::SetLevelCount(int count) { + levelCount_ = count; + if (levelCountUniform_) { + levelCountUniform_->set(float(levelCount_)); + } +} + +void ConeWave::SetLevelHeight(float height) { + levelHeight_ = height; + if (levelHeightUniform_) { + levelHeightUniform_->set(levelHeight_); + } +} + void ConeWave::CreateTexturedCone(osg::Geode* geode) { cone_ = new osg::Cone(osg::Vec3(0, 0, 0.), radius_, height_); osg::TessellationHints* tesselate = new osg::TessellationHints; @@ -70,57 +78,59 @@ void ConeWave::CreateTexturedCone(osg::Geode* geode) { coneDrawable_ = new osg::ShapeDrawable(cone_, tesselate); addDrawable(coneDrawable_); - osg::Texture2D* texture = new osg::Texture2D; - texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR); - texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR); - texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT); - texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT); - osg::Image* image = osgDB::readImageFile(txturePath_); - texture->setImage(image); + static const char* vertSource = { + "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* fragSource = { + "uniform float num; \n" + "uniform float height; \n" + "uniform vec4 baseColor;\n" + "varying vec3 pos;\n" + "float Alpha = 1.0; \n" + "float f = pos.z;\n" + "uniform float osg_FrameTime;\n" + "void main()\n" + "{\n" + "if (sin(f/height*3.14*2*num+ osg_FrameTime*10) > 0)\n" + "{\n" + " Alpha = 0.8;\n" + "}\n" + "else\n" + "{\n" + " Alpha = 0.3;\n" + "}\n" + " gl_FragColor = vec4(baseColor.rgb, Alpha);\n" + "}\n " + }; osg::ref_ptr vertexShader = new osg::Shader(osg::Shader::VERTEX); - vertexShader->setShaderSource( - "#version 330\n" - "in vec4 osg_Vertex;\n" - "in vec2 osg_MultiTexCoord0;\n" - "uniform mat4 osg_ModelViewProjectionMatrix;\n" - "uniform float time;\n" - "out vec2 texCoord;\n" - "void main()\n" - "{\n" - " gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;\n" - " texCoord = osg_MultiTexCoord0 + vec2(0.0, time);\n" - "}\n" - ); - + vertexShader->setShaderSource(vertSource); osg::ref_ptr fragmentShader = new osg::Shader(osg::Shader::FRAGMENT); - fragmentShader->setShaderSource( - "#version 330\n" - "in vec2 texCoord;\n" - "uniform sampler2D texture;\n" - "uniform vec4 baseColor;\n" - "out vec4 fragColor;\n" - "void main()\n" - "{\n" - " vec4 color = texture2D(texture, texCoord);\n" - " fragColor = clamp(texture2D(texture, texCoord) * baseColor + vec4(0,0,0, 0.2), 0.0, 1.0);\n" - "}\n" - ); + fragmentShader->setShaderSource(fragSource); osg::StateSet* stateset = coneDrawable_->getOrCreateStateSet(); - - stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); - stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); + stateset->setRenderBinDetails(10000, "RenderBin"); osg::ref_ptr blendFunc = new osg::BlendFunc(); stateset->setAttributeAndModes(blendFunc, osg::StateAttribute::ON); osg::ref_ptr program = new osg::Program(); program->addShader(vertexShader); program->addShader(fragmentShader); - stateset->addUniform(new osg::Uniform("texture", 0)); + baseColorUniform_ = new osg::Uniform("baseColor", baseColor_); + stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON); stateset->addUniform(baseColorUniform_); stateset->setAttributeAndModes(program, osg::StateAttribute::ON); - timeUniform_ = new osg::Uniform("time", 0.0f); - stateset->addUniform(timeUniform_); + levelCountUniform_ = new osg::Uniform("num", float(levelCount_)); + levelHeightUniform_ = new osg::Uniform("height", levelHeight_); + + stateset->addUniform(levelCountUniform_); + stateset->addUniform(levelHeightUniform_.get()); } diff --git a/Source/src/effects/ConeWave.h b/Source/src/effects/ConeWave.h index 53349edf..e8a04f9f 100644 --- a/Source/src/effects/ConeWave.h +++ b/Source/src/effects/ConeWave.h @@ -12,7 +12,7 @@ class ConeWave : public osg::Geode , public UpdateRenderStd { public: - explicit ConeWave(const std::string& path); + explicit ConeWave(); ~ConeWave(void) override; void Render(double dt) override; @@ -35,16 +35,29 @@ public: return baseColor_; } + void SetLevelCount(int count); + int GetLevelCount() const { + return levelCount_; + } + + void SetLevelHeight(float height); + float GetLevelHeihgt() const { + return levelHeight_; + } + protected: virtual void CreateTexturedCone(osg::Geode* geode); private: - std::string txturePath_; osg::ref_ptr cone_; osg::ref_ptr coneDrawable_; - osg::ref_ptr timeUniform_; osg::ref_ptr baseColorUniform_; osg::Vec4 baseColor_{ 0.0f, 0.2f, 0.5f, 0.2f }; + osg::ref_ptr levelCountUniform_; + int levelCount_{ 4 }; + osg::ref_ptr levelHeightUniform_; + float levelHeight_{ 500.0f }; + float height_{ 60.0f }; float radius_{ 10.0f }; }; diff --git a/Source/src/entities/ConeWaveComponent.cpp b/Source/src/entities/ConeWaveComponent.cpp index c0b37a88..3c077d0f 100644 --- a/Source/src/entities/ConeWaveComponent.cpp +++ b/Source/src/entities/ConeWaveComponent.cpp @@ -13,8 +13,7 @@ ConeWaveComponent::ConeWaveComponent(SceneComponent* parent) : SceneComponent(parent) { - const QString txturePath = RecourceHelper::Get().GetBasePath() + "/resources/textures/stripes_h.png"; - coneWave_ = new ConeWave(txturePath.toStdString()); + coneWave_ = new ConeWave(); coneWave_->InitGeode(); colorMap_[1] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f); @@ -43,6 +42,10 @@ bool ConeWaveComponent::SetAttribute(const char* name, const char* value) { SetHeight(atof(value)); } else if (0 == strcmp("event", name)) { SetTimeAction(value); + } else if (0 == strcmp("levelCount", name)) { + SetHeight(atof(value)); + } else if (0 == strcmp("levelHeihgt", name)) { + SetTimeAction(value); } return SceneComponent::SetAttribute(name, value); @@ -66,6 +69,8 @@ bool ConeWaveComponent::SaveAttribute(tinyxml2::XMLElement* element) { } element->SetAttribute("radius", std::to_string(GetRadius()).c_str()); element->SetAttribute("height", std::to_string(GetHeight()).c_str()); + element->SetAttribute("levelCount", std::to_string(GetLevelCount()).c_str()); + element->SetAttribute("levelHeihgt", std::to_string(GetLevelHeight()).c_str()); element->SetAttribute("event", timeAction_ == nullptr ? "" : timeAction_->GetPath().toStdString().c_str()); return SceneComponent::SaveAttribute(element); } @@ -97,6 +102,22 @@ float ConeWaveComponent::GetRadius() const { return coneWave_->GetRadius(); } +void ConeWaveComponent::SetLevelCount(int count) { + coneWave_->SetLevelCount(count); +} + +float ConeWaveComponent::GetLevelCount() const { + return coneWave_->GetLevelCount(); +} + +void ConeWaveComponent::SetLevelHeight(float height) { + coneWave_->SetLevelHeight(height); +} + +float ConeWaveComponent::GetLevelHeight() const { + return coneWave_->GetLevelHeihgt(); +} + void ConeWaveComponent::SetBaseColor(const osg::Vec4& color) { coneWave_->SetBaseColor(color); } diff --git a/Source/src/entities/ConeWaveComponent.h b/Source/src/entities/ConeWaveComponent.h index 5cf15c19..0c2ed04b 100644 --- a/Source/src/entities/ConeWaveComponent.h +++ b/Source/src/entities/ConeWaveComponent.h @@ -31,6 +31,12 @@ public: void SetRadius(float radius); float GetRadius() const; + void SetLevelCount(int count); + float GetLevelCount() const; + + void SetLevelHeight(float height); + float GetLevelHeight() const; + void SetBaseColor(const osg::Vec4& color); const osg::Vec4 GetBaseColor() const; diff --git a/Source/src/translations/Dyt_zh_CN.ts b/Source/src/translations/Dyt_zh_CN.ts index 28d96a96..b5788e64 100644 --- a/Source/src/translations/Dyt_zh_CN.ts +++ b/Source/src/translations/Dyt_zh_CN.ts @@ -629,12 +629,12 @@ - + ModelBase - + color base @@ -761,33 +761,43 @@ QtConeWaveComponentManager - - + + ConeWaveComponent - + Height - + Radius - + + levelCount + + + + + levelHeight + + + + Color1 - + Color2 - + Color3 @@ -893,28 +903,28 @@ QtDashedLineComponentManager - - + + DashedLineComponent - + Start - + End - + Radius - + Color diff --git a/Source/src/ui/PropertyBrowser.cpp b/Source/src/ui/PropertyBrowser.cpp index a467516f..7ac02590 100644 --- a/Source/src/ui/PropertyBrowser.cpp +++ b/Source/src/ui/PropertyBrowser.cpp @@ -160,8 +160,6 @@ void PropertyBrowser::InitComponentPropertyManager() { QtColorEditorFactory* colorFactory = new QtColorEditorFactory(this); QtFilePathFactory* filePathFactory = new QtFilePathFactory(this); QtEntityUUIDEditorFactory* entityUUIDFactory = new QtEntityUUIDEditorFactory(this); - //QtStringEdit* stringEdit = new QtStringEdit(this); - //QtStringEditorFactory* stringFactory = new QtStringEditorFactory(this); QtMeshComponetManager* meshComponentManager = new QtMeshComponetManager(this); browser_->setFactoryForManager(meshComponentManager->subStringProperyManager(), lineEditFactory); @@ -173,6 +171,7 @@ void PropertyBrowser::InitComponentPropertyManager() { QtConeWaveComponentManager* coneWaveComponentManager = new QtConeWaveComponentManager(this); browser_->setFactoryForManager(coneWaveComponentManager->subDoubleProperyManager(), doubleSpinBoxFactory); + browser_->setFactoryForManager(coneWaveComponentManager->subIntProperyManager(), spinBoxFactory); browser_->setFactoryForManager(coneWaveComponentManager->subColorProperyManager(), colorFactory); componetManager_[coneWaveComponentManager->GetPropertyId()] = coneWaveComponentManager; diff --git a/Source/src/ui/PropertyBrowser/qtpropertymanager.cpp b/Source/src/ui/PropertyBrowser/qtpropertymanager.cpp index 16a9aad2..858badbc 100644 --- a/Source/src/ui/PropertyBrowser/qtpropertymanager.cpp +++ b/Source/src/ui/PropertyBrowser/qtpropertymanager.cpp @@ -8248,6 +8248,7 @@ class QtConeWaveComponentManagerPrivate { public: void slotDoubleChanged(QtProperty* property, double value); + void slotIntChanged(QtProperty* property, int value); void slotColorChanged(QtProperty* property, const QColor& value); void slotPropertyDestroyed(QtProperty* property); @@ -8257,16 +8258,21 @@ public: QtDoublePropertyManager* m_doubleProperyManager; + QtIntPropertyManager* m_intProperyManager; QtColorPropertyManager* m_colorProperyManager; QMap m_properyToRadius; QMap m_properyToHeight; + QMap m_properyToLevelCount; + QMap m_properyToLevelHeight; QMap m_properyToColor1; QMap m_properyToColor2; QMap m_properyToColor3; QMap m_radiusToPropery; QMap m_heightToPropery; + QMap m_levelCountToPropery; + QMap m_levelHeightToPropery; QMap m_color1ToPropery; QMap m_color2ToPropery; QMap m_color3ToPropery; @@ -8278,14 +8284,25 @@ void QtConeWaveComponentManagerPrivate::slotDoubleChanged(QtProperty* property, QConeWaveComponentAttribute c = m_values[prop]; c.SetRadius(value); q_ptr->setValue(prop, c); - } - if (QtProperty* prop = m_heightToPropery.value(property, 0)) { + } else if (QtProperty* prop = m_heightToPropery.value(property, 0)) { QConeWaveComponentAttribute c = m_values[prop]; c.SetHeight(value); q_ptr->setValue(prop, c); + } else if (QtProperty* prop = m_levelHeightToPropery.value(property, 0)) { + QConeWaveComponentAttribute c = m_values[prop]; + c.SetLevelHeight(value); + q_ptr->setValue(prop, c); } } +void QtConeWaveComponentManagerPrivate::slotIntChanged(QtProperty* property, int value) { + if (QtProperty* prop = m_levelCountToPropery.value(property, 0)) { + QConeWaveComponentAttribute c = m_values[prop]; + c.SetLevelCount(value); + q_ptr->setValue(prop, c); + } +} + void QtConeWaveComponentManagerPrivate::slotColorChanged(QtProperty* property, const QColor& value) { if (QtProperty* prop = m_color1ToPropery.value(property, 0)) { QConeWaveComponentAttribute c = m_values[prop]; @@ -8311,6 +8328,14 @@ void QtConeWaveComponentManagerPrivate::slotPropertyDestroyed(QtProperty* proper m_heightToPropery[subProp] = 0; m_heightToPropery.remove(property); } + if (QtProperty* subProp = m_levelCountToPropery.value(property, nullptr)) { + m_levelCountToPropery[subProp] = 0; + m_levelCountToPropery.remove(property); + } + if (QtProperty* subProp = m_levelHeightToPropery.value(property, nullptr)) { + m_levelHeightToPropery[subProp] = 0; + m_levelHeightToPropery.remove(property); + } if (QtProperty* subProp = m_color1ToPropery.value(property, nullptr)) { m_color1ToPropery[subProp] = 0; m_color1ToPropery.remove(property); @@ -8335,6 +8360,12 @@ QtConeWaveComponentManager::QtConeWaveComponentManager(QObject* parent) this, SLOT(slotDoubleChanged(QtProperty*, double))); connect(d_ptr->m_doubleProperyManager, SIGNAL(propertyDestroyed(QtProperty*)), this, SLOT(slotPropertyDestroyed(QtProperty*))); + + d_ptr->m_intProperyManager = new QtIntPropertyManager(this); + connect(d_ptr->m_intProperyManager, SIGNAL(valueChanged(QtProperty*, int)), + this, SLOT(slotIntChanged(QtProperty*, int))); + connect(d_ptr->m_intProperyManager, SIGNAL(propertyDestroyed(QtProperty*)), + this, SLOT(slotPropertyDestroyed(QtProperty*))); d_ptr->m_colorProperyManager = new QtColorPropertyManager(this); connect(d_ptr->m_colorProperyManager, SIGNAL(valueChanged(QtProperty*, const QColor&)), @@ -8379,6 +8410,10 @@ QtDoublePropertyManager* QtConeWaveComponentManager::subDoubleProperyManager() c return d_ptr->m_doubleProperyManager; } +QtIntPropertyManager* QtConeWaveComponentManager::subIntProperyManager() const { + return d_ptr->m_intProperyManager; +} + QtColorPropertyManager* QtConeWaveComponentManager::subColorProperyManager() const { return d_ptr->m_colorProperyManager; } @@ -8425,6 +8460,8 @@ void QtConeWaveComponentManager::setValue(QtProperty* property, const QConeWaveC d_ptr->m_doubleProperyManager->setValue(d_ptr->m_properyToRadius[property], value.GetRadius()); d_ptr->m_doubleProperyManager->setValue(d_ptr->m_properyToHeight[property], value.GetHeight()); + d_ptr->m_doubleProperyManager->setValue(d_ptr->m_properyToLevelHeight[property], value.GetLevelHeight()); + d_ptr->m_intProperyManager->setValue(d_ptr->m_properyToLevelCount[property], value.GetLevelCount()); d_ptr->m_colorProperyManager->setValue(d_ptr->m_properyToColor1[property], value.GetColor1()); d_ptr->m_colorProperyManager->setValue(d_ptr->m_properyToColor2[property], value.GetColor2()); d_ptr->m_colorProperyManager->setValue(d_ptr->m_properyToColor3[property], value.GetColor3()); @@ -8452,6 +8489,20 @@ void QtConeWaveComponentManager::initializeProperty(QtProperty* property) { d_ptr->m_doubleProperyManager->setValueOnly(prop, val.GetHeight()); d_ptr->m_properyToRadius[property] = prop; d_ptr->m_radiusToPropery[prop] = property; + property->addSubProperty(prop); + + prop = d_ptr->m_intProperyManager->addProperty(); + prop->setPropertyName(tr("levelCount")); + d_ptr->m_intProperyManager->setValueOnly(prop, val.GetLevelCount()); + d_ptr->m_properyToLevelCount[property] = prop; + d_ptr->m_levelCountToPropery[prop] = property; + property->addSubProperty(prop); + + prop = d_ptr->m_doubleProperyManager->addProperty(); + prop->setPropertyName(tr("levelHeight")); + d_ptr->m_doubleProperyManager->setValueOnly(prop, val.GetLevelHeight()); + d_ptr->m_properyToLevelHeight[property] = prop; + d_ptr->m_levelHeightToPropery[prop] = property; property->addSubProperty(prop); prop = d_ptr->m_colorProperyManager->addProperty(); @@ -8492,6 +8543,20 @@ void QtConeWaveComponentManager::uninitializeProperty(QtProperty* property) { d_ptr->m_heightToPropery.remove(prop); delete prop; } + d_ptr->m_properyToHeight.remove(property); + + prop = d_ptr->m_levelCountToPropery[property]; + if (prop) { + d_ptr->m_levelCountToPropery.remove(prop); + delete prop; + } + d_ptr->m_properyToRadius.remove(property); + + prop = d_ptr->m_levelHeightToPropery[property]; + if (prop) { + d_ptr->m_levelHeightToPropery.remove(prop); + delete prop; + } d_ptr->m_properyToHeight.remove(property); prop = d_ptr->m_color1ToPropery[property]; diff --git a/Source/src/ui/PropertyBrowser/qtpropertymanager.h b/Source/src/ui/PropertyBrowser/qtpropertymanager.h index a69adfaa..9365a2b1 100644 --- a/Source/src/ui/PropertyBrowser/qtpropertymanager.h +++ b/Source/src/ui/PropertyBrowser/qtpropertymanager.h @@ -1123,6 +1123,7 @@ public: QConeWaveComponentAttribute value(const QtProperty* property) const; QtDoublePropertyManager* subDoubleProperyManager() const; + QtIntPropertyManager* subIntProperyManager() const; QtColorPropertyManager* subColorProperyManager() const; public Q_SLOTS: diff --git a/Source/src/ui/PropertyBrowser/qtworkspaceattribute.cpp b/Source/src/ui/PropertyBrowser/qtworkspaceattribute.cpp index fa764058..70eca8f2 100644 --- a/Source/src/ui/PropertyBrowser/qtworkspaceattribute.cpp +++ b/Source/src/ui/PropertyBrowser/qtworkspaceattribute.cpp @@ -362,6 +362,44 @@ float QConeWaveComponentAttribute::GetHeight() const { return object_->GetHeight(); } +void QConeWaveComponentAttribute::SetLevelCount(int c) { + if (nullptr == object_) { + return; + } + + if (c == object_->GetLevelCount()) { + return; + } + + object_->SetLevelCount(c); +} + +int QConeWaveComponentAttribute::GetLevelCount() const { + if (nullptr == object_) { + return 4; + } + return object_->GetLevelCount(); +} + +void QConeWaveComponentAttribute::SetLevelHeight(float h) { + if (nullptr == object_) { + return; + } + + if (h == object_->GetLevelHeight()) { + return; + } + + object_->SetLevelHeight(h); +} + +float QConeWaveComponentAttribute::GetLevelHeight() const { + if (nullptr == object_) { + return 500.0f; + } + return object_->GetLevelHeight(); +} + bool QConeWaveComponentAttribute::operator==(const QConeWaveComponentAttribute& other) { return object_ == other.object_; } diff --git a/Source/src/ui/PropertyBrowser/qtworkspaceattribute.h b/Source/src/ui/PropertyBrowser/qtworkspaceattribute.h index 957cd515..8ba881c6 100644 --- a/Source/src/ui/PropertyBrowser/qtworkspaceattribute.h +++ b/Source/src/ui/PropertyBrowser/qtworkspaceattribute.h @@ -164,6 +164,12 @@ public: void SetHeight(float h); float GetHeight() const; + void SetLevelCount(int c); + int GetLevelCount() const; + + void SetLevelHeight(float h); + float GetLevelHeight() const; + private: class ConeWaveComponent* object_{ nullptr }; diff --git a/Source/workspace/default2.dyt b/Source/workspace/default2.dyt index 479eea30..f571d5ac 100644 --- a/Source/workspace/default2.dyt +++ b/Source/workspace/default2.dyt @@ -14,12 +14,12 @@ - + - + @@ -29,8 +29,8 @@ - - + +