Skip to content

Commit

Permalink
Shadow cast on water
Browse files Browse the repository at this point in the history
  • Loading branch information
scrawl committed Mar 20, 2013
1 parent 4cbb202 commit 11aed74
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
19 changes: 19 additions & 0 deletions files/materials/water.mat
Expand Up @@ -54,5 +54,24 @@ material Water
scale 0.1 0.1
alpha_op_ex source1 src_manual src_current 0.7
}

texture_unit shadowMap0
{
content_type shadow
tex_address_mode clamp
filtering none
}
texture_unit shadowMap1
{
content_type shadow
tex_address_mode clamp
filtering none
}
texture_unit shadowMap2
{
content_type shadow
tex_address_mode clamp
filtering none
}
}
}
76 changes: 74 additions & 2 deletions files/materials/water.shader
Expand Up @@ -50,6 +50,13 @@

// Inspired by Blender GLSL Water by martinsh ( http://devlog-martinsh.blogspot.de/2012/07/waterundewater-shader-wip.html )

#define SHADOWS_PSSM @shGlobalSettingBool(shadows_pssm)
#define SHADOWS @shGlobalSettingBool(shadows)

#if SHADOWS || SHADOWS_PSSM
#include "shadows.h"
#endif

#define RIPPLES 1
#define REFRACTION @shGlobalSettingBool(refraction)

Expand All @@ -64,6 +71,23 @@
shOutput(float4, position)
shOutput(float, depthPassthrough)


#if SHADOWS
shOutput(float4, lightSpacePos0)
shUniform(float4x4, texViewProjMatrix0) @shAutoConstant(texViewProjMatrix0, texture_viewproj_matrix)
#endif

#if SHADOWS_PSSM
@shForeach(3)
shOutput(float4, lightSpacePos@shIterator)
shUniform(float4x4, texViewProjMatrix@shIterator) @shAutoConstant(texViewProjMatrix@shIterator, texture_viewproj_matrix, @shIterator)
@shEndForeach
#endif

#if SHADOWS || SHADOWS_PSSM
shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
#endif

SH_START_PROGRAM
{
shOutputPosition = shMatrixMult(wvp, shInputPosition);
Expand All @@ -88,6 +112,17 @@
position = shInputPosition;

depthPassthrough = shOutputPosition.z;


#if SHADOWS
lightSpacePos0 = shMatrixMult(texViewProjMatrix0, shMatrixMult(worldMatrix, shInputPosition));
#endif
#if SHADOWS_PSSM
float4 wPos = shMatrixMult(worldMatrix, shInputPosition);
@shForeach(3)
lightSpacePos@shIterator = shMatrixMult(texViewProjMatrix@shIterator, wPos);
@shEndForeach
#endif
}

#else
Expand Down Expand Up @@ -186,10 +221,47 @@
shUniform(float4, fogParams) @shAutoConstant(fogParams, fog_params)

shUniform(float4, cameraPos) @shAutoConstant(cameraPos, camera_position_object_space)


#if SHADOWS
shInput(float4, lightSpacePos0)
shSampler2D(shadowMap0)
shUniform(float2, invShadowmapSize0) @shAutoConstant(invShadowmapSize0, inverse_texture_size, 1)
#endif
#if SHADOWS_PSSM
@shForeach(3)
shInput(float4, lightSpacePos@shIterator)
shSampler2D(shadowMap@shIterator)
shUniform(float2, invShadowmapSize@shIterator) @shAutoConstant(invShadowmapSize@shIterator, inverse_texture_size, @shIterator(1))
@shEndForeach
shUniform(float3, pssmSplitPoints) @shSharedParameter(pssmSplitPoints)
#endif

#if SHADOWS || SHADOWS_PSSM
shUniform(float4, shadowFar_fadeStart) @shSharedParameter(shadowFar_fadeStart)
#endif


SH_START_PROGRAM
{
#if SHADOWS
float shadow = depthShadowPCF (shadowMap0, lightSpacePos0, invShadowmapSize0);
#endif
#if SHADOWS_PSSM
float shadow = pssmDepthShadow (lightSpacePos0, invShadowmapSize0, shadowMap0, lightSpacePos1, invShadowmapSize1, shadowMap1, lightSpacePos2, invShadowmapSize2, shadowMap2, depthPassthrough, pssmSplitPoints);
#endif

#if SHADOWS || SHADOWS_PSSM
float fadeRange = shadowFar_fadeStart.x - shadowFar_fadeStart.y;
float fade = 1-((depthPassthrough - shadowFar_fadeStart.y) / fadeRange);
shadow = (depthPassthrough > shadowFar_fadeStart.x) ? 1.0 : ((depthPassthrough > shadowFar_fadeStart.y) ? 1.0-((1.0-shadow)*fade) : shadow);
#endif

#if !SHADOWS && !SHADOWS_PSSM
float shadow = 1.0;
#endif


float2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z;
screenCoords.y = (1-shSaturate(renderTargetFlipping))+renderTargetFlipping*screenCoords.y;

Expand Down Expand Up @@ -244,7 +316,7 @@
float3 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));
float lightScatter = shadow * shSaturate(dot(-lVec,lNormal)*0.7+0.3) * s * SCATTER_AMOUNT * waterSunFade_sunHeight.x * shSaturate(1.0-exp(-waterSunFade_sunHeight.y));
float3 scatterColour = shLerp(float3(SCATTER_COLOUR)*float3(1.0,0.4,0.0), SCATTER_COLOUR, shSaturate(1.0-exp(-waterSunFade_sunHeight.y*SUN_EXT)));

// fresnel
Expand All @@ -267,7 +339,7 @@
#endif

// specular
float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS);
float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS) * shadow;

#if REFRACTION
shOutputColour(0).xyz = shLerp( shLerp(refraction, scatterColour, lightScatter), reflection, fresnel) + specular * sunSpecular.xyz;
Expand Down

0 comments on commit 11aed74

Please sign in to comment.