Skip to content

Commit

Permalink
Merge branch 'range' into 'master'
Browse files Browse the repository at this point in the history
Use a dynamic falloff range for soft effect

See merge request OpenMW/openmw!3532
  • Loading branch information
Capostrophic committed Oct 29, 2023
2 parents 62b787a + 09928ba commit 6d776ac
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion components/nifosg/nifloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2708,7 +2708,8 @@ namespace NifOsg
stateset->setAttributeAndModes(polygonOffset, osg::StateAttribute::ON);
}
if (shaderprop->softEffect())
SceneUtil::setupSoftEffect(*node, shaderprop->mFalloffDepth, true);
SceneUtil::setupSoftEffect(
*node, shaderprop->mFalloffDepth, true, shaderprop->mFalloffDepth);
break;
}
default:
Expand Down
11 changes: 8 additions & 3 deletions components/sceneutil/extradata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

namespace SceneUtil
{
void setupSoftEffect(osg::Node& node, float size, bool falloff)
void setupSoftEffect(osg::Node& node, float size, bool falloff, float falloffDepth)
{
static const osg::ref_ptr<SceneUtil::AutoDepth> depth = new SceneUtil::AutoDepth(osg::Depth::LESS, 0, 1, false);

osg::StateSet* stateset = node.getOrCreateStateSet();

stateset->addUniform(new osg::Uniform("particleSize", size));
stateset->addUniform(new osg::Uniform("particleFade", falloff));
stateset->addUniform(new osg::Uniform("softFalloffDepth", falloffDepth));
stateset->setAttributeAndModes(depth, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

node.setUserValue(Misc::OsgUserValues::sXSoftEffect, true);
Expand All @@ -35,6 +36,8 @@ namespace SceneUtil

std::string source;

constexpr float defaultFalloffDepth = 300.f; // arbitrary value that simply looks good with common cases

if (node.getUserValue(Misc::OsgUserValues::sExtraData, source) && !source.empty())
{
YAML::Node root = YAML::Load(source);
Expand All @@ -47,16 +50,18 @@ namespace SceneUtil
{
auto size = it.second["size"].as<float>(45.f);
auto falloff = it.second["falloff"].as<bool>(false);
auto falloffDepth = it.second["falloffDepth"].as<float>(defaultFalloffDepth);

setupSoftEffect(node, size, falloff);
setupSoftEffect(node, size, falloff, falloffDepth);
}
}

node.setUserValue(Misc::OsgUserValues::sExtraData, std::string{});
}
else if (osgParticle::ParticleSystem* partsys = dynamic_cast<osgParticle::ParticleSystem*>(&node))
{
setupSoftEffect(node, partsys->getDefaultParticleTemplate().getSizeRange().maximum, false);
setupSoftEffect(
node, partsys->getDefaultParticleTemplate().getSizeRange().maximum, false, defaultFalloffDepth);
}

traverse(node);
Expand Down
2 changes: 1 addition & 1 deletion components/sceneutil/extradata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace osg

namespace SceneUtil
{
void setupSoftEffect(osg::Node& node, float size, bool falloff);
void setupSoftEffect(osg::Node& node, float size, bool falloff, float falloffDepth);

class ProcessExtraDataVisitor : public osg::NodeVisitor
{
Expand Down
17 changes: 10 additions & 7 deletions docs/source/reference/modding/custom-shader-effects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ This setting can either be activated in the OpenMW launcher or changed in `setti

Variables.

+---------+--------------------------------------------------------------------------------------------------------+---------+---------+
| Name | Description | Type | Default |
+---------+--------------------------------------------------------------------------------------------------------+---------+---------+
| size | Scaling ratio. Larger values will make a softer fade effect. Larger geometry requires higher values. | integer | 45 |
+---------+--------------------------------------------------------------------------------------------------------+---------+---------+
| falloff | Fades away geometry as camera gets closer. Geometry full fades when parallel to camera. | boolean | false |
+---------+--------------------------------------------------------------------------------------------------------+---------+---------+
+--------------+--------------------------------------------------------------------------------------------------------+---------+---------+
| Name | Description | Type | Default |
+--------------+--------------------------------------------------------------------------------------------------------+---------+---------+
| size | Scaling ratio. Larger values will make a softer fade effect. Larger geometry requires higher values. | integer | 45 |
+--------------+--------------------------------------------------------------------------------------------------------+---------+---------+
| falloff | Fades away geometry as camera gets closer. Geometry full fades when parallel to camera. | boolean | false |
+--------------+--------------------------------------------------------------------------------------------------------+---------+---------+
| falloffDepth | The units at which geometry starts to fade. | float | 300 |
+--------------+--------------------------------------------------------------------------------------------------------+---------+---------+

Example usage.

Expand All @@ -48,6 +50,7 @@ Example usage.
"soft_effect" : {
"size": 250,
"falloff" : false,
"falloffDepth": 5,
}
}
}
4 changes: 3 additions & 1 deletion files/shaders/compatibility/bs/nolighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ uniform float alphaRef;
uniform sampler2D opaqueDepthTex;
uniform float particleSize;
uniform bool particleFade;
uniform float softFalloffDepth;
#endif

void main()
Expand Down Expand Up @@ -71,7 +72,8 @@ void main()
far,
texture2D(opaqueDepthTex, screenCoords).x,
particleSize,
particleFade
particleFade,
softFalloffDepth
);
#endif

Expand Down
4 changes: 3 additions & 1 deletion files/shaders/compatibility/objects.frag
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ varying vec3 passNormal;
uniform sampler2D opaqueDepthTex;
uniform float particleSize;
uniform bool particleFade;
uniform float softFalloffDepth;
#endif

#if @particleOcclusion
Expand Down Expand Up @@ -256,7 +257,8 @@ vec3 viewNormal = normalize(gl_NormalMatrix * normal);
far,
texture2D(opaqueDepthTex, screenCoords).x,
particleSize,
particleFade
particleFade,
softFalloffDepth
);
#endif

Expand Down
6 changes: 3 additions & 3 deletions files/shaders/lib/particle/soft.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ float calcSoftParticleFade(
float far,
float depth,
float size,
bool fade
bool fade,
float softFalloffDepth
)
{
float euclidianDepth = length(viewPos);
Expand All @@ -32,13 +33,12 @@ float calcSoftParticleFade(
float falloff = size * falloffMultiplier;
float delta = particleDepth - sceneDepth;

const float nearMult = 300.0;
float viewBias = 1.0;

if (fade)
{
float VdotN = dot(viewDir, viewNormal);
viewBias = abs(VdotN) * quickstep(euclidianDepth / nearMult) * (1.0 - pow(1.0 + VdotN, 1.3));
viewBias = abs(VdotN) * quickstep(euclidianDepth / softFalloffDepth) * (1.0 - pow(1.0 - abs(VdotN), 1.3));
}

const float shift = 0.845;
Expand Down

0 comments on commit 6d776ac

Please sign in to comment.