]> gitweb.ps.run Git - subsurface_scattering/blobdiff - shaders/fbo_frag.glsl
fix translucency, add gaussian blur to shader
[subsurface_scattering] / shaders / fbo_frag.glsl
index f6f12cd88f0c530627682d68a0a86e6472fa07cd..11029923d8555e7017f756de2f733484597f0acc 100644 (file)
@@ -3,31 +3,62 @@ out vec4 FragColor;
   \r
 in vec2 TexCoords;\r
 \r
-uniform sampler2D screenTexture;\r
-uniform int applySSSSS;\r
-uniform int N;\r
+uniform sampler2D shadowmapTexture;\r
+uniform sampler2D irradianceTexture;\r
+uniform int screenWidth;\r
+uniform int screenHeight;\r
+uniform int renderState;\r
+uniform vec2 samplePositions[13];\r
+uniform vec3 sampleWeights[13];\r
+\r
+vec4 blur(sampler2D tex, vec2 uv, vec2 res) {\r
+  float Pi = 6.28318530718; // Pi*2\r
+    \r
+  // GAUSSIAN BLUR SETTINGS {{{\r
+  float Directions = 16.0; // BLUR DIRECTIONS (Default 16.0 - More is better but slower)\r
+  float Quality = 4.0; // BLUR QUALITY (Default 4.0 - More is better but slower)\r
+  float Size = 8.0; // BLUR SIZE (Radius)\r
+  // GAUSSIAN BLUR SETTINGS }}}\r
+  \r
+  vec2 Radius = Size/res;\r
+  \r
+  // Pixel colour\r
+  vec4 Color = texture(tex, uv);\r
+  \r
+  // Blur calculations\r
+  for( float d=0.0; d<Pi; d+=Pi/Directions) {\r
+    for(float i=1.0/Quality; i<=1.0; i+=1.0/Quality) {\r
+      Color += texture( tex, uv+vec2(cos(d),sin(d))*Radius*i);         \r
+    }\r
+  }\r
+  \r
+  // Output to screen\r
+  Color /= Quality * Directions - 15.0;\r
+  return Color;\r
+}\r
 \r
 void main()\r
 {\r
-    if (applySSSSS == 1) {\r
-        float x = 1.0/1600.0;\r
-        float y = 1.0/900.0;\r
-\r
-        float maxDist = N*N + N*N;\r
-\r
-        vec4 color = vec4(0, 0, 0, 1);\r
-        for (int i = -N; i <= N; i++) {\r
-            for (int j = -N; j <= N; j++) {\r
-                float dist = i*i + j*j;\r
-                vec4 newC = texture(screenTexture, TexCoords + vec2(i*x, j*y)) / (2*N*N);\r
-                float factor = 1 - (dist / maxDist);\r
-                factor = pow(factor, 2);\r
-                color += newC * factor;\r
-            }\r
-        }\r
-        FragColor = color;\r
+    if (renderState == 0) {\r
+        FragColor = blur(shadowmapTexture, TexCoords, vec2(screenWidth, screenHeight));\r
     }\r
-    else {\r
-        FragColor = texture(screenTexture, TexCoords);\r
+    else if (renderState == 1) {\r
+        FragColor = texture(shadowmapTexture, TexCoords);\r
+    }\r
+    // stencil buffer\r
+    else if (renderState == 2) {\r
+        FragColor = texture(irradianceTexture, TexCoords);\r
+    }\r
+    else if (renderState == 3) {\r
+        vec4 result = vec4(0, 0, 0, 1);\r
+        for (int i = 0; i < 13; i++) {\r
+            vec2 sampleCoords = TexCoords + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);\r
+            //vec4 sample = texture(irradianceTexture, sampleCoords)\r
+            //            * texture(shadowmapTexture, sampleCoords);\r
+            vec4 sample = texture(irradianceTexture, sampleCoords);\r
+            vec4 weight = vec4(sampleWeights[i], 1);\r
+            result += sample * weight;\r
+        }\r
+        FragColor = result;\r
     }\r
 }\r