diff options
author | Charlie Jolly <charlie> | 2019-12-06 02:02:05 +0300 |
---|---|---|
committer | Charlie Jolly <mistajolly@gmail.com> | 2019-12-07 15:33:07 +0300 |
commit | 0406eb110332a863811d7fb6228f9a7ca514e022 (patch) | |
tree | 9809062966699e31c785ae0dc97ccb6e9f6c60f7 /intern/cycles/util | |
parent | 6a78ace569ec7c0e076e5af34d9496ce3363b81e (diff) |
Maths Node: Additional functions
When creating shaders and using maths functions it is expected that Blender should match functions in other DCC applications, game engines and shading languages such as GLSL and OSL.
This patch adds missing functions to the Blender maths node.
Ideally, it would be nice to have these functions available to vectors too but that is not part of this patch.
This patch adds the following functions trunc, snap, wrap, compare, pingpong, sign, radians, degrees, cosh, sinh, tanh, exp, smoothmin and inversesqrt.
Sign function is based on GLSL and OSL functions and returns zero when x == 0.
Differential Revision: https://developer.blender.org/D5957
Diffstat (limited to 'intern/cycles/util')
-rw-r--r-- | intern/cycles/util/util_math.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h index 53e528de66e..dc211d2ed4e 100644 --- a/intern/cycles/util/util_math.h +++ b/intern/cycles/util/util_math.h @@ -344,6 +344,29 @@ ccl_device_inline int ceil_to_int(float f) return float_to_int(ceilf(f)); } +ccl_device_inline float fractf(float x) +{ + return x - floorf(x); +} + +/* Adapted from godotengine math_funcs.h. */ +ccl_device_inline float wrapf(float value, float max, float min) +{ + float range = max - min; + return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min; +} + +ccl_device_inline float pingpongf(float a, float b) +{ + return (b != 0.0f) ? fabsf(fractf((a - b) / (b * 2.0f)) * b * 2.0f - b) : 0.0f; +} + +ccl_device_inline float smoothminf(float a, float b, float k) +{ + float h = fmaxf(k - fabsf(a - b), 0.0f) / k; + return fminf(a, b) - h * h * h * k * (1.0f / 6.0f); +} + ccl_device_inline float signf(float f) { return (f < 0.0f) ? -1.0f : 1.0f; @@ -357,6 +380,17 @@ ccl_device_inline float nonzerof(float f, float eps) return f; } +/* Signum function testing for zero. Matches GLSL and OSL functions. */ +ccl_device_inline float compatible_signf(float f) +{ + if (f == 0.0f) { + return 0.0f; + } + else { + return signf(f); + } +} + ccl_device_inline float smoothstepf(float f) { float ff = f * f; @@ -549,6 +583,11 @@ ccl_device_inline float safe_sqrtf(float f) return sqrtf(max(f, 0.0f)); } +ccl_device_inline float inversesqrtf(float f) +{ + return (f > 0.0f) ? 1.0f / sqrtf(f) : 0.0f; +} + ccl_device float safe_asinf(float a) { return asinf(clamp(a, -1.0f, 1.0f)); |