2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdosl.h"
|
|
|
|
|
|
|
|
float safe_divide(float a, float b)
|
|
|
|
{
|
|
|
|
float result;
|
|
|
|
|
2012-10-17 01:47:37 +00:00
|
|
|
if (b == 0.0)
|
2011-04-27 11:58:34 +00:00
|
|
|
result = 0.0;
|
|
|
|
else
|
2012-10-17 01:47:37 +00:00
|
|
|
result = a / b;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-05-20 14:38:47 +00:00
|
|
|
float safe_modulo(float a, float b)
|
|
|
|
{
|
|
|
|
float result;
|
|
|
|
|
|
|
|
if (b == 0.0)
|
|
|
|
result = 0.0;
|
|
|
|
else
|
|
|
|
result = fmod(a, b);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
float safe_log(float a, float b)
|
|
|
|
{
|
2012-10-17 01:47:37 +00:00
|
|
|
if (a < 0.0 || b < 0.0)
|
2011-04-27 11:58:34 +00:00
|
|
|
return 0.0;
|
|
|
|
|
2012-10-17 01:47:37 +00:00
|
|
|
return log(a) / log(b);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shader node_math(
|
|
|
|
string type = "Add",
|
2012-09-02 12:24:04 +00:00
|
|
|
int Clamp = 0,
|
2011-04-27 11:58:34 +00:00
|
|
|
float Value1 = 0.0,
|
|
|
|
float Value2 = 0.0,
|
|
|
|
output float Value = 0.0)
|
|
|
|
{
|
|
|
|
/* OSL asin, acos, pow check for values that could give rise to nan */
|
|
|
|
|
2012-10-17 01:47:37 +00:00
|
|
|
if (type == "Add")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = Value1 + Value2;
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Subtract")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = Value1 - Value2;
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Multiply")
|
2012-10-17 01:47:37 +00:00
|
|
|
Value = Value1 * Value2;
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Divide")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = safe_divide(Value1, Value2);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Sine")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = sin(Value1);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Cosine")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = cos(Value1);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Tangent")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = tan(Value1);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Arcsine")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = asin(Value1);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Arccosine")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = acos(Value1);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Arctangent")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = atan(Value1);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Power")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = pow(Value1, Value2);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Logarithm")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = safe_log(Value1, Value2);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Minimum")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = min(Value1, Value2);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Maximum")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = max(Value1, Value2);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Round")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = floor(Value1 + 0.5);
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Less Than")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = Value1 < Value2;
|
2013-03-10 00:42:47 +00:00
|
|
|
else if (type == "Greater Than")
|
2011-04-27 11:58:34 +00:00
|
|
|
Value = Value1 > Value2;
|
2013-05-20 14:38:47 +00:00
|
|
|
else if (type == "Modulo")
|
|
|
|
Value = safe_modulo(Value1, Value2);
|
2015-04-21 05:53:32 +00:00
|
|
|
else if (type == "Absolute")
|
|
|
|
Value = fabs(Value1);
|
2012-08-29 17:30:14 +00:00
|
|
|
|
2012-10-17 01:47:37 +00:00
|
|
|
if (Clamp)
|
2013-10-23 11:30:18 +00:00
|
|
|
Value = clamp(Value, 0.0, 1.0);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|