Nodes: add absolute value operation to all math nodes

Reviewed By: dingto, brecht

Differential Revision: https://developer.blender.org/D507
This commit is contained in:
Matt Heimlich 2014-05-07 16:20:17 +02:00 committed by Brecht Van Lommel
parent 8b8d5a441f
commit 3fbc984b06
12 changed files with 48 additions and 4 deletions

View File

@ -93,6 +93,8 @@ shader node_math(
Value = Value1 > Value2;
else if (type == "Modulo")
Value = safe_modulo(Value1, Value2);
else if (type == "Absolute")
Value = fabs(Value1);
if (Clamp)
Value = clamp(Value, 0.0, 1.0);

View File

@ -56,6 +56,8 @@ ccl_device float svm_math(NodeMath type, float Fac1, float Fac2)
Fac = Fac1 > Fac2;
else if(type == NODE_MATH_MODULO)
Fac = safe_modulo(Fac1, Fac2);
else if(type == NODE_MATH_ABSOLUTE)
Fac = fabsf(Fac1);
else if(type == NODE_MATH_CLAMP)
Fac = clamp(Fac1, 0.0f, 1.0f);
else

View File

@ -221,6 +221,7 @@ typedef enum NodeMath {
NODE_MATH_LESS_THAN,
NODE_MATH_GREATER_THAN,
NODE_MATH_MODULO,
NODE_MATH_ABSOLUTE,
NODE_MATH_CLAMP /* used for the clamp UI option */
} NodeMath;

View File

@ -3544,6 +3544,7 @@ static ShaderEnum math_type_init()
enm.insert("Less Than", NODE_MATH_LESS_THAN);
enm.insert("Greater Than", NODE_MATH_GREATER_THAN);
enm.insert("Modulo", NODE_MATH_MODULO);
enm.insert("Absolute", NODE_MATH_ABSOLUTE);
return enm;
}

View File

@ -183,7 +183,7 @@ ccl_device_inline float signf(float f)
ccl_device_inline float nonzerof(float f, float eps)
{
if(fabsf(f) < eps)
if(fabsf(f) < eps)
return signf(f)*eps;
else
return f;

View File

@ -83,6 +83,9 @@ void MathNode::convertToOperations(NodeConverter &converter, const CompositorCon
case 17: /* Modulo */
operation = new MathModuloOperation();
break;
case 18: /* Absolute Value */
operation = new MathAbsoluteOperation();
break;
}
if (operation) {

View File

@ -333,3 +333,14 @@ void MathModuloOperation::executePixelSampled(float output[4], float x, float y,
clampIfNeeded(output);
}
void MathAbsoluteOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
{
float inputValue1[4];
this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler);
output[0] = fabs(inputValue1[0]);
clampIfNeeded(output);
}

View File

@ -163,4 +163,10 @@ public:
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler);
};
class MathAbsoluteOperation : public MathBaseOperation {
public:
MathAbsoluteOperation() : MathBaseOperation() {}
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler);
};
#endif

View File

@ -299,6 +299,11 @@ void math_modulo(float val1, float val2, out float outval)
outval = mod(val1, val2);
}
void math_abs(float val1, out float outval)
{
outval = abs(val1);
}
void squeeze(float val, float width, float center, out float outval)
{
outval = 1.0/(1.0 + pow(2.71828183, -((val-center)*width)));

View File

@ -129,7 +129,8 @@ EnumPropertyItem node_math_items[] = {
{14, "ROUND", 0, "Round", ""},
{15, "LESS_THAN", 0, "Less Than", ""},
{16, "GREATER_THAN", 0, "Greater Than", ""},
{17, "MODULO", 0, "Modulo", ""},
{17, "MODULO", 0, "Modulo", ""},
{18, "ABSOLUTE", 0, "Absolute", ""},
{0, NULL, 0, NULL, NULL}
};

View File

@ -216,6 +216,11 @@ static void node_shader_exec_math(void *UNUSED(data), int UNUSED(thread), bNode
r = fmod(a, b);
break;
}
case 18: /* Absolute */
{
r = fabs(a);
break;
}
}
out[0]->vec[0] = r;
@ -226,7 +231,7 @@ static int gpu_shader_math(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(
static const char *names[] = {"math_add", "math_subtract", "math_multiply",
"math_divide", "math_sine", "math_cosine", "math_tangent", "math_asin",
"math_acos", "math_atan", "math_pow", "math_log", "math_min", "math_max",
"math_round", "math_less_than", "math_greater_than", "math_modulo"};
"math_round", "math_less_than", "math_greater_than", "math_modulo", "math_absolute"};
switch (node->custom1) {
case 0:

View File

@ -174,7 +174,7 @@ static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, shor
break;
}
case 17: /* Modulo */
case 17: /* Modulo */
{
if (in1 == 0.0f)
*out = 0.0f;
@ -182,6 +182,13 @@ static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, shor
*out = fmod(in0, in1);
break;
}
case 18: /* Absolute */
{
*out = fabs(in0);
break;
}
default:
{
BLI_assert(0);