1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 23:23:52 +00:00

Fading for weather particle effects

This commit is contained in:
scrawl 2015-06-29 20:19:46 +02:00
parent bbf4927a10
commit 017c9f7ac9
2 changed files with 70 additions and 13 deletions

View file

@ -687,10 +687,11 @@ private:
float mAngle; float mAngle;
}; };
class RainFader : public SceneUtil::StateSetUpdater // Updater for alpha value on a node's StateSet. Assumes the node has an existing Material StateAttribute.
class AlphaFader : public SceneUtil::StateSetUpdater
{ {
public: public:
RainFader() AlphaFader()
: mAlpha(1.f) : mAlpha(1.f)
{ {
} }
@ -700,20 +701,71 @@ public:
mAlpha = alpha; mAlpha = alpha;
} }
virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv)
{
osg::Material* mat = static_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL));
mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,mAlpha));
}
// Helper for adding AlphaFader to a subgraph
class SetupVisitor : public osg::NodeVisitor
{
public:
SetupVisitor()
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
mAlphaFader = new AlphaFader;
}
virtual void apply(osg::Node &node)
{
if (osg::StateSet* stateset = node.getStateSet())
{
if (stateset->getAttribute(osg::StateAttribute::MATERIAL))
{
SceneUtil::CompositeStateSetUpdater* composite = NULL;
osg::NodeCallback* callback = node.getUpdateCallback();
while (callback)
{
if ((composite = dynamic_cast<SceneUtil::CompositeStateSetUpdater*>(callback)))
break;
callback = callback->getNestedCallback();
}
if (composite)
composite->addController(mAlphaFader);
else
node.addUpdateCallback(mAlphaFader);
}
}
traverse(node);
}
osg::ref_ptr<AlphaFader> getAlphaFader()
{
return mAlphaFader;
}
private:
osg::ref_ptr<AlphaFader> mAlphaFader;
};
private:
float mAlpha;
};
class RainFader : public AlphaFader
{
public:
virtual void setDefaults(osg::StateSet* stateset) virtual void setDefaults(osg::StateSet* stateset)
{ {
osg::ref_ptr<osg::Material> mat (new osg::Material); osg::ref_ptr<osg::Material> mat (new osg::Material);
mat->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1)); mat->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1));
mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,1)); mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,1));
mat->setColorMode(osg::Material::OFF);
stateset->setAttributeAndModes(mat, osg::StateAttribute::ON); stateset->setAttributeAndModes(mat, osg::StateAttribute::ON);
} }
virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv)
{
osg::Material* mat = static_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL));
mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0,0,0,mAlpha));
}
private: private:
float mAlpha; float mAlpha;
}; };
@ -928,6 +980,7 @@ void SkyManager::setWeather(const MWWorld::WeatherResult& weather)
mParticleNode = NULL; mParticleNode = NULL;
} }
mParticleEffect = NULL; mParticleEffect = NULL;
mParticleFader = NULL;
} }
else else
{ {
@ -940,6 +993,10 @@ void SkyManager::setWeather(const MWWorld::WeatherResult& weather)
SceneUtil::AssignControllerSourcesVisitor assignVisitor(boost::shared_ptr<SceneUtil::ControllerSource>(new SceneUtil::FrameTimeSource)); SceneUtil::AssignControllerSourcesVisitor assignVisitor(boost::shared_ptr<SceneUtil::ControllerSource>(new SceneUtil::FrameTimeSource));
mParticleEffect->accept(assignVisitor); mParticleEffect->accept(assignVisitor);
AlphaFader::SetupVisitor alphaFaderSetupVisitor;
mParticleEffect->accept(alphaFaderSetupVisitor);
mParticleFader = alphaFaderSetupVisitor.getAlphaFader();
} }
} }
@ -1025,14 +1082,12 @@ void SkyManager::setWeather(const MWWorld::WeatherResult& weather)
mSunGlare->setVisibility(weather.mGlareView * mGlareFade * strength); mSunGlare->setVisibility(weather.mGlareView * mGlareFade * strength);
mSun->setVisibility(weather.mGlareView * strength); mSun->setVisibility(weather.mGlareView * strength);
if (mParticle.get())
setAlpha(mParticle, weather.mEffectFade);
*/ */
if (mRainFader) if (mRainFader)
mRainFader->setAlpha(weather.mEffectFade * 0.6); // * Rain_Threshold? mRainFader->setAlpha(weather.mEffectFade * 0.6); // * Rain_Threshold?
if (mParticleFader)
mParticleFader->setAlpha(weather.mEffectFade);
} }
void SkyManager::setGlare(const float glare) void SkyManager::setGlare(const float glare)

View file

@ -31,6 +31,7 @@ namespace MWRender
class Moon; class Moon;
class RainShooter; class RainShooter;
class RainFader; class RainFader;
class AlphaFader;
class SkyManager class SkyManager
{ {
@ -104,6 +105,7 @@ namespace MWRender
osg::ref_ptr<osg::PositionAttitudeTransform> mParticleNode; osg::ref_ptr<osg::PositionAttitudeTransform> mParticleNode;
osg::ref_ptr<osg::Node> mParticleEffect; osg::ref_ptr<osg::Node> mParticleEffect;
osg::ref_ptr<AlphaFader> mParticleFader;
osg::ref_ptr<osg::PositionAttitudeTransform> mCloudNode; osg::ref_ptr<osg::PositionAttitudeTransform> mCloudNode;