node_math.osl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "stdosl.h"
  17. float safe_divide(float a, float b)
  18. {
  19. float result;
  20. if (b == 0.0)
  21. result = 0.0;
  22. else
  23. result = a / b;
  24. return result;
  25. }
  26. float safe_modulo(float a, float b)
  27. {
  28. float result;
  29. if (b == 0.0)
  30. result = 0.0;
  31. else
  32. result = fmod(a, b);
  33. return result;
  34. }
  35. float safe_sqrt(float a)
  36. {
  37. float result;
  38. if (a > 0.0)
  39. result = sqrt(a);
  40. else
  41. result = 0.0;
  42. return result;
  43. }
  44. float safe_log(float a, float b)
  45. {
  46. if (a < 0.0 || b < 0.0)
  47. return 0.0;
  48. return log(a) / log(b);
  49. }
  50. shader node_math(string type = "add",
  51. int use_clamp = 0,
  52. float Value1 = 0.0,
  53. float Value2 = 0.0,
  54. output float Value = 0.0)
  55. {
  56. /* OSL asin, acos, pow check for values that could give rise to nan */
  57. if (type == "add")
  58. Value = Value1 + Value2;
  59. else if (type == "subtract")
  60. Value = Value1 - Value2;
  61. else if (type == "multiply")
  62. Value = Value1 * Value2;
  63. else if (type == "divide")
  64. Value = safe_divide(Value1, Value2);
  65. else if (type == "sine")
  66. Value = sin(Value1);
  67. else if (type == "cosine")
  68. Value = cos(Value1);
  69. else if (type == "tangent")
  70. Value = tan(Value1);
  71. else if (type == "arcsine")
  72. Value = asin(Value1);
  73. else if (type == "arccosine")
  74. Value = acos(Value1);
  75. else if (type == "arctangent")
  76. Value = atan(Value1);
  77. else if (type == "power")
  78. Value = pow(Value1, Value2);
  79. else if (type == "logarithm")
  80. Value = safe_log(Value1, Value2);
  81. else if (type == "minimum")
  82. Value = min(Value1, Value2);
  83. else if (type == "maximum")
  84. Value = max(Value1, Value2);
  85. else if (type == "round")
  86. Value = floor(Value1 + 0.5);
  87. else if (type == "less_than")
  88. Value = Value1 < Value2;
  89. else if (type == "greater_than")
  90. Value = Value1 > Value2;
  91. else if (type == "modulo")
  92. Value = safe_modulo(Value1, Value2);
  93. else if (type == "absolute")
  94. Value = fabs(Value1);
  95. else if (type == "arctan2")
  96. Value = atan2(Value1, Value2);
  97. else if (type == "floor")
  98. Value = floor(Value1);
  99. else if (type == "ceil")
  100. Value = ceil(Value1);
  101. else if (type == "fract")
  102. Value = Value1 - floor(Value1);
  103. else if (type == "sqrt")
  104. Value = safe_sqrt(Value1);
  105. if (use_clamp)
  106. Value = clamp(Value, 0.0, 1.0);
  107. }