From 30bffb5a3afa2fde165d4fb63a115310d5ddc3e3 Mon Sep 17 00:00:00 2001 From: Charlie Jolly Date: Thu, 12 Jul 2018 23:40:18 +0200 Subject: Nodes: add sqrt, ceil, floor and fract to math nodes. This works for Cycles, Eevee, texture nodes and compositing. It helps to reduce the number of math nodes required in various node setups. Differential Revision: https://developer.blender.org/D3537 --- intern/cycles/kernel/shaders/node_math.osl | 20 ++++++++++++++++++++ intern/cycles/kernel/svm/svm_math_util.h | 8 ++++++++ intern/cycles/kernel/svm/svm_types.h | 4 ++++ intern/cycles/render/nodes.cpp | 4 ++++ 4 files changed, 36 insertions(+) (limited to 'intern') diff --git a/intern/cycles/kernel/shaders/node_math.osl b/intern/cycles/kernel/shaders/node_math.osl index c5fcbc311d3..aa9f6e671c3 100644 --- a/intern/cycles/kernel/shaders/node_math.osl +++ b/intern/cycles/kernel/shaders/node_math.osl @@ -40,6 +40,18 @@ float safe_modulo(float a, float b) return result; } +float safe_sqrt(float a) +{ + float result; + + if (a > 0.0) + result = sqrt(a); + else + result = 0.0; + + return result; +} + float safe_log(float a, float b) { if (a < 0.0 || b < 0.0) @@ -97,6 +109,14 @@ shader node_math( Value = fabs(Value1); else if (type == "arctan2") Value = atan2(Value1, Value2); + else if (type == "floor") + Value = floor(Value1); + else if (type == "ceil") + Value = ceil(Value1); + else if (type == "fract") + Value = Value1 - floor(Value1); + else if (type == "sqrt") + Value = safe_sqrt(Value1); if (use_clamp) Value = clamp(Value, 0.0, 1.0); diff --git a/intern/cycles/kernel/svm/svm_math_util.h b/intern/cycles/kernel/svm/svm_math_util.h index 04864bd610a..d3490ab284f 100644 --- a/intern/cycles/kernel/svm/svm_math_util.h +++ b/intern/cycles/kernel/svm/svm_math_util.h @@ -94,6 +94,14 @@ ccl_device float svm_math(NodeMath type, float Fac1, float Fac2) Fac = fabsf(Fac1); else if(type == NODE_MATH_ARCTAN2) Fac = atan2f(Fac1, Fac2); + else if (type == NODE_MATH_FLOOR) + Fac = floorf(Fac1); + else if (type == NODE_MATH_CEIL) + Fac = ceilf(Fac1); + else if (type == NODE_MATH_FRACT) + Fac = Fac1 - floorf(Fac1); + else if (type == NODE_MATH_SQRT) + Fac = safe_sqrtf(Fac1); else if(type == NODE_MATH_CLAMP) Fac = saturate(Fac1); else diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index b94a83b6712..0fde5126434 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -261,6 +261,10 @@ typedef enum NodeMath { NODE_MATH_MODULO, NODE_MATH_ABSOLUTE, NODE_MATH_ARCTAN2, + NODE_MATH_FLOOR, + NODE_MATH_CEIL, + NODE_MATH_FRACT, + NODE_MATH_SQRT, NODE_MATH_CLAMP /* used for the clamp UI option */ } NodeMath; diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index beff7f91348..fe2916d21d4 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -5071,6 +5071,10 @@ NODE_DEFINE(MathNode) type_enum.insert("modulo", NODE_MATH_MODULO); type_enum.insert("absolute", NODE_MATH_ABSOLUTE); type_enum.insert("arctan2", NODE_MATH_ARCTAN2); + type_enum.insert("floor", NODE_MATH_FLOOR); + type_enum.insert("ceil", NODE_MATH_CEIL); + type_enum.insert("fract", NODE_MATH_FRACT); + type_enum.insert("sqrt", NODE_MATH_SQRT); SOCKET_ENUM(type, "Type", type_enum, NODE_MATH_ADD); SOCKET_BOOLEAN(use_clamp, "Use Clamp", false); -- cgit v1.2.3