Blender  V3.3
node_noise.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "vector2.h"
5 #include "vector4.h"
6 
7 #define vector3 point
8 
9 float safe_noise(float p)
10 {
11  float f = noise("noise", p);
12  if (isinf(f))
13  return 0.5;
14  return f;
15 }
16 
17 float safe_noise(vector2 p)
18 {
19  float f = noise("noise", p.x, p.y);
20  if (isinf(f))
21  return 0.5;
22  return f;
23 }
24 
26 {
27  float f = noise("noise", p);
28  if (isinf(f))
29  return 0.5;
30  return f;
31 }
32 
33 float safe_noise(vector4 p)
34 {
35  float f = noise("noise", vector3(p.x, p.y, p.z), p.w);
36  if (isinf(f))
37  return 0.5;
38  return f;
39 }
40 
41 float safe_snoise(float p)
42 {
43  float f = noise("snoise", p);
44  if (isinf(f))
45  return 0.0;
46  return f;
47 }
48 
49 float safe_snoise(vector2 p)
50 {
51  float f = noise("snoise", p.x, p.y);
52  if (isinf(f))
53  return 0.0;
54  return f;
55 }
56 
58 {
59  float f = noise("snoise", p);
60  if (isinf(f))
61  return 0.0;
62  return f;
63 }
64 
65 float safe_snoise(vector4 p)
66 {
67  float f = noise("snoise", vector3(p.x, p.y, p.z), p.w);
68  if (isinf(f))
69  return 0.0;
70  return f;
71 }
72 
73 /* The fractal_noise functions are all exactly the same except for the input type. */
74 float fractal_noise(float p, float details, float roughness)
75 {
76  float fscale = 1.0;
77  float amp = 1.0;
78  float maxamp = 0.0;
79  float sum = 0.0;
80  float octaves = clamp(details, 0.0, 15.0);
81  int n = (int)octaves;
82  for (int i = 0; i <= n; i++) {
83  float t = safe_noise(fscale * p);
84  sum += t * amp;
85  maxamp += amp;
86  amp *= clamp(roughness, 0.0, 1.0);
87  fscale *= 2.0;
88  }
89  float rmd = octaves - floor(octaves);
90  if (rmd != 0.0) {
91  float t = safe_noise(fscale * p);
92  float sum2 = sum + t * amp;
93  sum /= maxamp;
94  sum2 /= maxamp + amp;
95  return (1.0 - rmd) * sum + rmd * sum2;
96  }
97  else {
98  return sum / maxamp;
99  }
100 }
101 
102 /* The fractal_noise functions are all exactly the same except for the input type. */
103 float fractal_noise(vector2 p, float details, float roughness)
104 {
105  float fscale = 1.0;
106  float amp = 1.0;
107  float maxamp = 0.0;
108  float sum = 0.0;
109  float octaves = clamp(details, 0.0, 15.0);
110  int n = (int)octaves;
111  for (int i = 0; i <= n; i++) {
112  float t = safe_noise(fscale * p);
113  sum += t * amp;
114  maxamp += amp;
115  amp *= clamp(roughness, 0.0, 1.0);
116  fscale *= 2.0;
117  }
118  float rmd = octaves - floor(octaves);
119  if (rmd != 0.0) {
120  float t = safe_noise(fscale * p);
121  float sum2 = sum + t * amp;
122  sum /= maxamp;
123  sum2 /= maxamp + amp;
124  return (1.0 - rmd) * sum + rmd * sum2;
125  }
126  else {
127  return sum / maxamp;
128  }
129 }
130 
131 /* The fractal_noise functions are all exactly the same except for the input type. */
132 float fractal_noise(vector3 p, float details, float roughness)
133 {
134  float fscale = 1.0;
135  float amp = 1.0;
136  float maxamp = 0.0;
137  float sum = 0.0;
138  float octaves = clamp(details, 0.0, 15.0);
139  int n = (int)octaves;
140  for (int i = 0; i <= n; i++) {
141  float t = safe_noise(fscale * p);
142  sum += t * amp;
143  maxamp += amp;
144  amp *= clamp(roughness, 0.0, 1.0);
145  fscale *= 2.0;
146  }
147  float rmd = octaves - floor(octaves);
148  if (rmd != 0.0) {
149  float t = safe_noise(fscale * p);
150  float sum2 = sum + t * amp;
151  sum /= maxamp;
152  sum2 /= maxamp + amp;
153  return (1.0 - rmd) * sum + rmd * sum2;
154  }
155  else {
156  return sum / maxamp;
157  }
158 }
159 
160 /* The fractal_noise functions are all exactly the same except for the input type. */
161 float fractal_noise(vector4 p, float details, float roughness)
162 {
163  float fscale = 1.0;
164  float amp = 1.0;
165  float maxamp = 0.0;
166  float sum = 0.0;
167  float octaves = clamp(details, 0.0, 15.0);
168  int n = (int)octaves;
169  for (int i = 0; i <= n; i++) {
170  float t = safe_noise(fscale * p);
171  sum += t * amp;
172  maxamp += amp;
173  amp *= clamp(roughness, 0.0, 1.0);
174  fscale *= 2.0;
175  }
176  float rmd = octaves - floor(octaves);
177  if (rmd != 0.0) {
178  float t = safe_noise(fscale * p);
179  float sum2 = sum + t * amp;
180  sum /= maxamp;
181  sum2 /= maxamp + amp;
182  return (1.0 - rmd) * sum + rmd * sum2;
183  }
184  else {
185  return sum / maxamp;
186  }
187 }
188 
189 #undef vector3
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
static T sum(const btAlignedObjectArray< T > &items)
T clamp(const T &a, const T &min, const T &max)
T floor(const T &a)
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
float fractal_noise(float p, float details, float roughness)
Definition: node_noise.h:74
#define vector3
Definition: node_noise.h:7
float safe_snoise(float p)
Definition: node_noise.h:41
float safe_noise(float p)
Definition: node_noise.h:9
static float noise(int n)