Blender V4.5
node_math.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5float safe_divide(float a, float b)
6{
7 return (b != 0.0) ? a / b : 0.0;
8}
9
11{
12 return vector((b[0] != 0.0) ? a[0] / b[0] : 0.0,
13 (b[1] != 0.0) ? a[1] / b[1] : 0.0,
14 (b[2] != 0.0) ? a[2] / b[2] : 0.0);
15}
16
17float safe_modulo(float a, float b)
18{
19 return (b != 0.0) ? fmod(a, b) : 0.0;
20}
21
22float safe_floored_modulo(float a, float b)
23{
24 return (b != 0.0) ? a - floor(a / b) * b : 0.0;
25}
26
27float fract(float a)
28{
29 return a - floor(a);
30}
31
32/* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
33float smoothmin(float a, float b, float c)
34{
35 if (c != 0.0) {
36 float h = max(c - abs(a - b), 0.0) / c;
37 return min(a, b) - h * h * h * c * (1.0 / 6.0);
38 }
39 else {
40 return min(a, b);
41 }
42}
43
44float pingpong(float a, float b)
45{
46 return (b != 0.0) ? abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b) : 0.0;
47}
48
49float safe_sqrt(float a)
50{
51 return (a > 0.0) ? sqrt(a) : 0.0;
52}
53
54float safe_log(float a, float b)
55{
56 return (a > 0.0 && b > 0.0) ? log(a) / log(b) : 0.0;
57}
58
60{
61 float lenSquared = dot(v_proj, v_proj);
62 return (lenSquared != 0.0) ? (dot(v, v_proj) / lenSquared) * v_proj : vector(0.0);
63}
64
66{
67 return floor(safe_divide(a, b)) * b;
68}
69
70/* Adapted from GODOT-engine math_funcs.h. */
71float wrap(float value, float max, float min)
72{
73 float range = max - min;
74 return (range != 0.0) ? value - (range * floor((value - min) / range)) : min;
75}
76
77point wrap(point value, point max, point min)
78{
79 return point(wrap(value[0], max[0], min[0]),
80 wrap(value[1], max[1], min[1]),
81 wrap(value[2], max[2], min[2]));
82}
83
84/* Built in OSL faceforward is `(dot(I, Nref) > 0) ? -N : N;` which is different to
85 * GLSL `dot(Nref, I) < 0 ? N : -N` for zero values. */
86point compatible_faceforward(point vec, point incident, point reference)
87{
88 return dot(reference, incident) < 0.0 ? vec : -vec;
89}
90
91matrix euler_to_mat(point euler)
92{
93 float cx = cos(euler[0]);
94 float cy = cos(euler[1]);
95 float cz = cos(euler[2]);
96 float sx = sin(euler[0]);
97 float sy = sin(euler[1]);
98 float sz = sin(euler[2]);
99 matrix mat = matrix(1.0);
100 mat[0][0] = cy * cz;
101 mat[0][1] = cy * sz;
102 mat[0][2] = -sy;
103 mat[1][0] = sy * sx * cz - cx * sz;
104 mat[1][1] = sy * sx * sz + cx * cz;
105 mat[1][2] = cy * sx;
106 +mat[2][0] = sy * cx * cz + sx * sz;
107 mat[2][1] = sy * cx * sz - sx * cz;
108 mat[2][2] = cy * cx;
109 return mat;
110}
ATTR_WARN_UNUSED_RESULT const BMVert * v
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define log
#define sin
#define cos
#define abs
#define floor
#define sqrt
ccl_device_inline float2 fmod(const float2 a, const float b)
vector snap(vector a, vector b)
Definition node_math.h:65
matrix euler_to_mat(point euler)
Definition node_math.h:91
float safe_floored_modulo(float a, float b)
Definition node_math.h:22
float fract(float a)
Definition node_math.h:27
float safe_divide(float a, float b)
Definition node_math.h:5
float wrap(float value, float max, float min)
Definition node_math.h:71
float safe_sqrt(float a)
Definition node_math.h:49
vector project(vector v, vector v_proj)
Definition node_math.h:59
float pingpong(float a, float b)
Definition node_math.h:44
float safe_log(float a, float b)
Definition node_math.h:54
float smoothmin(float a, float b, float c)
Definition node_math.h:33
point compatible_faceforward(point vec, point incident, point reference)
Definition node_math.h:86
float safe_modulo(float a, float b)
Definition node_math.h:17
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251