Merge pull request #1483 from drummyfish/master

water shader improvements plus rain ripple effect (#452)
new-script-api
scrawl 7 years ago committed by GitHub
commit 05348ab7ed

@ -251,6 +251,10 @@ namespace MWRender
sceneRoot->setNodeMask(Mask_Scene); sceneRoot->setNodeMask(Mask_Scene);
sceneRoot->setName("Scene Root"); sceneRoot->setName("Scene Root");
mUniformRainIntensity = new osg::Uniform("rainIntensity",(float) 0.0);
mRootNode->getOrCreateStateSet()->addUniform(mUniformRainIntensity);
mSky.reset(new SkyManager(sceneRoot, resourceSystem->getSceneManager())); mSky.reset(new SkyManager(sceneRoot, resourceSystem->getSceneManager()));
source->setStateSetModes(*mRootNode->getOrCreateStateSet(), osg::StateAttribute::ON); source->setStateSetModes(*mRootNode->getOrCreateStateSet(), osg::StateAttribute::ON);
@ -799,6 +803,7 @@ namespace MWRender
{ {
mEffectManager->clear(); mEffectManager->clear();
mWater->clearRipples(); mWater->clearRipples();
mUniformRainIntensity->set((float) 0.0); // for interiors
} }
void RenderingManager::clear() void RenderingManager::clear()

@ -85,6 +85,7 @@ namespace MWRender
osg::Uniform* mUniformNear; osg::Uniform* mUniformNear;
osg::Uniform* mUniformFar; osg::Uniform* mUniformFar;
osg::Uniform* mUniformRainIntensity;
void preloadCommonAssets(); void preloadCommonAssets();

@ -1305,13 +1305,19 @@ public:
std::vector<osg::ref_ptr<AlphaFader> > mAlphaFaders; std::vector<osg::ref_ptr<AlphaFader> > mAlphaFaders;
}; };
private: protected:
float mAlpha; float mAlpha;
}; };
class RainFader : public AlphaFader class RainFader : public AlphaFader
{ {
public: public:
RainFader(osg::Uniform *rainIntensityUniform): AlphaFader()
{
mRainIntensityUniform = rainIntensityUniform;
}
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);
@ -1320,6 +1326,15 @@ public:
mat->setColorMode(osg::Material::OFF); 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)
{
AlphaFader::apply(stateset,nv);
mRainIntensityUniform->set((float) (mAlpha * 2.0)); // mAlpha is limited to 0.6 so multiply by 2 to reach full intensity
}
protected:
osg::Uniform* mRainIntensityUniform;
}; };
void SkyManager::createRain() void SkyManager::createRain()
@ -1375,7 +1390,7 @@ void SkyManager::createRain()
mRainNode->addChild(mRainParticleSystem); mRainNode->addChild(mRainParticleSystem);
mRainNode->addChild(updater); mRainNode->addChild(updater);
mRainFader = new RainFader; mRainFader = new RainFader(mRootNode->getParent(0)->getParent(0)->getStateSet()->getUniform("rainIntensity"));
mRainNode->addUpdateCallback(mRainFader); mRainNode->addUpdateCallback(mRainFader);
mRainNode->addCullCallback(mUnderwaterSwitch); mRainNode->addCullCallback(mUnderwaterSwitch);
mRainNode->setNodeMask(Mask_WeatherParticles); mRainNode->setNodeMask(Mask_WeatherParticles);

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <iostream>
#include <osg/ref_ptr> #include <osg/ref_ptr>
#include <osg/Vec4f> #include <osg/Vec4f>

@ -38,10 +38,69 @@ const float WIND_SPEED = 0.2f;
const vec3 WATER_COLOR = vec3(0.090195, 0.115685, 0.12745); const vec3 WATER_COLOR = vec3(0.090195, 0.115685, 0.12745);
// ---------------- rain ripples related stuff ---------------------
const float RAIN_RIPPLE_GAPS = 5.0;
const float RAIN_RIPPLE_RADIUS = 0.1;
int modulo(int v1, int v2)
{
return v1 - v2 * int(floor(float(v1) / float(v2)));
}
vec2 randOffset(vec2 c)
{
return fract(vec2(
c.x * c.y / 8.0 + c.y * 0.3 + c.x * 0.2,
c.x * c.y / 14.0 + c.y * 0.5 + c.x * 0.7));
}
float randPhase(vec2 c)
{
return fract((c.x * c.y) / (c.x + c.y + 0.1));
}
vec4 circle(vec2 coords, vec2 i_part, float phase)
{
vec2 center = vec2(0.5,0.5) + (0.5 - RAIN_RIPPLE_RADIUS) * (2.0 * randOffset(i_part) - 1.0);
vec2 toCenter = coords - center;
float d = length(toCenter);
float r = RAIN_RIPPLE_RADIUS * phase;
if (d > r)
return vec4(0.0,0.0,1.0,0.0);
float sinValue = (sin(d / r * 1.2) + 0.7) / 2.0;
float height = (1.0 - abs(phase)) * pow(sinValue,3.0);
vec3 normal = normalize(mix(vec3(0.0,0.0,1.0),vec3(normalize(toCenter),0.0),height));
return vec4(normal,height);
}
vec4 rain(vec2 uv, float time)
{
vec2 i_part = floor(uv * RAIN_RIPPLE_GAPS);
vec2 f_part = fract(uv * RAIN_RIPPLE_GAPS);
return circle(f_part,i_part,fract(time * 1.2 + randPhase(i_part)));
}
vec4 rainCombined(vec2 uv, float time) // returns ripple normal in xyz and ripple height in w
{
return
rain(uv,time) +
rain(uv + vec2(10.5,5.7),time) +
rain(uv * 0.75 + vec2(3.7,18.9),time) +
rain(uv * 0.9 + vec2(5.7,30.1),time) +
rain(uv * 0.8 + vec2(1.2,3.0),time);
}
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta) float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
{ {
float c = abs(dot(Incoming, Normal)); float c = abs(dot(Incoming, Normal));
float g = eta * eta - 1.0 + c * c; float g = eta * eta - 1.0 + c * c;
float result; float result;
@ -56,7 +115,12 @@ float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
result = 1.0; /* TIR (no refracted component) */ result = 1.0; /* TIR (no refracted component) */
return result; return result;
} }
vec2 normalCoords(vec2 uv, float scale, float speed, float time, float timer1, float timer2, vec3 previousNormal)
{
return uv * (WAVE_SCALE * scale) + WIND_DIR * time * (WIND_SPEED * speed) -(previousNormal.xy/previousNormal.zz) * WAVE_CHOPPYNESS + vec2(time * timer1,time * timer2);
}
varying vec3 screenCoordsPassthrough; varying vec3 screenCoordsPassthrough;
varying vec4 position; varying vec4 position;
@ -76,6 +140,8 @@ uniform float near;
uniform float far; uniform float far;
uniform vec3 nodePosition; uniform vec3 nodePosition;
uniform float rainIntensity;
float frustumDepth; float frustumDepth;
float linearizeDepth(float depth) // takes <0,1> non-linear depth value and returns <0,1> linearized value float linearizeDepth(float depth) // takes <0,1> non-linear depth value and returns <0,1> linearized value
@ -101,26 +167,26 @@ void main(void)
#define waterTimer osg_SimulationTime #define waterTimer osg_SimulationTime
nCoord = UV * (WAVE_SCALE * 0.05) + WIND_DIR * waterTimer * (WIND_SPEED*0.04); vec3 normal0 = 2.0 * texture2D(normalMap,normalCoords(UV, 0.05, 0.04, waterTimer, -0.015, -0.005, vec3(0.0,0.0,0.0))).rgb - 1.0;
vec3 normal0 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.015,-waterTimer*0.005)).rgb - 1.0; vec3 normal1 = 2.0 * texture2D(normalMap,normalCoords(UV, 0.1, 0.08, waterTimer, 0.02, 0.015, normal0)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.1) + WIND_DIR * waterTimer * (WIND_SPEED*0.08)-(normal0.xy/normal0.zz)*WAVE_CHOPPYNESS; vec3 normal2 = 2.0 * texture2D(normalMap,normalCoords(UV, 0.25, 0.07, waterTimer, -0.04, -0.03, normal1)).rgb - 1.0;
vec3 normal1 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.020,+waterTimer*0.015)).rgb - 1.0; vec3 normal3 = 2.0 * texture2D(normalMap,normalCoords(UV, 0.5, 0.09, waterTimer, 0.03, 0.04, normal2)).rgb - 1.0;
vec3 normal4 = 2.0 * texture2D(normalMap,normalCoords(UV, 1.0, 0.4, waterTimer, -0.02, 0.1, normal3)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.25) + WIND_DIR * waterTimer * (WIND_SPEED*0.07)-(normal1.xy/normal1.zz)*WAVE_CHOPPYNESS; vec3 normal5 = 2.0 * texture2D(normalMap,normalCoords(UV, 2.0, 0.7, waterTimer, 0.1, -0.06, normal4)).rgb - 1.0;
vec3 normal2 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.04,-waterTimer*0.03)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.5) + WIND_DIR * waterTimer * (WIND_SPEED*0.09)-(normal2.xy/normal2.z)*WAVE_CHOPPYNESS;
vec3 normal3 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.03,+waterTimer*0.04)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE* 1.0) + WIND_DIR * waterTimer * (WIND_SPEED*0.4)-(normal3.xy/normal3.zz)*WAVE_CHOPPYNESS;
vec3 normal4 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.02,+waterTimer*0.1)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 2.0) + WIND_DIR * waterTimer * (WIND_SPEED*0.7)-(normal4.xy/normal4.zz)*WAVE_CHOPPYNESS;
vec3 normal5 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.1,-waterTimer*0.06)).rgb - 1.0;
vec4 rainRipple;
if (rainIntensity > 0.01)
rainRipple = rainCombined(position.xy / 1000.0,waterTimer) * clamp(rainIntensity,0.0,1.0);
else
rainRipple = vec4(0.0,0.0,0.0,0.0);
vec3 rippleAdd = rainRipple.xyz * rainRipple.w * 10.0;
vec3 normal = (normal0 * BIG_WAVES_X + normal1 * BIG_WAVES_Y + vec3 normal = (normal0 * BIG_WAVES_X + normal1 * BIG_WAVES_Y +
normal2 * MID_WAVES_X + normal3 * MID_WAVES_Y + normal2 * MID_WAVES_X + normal3 * MID_WAVES_Y +
normal4 * SMALL_WAVES_X + normal5 * SMALL_WAVES_Y); normal4 * SMALL_WAVES_X + normal5 * SMALL_WAVES_Y +
rippleAdd);
normal = normalize(vec3(normal.x * BUMP, normal.y * BUMP, normal.z)); normal = normalize(vec3(normal.x * BUMP, normal.y * BUMP, normal.z));
@ -129,11 +195,12 @@ void main(void)
// normal for sunlight scattering // normal for sunlight scattering
vec3 lNormal = (normal0 * BIG_WAVES_X*0.5 + normal1 * BIG_WAVES_Y*0.5 + vec3 lNormal = (normal0 * BIG_WAVES_X*0.5 + normal1 * BIG_WAVES_Y*0.5 +
normal2 * MID_WAVES_X*0.2 + normal3 * MID_WAVES_Y*0.2 + normal2 * MID_WAVES_X*0.2 + normal3 * MID_WAVES_Y*0.2 +
normal4 * SMALL_WAVES_X*0.1 + normal5 * SMALL_WAVES_Y*0.1).xyz; normal4 * SMALL_WAVES_X*0.1 + normal5 * SMALL_WAVES_Y*0.1 +
rippleAdd).xyz;
lNormal = normalize(vec3(lNormal.x * BUMP, lNormal.y * BUMP, lNormal.z)); lNormal = normalize(vec3(lNormal.x * BUMP, lNormal.y * BUMP, lNormal.z));
lNormal = vec3(-lNormal.x, -lNormal.y, lNormal.z); lNormal = vec3(-lNormal.x, -lNormal.y, lNormal.z);
vec3 lVec = normalize((gl_ModelViewMatrixInverse * vec4(gl_LightSource[0].position.xyz, 0.0)).xyz); vec3 lVec = normalize((gl_ModelViewMatrixInverse * vec4(gl_LightSource[0].position.xyz, 0.0)).xyz);
vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0,0,0,1)).xyz; vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0,0,0,1)).xyz;
@ -151,10 +218,10 @@ void main(void)
float s = clamp(dot(lR, vVec)*2.0-1.2, 0.0, 1.0); float s = clamp(dot(lR, vVec)*2.0-1.2, 0.0, 1.0);
float lightScatter = shadow * clamp(dot(lVec,lNormal)*0.7+0.3, 0.0, 1.0) * s * SCATTER_AMOUNT * sunFade * clamp(1.0-exp(-sunHeight), 0.0, 1.0); float lightScatter = shadow * clamp(dot(lVec,lNormal)*0.7+0.3, 0.0, 1.0) * s * SCATTER_AMOUNT * sunFade * clamp(1.0-exp(-sunHeight), 0.0, 1.0);
vec3 scatterColour = mix(vec3(SCATTER_COLOUR)*vec3(1.0,0.4,0.0), SCATTER_COLOUR, clamp(1.0-exp(-sunHeight*SUN_EXT), 0.0, 1.0)); vec3 scatterColour = mix(vec3(SCATTER_COLOUR)*vec3(1.0,0.4,0.0), SCATTER_COLOUR, clamp(1.0-exp(-sunHeight*SUN_EXT), 0.0, 1.0));
// fresnel // fresnel
float ior = (cameraPos.z>0.0)?(1.333/1.0):(1.0/1.333); //air to water; water to air float ior = (cameraPos.z>0.0)?(1.333/1.0):(1.0/1.333); // air to water; water to air
float fresnel = fresnel_dielectric(vVec, normal, ior); float fresnel = fresnel_dielectric(vVec, normal, ior);
fresnel = clamp(fresnel, 0.0, 1.0); fresnel = clamp(fresnel, 0.0, 1.0);
@ -164,18 +231,19 @@ void main(void)
float depthSample = linearizeDepth(texture2D(refractionDepthMap,screenCoords).x) * normalization; float depthSample = linearizeDepth(texture2D(refractionDepthMap,screenCoords).x) * normalization;
float depthSampleDistorted = linearizeDepth(texture2D(refractionDepthMap,screenCoords-(normal.xy*REFR_BUMP)).x) * normalization; float depthSampleDistorted = linearizeDepth(texture2D(refractionDepthMap,screenCoords-(normal.xy*REFR_BUMP)).x) * normalization;
float surfaceDepth = linearizeDepth(gl_FragCoord.z) * normalization; float surfaceDepth = linearizeDepth(gl_FragCoord.z) * normalization;
float realWaterDepth = depthSample - surfaceDepth; // undistorted water depth in view direction, independent of frustum float realWaterDepth = depthSample - surfaceDepth; // undistorted water depth in view direction, independent of frustum
float shore = clamp(realWaterDepth / BUMP_SUPPRESS_DEPTH,0,1); float shore = clamp(realWaterDepth / BUMP_SUPPRESS_DEPTH,0,1);
#else #else
float shore = 1.0; float shore = 1.0;
#endif #endif
vec2 screenCoordsOffset = normal.xy * REFL_BUMP * shore;
// reflection // reflection
vec3 reflection = texture2D(reflectionMap, screenCoords+(normal.xy*REFL_BUMP*shore)).rgb; vec3 reflection = texture2D(reflectionMap, screenCoords + screenCoordsOffset).rgb;
// refraction // refraction
#if REFRACTION #if REFRACTION
vec3 refraction = texture2D(refractionMap, screenCoords-(normal.xy*REFR_BUMP*shore)).rgb; vec3 refraction = texture2D(refractionMap, screenCoords - screenCoordsOffset).rgb;
// brighten up the refraction underwater // brighten up the refraction underwater
refraction = (cameraPos.z < 0.0) ? clamp(refraction * 1.5, 0.0, 1.0) : refraction; refraction = (cameraPos.z < 0.0) ? clamp(refraction * 1.5, 0.0, 1.0) : refraction;
@ -195,14 +263,19 @@ void main(void)
#else #else
gl_FragData[0].xyz = mix(reflection, waterColor, (1.0-fresnel)*0.5) + specular * gl_LightSource[0].specular.xyz; gl_FragData[0].xyz = mix(reflection, waterColor, (1.0-fresnel)*0.5) + specular * gl_LightSource[0].specular.xyz;
#endif #endif
// fog // fog
float fogValue = clamp((depthPassthrough - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0); float fogValue = clamp((depthPassthrough - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue); gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue);
#if REFRACTION
gl_FragData[0].xyz += vec3(rainRipple.w) * 0.2;
#else
gl_FragData[0].xyz += vec3(rainRipple.w) * 0.7;
#endif
#if REFRACTION #if REFRACTION
gl_FragData[0].w = 1.0; gl_FragData[0].w = 1.0;
#else #else
gl_FragData[0].w = clamp(fresnel*2.0 + specular, 0.0, 1.0); gl_FragData[0].w = clamp(fresnel*6.0 + specular, 0.0, 1.0); //clamp(fresnel*2.0 + specular, 0.0, 1.0);
#endif #endif
} }

Loading…
Cancel
Save