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:
authorMai Lavelle <mai.lavelle@gmail.com>2016-09-02 07:41:04 +0300
committerMai Lavelle <mai.lavelle@gmail.com>2016-09-11 18:20:21 +0300
commit92a2c49aab55a2c459a8f6d50e730bdcda966eb5 (patch)
tree8524d21897c5c2b4da74f08ef7b27046c8682eed /intern/cycles/kernel
parentac2fe8312bd2a609eb884ad95dd27bcbbf7f1b0e (diff)
Cycles: Fix bump mapping to use object space when used with true displacement
Bump mapping was happening in world space while displacement happens in object space, causing shading errors when displacement type was used with bump mapping. To fix this the proper transforms are added to bump nodes. This is only done for automatic bump mapping however, to avoid visual changes from other uses of bump mapping. It would be nice to do this for all bump mapping to be consistent but that will have to wait till we can break compatibility. Reviewed By: brecht Differential Revision: https://developer.blender.org/D2191
Diffstat (limited to 'intern/cycles/kernel')
-rw-r--r--intern/cycles/kernel/shaders/node_bump.osl25
-rw-r--r--intern/cycles/kernel/svm/svm_displace.h24
2 files changed, 38 insertions, 11 deletions
diff --git a/intern/cycles/kernel/shaders/node_bump.osl b/intern/cycles/kernel/shaders/node_bump.osl
index 9882857f2ec..7f01cf2ca91 100644
--- a/intern/cycles/kernel/shaders/node_bump.osl
+++ b/intern/cycles/kernel/shaders/node_bump.osl
@@ -21,6 +21,7 @@
surface node_bump(
int invert = 0,
+ int use_object_space = 0,
normal NormalIn = N,
float Strength = 0.1,
float Distance = 1.0,
@@ -29,12 +30,20 @@ surface node_bump(
float SampleY = 0.0,
output normal NormalOut = N)
{
+ point Ptmp = P;
+ normal Normal = NormalIn;
+
+ if (use_object_space) {
+ Ptmp = transform("object", Ptmp);
+ Normal = normalize(transform("object", Normal));
+ }
+
/* get surface tangents from normal */
- vector dPdx = Dx(P);
- vector dPdy = Dy(P);
+ vector dPdx = Dx(Ptmp);
+ vector dPdy = Dy(Ptmp);
- vector Rx = cross(dPdy, NormalIn);
- vector Ry = cross(NormalIn, dPdx);
+ vector Rx = cross(dPdy, Normal);
+ vector Ry = cross(Normal, dPdx);
/* compute surface gradient and determinant */
float det = dot(dPdx, Rx);
@@ -49,7 +58,11 @@ surface node_bump(
dist *= -1.0;
/* compute and output perturbed normal */
- NormalOut = normalize(absdet * NormalIn - dist * sign(det) * surfgrad);
- NormalOut = normalize(strength * NormalOut + (1.0 - strength) * NormalIn);
+ NormalOut = normalize(absdet * Normal - dist * sign(det) * surfgrad);
+ NormalOut = normalize(strength * NormalOut + (1.0 - strength) * Normal);
+
+ if (use_object_space) {
+ NormalOut = normalize(transform("object", "world", NormalOut));
+ }
}
diff --git a/intern/cycles/kernel/svm/svm_displace.h b/intern/cycles/kernel/svm/svm_displace.h
index 8d4b07c9973..12a6f9a7d18 100644
--- a/intern/cycles/kernel/svm/svm_displace.h
+++ b/intern/cycles/kernel/svm/svm_displace.h
@@ -22,14 +22,23 @@ ccl_device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stac
{
#ifdef __RAY_DIFFERENTIALS__
/* get normal input */
- uint normal_offset, distance_offset, invert;
- decode_node_uchar4(node.y, &normal_offset, &distance_offset, &invert, NULL);
+ uint normal_offset, distance_offset, invert, use_object_space;
+ decode_node_uchar4(node.y, &normal_offset, &distance_offset, &invert, &use_object_space);
float3 normal_in = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): ccl_fetch(sd, N);
+ float3 dPdx = ccl_fetch(sd, dP).dx;
+ float3 dPdy = ccl_fetch(sd, dP).dy;
+
+ if(use_object_space) {
+ object_inverse_normal_transform(kg, sd, &normal_in);
+ object_inverse_dir_transform(kg, sd, &dPdx);
+ object_inverse_dir_transform(kg, sd, &dPdy);
+ }
+
/* get surface tangents from normal */
- float3 Rx = cross(ccl_fetch(sd, dP).dy, normal_in);
- float3 Ry = cross(normal_in, ccl_fetch(sd, dP).dx);
+ float3 Rx = cross(dPdy, normal_in);
+ float3 Ry = cross(normal_in, dPdx);
/* get bump values */
uint c_offset, x_offset, y_offset, strength_offset;
@@ -40,7 +49,7 @@ ccl_device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stac
float h_y = stack_load_float(stack, y_offset);
/* compute surface gradient and determinant */
- float det = dot(ccl_fetch(sd, dP).dx, Rx);
+ float det = dot(dPdx, Rx);
float3 surfgrad = (h_x - h_c)*Rx + (h_y - h_c)*Ry;
float absdet = fabsf(det);
@@ -56,6 +65,11 @@ ccl_device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stac
/* compute and output perturbed normal */
float3 normal_out = normalize(absdet*normal_in - distance*signf(det)*surfgrad);
normal_out = normalize(strength*normal_out + (1.0f - strength)*normal_in);
+
+ if(use_object_space) {
+ object_normal_transform(kg, sd, &normal_out);
+ }
+
stack_store_float3(stack, node.w, normal_out);
#endif
}