This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz42FractalNoise.js
117 lines (93 loc) · 3.79 KB
/
z42FractalNoise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
2D fractal noise image generation. Copyright (c) 2019 zett42.
https://github.com/zett42/PlasmaFractal
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
(function(global){
'use strict';
var module = global.z42fractal = {};
//----------------------------------------------------------------------------------------------------------------
/// Generate a grayscale fractal noise image.
module.generateFractalNoiseImageUint16 = function( destPixelsUint16, width, height, outputRange, params )
{
if( destPixelsUint16.length < width * height )
{
console.assert( false, "Array argument destPixelsUint16 is too small" );
return;
}
const scale = 0.001 * params.frequency;
const amp = params.amplitude * outputRange;
let i = 0;
for( let y = 0; y < height; ++y )
{
const yn = y * scale;
for( let x = 0; x < width; ++x, ++i )
{
const xn = x * scale;
const value = module.fractalNoiseHighContrast( xn, yn, 0, params.octaves, params.gain, params.lacunarity );
destPixelsUint16[ i ] = module.mod( value * amp, outputRange );
}
}
}
//----------------------------------------------------------------------------------------------------------------
/// Return fractal noise value in range of 0..1.
module.fractalNoise = function( x, y, z, octaves, gain, lacunarity )
{
let res = 0; // result
let f = 1; // frequency
let a = 1; // amplitude
let aSum = 0; // amplitude sum
for( let i = 0; i < octaves; i++ )
{
// All noise functions return values in the range of -1 to 1. Normalize to 0..1.
let n = 0.5 + noise.perlin2( x * f, y * f ) * 0.5;
res += n * a;
aSum += a;
a *= gain;
f *= lacunarity;
}
return res / aSum; // rescale back to 0..1
}
//----------------------------------------------------------------------------------------------------------------
/// Return fractal noise value in range of 0..1.
/// This function generates higher contrast output than fractalNoise() but values may occassionally
/// be outside of 0..1 range!
module.fractalNoiseHighContrast = function( x, y, z, octaves, gain, lacunarity )
{
let res = 0; // result
let f = 1; // frequency
let a = 1; // amplitude
for( let i = 0; i < octaves; i++ )
{
// All noise functions return values in the range of -1 to 1.
let n = noise.perlin2( x * f, y * f );
res += n * a;
a *= gain;
f *= lacunarity;
}
// scale to 0..1 (approx.)
return ( res + 1 ) * 0.5;
}
//----------------------------------------------------------------------------------------------------------------
/// True modulo function that only returns positive numbers
/// (compare with JS "%" operator which returns the remainder instead, which can be negative).
module.mod = function( a, n )
{
return a - ( n * Math.floor( a / n ) );
}
})(this);