Merge pull request #1469 from drummyfish/master

water shader improvements (partially fixes #3365)
experimental
scrawl 7 years ago committed by GitHub
commit 2d873a13ac

@ -279,11 +279,14 @@ namespace MWRender
mViewDistance = Settings::Manager::getFloat("viewing distance", "Camera"); mViewDistance = Settings::Manager::getFloat("viewing distance", "Camera");
mFieldOfView = Settings::Manager::getFloat("field of view", "Camera"); mFieldOfView = Settings::Manager::getFloat("field of view", "Camera");
mFirstPersonFieldOfView = Settings::Manager::getFloat("first person field of view", "Camera"); mFirstPersonFieldOfView = Settings::Manager::getFloat("first person field of view", "Camera");
updateProjectionMatrix();
mStateUpdater->setFogEnd(mViewDistance); mStateUpdater->setFogEnd(mViewDistance);
mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("near", mNearClip)); mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("near", mNearClip));
mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("far", mViewDistance)); mRootNode->getOrCreateStateSet()->addUniform(new osg::Uniform("far", mViewDistance));
mUniformNear = mRootNode->getOrCreateStateSet()->getUniform("near");
mUniformFar = mRootNode->getOrCreateStateSet()->getUniform("far");
updateProjectionMatrix();
} }
RenderingManager::~RenderingManager() RenderingManager::~RenderingManager()
@ -889,6 +892,9 @@ namespace MWRender
if (mFieldOfViewOverridden) if (mFieldOfViewOverridden)
fov = mFieldOfViewOverride; fov = mFieldOfViewOverride;
mViewer->getCamera()->setProjectionMatrixAsPerspective(fov, aspect, mNearClip, mViewDistance); mViewer->getCamera()->setProjectionMatrixAsPerspective(fov, aspect, mNearClip, mViewDistance);
mUniformNear->set(mNearClip);
mUniformFar->set(mViewDistance);
} }
void RenderingManager::updateTextureFiltering() void RenderingManager::updateTextureFiltering()

@ -83,6 +83,9 @@ namespace MWRender
SceneUtil::UnrefQueue* getUnrefQueue(); SceneUtil::UnrefQueue* getUnrefQueue();
Terrain::World* getTerrain(); Terrain::World* getTerrain();
osg::Uniform* mUniformNear;
osg::Uniform* mUniformFar;
void preloadCommonAssets(); void preloadCommonAssets();
double getReferenceTime() const; double getReferenceTime() const;

@ -336,8 +336,7 @@ public:
void setWaterLevel(float waterLevel) void setWaterLevel(float waterLevel)
{ {
setViewMatrix(osg::Matrix::translate(0,0,-waterLevel) * osg::Matrix::scale(1,1,-1) * osg::Matrix::translate(0,0,waterLevel)); setViewMatrix(osg::Matrix::scale(1,1,-1) * osg::Matrix::translate(0,0,2 * waterLevel));
mClipCullNode->setPlane(osg::Plane(osg::Vec3d(0,0,1), osg::Vec3d(0,0,waterLevel))); mClipCullNode->setPlane(osg::Plane(osg::Vec3d(0,0,1), osg::Vec3d(0,0,waterLevel)));
} }

@ -6,7 +6,7 @@
// tweakables -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- // tweakables -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
const float VISIBILITY = 1200.0; // how far you can look through water const float VISIBILITY = 2.5;
const float BIG_WAVES_X = 0.1; // strength of big waves const float BIG_WAVES_X = 0.1; // strength of big waves
const float BIG_WAVES_Y = 0.1; const float BIG_WAVES_Y = 0.1;
@ -31,6 +31,8 @@ const vec3 SUN_EXT = vec3(0.45, 0.55, 0.68); //sunlight extinction
const float SPEC_HARDNESS = 256.0; // specular highlights hardness const float SPEC_HARDNESS = 256.0; // specular highlights hardness
const float BUMP_SUPPRESS_DEPTH = 0.3; // at what water depth bumpmap will be supressed for reflections and refractions (prevents artifacts at shores)
const vec2 WIND_DIR = vec2(0.5f, -0.8f); const vec2 WIND_DIR = vec2(0.5f, -0.8f);
const float WIND_SPEED = 0.2f; const float WIND_SPEED = 0.2f;
@ -56,9 +58,9 @@ float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
return result; return result;
} }
varying vec3 screenCoordsPassthrough; varying vec3 screenCoordsPassthrough;
varying vec4 position; varying vec4 position;
varying float depthPassthrough; varying float depthPassthrough;
uniform sampler2D normalMap; uniform sampler2D normalMap;
@ -74,8 +76,18 @@ uniform float near;
uniform float far; uniform float far;
uniform vec3 nodePosition; uniform vec3 nodePosition;
float frustumDepth;
float linearizeDepth(float depth) // takes <0,1> non-linear depth value and returns <0,1> linearized value
{
float z_n = 2.0 * depth - 1.0;
depth = 2.0 * near * far / (far + near - z_n * frustumDepth);
return depth / frustumDepth;
}
void main(void) void main(void)
{ {
frustumDepth = abs(far - near);
vec3 worldPos = position.xyz + nodePosition.xyz; vec3 worldPos = position.xyz + nodePosition.xyz;
vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0; vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0;
UV.y *= -1.0; UV.y *= -1.0;
@ -147,32 +159,37 @@ void main(void)
fresnel = clamp(fresnel, 0.0, 1.0); fresnel = clamp(fresnel, 0.0, 1.0);
#if REFRACTION
float normalization = frustumDepth / 1000;
float depthSample = linearizeDepth(texture2D(refractionDepthMap,screenCoords).x) * normalization;
float depthSampleDistorted = linearizeDepth(texture2D(refractionDepthMap,screenCoords-(normal.xy*REFR_BUMP)).x) * normalization;
float surfaceDepth = linearizeDepth(gl_FragCoord.z) * normalization;
float realWaterDepth = depthSample - surfaceDepth; // undistorted water depth in view direction, independent of frustum
float shore = clamp(realWaterDepth / BUMP_SUPPRESS_DEPTH,0,1);
#else
float shore = 1.0;
#endif
// reflection // reflection
vec3 reflection = texture2D(reflectionMap, screenCoords+(normal.xy*REFL_BUMP)).rgb; vec3 reflection = texture2D(reflectionMap, screenCoords+(normal.xy*REFL_BUMP*shore)).rgb;
// refraction // refraction
#if REFRACTION #if REFRACTION
vec3 refraction = texture2D(refractionMap, screenCoords-(normal.xy*REFR_BUMP)).rgb; vec3 refraction = texture2D(refractionMap, screenCoords-(normal.xy*REFR_BUMP*shore)).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;
#endif #endif
// specular // specular
vec3 R = reflect(vVec, normal); vec3 R = reflect(vVec, normal);
float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS) * shadow; float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS) * shadow;
vec3 waterColor = WATER_COLOR; vec3 waterColor = WATER_COLOR;
waterColor = waterColor * length(gl_LightModel.ambient.xyz); waterColor = waterColor * length(gl_LightModel.ambient.xyz);
#if REFRACTION
float refractionDepth = texture2D(refractionDepthMap, screenCoords-(normal.xy*REFR_BUMP)).x;
float z_n = 2.0 * refractionDepth - 1.0;
refractionDepth = 2.0 * near * far / (far + near - z_n * (far - near));
float waterDepth = refractionDepth - depthPassthrough;
#if REFRACTION
if (cameraPos.z > 0.0) if (cameraPos.z > 0.0)
refraction = mix(refraction, waterColor, clamp(waterDepth/VISIBILITY, 0.0, 1.0)); refraction = mix(refraction, waterColor, clamp(depthSampleDistorted/VISIBILITY, 0.0, 1.0));
gl_FragData[0].xyz = mix( mix(refraction, scatterColour, lightScatter), reflection, fresnel) + specular * gl_LightSource[0].specular.xyz; gl_FragData[0].xyz = mix( mix(refraction, scatterColour, lightScatter), reflection, fresnel) + specular * gl_LightSource[0].specular.xyz;
#else #else

Loading…
Cancel
Save