summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomi Korpipää <tomi.korpipaa@digia.com>2013-05-14 13:48:17 +0300
committerTomi Korpipää <tomi.korpipaa@digia.com>2013-05-14 14:10:41 +0300
commitf6cfd480b4e873c29f4b9c0df1e450ddd0516312 (patch)
tree0e890040280e97bb6538b042d395f7d83055a0b6
parentc226c07cea53be22e164db0397ac60ef2ee0f0b0 (diff)
Poisson disk sampling added to shadow shaders
Change-Id: I5a70cc738239409652db4978f91de0d792539597 Change-Id: I5a70cc738239409652db4978f91de0d792539597 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
-rw-r--r--src/engine/shaders/fragmentShadow45
-rw-r--r--src/engine/shaders/fragmentShadowNoTex45
-rw-r--r--src/engine/shaders/fragmentShadowNoTexColorOnY43
3 files changed, 124 insertions, 9 deletions
diff --git a/src/engine/shaders/fragmentShadow b/src/engine/shaders/fragmentShadow
index 6a8f6acc..143da625 100644
--- a/src/engine/shaders/fragmentShadow
+++ b/src/engine/shaders/fragmentShadow
@@ -14,6 +14,29 @@ varying highp vec3 normal_cmr;
varying highp vec3 eyeDirection_cmr;
varying highp vec3 lightDirection_cmr;
+const highp vec2 poissonDisk[16] = vec2[](vec2(-0.94201624, -0.39906216),
+ vec2(0.94558609, -0.76890725),
+ vec2(-0.094184101, -0.92938870),
+ vec2(0.34495938, 0.29387760),
+ vec2(-0.91588581, 0.45771432),
+ vec2(-0.81544232, -0.87912464),
+ vec2(-0.38277543, 0.27676845),
+ vec2(0.97484398, 0.75648379),
+ vec2(0.44323325, -0.97511554),
+ vec2(0.53742981, -0.47373420),
+ vec2(-0.26496911, -0.41893023),
+ vec2(0.79197514, 0.19090188),
+ vec2(-0.24188840, 0.99706507),
+ vec2(-0.81409955, 0.91437590),
+ vec2(0.19984126, 0.78641367),
+ vec2(0.14383161, -0.14100790));
+
+/*float random(vec3 seed, int i) {
+ vec4 seed4 = vec4(seed, i);
+ float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673));
+ return fract(sin(dot_product) * 43758.5453);
+}*/
+
// Version 1: Causes self-shadowing, but shadows are smooth
/*void main() {
float shadowFactor = 1.0; // default to '1' meaning "no shadow"
@@ -86,10 +109,26 @@ void main() {
highp float bias = 0.005 * tan(acos(cosTheta));
bias = clamp(bias, 0.0, 0.01);
- vec4 shadCoordsPD = shadowCoord;
- shadCoordsPD.z -= bias;
+ vec4 shadCoords = shadowCoord;
+ shadCoords.z -= bias;
// adjust shadow strength by increasing the multiplier and lowering the addition (their sum must be 1)
- highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoordsPD).r + 0.25;
+ // direct method; needs large shadow texture to look good
+ //highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoords).r + 0.25;
+ // poisson disk sampling; smoothes edges
+ highp float visibility = 0.2;
+ for (int i = 0; i < 15; i++) {
+ vec4 shadCoordsPD = shadCoords;
+ shadCoordsPD.xy += poissonDisk[i] / 100.0;
+ visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r;
+ }
+ // stratified poisson; produces noise but hides pixel edges well
+ /*for (int i = 0; i < 15; i++) {
+ vec4 shadCoordsPD = shadCoords;
+ int index = int(16.0 * random(gl_FragCoord.xyy, i));
+ shadCoordsPD.xy += poissonDisk[index] / 150.0;
+ visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r;
+ }
+ */
gl_FragColor.rgb =
visibility * (materialAmbientColor +
diff --git a/src/engine/shaders/fragmentShadowNoTex b/src/engine/shaders/fragmentShadowNoTex
index a394dc86..ff08ea51 100644
--- a/src/engine/shaders/fragmentShadowNoTex
+++ b/src/engine/shaders/fragmentShadowNoTex
@@ -14,6 +14,29 @@ varying highp vec3 normal_cmr;
varying highp vec3 eyeDirection_cmr;
varying highp vec3 lightDirection_cmr;
+const highp vec2 poissonDisk[16] = vec2[](vec2(-0.94201624, -0.39906216),
+ vec2(0.94558609, -0.76890725),
+ vec2(-0.094184101, -0.92938870),
+ vec2(0.34495938, 0.29387760),
+ vec2(-0.91588581, 0.45771432),
+ vec2(-0.81544232, -0.87912464),
+ vec2(-0.38277543, 0.27676845),
+ vec2(0.97484398, 0.75648379),
+ vec2(0.44323325, -0.97511554),
+ vec2(0.53742981, -0.47373420),
+ vec2(-0.26496911, -0.41893023),
+ vec2(0.79197514, 0.19090188),
+ vec2(-0.24188840, 0.99706507),
+ vec2(-0.81409955, 0.91437590),
+ vec2(0.19984126, 0.78641367),
+ vec2(0.14383161, -0.14100790));
+
+/*float random(vec3 seed, int i) {
+ vec4 seed4 = vec4(seed, i);
+ float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673));
+ return fract(sin(dot_product) * 43758.5453);
+}*/
+
// Version 1: Causes self-shadowing, but shadows are smooth
/*void main() {
float shadowFactor = 1.0; // default to '1' meaning "no shadow"
@@ -76,10 +99,26 @@ void main() {
highp float bias = 0.005 * tan(acos(cosTheta));
bias = clamp(bias, 0.0, 0.01);
- vec4 shadCoordsPD = shadowCoord;
- shadCoordsPD.z -= bias;
+ vec4 shadCoords = shadowCoord;
+ shadCoords.z -= bias;
// adjust shadow strength by increasing the multiplier and lowering the addition (their sum must be 1)
- highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoordsPD).r + 0.25;
+ // direct method; needs large shadow texture to look good
+ //highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoords).r + 0.25;
+ // poisson disk sampling; smoothes edges
+ highp float visibility = 0.2;
+ for (int i = 0; i < 15; i++) {
+ vec4 shadCoordsPD = shadCoords;
+ shadCoordsPD.xy += poissonDisk[i] / 100.0;
+ visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r;
+ }
+ // stratified poisson; produces noise but hides pixel edges well
+ /*for (int i = 0; i < 15; i++) {
+ vec4 shadCoordsPD = shadCoords;
+ int index = int(16.0 * random(gl_FragCoord.xyy, i));
+ shadCoordsPD.xy += poissonDisk[index] / 150.0;
+ visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r;
+ }
+ */
gl_FragColor.rgb =
visibility * (materialAmbientColor +
diff --git a/src/engine/shaders/fragmentShadowNoTexColorOnY b/src/engine/shaders/fragmentShadowNoTexColorOnY
index c6b37539..0177abe3 100644
--- a/src/engine/shaders/fragmentShadowNoTexColorOnY
+++ b/src/engine/shaders/fragmentShadowNoTexColorOnY
@@ -14,6 +14,29 @@ varying highp vec3 eyeDirection_cmr;
varying highp vec3 lightDirection_cmr;
varying highp vec2 coords_mdl;
+const highp vec2 poissonDisk[16] = vec2[](vec2(-0.94201624, -0.39906216),
+ vec2(0.94558609, -0.76890725),
+ vec2(-0.094184101, -0.92938870),
+ vec2(0.34495938, 0.29387760),
+ vec2(-0.91588581, 0.45771432),
+ vec2(-0.81544232, -0.87912464),
+ vec2(-0.38277543, 0.27676845),
+ vec2(0.97484398, 0.75648379),
+ vec2(0.44323325, -0.97511554),
+ vec2(0.53742981, -0.47373420),
+ vec2(-0.26496911, -0.41893023),
+ vec2(0.79197514, 0.19090188),
+ vec2(-0.24188840, 0.99706507),
+ vec2(-0.81409955, 0.91437590),
+ vec2(0.19984126, 0.78641367),
+ vec2(0.14383161, -0.14100790));
+
+/*float random(vec3 seed, int i) {
+ vec4 seed4 = vec4(seed, i);
+ float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673));
+ return fract(sin(dot_product) * 43758.5453);
+}*/
+
// Version 1: Causes self-shadowing, but shadows are smooth
/*void main() {
float shadowFactor = 1.0; // default to '1' meaning "no shadow"
@@ -76,10 +99,24 @@ void main() {
highp float bias = 0.005 * tan(acos(cosTheta));
bias = clamp(bias, 0.0, 0.01);
- vec4 shadCoordsPD = shadowCoord;
- shadCoordsPD.z -= bias;
+ vec4 shadCoords = shadowCoord;
+ shadCoords.z -= bias;
// adjust shadow strength by increasing the multiplier and lowering the addition (their sum must be 1)
- highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoordsPD).r + 0.25;
+ // direct method; needs large shadow texture to look good
+ //highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoords).r + 0.25;
+ // poisson disk sampling; smoothes edges
+ highp float visibility = 0.2;
+ for (int i = 0; i < 15; i++) {
+ vec4 shadCoordsPD = shadCoords;
+ shadCoordsPD.xy += poissonDisk[i] / 100.0;
+ visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r;
+ }
+ /*for (int i = 0; i < 15; i++) {
+ vec4 shadCoordsPD = shadCoords;
+ int index = int(16.0 * random(gl_FragCoord.xyy, i));
+ shadCoordsPD.xy += poissonDisk[index] / 150.0;
+ visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r;
+ }*/
gl_FragColor.rgb =
visibility * (materialAmbientColor +