Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2017-08-09 17:54:18 +0300
committerClément Foucault <foucault.clem@gmail.com>2017-08-10 16:43:48 +0300
commitd16342e5fd1f51c01bc1f9becc6ec0704d783de8 (patch)
tree3c070ef24e1e7fa3b7dc444e229d45a7b9bcff78 /source/blender/draw/engines/eevee/shaders/ssr_lib.glsl
parent98a7f1b3356bd5dd5190150b12f8b00f7fbe7455 (diff)
Eevee: Add Screen Space Refraction.
For the moment the only way to enable this is to: - enable Screen Space REFLECTIONS. - enable Screen Space Refraction in the SSR parameters. - enable Screen Space Refraction in the material tab.
Diffstat (limited to 'source/blender/draw/engines/eevee/shaders/ssr_lib.glsl')
-rw-r--r--source/blender/draw/engines/eevee/shaders/ssr_lib.glsl73
1 files changed, 73 insertions, 0 deletions
diff --git a/source/blender/draw/engines/eevee/shaders/ssr_lib.glsl b/source/blender/draw/engines/eevee/shaders/ssr_lib.glsl
new file mode 100644
index 00000000000..5b36da27e3f
--- /dev/null
+++ b/source/blender/draw/engines/eevee/shaders/ssr_lib.glsl
@@ -0,0 +1,73 @@
+/* ------------ Refraction ------------ */
+
+#define BTDF_BIAS 0.85
+
+vec4 screen_space_refraction(vec3 viewPosition, vec3 N, vec3 V, float ior, float roughnessSquared, vec3 rand, float ofs)
+{
+ float a2 = roughnessSquared * roughnessSquared;
+ float jitter = fract(rand.x + ofs);
+
+ /* Importance sampling bias */
+ rand.x = mix(rand.x, 0.0, BTDF_BIAS);
+
+ vec3 T, B;
+ float NH;
+ make_orthonormal_basis(N, T, B);
+ vec3 H = sample_ggx(rand, a2, N, T, B, NH); /* Microfacet normal */
+ float pdf = pdf_ggx_reflect(NH, a2);
+
+ /* If ray is bad (i.e. going below the plane) regenerate. */
+ if (F_eta(ior, dot(H, V)) < 1.0) {
+ H = sample_ggx(rand * vec3(1.0, -1.0, -1.0), a2, N, T, B, NH); /* Microfacet normal */
+ pdf = pdf_ggx_reflect(NH, a2);
+ }
+
+ vec3 vV = viewCameraVec;
+ float eta = 1.0/ior;
+ if (dot(H, V) < 0.0) {
+ H = -H;
+ eta = ior;
+ }
+
+ vec3 R = refract(-V, H, 1.0 / ior);
+
+ R = transform_direction(ViewMatrix, R);
+
+ vec3 hit_pos = raycast(-1, viewPosition, R, ssrThickness, jitter, roughnessSquared);
+
+ if ((hit_pos.z < 0.0) && (F_eta(ior, dot(H, V)) < 1.0)) {
+ float hit_dist = distance(hit_pos, viewPosition);
+
+ float cone_cos = cone_cosine(roughnessSquared);
+ float cone_tan = sqrt(1 - cone_cos * cone_cos) / cone_cos;
+
+ /* Empirical fit for refraction. */
+ /* TODO find a better fit or precompute inside the LUT. */
+ cone_tan *= 0.5 * fast_sqrt(f0_from_ior((ior < 1.0) ? 1.0 / ior : ior));
+
+ float cone_footprint = hit_dist * cone_tan;
+
+ /* find the offset in screen space by multiplying a point
+ * in camera space at the depth of the point by the projection matrix. */
+ float homcoord = ProjectionMatrix[2][3] * hit_pos.z + ProjectionMatrix[3][3];
+ /* UV space footprint */
+ cone_footprint = BTDF_BIAS * 0.5 * max(ProjectionMatrix[0][0], ProjectionMatrix[1][1]) * cone_footprint / homcoord;
+
+ vec2 hit_uvs = project_point(ProjectionMatrix, hit_pos).xy * 0.5 + 0.5;
+
+ /* Texel footprint */
+ vec2 texture_size = vec2(textureSize(colorBuffer, 0).xy);
+ float mip = clamp(log2(cone_footprint * max(texture_size.x, texture_size.y)), 0.0, 9.0);
+
+ /* Correct UVs for mipmaping mis-alignment */
+ float low_mip = floor(mip);
+ hit_uvs *= mix(mipRatio[int(low_mip)], mipRatio[int(low_mip + 1.0)], mip - low_mip);
+
+ vec3 spec = textureLod(colorBuffer, hit_uvs, mip).xyz;
+ float mask = screen_border_mask(hit_uvs);
+
+ return vec4(spec, mask);
+ }
+
+ return vec4(0.0);
+}