Blender V4.3
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}
sqrt(x)+1/max(0
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a vector
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a point
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
additional_info("compositor_sum_squared_difference_float_shared") .push_constant(Type output_img float dot(value.rgb, luminance_coefficients)") .define("LOAD(value)"
ccl_device_inline float2 fmod(const float2 a, const float b)
ccl_device_inline float2 floor(const float2 a)
ccl_device_inline float3 cos(float3 v)
ccl_device_inline float3 log(float3 v)
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.c:32
float max
ccl_device_inline int abs(int x)
Definition util/math.h:120