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:
authorLukas Stockner <lukas.stockner@freenet.de>2018-07-26 17:48:15 +0300
committerLukas Stockner <lukas.stockner@freenet.de>2018-07-26 18:00:31 +0300
commitd6e769d32e7939e3bbd1986cdc4abd2b13135eab (patch)
tree394509448476044962a264b77b075b0ed3bae789 /intern/cycles/kernel/shaders
parent1c41dbb079e391a1d684a7dedf40728025ef8839 (diff)
Cycles: Add reflection fix to Bump and Normal Map nodes
While changing the shading normal is a great way to add additional detail to a model, there are some problems with it. One of them is that at grazing angles and/or strong changes to the normal, the reflected ray can end up pointing into the actual geometry, which results in a black spot. This patch helps avoid this by automatically reducing the strength of the bump/normal map if the reflected direction would end up too shallow or inside the geometry. Differential Revision: https://developer.blender.org/D2574
Diffstat (limited to 'intern/cycles/kernel/shaders')
-rw-r--r--intern/cycles/kernel/shaders/node_bump.osl2
-rw-r--r--intern/cycles/kernel/shaders/node_normal_map.osl2
-rw-r--r--intern/cycles/kernel/shaders/stdosl.h30
3 files changed, 34 insertions, 0 deletions
diff --git a/intern/cycles/kernel/shaders/node_bump.osl b/intern/cycles/kernel/shaders/node_bump.osl
index 7f01cf2ca91..a2a4468d5f3 100644
--- a/intern/cycles/kernel/shaders/node_bump.osl
+++ b/intern/cycles/kernel/shaders/node_bump.osl
@@ -64,5 +64,7 @@ surface node_bump(
if (use_object_space) {
NormalOut = normalize(transform("object", "world", NormalOut));
}
+
+ NormalOut = ensure_valid_reflection(Ng, I, NormalOut);
}
diff --git a/intern/cycles/kernel/shaders/node_normal_map.osl b/intern/cycles/kernel/shaders/node_normal_map.osl
index 41bcac4fb10..fda6f12a5da 100644
--- a/intern/cycles/kernel/shaders/node_normal_map.osl
+++ b/intern/cycles/kernel/shaders/node_normal_map.osl
@@ -88,5 +88,7 @@ shader node_normal_map(
if (Strength != 1.0)
Normal = normalize(NormalIn + (Normal - NormalIn) * max(Strength, 0.0));
+
+ Normal = ensure_valid_reflection(Ng, I, Normal);
}
diff --git a/intern/cycles/kernel/shaders/stdosl.h b/intern/cycles/kernel/shaders/stdosl.h
index df9c2010872..4a8378796ba 100644
--- a/intern/cycles/kernel/shaders/stdosl.h
+++ b/intern/cycles/kernel/shaders/stdosl.h
@@ -282,6 +282,36 @@ point rotate (point p, float angle, point a, point b)
return transform (M, p-a) + a;
}
+normal ensure_valid_reflection(normal Ng, vector I, normal N)
+{
+ float sqr(float x) { return x*x; }
+
+ vector R = 2*dot(N, I)*N - I;
+ if (dot(Ng, R) >= 0.05) {
+ return N;
+ }
+
+ /* Form coordinate system with Ng as the Z axis and N inside the X-Z-plane.
+ * The X axis is found by normalizing the component of N that's orthogonal to Ng.
+ * The Y axis isn't actually needed.
+ */
+ vector X = normalize(N - dot(N, Ng)*Ng);
+
+ /* Calculate N.z and N.x in the local coordinate system. */
+ float Ix = dot(I, X), Iz = dot(I, Ng);
+ float Ix2 = sqr(dot(I, X)), Iz2 = sqr(dot(I, Ng));
+ float Ix2Iz2 = Ix2 + Iz2;
+
+ float a = sqrt(Ix2*(Ix2Iz2 - sqr(0.05)));
+ float b = Iz*0.05 + Ix2Iz2;
+ float c = (a + b > 0.0)? (a + b) : (-a + b);
+
+ float Nz = sqrt(0.5 * c * (1.0 / Ix2Iz2));
+ float Nx = sqrt(1.0 - sqr(Nz));
+
+ /* Transform back into global coordinates. */
+ return Nx*X + Nz*Ng;
+}
// Color functions