Blender V4.3
node_noise.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "vector2.h"
6#include "vector4.h"
7
8#define vector3 point
9
10float safe_noise(float co)
11{
12 float precision_correction = 0.5 * float(fabs(co) >= 1000000.0);
13 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
14 * representation issues. */
15 float p = fmod(co, 100000.0) + precision_correction;
16
17 return noise("noise", p);
18}
19
20float safe_noise(vector2 co)
21{
22 vector2 precision_correction = 0.5 * vector2(float(fabs(co.x) >= 1000000.0),
23 float(fabs(co.y) >= 1000000.0));
24 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
25 * representation issues. This causes discontinuities every 100000.0, however at such scales this
26 * usually shouldn't be noticeable. */
27 vector2 p = fmod(co, 100000.0) + precision_correction;
28
29 return noise("noise", p.x, p.y);
30}
31
33{
34 vector3 precision_correction = 0.5 * vector3(float(fabs(co.x) >= 1000000.0),
35 float(fabs(co.y) >= 1000000.0),
36 float(fabs(co.z) >= 1000000.0));
37 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
38 * representation issues. This causes discontinuities every 100000.0, however at such scales this
39 * usually shouldn't be noticeable. */
40 vector3 p = fmod(co, 100000.0) + precision_correction;
41
42 return noise("noise", p);
43}
44
45float safe_noise(vector4 co)
46{
47 vector4 precision_correction = 0.5 * vector4(float(fabs(co.x) >= 1000000.0),
48 float(fabs(co.y) >= 1000000.0),
49 float(fabs(co.z) >= 1000000.0),
50 float(fabs(co.w) >= 1000000.0));
51 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
52 * representation issues. This causes discontinuities every 100000.0, however at such scales this
53 * usually shouldn't be noticeable. */
54 vector4 p = fmod(co, 100000.0) + precision_correction;
55
56 return noise("noise", vector3(p.x, p.y, p.z), p.w);
57}
58
59float safe_snoise(float co)
60{
61 float precision_correction = 0.5 * float(fabs(co) >= 1000000.0);
62 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
63 * representation issues. */
64 float p = fmod(co, 100000.0) + precision_correction;
65
66 return noise("snoise", p);
67}
68
69float safe_snoise(vector2 co)
70{
71 vector2 precision_correction = 0.5 * vector2(float(fabs(co.x) >= 1000000.0),
72 float(fabs(co.y) >= 1000000.0));
73 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
74 * representation issues. This causes discontinuities every 100000.0, however at such scales this
75 * usually shouldn't be noticeable. */
76 vector2 p = fmod(co, 100000.0) + precision_correction;
77
78 return noise("snoise", p.x, p.y);
79}
80
82{
83 vector3 precision_correction = 0.5 * vector3(float(fabs(co.x) >= 1000000.0),
84 float(fabs(co.y) >= 1000000.0),
85 float(fabs(co.z) >= 1000000.0));
86 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
87 * representation issues. This causes discontinuities every 100000.0, however at such scales this
88 * usually shouldn't be noticeable. */
89 vector3 p = fmod(co, 100000.0) + precision_correction;
90
91 return noise("snoise", p);
92}
93
94float safe_snoise(vector4 co)
95{
96 vector4 precision_correction = 0.5 * vector4(float(fabs(co.x) >= 1000000.0),
97 float(fabs(co.y) >= 1000000.0),
98 float(fabs(co.z) >= 1000000.0),
99 float(fabs(co.w) >= 1000000.0));
100 /* Repeat Perlin noise texture every 100000.0 on each axis to prevent floating point
101 * representation issues. This causes discontinuities every 100000.0, however at such scales this
102 * usually shouldn't be noticeable. */
103 vector4 p = fmod(co, 100000.0) + precision_correction;
104
105 return noise("snoise", vector3(p.x, p.y, p.z), p.w);
106}
107
108#define NOISE_FBM(T) \
109 float noise_fbm(T co, float detail, float roughness, float lacunarity, int use_normalize) \
110 { \
111 T p = co; \
112 float fscale = 1.0; \
113 float amp = 1.0; \
114 float maxamp = 0.0; \
115 float sum = 0.0; \
116\
117 for (int i = 0; i <= int(detail); i++) { \
118 float t = safe_snoise(fscale * p); \
119 sum += t * amp; \
120 maxamp += amp; \
121 amp *= roughness; \
122 fscale *= lacunarity; \
123 } \
124 float rmd = detail - floor(detail); \
125 if (rmd != 0.0) { \
126 float t = safe_snoise(fscale * p); \
127 float sum2 = sum + t * amp; \
128 return use_normalize ? \
129 mix(0.5 * sum / maxamp + 0.5, 0.5 * sum2 / (maxamp + amp) + 0.5, rmd) : \
130 mix(sum, sum2, rmd); \
131 } \
132 else { \
133 return use_normalize ? 0.5 * sum / maxamp + 0.5 : sum; \
134 } \
135 }
136
137#define NOISE_MULTI_FRACTAL(T) \
138 float noise_multi_fractal(T co, float detail, float roughness, float lacunarity) \
139 { \
140 T p = co; \
141 float value = 1.0; \
142 float pwr = 1.0; \
143\
144 for (int i = 0; i <= (int)detail; i++) { \
145 value *= (pwr * safe_snoise(p) + 1.0); \
146 pwr *= roughness; \
147 p *= lacunarity; \
148 } \
149\
150 float rmd = detail - floor(detail); \
151 if (rmd != 0.0) { \
152 value *= (rmd * pwr * safe_snoise(p) + 1.0); /* correct? */ \
153 } \
154\
155 return value; \
156 }
157
158#define NOISE_HETERO_TERRAIN(T) \
159 float noise_hetero_terrain(T co, float detail, float roughness, float lacunarity, float offset) \
160 { \
161 T p = co; \
162 float pwr = roughness; \
163\
164 /* first unscaled octave of function; later octaves are scaled */ \
165 float value = offset + safe_snoise(p); \
166 p *= lacunarity; \
167\
168 for (int i = 1; i <= (int)detail; i++) { \
169 float increment = (safe_snoise(p) + offset) * pwr * value; \
170 value += increment; \
171 pwr *= roughness; \
172 p *= lacunarity; \
173 } \
174\
175 float rmd = detail - floor(detail); \
176 if (rmd != 0.0) { \
177 float increment = (safe_snoise(p) + offset) * pwr * value; \
178 value += rmd * increment; \
179 } \
180\
181 return value; \
182 }
183
184#define NOISE_HYBRID_MULTI_FRACTAL(T) \
185 float noise_hybrid_multi_fractal( \
186 T co, float detail, float roughness, float lacunarity, float offset, float gain) \
187 { \
188 T p = co; \
189 float pwr = 1.0; \
190 float value = 0.0; \
191 float weight = 1.0; \
192\
193 for (int i = 0; (weight > 0.001) && (i <= (int)detail); i++) { \
194 if (weight > 1.0) { \
195 weight = 1.0; \
196 } \
197\
198 float signal = (safe_snoise(p) + offset) * pwr; \
199 pwr *= roughness; \
200 value += weight * signal; \
201 weight *= gain * signal; \
202 p *= lacunarity; \
203 } \
204\
205 float rmd = detail - floor(detail); \
206 if ((rmd != 0.0) && (weight > 0.001)) { \
207 if (weight > 1.0) { \
208 weight = 1.0; \
209 } \
210 float signal = (safe_snoise(p) + offset) * pwr; \
211 value += rmd * weight * signal; \
212 } \
213\
214 return value; \
215 }
216
217#define NOISE_RIDGED_MULTI_FRACTAL(T) \
218 float noise_ridged_multi_fractal( \
219 T co, float detail, float roughness, float lacunarity, float offset, float gain) \
220 { \
221 T p = co; \
222 float pwr = roughness; \
223\
224 float signal = offset - fabs(safe_snoise(p)); \
225 signal *= signal; \
226 float value = signal; \
227 float weight = 1.0; \
228\
229 for (int i = 1; i <= (int)detail; i++) { \
230 p *= lacunarity; \
231 weight = clamp(signal * gain, 0.0, 1.0); \
232 signal = offset - fabs(safe_snoise(p)); \
233 signal *= signal; \
234 signal *= weight; \
235 value += signal * pwr; \
236 pwr *= roughness; \
237 } \
238\
239 return value; \
240 }
241
242/* Noise fBM. */
243
244NOISE_FBM(float)
245NOISE_FBM(vector2)
247NOISE_FBM(vector4)
248
249/* Noise Multi-fractal. */
250
252NOISE_MULTI_FRACTAL(vector2)
254NOISE_MULTI_FRACTAL(vector4)
255
256/* Noise Hetero Terrain. */
257
262
263/* Noise Hybrid Multi-fractal. */
264
269
270/* Noise Ridged Multi-fractal. */
271
276
277#undef vector3
draw_view in_light_buf[] float
ccl_device_inline float2 fmod(const float2 a, const float b)
ccl_device_inline float2 fabs(const float2 a)
float safe_snoise(float co)
Definition node_noise.h:59
float safe_noise(float co)
Definition node_noise.h:10
#define NOISE_MULTI_FRACTAL(T)
Definition node_noise.h:137
#define NOISE_HETERO_TERRAIN(T)
Definition node_noise.h:158
#define vector3
Definition node_noise.h:8
#define NOISE_FBM(T)
Definition node_noise.h:108
#define NOISE_HYBRID_MULTI_FRACTAL(T)
Definition node_noise.h:184
#define NOISE_RIDGED_MULTI_FRACTAL(T)
Definition node_noise.h:217