new water WIP, caustics, chromatic abberation, accurate fresnel, underwater reflection, etc
parent
4434fb640e
commit
739455e6f8
@ -1 +1 @@
|
||||
Subproject commit 5a9bda6010413555736479ef03103f764fecb91d
|
||||
Subproject commit bf003238a27d94be43724e6774d25c38b4d578c8
|
@ -0,0 +1,117 @@
|
||||
#define VISIBILITY 1500.0 // how far you can look through water
|
||||
|
||||
#define BIG_WAVES_X 0.3 // strength of big waves
|
||||
#define BIG_WAVES_Y 0.3
|
||||
|
||||
#define MID_WAVES_X 0.3 // strength of middle sized waves
|
||||
#define MID_WAVES_Y 0.15
|
||||
|
||||
#define SMALL_WAVES_X 0.15 // strength of small waves
|
||||
#define SMALL_WAVES_Y 0.1
|
||||
|
||||
#define WAVE_CHOPPYNESS 0.15 // wave choppyness
|
||||
#define WAVE_SCALE 0.01 // overall wave scale
|
||||
|
||||
#define ABBERATION 0.001 // chromatic abberation amount
|
||||
|
||||
float3 intercept(float3 lineP,
|
||||
float3 lineN,
|
||||
float3 planeN,
|
||||
float planeD)
|
||||
{
|
||||
|
||||
float distance = (planeD - dot(planeN, lineP)) / dot(lineN, planeN);
|
||||
return lineP + lineN * distance;
|
||||
}
|
||||
|
||||
float3 perturb1(shTexture2D tex, float2 coords, float bend, float2 windDir, float windSpeed, float timer)
|
||||
{
|
||||
float2 nCoord = float2(0.0);
|
||||
bend *= WAVE_CHOPPYNESS;
|
||||
nCoord = coords * (WAVE_SCALE * 0.05) + windDir * timer * (windSpeed*0.04);
|
||||
float3 normal0 = 2.0 * shSample(tex, nCoord + float2(-timer*0.015,-timer*0.05)).rgb - 1.0;
|
||||
nCoord = coords * (WAVE_SCALE * 0.1) + windDir * timer * (windSpeed*0.08)-normal0.xy*bend;
|
||||
float3 normal1 = 2.0 * shSample(tex, nCoord + float2(+timer*0.020,+timer*0.015)).rgb - 1.0;
|
||||
|
||||
nCoord = coords * (WAVE_SCALE * 0.25) + windDir * timer * (windSpeed*0.07)-normal1.xy*bend;
|
||||
float3 normal2 = 2.0 * shSample(tex, nCoord + float2(-timer*0.04,-timer*0.03)).rgb - 1.0;
|
||||
nCoord = coords * (WAVE_SCALE * 0.5) + windDir * timer * (windSpeed*0.09)-normal2.xy*bend;
|
||||
float3 normal3 = 2.0 * shSample(tex, nCoord + float2(+timer*0.03,+timer*0.04)).rgb - 1.0;
|
||||
|
||||
nCoord = coords * (WAVE_SCALE* 1.0) + windDir * timer * (windSpeed*0.4)-normal3.xy*bend;
|
||||
float3 normal4 = 2.0 * shSample(tex, nCoord + float2(-timer*0.2,+timer*0.1)).rgb - 1.0;
|
||||
nCoord = coords * (WAVE_SCALE * 2.0) + windDir * timer * (windSpeed*0.7)-normal4.xy*bend;
|
||||
float3 normal5 = 2.0 * shSample(tex, nCoord + float2(+timer*0.1,-timer*0.06)).rgb - 1.0;
|
||||
|
||||
|
||||
float3 normal = normalize(normal0 * BIG_WAVES_X + normal1 * BIG_WAVES_Y +
|
||||
normal2 * MID_WAVES_X + normal3 * MID_WAVES_Y +
|
||||
normal4 * SMALL_WAVES_X + normal5 * SMALL_WAVES_Y);
|
||||
return normal;
|
||||
}
|
||||
|
||||
float3 perturb(shTexture2D tex, float2 coords, float bend, float2 windDir, float windSpeed, float timer)
|
||||
{
|
||||
bend *= WAVE_CHOPPYNESS;
|
||||
float3 col = float3(0.0);
|
||||
float2 nCoord = float2(0.0); //normal coords
|
||||
|
||||
nCoord = coords * (WAVE_SCALE * 0.025) + windDir * timer * (windSpeed*0.03);
|
||||
col += shSample(tex,nCoord + float2(-timer*0.005,-timer*0.01)).rgb*0.20;
|
||||
nCoord = coords * (WAVE_SCALE * 0.1) + windDir * timer * (windSpeed*0.05)-(col.xy/col.zz)*bend;
|
||||
col += shSample(tex,nCoord + float2(+timer*0.01,+timer*0.005)).rgb*0.20;
|
||||
|
||||
nCoord = coords * (WAVE_SCALE * 0.2) + windDir * timer * (windSpeed*0.1)-(col.xy/col.zz)*bend;
|
||||
col += shSample(tex,nCoord + float2(-timer*0.02,-timer*0.03)).rgb*0.20;
|
||||
nCoord = coords * (WAVE_SCALE * 0.5) + windDir * timer * (windSpeed*0.2)-(col.xy/col.zz)*bend;
|
||||
col += shSample(tex,nCoord + float2(+timer*0.03,+timer*0.02)).rgb*0.15;
|
||||
|
||||
nCoord = coords * (WAVE_SCALE* 0.8) + windDir * timer * (windSpeed*1.0)-(col.xy/col.zz)*bend;
|
||||
col += shSample(tex, nCoord + float2(-timer*0.06,+timer*0.08)).rgb*0.15;
|
||||
nCoord = coords * (WAVE_SCALE * 1.0) + windDir * timer * (windSpeed*1.3)-(col.xy/col.zz)*bend;
|
||||
col += shSample(tex,nCoord + float2(+timer*0.08,-timer*0.06)).rgb*0.10;
|
||||
|
||||
return col;
|
||||
}
|
||||
|
||||
|
||||
float3 getCaustics (shTexture2D causticMap, float3 worldPos, float3 eyePosWS, float3 worldNormal, float3 lightDirectionWS0, float waterLevel, float waterTimer, float3 windDir_windSpeed)
|
||||
{
|
||||
float3 waterEyePos = intercept(worldPos.xyz, eyePosWS - worldPos, float3(0,1,0), waterLevel);
|
||||
float waterDepth = shSaturate((waterEyePos.y - worldPos.y) / 50.0);
|
||||
|
||||
float3 causticPos = intercept(worldPos.xyz, lightDirectionWS0.xyz, float3(0,1,0), waterLevel);
|
||||
|
||||
///\ todo clean this up
|
||||
float causticdepth = length(causticPos-worldPos.xyz);
|
||||
causticdepth = 1.0-shSaturate(causticdepth / VISIBILITY);
|
||||
causticdepth = shSaturate(causticdepth);
|
||||
|
||||
// NOTE: the original shader calculated a tangent space basis here,
|
||||
// but using only the world normal is cheaper and i couldn't see a visual difference
|
||||
// also, if this effect gets moved to screen-space some day, it's unlikely to have tangent information
|
||||
float3 causticNorm = worldNormal.xyz * perturb(causticMap, causticPos.xz, causticdepth, windDir_windSpeed.xy, windDir_windSpeed.z, waterTimer).xzy * 2 - 1;
|
||||
|
||||
//float fresnel = pow(clamp(dot(LV,causticnorm),0.0,1.0),2.0);
|
||||
|
||||
float NdotL = max(dot(worldNormal.xyz, lightDirectionWS0.xyz),0.0);
|
||||
|
||||
float causticR = 1.0-perturb(causticMap, causticPos.xz, causticdepth, windDir_windSpeed.xy, windDir_windSpeed.z, waterTimer).z;
|
||||
|
||||
/// \todo sunFade
|
||||
|
||||
// float3 caustics = clamp(pow(float3(causticR)*5.5,float3(5.5*causticdepth)),0.0,1.0)*NdotL*sunFade*causticdepth;
|
||||
float3 caustics = clamp(pow(float3(causticR)*5.5,float3(5.5*causticdepth)),0.0,1.0)*NdotL*causticdepth;
|
||||
float causticG = 1.0-perturb(causticMap,causticPos.xz+(1.0-causticdepth)*ABBERATION, causticdepth, windDir_windSpeed.xy, windDir_windSpeed.z, waterTimer).z;
|
||||
float causticB = 1.0-perturb(causticMap,causticPos.xz+(1.0-causticdepth)*ABBERATION*2.0, causticdepth, windDir_windSpeed.xy, windDir_windSpeed.z, waterTimer).z;
|
||||
//caustics = shSaturate(pow(float3(causticR,causticG,causticB)*5.5,float3(5.5*causticdepth)))*NdotL*sunFade*causticdepth;
|
||||
caustics = shSaturate(pow(float3(causticR,causticG,causticB)*5.5,float3(5.5*causticdepth)))*NdotL*causticdepth;
|
||||
|
||||
caustics *= 3;
|
||||
|
||||
// shore transition
|
||||
caustics = shLerp (float3(1,1,1), caustics, waterDepth);
|
||||
|
||||
return caustics;
|
||||
}
|
||||
|
@ -1,30 +1,33 @@
|
||||
// note: the fixed function water is created manually, not here
|
||||
|
||||
material openmw_water
|
||||
material Water
|
||||
{
|
||||
pass
|
||||
{
|
||||
vertex_program water_vertex
|
||||
fragment_program water_fragment
|
||||
|
||||
cull_hardware none
|
||||
|
||||
texture_unit reflectionMap
|
||||
{
|
||||
texture_alias WaterReflection
|
||||
tex_address_mode clamp
|
||||
}
|
||||
|
||||
texture_unit refractionMap
|
||||
{
|
||||
texture_alias WaterRefraction
|
||||
tex_address_mode clamp
|
||||
}
|
||||
|
||||
texture_unit depthMap
|
||||
{
|
||||
|
||||
texture_alias SceneDepth
|
||||
tex_address_mode clamp
|
||||
}
|
||||
|
||||
texture_unit normalMap
|
||||
{
|
||||
texture
|
||||
direct_texture water_nm.png
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,228 @@
|
||||
#include "core.h"
|
||||
|
||||
// Inspired by Blender GLSL Water by martinsh ( http://devlog-martinsh.blogspot.de/2012/07/waterundewater-shader-wip.html )
|
||||
|
||||
#ifdef SH_VERTEX_SHADER
|
||||
|
||||
SH_BEGIN_PROGRAM
|
||||
shUniform(float4x4, wvp) @shAutoConstant(wvp, worldviewproj_matrix)
|
||||
shInput(float2, uv0)
|
||||
shOutput(float2, UV)
|
||||
|
||||
shOutput(float3, screenCoordsPassthrough)
|
||||
shOutput(float4, position)
|
||||
shOutput(float, depthPassthrough)
|
||||
|
||||
SH_START_PROGRAM
|
||||
{
|
||||
shOutputPosition = shMatrixMult(wvp, shInputPosition);
|
||||
UV = uv0;
|
||||
|
||||
|
||||
#if !SH_GLSL
|
||||
float4x4 scalemat = float4x4( 0.5, 0, 0, 0.5,
|
||||
0, -0.5, 0, 0.5,
|
||||
0, 0, 0.5, 0.5,
|
||||
0, 0, 0, 1 );
|
||||
#else
|
||||
mat4 scalemat = mat4(0.5, 0.0, 0.0, 0.0,
|
||||
0.0, -0.5, 0.0, 0.0,
|
||||
0.0, 0.0, 0.5, 0.0,
|
||||
0.5, 0.5, 0.5, 1.0);
|
||||
#endif
|
||||
|
||||
float4 texcoordProj = shMatrixMult(scalemat, shOutputPosition);
|
||||
screenCoordsPassthrough = float3(texcoordProj.x, texcoordProj.y, texcoordProj.w);
|
||||
|
||||
position = shInputPosition;
|
||||
|
||||
depthPassthrough = shOutputPosition.z;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// tweakables ----------------------------------------------------
|
||||
|
||||
#define BIG_WAVES_X 0.3 // strength of big waves
|
||||
#define BIG_WAVES_Y 0.3
|
||||
|
||||
#define MID_WAVES_X 0.3 // strength of middle sized waves
|
||||
#define MID_WAVES_Y 0.15
|
||||
|
||||
#define SMALL_WAVES_X 0.15 // strength of small waves
|
||||
#define SMALL_WAVES_Y 0.1
|
||||
|
||||
#define WAVE_CHOPPYNESS 0.15 // wave choppyness
|
||||
#define WAVE_SCALE 150 // overall wave scale
|
||||
|
||||
#define ABBERATION 0.001 // chromatic abberation amount
|
||||
#define BUMP 1.5 // overall water surface bumpiness
|
||||
#define REFL_BUMP 0.11 // reflection distortion amount
|
||||
#define REFR_BUMP 0.08 // refraction distortion amount
|
||||
|
||||
#define SCATTER_AMOUNT 3.0 // amount of sunlight scattering
|
||||
#define SCATTER_COLOUR float3(0.0,1.0,0.95) // colour of sunlight scattering
|
||||
|
||||
#define SUN_EXT float3(0.45, 0.55, 0.68) //sunlight extinction
|
||||
|
||||
#define SPEC_HARDNESS 256 // specular highlights hardness
|
||||
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
float fresnel_dielectric(float3 Incoming, float3 Normal, float eta)
|
||||
{
|
||||
/* compute fresnel reflectance without explicitly computing
|
||||
the refracted direction */
|
||||
float c = abs(dot(Incoming, Normal));
|
||||
float g = eta * eta - 1.0 + c * c;
|
||||
float result;
|
||||
|
||||
if(g > 0.0) {
|
||||
g = sqrt(g);
|
||||
float A =(g - c)/(g + c);
|
||||
float B =(c *(g + c)- 1.0)/(c *(g - c)+ 1.0);
|
||||
result = 0.5 * A * A *(1.0 + B * B);
|
||||
}
|
||||
else
|
||||
result = 1.0; /* TIR (no refracted component) */
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SH_BEGIN_PROGRAM
|
||||
shInput(float2, UV)
|
||||
shInput(float3, screenCoordsPassthrough)
|
||||
shInput(float4, position)
|
||||
shInput(float, depthPassthrough)
|
||||
|
||||
shUniform(float, far) @shAutoConstant(far, far_clip_distance)
|
||||
|
||||
shSampler2D(reflectionMap)
|
||||
shSampler2D(refractionMap)
|
||||
shSampler2D(depthMap)
|
||||
shSampler2D(normalMap)
|
||||
|
||||
shUniform(float3, windDir_windSpeed) @shSharedParameter(windDir_windSpeed)
|
||||
#define WIND_SPEED windDir_windSpeed.z
|
||||
#define WIND_DIR windDir_windSpeed.xy
|
||||
|
||||
shUniform(float, waterTimer) @shSharedParameter(waterTimer)
|
||||
shUniform(float2, waterSunFade_sunHeight) @shSharedParameter(waterSunFade_sunHeight)
|
||||
|
||||
shUniform(float4, sunPosition) @shAutoConstant(sunPosition, light_position, 0)
|
||||
shUniform(float4, sunSpecular) @shAutoConstant(sunSpecular, light_specular_colour, 0)
|
||||
|
||||
|
||||
|
||||
shUniform(float, renderTargetFlipping) @shAutoConstant(renderTargetFlipping, render_target_flipping)
|
||||
|
||||
|
||||
shUniform(float3, fogColor) @shAutoConstant(fogColor, fog_colour)
|
||||
shUniform(float4, fogParams) @shAutoConstant(fogParams, fog_params)
|
||||
|
||||
shUniform(float4, cameraPos) @shAutoConstant(cameraPos, camera_position_object_space)
|
||||
|
||||
|
||||
SH_START_PROGRAM
|
||||
{
|
||||
|
||||
float2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z;
|
||||
screenCoords.y = (1-shSaturate(renderTargetFlipping))+renderTargetFlipping*screenCoords.y;
|
||||
|
||||
float depth = shSample(depthMap, screenCoords).x * far - depthPassthrough;
|
||||
float shoreFade = shSaturate(depth / 50.0);
|
||||
|
||||
float2 nCoord = float2(0.0);
|
||||
|
||||
nCoord = UV * (WAVE_SCALE * 0.05) + WIND_DIR * waterTimer * (WIND_SPEED*0.04);
|
||||
float3 normal0 = 2.0 * shSample(normalMap, nCoord + float2(-waterTimer*0.015,-waterTimer*0.005)).rgb - 1.0;
|
||||
nCoord = UV * (WAVE_SCALE * 0.1) + WIND_DIR * waterTimer * (WIND_SPEED*0.08)-(normal0.xy/normal0.zz)*WAVE_CHOPPYNESS;
|
||||
float3 normal1 = 2.0 * shSample(normalMap, nCoord + float2(+waterTimer*0.020,+waterTimer*0.015)).rgb - 1.0;
|
||||
|
||||
nCoord = UV * (WAVE_SCALE * 0.25) + WIND_DIR * waterTimer * (WIND_SPEED*0.07)-(normal1.xy/normal1.zz)*WAVE_CHOPPYNESS;
|
||||
float3 normal2 = 2.0 * shSample(normalMap, nCoord + float2(-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;
|
||||
float3 normal3 = 2.0 * shSample(normalMap, nCoord + float2(+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;
|
||||
float3 normal4 = 2.0 * shSample(normalMap, nCoord + float2(-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;
|
||||
float3 normal5 = 2.0 * shSample(normalMap, nCoord + float2(+waterTimer*0.1,-waterTimer*0.06)).rgb - 1.0;
|
||||
|
||||
|
||||
|
||||
float3 normal = (normal0 * BIG_WAVES_X + normal1 * BIG_WAVES_Y +
|
||||
normal2 * MID_WAVES_X + normal3 * MID_WAVES_Y +
|
||||
normal4 * SMALL_WAVES_X + normal5 * SMALL_WAVES_Y).xzy;
|
||||
|
||||
normal = normalize(float3(normal.x * BUMP, normal.y, normal.z * BUMP));
|
||||
|
||||
// normal for sunlight scattering
|
||||
float3 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 +
|
||||
normal4 * SMALL_WAVES_X*0.1 + normal5 * SMALL_WAVES_Y*0.1).xzy;
|
||||
lNormal = normalize(float3(lNormal.x * BUMP, lNormal.y, lNormal.z * BUMP));
|
||||
|
||||
|
||||
float3 lVec = normalize(sunPosition.xyz);
|
||||
float3 vVec = normalize(position.xyz - cameraPos.xyz);
|
||||
|
||||
|
||||
float isUnderwater = (cameraPos.y > 0) ? 0.0 : 1.0;
|
||||
|
||||
// sunlight scattering
|
||||
float3 pNormal = float3(0,1,0);
|
||||
vec3 lR = reflect(lVec, lNormal);
|
||||
vec3 llR = reflect(lVec, pNormal);
|
||||
|
||||
float s = shSaturate(dot(lR, vVec)*2.0-1.2);
|
||||
float lightScatter = shSaturate(dot(-lVec,lNormal)*0.7+0.3) * s * SCATTER_AMOUNT * waterSunFade_sunHeight.x * shSaturate(1.0-exp(-waterSunFade_sunHeight.y));
|
||||
float3 scatterColour = shLerp(vec3(SCATTER_COLOUR)*vec3(1.0,0.4,0.0), SCATTER_COLOUR, shSaturate(1.0-exp(-waterSunFade_sunHeight.y*SUN_EXT)));
|
||||
|
||||
// fresnel
|
||||
float ior = (cameraPos.y>0)?(1.333/1.0):(1.0/1.333); //air to water; water to air
|
||||
float fresnel = fresnel_dielectric(-vVec, normal, ior);
|
||||
|
||||
fresnel = shSaturate(fresnel);
|
||||
|
||||
// reflection
|
||||
float3 reflection = shSample(reflectionMap, screenCoords+(normal.xz*REFL_BUMP)).rgb;
|
||||
|
||||
// refraction
|
||||
float3 R = reflect(vVec, normal);
|
||||
|
||||
float3 refraction = float3(0,0,0);
|
||||
refraction.r = shSample(refractionMap, (screenCoords-(shoreFade * normal.xz*REFR_BUMP))*1.0).r;
|
||||
refraction.g = shSample(refractionMap, (screenCoords-(shoreFade * normal.xz*REFR_BUMP))*1.0-(R.xy*ABBERATION)).g;
|
||||
refraction.b = shSample(refractionMap, (screenCoords-(shoreFade * normal.xz*REFR_BUMP))*1.0-(R.xy*ABBERATION*2.0)).b;
|
||||
|
||||
// brighten up the refraction underwater
|
||||
refraction = (cameraPos.y < 0) ? shSaturate(refraction * 1.5) : refraction;
|
||||
|
||||
float waterSunGradient = dot(-vVec, -lVec);
|
||||
waterSunGradient = shSaturate(pow(waterSunGradient*0.7+0.3,2.0));
|
||||
float3 waterSunColour = float3(0.0,1.0,0.85)*waterSunGradient * 0.5;
|
||||
|
||||
float waterGradient = dot(-vVec, float3(0.0,-1.0,0.0));
|
||||
waterGradient = clamp((waterGradient*0.5+0.5),0.2,1.0);
|
||||
float3 watercolour = (float3(0.0078, 0.5176, 0.700)+waterSunColour)*waterGradient*2.0;
|
||||
float3 waterext = float3(0.6, 0.9, 1.0);//water extinction
|
||||
watercolour = mix(watercolour*0.3*waterSunFade_sunHeight.x, watercolour, shSaturate(1.0-exp(-waterSunFade_sunHeight.y*SUN_EXT)));
|
||||
|
||||
// specular
|
||||
float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS);
|
||||
|
||||
shOutputColour(0).xyz = shLerp( shLerp(refraction, scatterColour, lightScatter), reflection, fresnel) + specular * sunSpecular.xyz;
|
||||
|
||||
// smooth transition to shore (above water only)
|
||||
shOutputColour(0).xyz = shLerp(shOutputColour(0).xyz, refraction, (1-shoreFade) * (1-isUnderwater));
|
||||
|
||||
// fog
|
||||
float fogValue = shSaturate((depthPassthrough - fogParams.y) * fogParams.w);
|
||||
shOutputColour(0).xyz = shLerp (shOutputColour(0).xyz, fogColor, fogValue);
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,15 @@
|
||||
shader_set water_vertex
|
||||
{
|
||||
source water.shader
|
||||
type vertex
|
||||
profiles_cg vs_2_0 vp40 arbvp1
|
||||
profiles_hlsl vs_2_0
|
||||
}
|
||||
|
||||
shader_set water_fragment
|
||||
{
|
||||
source water.shader
|
||||
type fragment
|
||||
profiles_cg ps_2_x ps_2_0 ps fp40 arbfp1
|
||||
profiles_hlsl ps_2_0
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
Loading…
Reference in New Issue