Class: Noisy::Noisy
- Inherits:
-
Object
- Object
- Noisy::Noisy
- Defined in:
- lib/noisy/noisy.rb
Instance Method Summary collapse
- #cosine_interpolate(a, b, x) ⇒ Object (also: #interpolate)
-
#initialize(seed = 1, octaves = 4) ⇒ Noisy
constructor
A new instance of Noisy.
- #interpolate_noise(x) ⇒ Object
- #interpolate_noise_2d(x, y) ⇒ Object
- #linear_interpolate(a, b, x) ⇒ Object
- #perlin_noise(x) ⇒ Object
- #perlin_noise_2d(x, y) ⇒ Object
- #perlin_noise_map(width, height) ⇒ Object
- #raw_noise(x) ⇒ Object
- #raw_noise_2d(x, y) ⇒ Object
- #smooth_noise(x) ⇒ Object
- #smooth_noise_2d(x, y) ⇒ Object
Constructor Details
#initialize(seed = 1, octaves = 4) ⇒ Noisy
Returns a new instance of Noisy.
3 4 5 6 7 |
# File 'lib/noisy/noisy.rb', line 3 def initialize(seed = 1, octaves = 4) @seed = seed @octaves = octaves @persistence = 0.25 end |
Instance Method Details
#cosine_interpolate(a, b, x) ⇒ Object Also known as: interpolate
38 39 40 41 |
# File 'lib/noisy/noisy.rb', line 38 def cosine_interpolate(a, b, x) f = (1 - Math.cos(x * Math::PI)) / 2 a * (1 - f) + b * f end |
#interpolate_noise(x) ⇒ Object
46 47 48 |
# File 'lib/noisy/noisy.rb', line 46 def interpolate_noise(x) interpolate(smooth_noise(x.floor), smooth_noise(x.floor + 1), x - x.floor) end |
#interpolate_noise_2d(x, y) ⇒ Object
50 51 52 53 54 |
# File 'lib/noisy/noisy.rb', line 50 def interpolate_noise_2d(x, y) a = interpolate(smooth_noise_2d(x.floor, y.floor), smooth_noise_2d(x.floor + 1, y.floor), x - x.floor) b = interpolate(smooth_noise_2d(x.floor, y.floor + 1), smooth_noise_2d(x.floor + 1, y.floor + 1), x - x.floor) interpolate(a, b, y - y.floor) end |
#linear_interpolate(a, b, x) ⇒ Object
34 35 36 |
# File 'lib/noisy/noisy.rb', line 34 def linear_interpolate(a, b, x) a * (1 - x) + b * x end |
#perlin_noise(x) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/noisy/noisy.rb', line 56 def perlin_noise(x) total = 0.0 (0...@octaves).each do |i| frequency = 2.0 ** i amplitude = @persistence ** i total += interpolate_noise(x * frequency) * amplitude end total end |
#perlin_noise_2d(x, y) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/noisy/noisy.rb', line 66 def perlin_noise_2d(x, y) total = 0.0 (0...@octaves).each do |i| frequency = 2.0 ** i amplitude = @persistence ** i total += interpolate_noise_2d(x * frequency, y * frequency) * amplitude end total end |
#perlin_noise_map(width, height) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/noisy/noisy.rb', line 76 def perlin_noise_map(width, height) noise = Array.new(width) noise.map! { Array.new(height) } (0...width).each do |x| (0...height).each do |y| noise[x][y] = perlin_noise_2d(x, y) end end noise end |
#raw_noise(x) ⇒ Object
9 10 11 12 |
# File 'lib/noisy/noisy.rb', line 9 def raw_noise(x) n = (x.to_i << 13) ^ x.to_i (1.0 - ((n * (n * n * 15731 * @seed + 789221 * @seed) + 1376312589 * @seed) & 0x7fffffff) / 1073741824.0) end |
#raw_noise_2d(x, y) ⇒ Object
14 15 16 17 18 |
# File 'lib/noisy/noisy.rb', line 14 def raw_noise_2d(x, y) n = (x + y * 57).to_i n = (n << 13) ^ n (1.0 - ((n * (n * n * 15731 * @seed + 789221 * @seed) + 1376312589 * @seed) & 0x7fffffff) / 1073741824.0) end |
#smooth_noise(x) ⇒ Object
20 21 22 23 24 |
# File 'lib/noisy/noisy.rb', line 20 def smooth_noise(x) left = raw_noise(x - 1.0) right = raw_noise(x + 1.0) raw_noise(x)/2.0 + left/4.0 + right/4.0 end |
#smooth_noise_2d(x, y) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/noisy/noisy.rb', line 26 def smooth_noise_2d(x, y) corners = raw_noise_2d(x - 1, y - 1) + raw_noise_2d(x - 1, y + 1) + raw_noise_2d(x + 1, y - 1) + raw_noise_2d(x + 1, y + 1) sides = raw_noise_2d(x, y - 1) + raw_noise_2d(x, y + 1) + raw_noise_2d(x - 1, y) + raw_noise_2d(x + 1, y) center = raw_noise_2d(x, y) center / 4 + sides / 8 + corners / 16 end |