Blender  V3.3
node_math.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 float safe_divide(float a, float b)
5 {
6  return (b != 0.0) ? a / b : 0.0;
7 }
8 
10 {
11  return vector((b[0] != 0.0) ? a[0] / b[0] : 0.0,
12  (b[1] != 0.0) ? a[1] / b[1] : 0.0,
13  (b[2] != 0.0) ? a[2] / b[2] : 0.0);
14 }
15 
16 float safe_modulo(float a, float b)
17 {
18  return (b != 0.0) ? fmod(a, b) : 0.0;
19 }
20 
21 float fract(float a)
22 {
23  return a - floor(a);
24 }
25 
26 /* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
27 float smoothmin(float a, float b, float c)
28 {
29  if (c != 0.0) {
30  float h = max(c - abs(a - b), 0.0) / c;
31  return min(a, b) - h * h * h * c * (1.0 / 6.0);
32  }
33  else {
34  return min(a, b);
35  }
36 }
37 
38 float pingpong(float a, float b)
39 {
40  return (b != 0.0) ? abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b) : 0.0;
41 }
42 
43 float safe_sqrt(float a)
44 {
45  return (a > 0.0) ? sqrt(a) : 0.0;
46 }
47 
48 float safe_log(float a, float b)
49 {
50  return (a > 0.0 && b > 0.0) ? log(a) / log(b) : 0.0;
51 }
52 
54 {
55  float lenSquared = dot(v_proj, v_proj);
56  return (lenSquared != 0.0) ? (dot(v, v_proj) / lenSquared) * v_proj : vector(0.0);
57 }
58 
60 {
61  return floor(safe_divide(a, b)) * b;
62 }
63 
64 /* Adapted from GODOT-engine math_funcs.h. */
65 float wrap(float value, float max, float min)
66 {
67  float range = max - min;
68  return (range != 0.0) ? value - (range * floor((value - min) / range)) : min;
69 }
70 
72 {
73  return point(wrap(value[0], max[0], min[0]),
74  wrap(value[1], max[1], min[1]),
75  wrap(value[2], max[2], min[2]));
76 }
77 
78 /* Built in OSL faceforward is `(dot(I, Nref) > 0) ? -N : N;` which is different to
79  * GLSL `dot(Nref, I) < 0 ? N : -N` for zero values. */
80 point compatible_faceforward(point vec, point incident, point reference)
81 {
82  return dot(reference, incident) < 0.0 ? vec : -vec;
83 }
84 
85 matrix euler_to_mat(point euler)
86 {
87  float cx = cos(euler[0]);
88  float cy = cos(euler[1]);
89  float cz = cos(euler[2]);
90  float sx = sin(euler[0]);
91  float sy = sin(euler[1]);
92  float sz = sin(euler[2]);
93  matrix mat = matrix(1.0);
94  mat[0][0] = cy * cz;
95  mat[0][1] = cy * sz;
96  mat[0][2] = -sy;
97  mat[1][0] = sy * sx * cz - cx * sz;
98  mat[1][1] = sy * sx * sz + cx * cz;
99  mat[1][2] = cy * sx;
100  +mat[2][0] = sy * cx * cz + sx * sz;
101  mat[2][1] = sy * cx * sz - sx * cz;
102  mat[2][2] = cy * cx;
103  return mat;
104 }
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 Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise 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 Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise 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
ccl_gpu_kernel_postfix ccl_global float int int sy
ccl_gpu_kernel_postfix ccl_global float int sx
ccl_device_inline float3 log(float3 v)
Definition: math_float3.h:397
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
T dot(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
T floor(const T &a)
T abs(const T &a)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
vector snap(vector a, vector b)
Definition: node_math.h:59
matrix euler_to_mat(point euler)
Definition: node_math.h:85
float fract(float a)
Definition: node_math.h:21
float safe_divide(float a, float b)
Definition: node_math.h:4
float wrap(float value, float max, float min)
Definition: node_math.h:65
float safe_sqrt(float a)
Definition: node_math.h:43
vector project(vector v, vector v_proj)
Definition: node_math.h:53
float pingpong(float a, float b)
Definition: node_math.h:38
float safe_log(float a, float b)
Definition: node_math.h:48
float smoothmin(float a, float b, float c)
Definition: node_math.h:27
point compatible_faceforward(point vec, point incident, point reference)
Definition: node_math.h:80
float safe_modulo(float a, float b)
Definition: node_math.h:16
#define min(a, b)
Definition: sort.c:35
float max