Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Jolly <charlie>2019-12-06 02:02:05 +0300
committerCharlie Jolly <mistajolly@gmail.com>2019-12-07 15:33:07 +0300
commit0406eb110332a863811d7fb6228f9a7ca514e022 (patch)
tree9809062966699e31c785ae0dc97ccb6e9f6c60f7 /source/blender/gpu/shaders
parent6a78ace569ec7c0e076e5af34d9496ce3363b81e (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 'source/blender/gpu/shaders')
-rw-r--r--source/blender/gpu/shaders/material/gpu_shader_material_math.glsl138
1 files changed, 114 insertions, 24 deletions
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_math.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_math.glsl
index 4fac770e8fe..de3be98b715 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_math.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_math.glsl
@@ -1,24 +1,24 @@
-void math_add(float a, float b, out float result)
+void math_add(float a, float b, float c, out float result)
{
result = a + b;
}
-void math_subtract(float a, float b, out float result)
+void math_subtract(float a, float b, float c, out float result)
{
result = a - b;
}
-void math_multiply(float a, float b, out float result)
+void math_multiply(float a, float b, float c, out float result)
{
result = a * b;
}
-void math_divide(float a, float b, out float result)
+void math_divide(float a, float b, float c, out float result)
{
result = safe_divide(a, b);
}
-void math_power(float a, float b, out float result)
+void math_power(float a, float b, float c, out float result)
{
if (a >= 0.0) {
result = compatible_pow(a, b);
@@ -34,97 +34,187 @@ void math_power(float a, float b, out float result)
}
}
-void math_logarithm(float a, float b, out float result)
+void math_logarithm(float a, float b, float c, out float result)
{
result = (a > 0.0 && b > 0.0) ? log2(a) / log2(b) : 0.0;
}
-void math_sqrt(float a, float b, out float result)
+void math_sqrt(float a, float b, float c, out float result)
{
result = (a > 0.0) ? sqrt(a) : 0.0;
}
-void math_absolute(float a, float b, out float result)
+void math_inversesqrt(float a, float b, float c, out float result)
+{
+ result = inversesqrt(a);
+}
+
+void math_absolute(float a, float b, float c, out float result)
{
result = abs(a);
}
-void math_minimum(float a, float b, out float result)
+void math_radians(float a, float b, float c, out float result)
+{
+ result = radians(a);
+}
+
+void math_degrees(float a, float b, float c, out float result)
+{
+ result = degrees(a);
+}
+
+void math_minimum(float a, float b, float c, out float result)
{
result = min(a, b);
}
-void math_maximum(float a, float b, out float result)
+void math_maximum(float a, float b, float c, out float result)
{
result = max(a, b);
}
-void math_less_than(float a, float b, out float result)
+void math_less_than(float a, float b, float c, out float result)
{
result = (a < b) ? 1.0 : 0.0;
}
-void math_greater_than(float a, float b, out float result)
+void math_greater_than(float a, float b, float c, out float result)
{
result = (a > b) ? 1.0 : 0.0;
}
-void math_round(float a, float b, out float result)
+void math_round(float a, float b, float c, out float result)
{
result = floor(a + 0.5);
}
-void math_floor(float a, float b, out float result)
+void math_floor(float a, float b, float c, out float result)
{
result = floor(a);
}
-void math_ceil(float a, float b, out float result)
+void math_ceil(float a, float b, float c, out float result)
{
result = ceil(a);
}
-void math_fraction(float a, float b, out float result)
+void math_fraction(float a, float b, float c, out float result)
{
result = a - floor(a);
}
-void math_modulo(float a, float b, out float result)
+void math_modulo(float a, float b, float c, out float result)
{
result = c_mod(a, b);
}
-void math_sine(float a, float b, out float result)
+void math_trunc(float a, float b, float c, out float result)
+{
+ result = trunc(a);
+}
+
+void math_snap(float a, float b, float c, out float result)
+{
+ result = floor(safe_divide(a, b)) * b;
+}
+
+void math_pingpong(float a, float b, float c, out float result)
+{
+ result = (b != 0.0) ? abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b) : 0.0;
+}
+
+/* Adapted from godotengine math_funcs.h. */
+void math_wrap(float a, float b, float c, out float result)
+{
+ float range = b - c;
+ result = (range != 0.0) ? a - (range * floor((a - c) / range)) : c;
+}
+
+void math_sine(float a, float b, float c, out float result)
{
result = sin(a);
}
-void math_cosine(float a, float b, out float result)
+void math_cosine(float a, float b, float c, out float result)
{
result = cos(a);
}
-void math_tangent(float a, float b, out float result)
+void math_tangent(float a, float b, float c, out float result)
{
result = tan(a);
}
-void math_arcsine(float a, float b, out float result)
+void math_sinh(float a, float b, float c, out float result)
+{
+ result = sinh(a);
+}
+
+void math_cosh(float a, float b, float c, out float result)
+{
+ result = cosh(a);
+}
+
+void math_tanh(float a, float b, float c, out float result)
+{
+ result = tanh(a);
+}
+
+void math_arcsine(float a, float b, float c, out float result)
{
result = (a <= 1.0 && a >= -1.0) ? asin(a) : 0.0;
}
-void math_arccosine(float a, float b, out float result)
+void math_arccosine(float a, float b, float c, out float result)
{
result = (a <= 1.0 && a >= -1.0) ? acos(a) : 0.0;
}
-void math_arctangent(float a, float b, out float result)
+void math_arctangent(float a, float b, float c, out float result)
{
result = atan(a);
}
-void math_arctan2(float a, float b, out float result)
+void math_arctan2(float a, float b, float c, out float result)
{
result = atan(a, b);
}
+
+void math_sign(float a, float b, float c, out float result)
+{
+ result = sign(a);
+}
+
+void math_exponent(float a, float b, float c, out float result)
+{
+ result = exp(a);
+}
+
+void math_compare(float a, float b, float c, out float result)
+{
+ result = (abs(a - b) <= max(c, 1e-5)) ? 1.0 : 0.0;
+}
+
+void math_multiply_add(float a, float b, float c, out float result)
+{
+ result = a * b + c;
+}
+
+/* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
+void math_smoothmin(float a, float b, float c, out float result)
+{
+ if (c != 0.0) {
+ float h = max(c - abs(a - b), 0.0) / c;
+ result = min(a, b) - h * h * h * c * (1.0 / 6.0);
+ }
+ else {
+ result = min(a, b);
+ }
+}
+
+void math_smoothmax(float a, float b, float c, out float result)
+{
+ math_smoothmin(-a, -b, c, result);
+ result = -result;
+}