Skip to content

Commit 9f81586

Browse files
committed
add definition and implementation of fractal2d for GLSL, MSL, OSL and MDL, along with simple unit tests for coverage - largely just cloning the work done for fractal3d
1 parent c829019 commit 9f81586

27 files changed

Lines changed: 525 additions & 42 deletions

documents/Specification/MaterialX.Proposals.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,6 @@ Inheritance of material-type custom nodes is also allowed, so that new or change
173173

174174
### Noise Nodes
175175

176-
<a id="node-fractal2d"> </a>
177-
178-
We have a standard 3d fractal noise, but a 2d variant would be useful as well.
179-
180-
* **`fractal2d`**: Zero-centered 2D Fractal noise in 1, 2, 3 or 4 channels, created by summing several octaves of 2D Perlin noise, increasing the frequency and decreasing the amplitude at each octave.
181-
* `amplitude` (float or vector<em>N</em>): the center-to-peak amplitude of the noise (peak-to-peak amplitude is 2x this value). Default is 1.0.
182-
* `octaves` (integer): the number of octaves of noise to be summed. Default is 3.
183-
* `lacunarity` (float or vector<em>N</em>): the exponential scale between successive octaves of noise; must be an integer value if period is non-zero so the result is properly tileable. VectorN-output types can provide either a float (isotropic) or vector<em>N</em> (anisotropic) values for lacunarity and diminish. Default is 2.0.
184-
* `diminish` (float or vector<em>N</em>): the rate at which noise amplitude is diminished for each octave. Should be between 0.0 and 1.0; default is 0.5. VectorN-output types can provide either a float (isotropic) or vector<em>N</em> (anisotropic) values for lacunarity and diminish.
185-
* `period` (float or vector<em>N</em>): the positive integer distance at which the noise function returns the same value for texture coordinates repeated at that step. Default is 0, meaning the noise is not periodic.
186-
* `texcoord` (vector2): the 2D texture coordinate at which the noise is evaluated. Default is to use the first set of texture coordinates.
187-
188176
<a id="node-cellnoise1d"> </a>
189177

190178
1D Cell noise was proposed an an alternative approach to random value generation.

documents/Specification/MaterialX.StandardNodes.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,37 +238,41 @@ Standard Noise nodes:
238238
* **`noise2d`**: 2D Perlin noise in 1, 2, 3 or 4 channels.
239239
* `amplitude` (float or vector<em>N</em>): the center-to-peak amplitude of the noise (peak-to-peak amplitude is 2x this value). Default is 1.0.
240240
* `pivot` (float): the center value of the output noise; effectively, this value is added to the result after the Perlin noise is multiplied by `amplitude`. Default is 0.0.
241-
* `period` (float or vector<em>N</em>): the positive integer distance at which the noise function returns the same value for texture coordinates repeated at that step. Default is 0, meaning the noise is not periodic.
242241
* `texcoord` (vector2): the 2D texture coordinate at which the noise is evaluated. Default is to use the first set of texture coordinates.
243242

244243
<a id="node-noise3d"> </a>
245244

246245
* **`noise3d`**: 3D Perlin noise in 1, 2, 3 or 4 channels.
247246
* `amplitude` (float or vector<em>N</em>): the center-to-peak amplitude of the noise (peak-to-peak amplitude is 2x this value). Default is 1.0.
248247
* `pivot` (float): the center value of the output noise; effectively, this value is added to the result after the Perlin noise is multiplied by `amplitude`. Default is 0.0.
249-
* `period` (float or vector<em>N</em>): the positive integer distance at which the noise function returns the same value for position coordinates repeated at that step. Default is 0, meaning the noise is not periodic.
250248
* `position` (vector3): the 3D position at which the noise is evaluated. Default is to use the current 3D object-space coordinate.
251249

250+
<a id="node-fractal2d"> </a>
251+
252+
* **`fractal2d`**: Zero-centered 2D Fractal noise in 1, 2, 3 or 4 channels, created by summing several octaves of 2D Perlin noise, increasing the frequency and decreasing the amplitude at each octave.
253+
* `amplitude` (float or vector<em>N</em>): the center-to-peak amplitude of the noise (peak-to-peak amplitude is 2x this value). Default is 1.0.
254+
* `octaves` (integer): the number of octaves of noise to be summed. Default is 3.
255+
* `lacunarity` (float or vector<em>N</em>): the exponential scale between successive octaves of noise; must be an integer value if period is non-zero so the result is properly tileable. Default is 2.0. Vector<em>N</em>-output types can provide either a float (isotropic) or vector<em>N</em> (anisotropic) values for `lacunarity` and `diminish`.
256+
* `diminish` (float or vector<em>N</em>): the rate at which noise amplitude is diminished for each octave. Should be between 0.0 and 1.0; default is 0.5. Vector<em>N</em>-output types can provide either a float (isotropic) or vector<em>N</em> (anisotropic) values for `lacunarity` and `diminish`.
257+
* `texcoord` (vector2): the 2D texture coordinate at which the noise is evaluated. Default is to use the first set of texture coordinates.
258+
252259
<a id="node-fractal3d"> </a>
253260

254261
* **`fractal3d`**: Zero-centered 3D Fractal noise in 1, 2, 3 or 4 channels, created by summing several octaves of 3D Perlin noise, increasing the frequency and decreasing the amplitude at each octave.
255262
* `amplitude` (float or vector<em>N</em>): the center-to-peak amplitude of the noise (peak-to-peak amplitude is 2x this value). Default is 1.0.
256263
* `octaves` (integer): the number of octaves of noise to be summed. Default is 3.
257264
* `lacunarity` (float or vector<em>N</em>): the exponential scale between successive octaves of noise; must be an integer value if period is non-zero so the result is properly tileable. Default is 2.0. Vector<em>N</em>-output types can provide either a float (isotropic) or vector<em>N</em> (anisotropic) values for `lacunarity` and `diminish`.
258265
* `diminish` (float or vector<em>N</em>): the rate at which noise amplitude is diminished for each octave. Should be between 0.0 and 1.0; default is 0.5. Vector<em>N</em>-output types can provide either a float (isotropic) or vector<em>N</em> (anisotropic) values for `lacunarity` and `diminish`.
259-
* `period` (float or vector<em>N</em>): the positive integer distance at which the noise function returns the same value for position coordinates repeated at that step. Default is 0, meaning the noise is not periodic.
260266
* `position` (vector3): the 3D position at which the noise is evaluated. Default is to use the current 3D object-space coordinate.
261267

262268
<a id="node-cellnoise2d"> </a>
263269

264270
* **`cellnoise2d`**: 2D cellular noise, 1 or 3 channels (type float or vector3).
265-
* `period` (float or vector3): the positive integer distance at which the noise function returns the same value for texture coordinates repeated at that step. Default is 0, meaning the noise is not periodic.
266271
* `texcoord` (vector2): the 2D position at which the noise is evaluated. Default is to use the first set of texture coordinates.
267272

268273
<a id="node-cellnoise3d"> </a>
269274

270275
* **`cellnoise3d`**: 3D cellular noise, 1 or 3 channels (type float or vector3).
271-
* `period` (float or vector3): the positive integer distance at which the noise function returns the same value for position coordinates repeated at that step. Default is 0, meaning the noise is not periodic.
272276
* `position` (vector3): the 3D position at which the noise is evaluated. Default is to use the current 3D object-space coordinate.
273277

274278
<a id="node-worleynoise2d"> </a>

libraries/stdlib/genglsl/lib/mx_noise.glsl

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ vec3 mx_cell_noise_vec3(vec4 p)
398398
);
399399
}
400400

401-
float mx_fractal_noise_float(vec3 p, int octaves, float lacunarity, float diminish)
401+
float mx_fractal2d_noise_float(vec2 p, int octaves, float lacunarity, float diminish)
402402
{
403403
float result = 0.0;
404404
float amplitude = 1.0;
@@ -411,7 +411,7 @@ float mx_fractal_noise_float(vec3 p, int octaves, float lacunarity, float dimini
411411
return result;
412412
}
413413

414-
vec3 mx_fractal_noise_vec3(vec3 p, int octaves, float lacunarity, float diminish)
414+
vec3 mx_fractal2d_noise_vec3(vec2 p, int octaves, float lacunarity, float diminish)
415415
{
416416
vec3 result = vec3(0.0);
417417
float amplitude = 1.0;
@@ -424,16 +424,55 @@ vec3 mx_fractal_noise_vec3(vec3 p, int octaves, float lacunarity, float diminish
424424
return result;
425425
}
426426

427-
vec2 mx_fractal_noise_vec2(vec3 p, int octaves, float lacunarity, float diminish)
427+
vec2 mx_fractal2d_noise_vec2(vec2 p, int octaves, float lacunarity, float diminish)
428428
{
429-
return vec2(mx_fractal_noise_float(p, octaves, lacunarity, diminish),
430-
mx_fractal_noise_float(p+vec3(19, 193, 17), octaves, lacunarity, diminish));
429+
return vec2(mx_fractal2d_noise_float(p, octaves, lacunarity, diminish),
430+
mx_fractal2d_noise_float(p+vec2(19, 193), octaves, lacunarity, diminish));
431431
}
432432

433-
vec4 mx_fractal_noise_vec4(vec3 p, int octaves, float lacunarity, float diminish)
433+
vec4 mx_fractal2d_noise_vec4(vec2 p, int octaves, float lacunarity, float diminish)
434434
{
435-
vec3 c = mx_fractal_noise_vec3(p, octaves, lacunarity, diminish);
436-
float f = mx_fractal_noise_float(p+vec3(19, 193, 17), octaves, lacunarity, diminish);
435+
vec3 c = mx_fractal2d_noise_vec3(p, octaves, lacunarity, diminish);
436+
float f = mx_fractal2d_noise_float(p+vec2(19, 193), octaves, lacunarity, diminish);
437+
return vec4(c, f);
438+
}
439+
440+
float mx_fractal3d_noise_float(vec3 p, int octaves, float lacunarity, float diminish)
441+
{
442+
float result = 0.0;
443+
float amplitude = 1.0;
444+
for (int i = 0; i < octaves; ++i)
445+
{
446+
result += amplitude * mx_perlin_noise_float(p);
447+
amplitude *= diminish;
448+
p *= lacunarity;
449+
}
450+
return result;
451+
}
452+
453+
vec3 mx_fractal3d_noise_vec3(vec3 p, int octaves, float lacunarity, float diminish)
454+
{
455+
vec3 result = vec3(0.0);
456+
float amplitude = 1.0;
457+
for (int i = 0; i < octaves; ++i)
458+
{
459+
result += amplitude * mx_perlin_noise_vec3(p);
460+
amplitude *= diminish;
461+
p *= lacunarity;
462+
}
463+
return result;
464+
}
465+
466+
vec2 mx_fractal3d_noise_vec2(vec3 p, int octaves, float lacunarity, float diminish)
467+
{
468+
return vec2(mx_fractal3d_noise_float(p, octaves, lacunarity, diminish),
469+
mx_fractal3d_noise_float(p+vec3(19, 193, 17), octaves, lacunarity, diminish));
470+
}
471+
472+
vec4 mx_fractal3d_noise_vec4(vec3 p, int octaves, float lacunarity, float diminish)
473+
{
474+
vec3 c = mx_fractal3d_noise_vec3(p, octaves, lacunarity, diminish);
475+
float f = mx_fractal3d_noise_float(p+vec3(19, 193, 17), octaves, lacunarity, diminish);
437476
return vec4(c, f);
438477
}
439478

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "lib/mx_noise.glsl"
2+
3+
void mx_fractal2d_float(float amplitude, int octaves, float lacunarity, float diminish, vec2 texcoord, out float result)
4+
{
5+
float value = mx_fractal2d_noise_float(texcoord, octaves, lacunarity, diminish);
6+
result = value * amplitude;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "lib/mx_noise.glsl"
2+
3+
void mx_fractal2d_vector2(vec2 amplitude, int octaves, float lacunarity, float diminish, vec2 texcoord, out vec2 result)
4+
{
5+
vec2 value = mx_fractal2d_noise_vec2(texcoord, octaves, lacunarity, diminish);
6+
result = value * amplitude;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "lib/mx_noise.glsl"
2+
3+
void mx_fractal2d_vector3(vec3 amplitude, int octaves, float lacunarity, float diminish, vec2 texcoord, out vec3 result)
4+
{
5+
vec3 value = mx_fractal2d_noise_vec3(texcoord, octaves, lacunarity, diminish);
6+
result = value * amplitude;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "lib/mx_noise.glsl"
2+
3+
void mx_fractal2d_vector4(vec4 amplitude, int octaves, float lacunarity, float diminish, vec2 texcoord, out vec4 result)
4+
{
5+
vec4 value = mx_fractal2d_noise_vec4(texcoord, octaves, lacunarity, diminish);
6+
result = value * amplitude;
7+
}

libraries/stdlib/genglsl/mx_fractal3d_float.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
void mx_fractal3d_float(float amplitude, int octaves, float lacunarity, float diminish, vec3 position, out float result)
44
{
5-
float value = mx_fractal_noise_float(position, octaves, lacunarity, diminish);
5+
float value = mx_fractal3d_noise_float(position, octaves, lacunarity, diminish);
66
result = value * amplitude;
77
}

libraries/stdlib/genglsl/mx_fractal3d_vector2.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
void mx_fractal3d_vector2(vec2 amplitude, int octaves, float lacunarity, float diminish, vec3 position, out vec2 result)
44
{
5-
vec2 value = mx_fractal_noise_vec2(position, octaves, lacunarity, diminish);
5+
vec2 value = mx_fractal3d_noise_vec2(position, octaves, lacunarity, diminish);
66
result = value * amplitude;
77
}

libraries/stdlib/genglsl/mx_fractal3d_vector3.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
void mx_fractal3d_vector3(vec3 amplitude, int octaves, float lacunarity, float diminish, vec3 position, out vec3 result)
44
{
5-
vec3 value = mx_fractal_noise_vec3(position, octaves, lacunarity, diminish);
5+
vec3 value = mx_fractal3d_noise_vec3(position, octaves, lacunarity, diminish);
66
result = value * amplitude;
77
}

0 commit comments

Comments
 (0)