perlin_noise 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.markdown +13 -9
- data/lib/perlin/curve.rb +6 -4
- data/lib/perlin/gradient_table.rb +13 -10
- data/lib/perlin/noise.rb +13 -13
- data/lib/perlin/version.rb +3 -1
- data/lib/perlin_noise.rb +2 -1
- data/perlin_noise.gemspec +1 -0
- metadata +11 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3d56e10274a7cddb359c2d92a70d74d9324284c004b5a9c4d597797fbfa6cc65
|
4
|
+
data.tar.gz: 84342c319a311ff1ced6309f886f6b96851da235d29afc1a278c9e6b15c72611
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 566092bd6aa5f67be9573df008a57b8af1a2098161bd006b502468e6646e80724a0a25e983196ce212ece258c7af67f49e5c0532a6da473ad01548cba35bf7f6
|
7
|
+
data.tar.gz: f0ae7777466b432346ddfc612e70a775eeffd2ae53fb7dc06bc5d74392b03c75b3dde8817098dc8df63d398ff3e6f9b7edb8b7e30076410be3d75bda2b053b95
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Junegunn Choi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -58,16 +58,16 @@ noises = Perlin::Noise.new 1, :seed => 12345
|
|
58
58
|
```
|
59
59
|
|
60
60
|
## Range of noise function
|
61
|
-
While the original algorithm outputs a number between -1.0 and 1.0,
|
61
|
+
While the original algorithm outputs a number between -1.0 and 1.0,
|
62
62
|
Perlin::Noise#[] manipulates this output and returns a number between 0.0 and 1.0 for ease of use.
|
63
63
|
Thus, noise values at all of the integer lattice points should be 0.5, not 0.
|
64
64
|
|
65
65
|
## Increasing the contrast
|
66
|
-
Even though the range of the noise function is from 0 to 1,
|
67
|
-
you'll rarely see a noise value close to either end,
|
66
|
+
Even though the range of the noise function is from 0 to 1,
|
67
|
+
you'll rarely see a noise value close to either end,
|
68
68
|
as most of the values are distributed around the center.
|
69
|
-
You might want to apply S-shaped curve functions defined in Perlin::Curve module
|
70
|
-
one or more times to push away those "grey" values to either end,
|
69
|
+
You might want to apply S-shaped curve functions defined in Perlin::Curve module
|
70
|
+
one or more times to push away those "grey" values to either end,
|
71
71
|
achiving more contrasted output.
|
72
72
|
|
73
73
|
```ruby
|
@@ -242,7 +242,7 @@ noises = Perlin::Noise.new(2)
|
|
242
242
|
contrast = Perlin::Curve.contrast(Perlin::Curve::CUBIC, 2)
|
243
243
|
|
244
244
|
bars = " ▁▂▃▄▅▆▇█".each_char.to_a
|
245
|
-
bar = lambda { |n|
|
245
|
+
bar = lambda { |n|
|
246
246
|
bars[ (bars.length * n).floor ]
|
247
247
|
}
|
248
248
|
|
@@ -312,8 +312,8 @@ end
|
|
312
312
|
▃▄▄▅▄▄▃▂▂▃▃▄▄▄▄▃▃▂▃▃▄▅▆▇███▇▇▆▆▆▅▄▃▂▁▂▂▃▄▄▅▆▇▇▇▇▆▆▅▅▄▃▃▃▃▃▄▅▆▆▇▇▇▆▅▄▃▂
|
313
313
|
▂▃▄▄▄▄▃▃▃▃▃▄▄▄▃▃▃▃▃▄▅▆▇▇█████▇▇▇▆▅▄▃▃▂▃▃▄▄▅▆▇▇▇▆▆▅▅▅▄▃▃▃▃▄▅▅▆▇▇▇▇▆▅▃▂▁
|
314
314
|
▂▃▃▄▄▄▄▃▃▃▃▄▄▃▃▃▃▃▄▄▅▆▇████████▇▇▆▆▅▄▄▄▄▄▅▅▅▆▆▆▆▅▅▄▄▄▃▃▃▃▄▅▆▆▇▇▇▇▆▄▂▁▁
|
315
|
-
▂▂▃▄▅▅▅▄▄▄▄▄▃▃▃▃▃▃▄▅▆▆▇██████████▇▇▆▆▅▅▅▅▅▅▅▅▅▅▄▄▄▄▃▃▃▃▃▃▄▅▆▆▇▇▇▆▅▃▂▁
|
316
|
-
▂▂▃▅▅▆▆▆▅▅▄▄▃▃▂▂▃▃▄▅▆▇▇▇██████████▇▇▇▆▆▅▅▅▄▄▄▃▃▃▃▃▃▃▃▂▂▂▃▃▄▅▆▇▇▇▆▅▃▂▁
|
315
|
+
▂▂▃▄▅▅▅▄▄▄▄▄▃▃▃▃▃▃▄▅▆▆▇██████████▇▇▆▆▅▅▅▅▅▅▅▅▅▅▄▄▄▄▃▃▃▃▃▃▄▅▆▆▇▇▇▆▅▃▂▁
|
316
|
+
▂▂▃▅▅▆▆▆▅▅▄▄▃▃▂▂▃▃▄▅▆▇▇▇██████████▇▇▇▆▆▅▅▅▄▄▄▃▃▃▃▃▃▃▃▂▂▂▃▃▄▅▆▇▇▇▆▅▃▂▁
|
317
317
|
▂▃▄▅▆▇▇▆▆▅▅▄▃▂▂▂▂▃▄▅▆▆▇▇▇▇▇▇▇▇▇▇▇██▇▇▇▆▆▅▅▄▃▃▂▂▂▂▂▃▃▂▂▂▂▂▃▃▄▅▆▇▇▆▅▃▂▁▁
|
318
318
|
▃▄▅▆▇▇▇▇▆▅▅▄▃▂▁▁▂▃▄▅▅▆▆▇▇▇▆▆▆▆▆▆▇▇▇▇▇▇▆▆▅▄▃▃▂▂▂▂▂▂▃▃▂▂▂▂▂▂▃▄▅▆▆▆▆▅▄▃▂▂
|
319
319
|
▃▄▅▆▇▇▇▇▆▅▄▃▂▂▁▁▁▂▃▄▅▅▆▆▆▆▅▅▅▅▅▅▆▇▇▇▇▇▆▅▅▄▃▂▂▂▂▂▃▃▃▃▃▃▂▂▂▂▂▃▄▅▆▆▆▅▅▄▃▃
|
@@ -342,7 +342,7 @@ end
|
|
342
342
|
▃▃▂▃▃▄▅▆▆▅▅▄▃▃▃▃▃▃▃▃▂▂▁ ▁▂▃▃▄▅▆▆▆▅▄▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▂▃▄▅▅▆▅▄▄▃▂
|
343
343
|
▄▃▂▃▃▄▅▆▆▆▅▄▄▃▃▃▃▃▂▂▁▁ ▁▂▃▃▄▅▅▅▅▅▄▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁ ▁▁▂▂▃▄▅▅▄▃▂▂▁
|
344
344
|
▄▃▃▃▄▅▆▆▇▇▆▅▅▄▄▃▃▂▂▁▁▁ ▁▁▂▃▃▄▄▅▅▅▅▄▄▃▃▃▂▁▁▁ ▁▁▁▁▁ ▁▁▂▃▃▄▄▄▃▂▁▁
|
345
|
-
▄▄▃▃▄▅▆▇▇▇▆▆▅▅▄▄▃▂▂▁▁ ▁▁▂▃▃▄▄▄▄▄▅▅▅▄▄▄▃▃▂▁▁ ▁▁▁▁▁ ▁▁▂▂▃▄▄▃▃▂▁
|
345
|
+
▄▄▃▃▄▅▆▇▇▇▆▆▅▅▄▄▃▂▂▁▁ ▁▁▂▃▃▄▄▄▄▄▅▅▅▄▄▄▃▃▂▁▁ ▁▁▁▁▁ ▁▁▂▂▃▄▄▃▃▂▁
|
346
346
|
▅▄▄▄▄▅▆▆▇▇▇▆▆▅▅▄▃▃▂▁▁▁ ▁▂▂▃▄▄▄▃▃▃▄▄▅▅▅▅▄▄▃▂▁▁ ▁▁▁▁▁▁▁▁▁▁▁▂▂▃▄▄▄▃▂▁▁
|
347
347
|
▅▄▄▄▄▄▅▆▇▇▆▆▆▆▅▅▄▃▃▂▁▁▁▁▂▃▄▄▅▅▄▃▃▃▃▄▅▅▆▅▅▄▄▂▁▁ ▁▁▂▂▂▂▂▂▁▁▁▁▂▃▄▄▅▄▄▃▂▁
|
348
348
|
▅▄▄▃▃▄▅▅▆▆▆▆▆▅▅▅▅▄▄▃▂▂▂▂▃▄▅▅▅▅▄▃▃▃▃▄▅▅▆▆▅▅▄▂▂▁▁▁▁▂▂▃▃▃▃▂▂▂▂▂▃▃▄▅▅▅▅▄▃▂
|
@@ -487,6 +487,10 @@ end
|
|
487
487
|
|
488
488
|
- [Matthew Johnston](https://github.com/warmwaffles)
|
489
489
|
|
490
|
+
## License
|
491
|
+
|
492
|
+
[MIT](LICENSE)
|
493
|
+
|
490
494
|
## References
|
491
495
|
- Texturing & modeling: a procedural approach by David S. Ebert et al.
|
492
496
|
- Improving Noise by Ken Perlin (http://mrl.nyu.edu/~perlin/paper445.pdf)
|
data/lib/perlin/curve.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Perlin
|
2
4
|
module Curve
|
3
5
|
LINEAR = proc { |t| t }
|
4
|
-
CUBIC = proc { |t| 3 * (t
|
5
|
-
QUINTIC = proc { |t| 6 * (t
|
6
|
+
CUBIC = proc { |t| 3 * (t**2) - 2 * (t**3) }
|
7
|
+
QUINTIC = proc { |t| 6 * (t**5) - 15 * (t**4) + 10 * (t**3) }
|
6
8
|
|
7
9
|
# Returns a Proc object which applies S-curve function to
|
8
10
|
# a given number between 0 and 1.
|
9
11
|
# @param[Proc] curve
|
10
|
-
# @param[
|
12
|
+
# @param[Integer] times
|
11
13
|
# @return[Proc]
|
12
|
-
def self.contrast
|
14
|
+
def self.contrast(curve, times)
|
13
15
|
lambda { |n|
|
14
16
|
times.times do
|
15
17
|
n = curve.call n
|
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Perlin
|
2
4
|
if RUBY_VERSION =~ /^1\.8\./
|
3
5
|
class Random
|
4
|
-
def initialize
|
6
|
+
def initialize(*seed)
|
5
7
|
# FIXME: Sets the global seed value; this is misleading
|
6
8
|
srand *seed
|
7
9
|
end
|
8
10
|
|
9
|
-
def rand
|
10
|
-
Kernel.rand
|
11
|
+
def rand(*interval)
|
12
|
+
Kernel.rand(*interval)
|
11
13
|
end
|
12
14
|
end
|
13
15
|
else
|
@@ -26,13 +28,14 @@ module Perlin
|
|
26
28
|
@vectors = Array.new(interval) { random_unit_vector }
|
27
29
|
end
|
28
30
|
|
29
|
-
def []
|
30
|
-
@vectors[index
|
31
|
+
def [](*coords)
|
32
|
+
@vectors[index(*coords)]
|
31
33
|
end
|
32
34
|
|
33
|
-
|
35
|
+
private
|
36
|
+
|
34
37
|
# A simple hashing
|
35
|
-
def index
|
38
|
+
def index(*coords)
|
36
39
|
s = coords.last
|
37
40
|
coords.reverse[1..-1].each do |c|
|
38
41
|
s = perm(s) + c
|
@@ -40,17 +43,17 @@ module Perlin
|
|
40
43
|
perm(s)
|
41
44
|
end
|
42
45
|
|
43
|
-
def perm
|
46
|
+
def perm(s)
|
44
47
|
@table[s % @interval]
|
45
48
|
end
|
46
49
|
|
47
50
|
def random_unit_vector
|
48
51
|
while true
|
49
52
|
v = Vector[*Array.new(@dim) { @random.rand * 2 - 1 }]
|
50
|
-
# Discards vectors whose length greater than 1 to avoid bias in
|
53
|
+
# Discards vectors whose length greater than 1 to avoid bias in
|
54
|
+
# distribution
|
51
55
|
break if v.r > 0 && v.r <= 1
|
52
56
|
end
|
53
|
-
r = v.r
|
54
57
|
v.map { |e| e / v.r }
|
55
58
|
end
|
56
59
|
end
|
data/lib/perlin/noise.rb
CHANGED
@@ -6,7 +6,7 @@ module Perlin
|
|
6
6
|
:seed => nil
|
7
7
|
}
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize(dim, options = {})
|
10
10
|
options = DEFAULT_OPTIONS.merge options
|
11
11
|
|
12
12
|
@dim = dim
|
@@ -14,10 +14,10 @@ module Perlin
|
|
14
14
|
@curve = options.fetch(:curve)
|
15
15
|
@seed = options.fetch(:seed)
|
16
16
|
|
17
|
-
raise ArgumentError.new(
|
18
|
-
raise ArgumentError.new(
|
19
|
-
raise ArgumentError.new(
|
20
|
-
raise ArgumentError.new(
|
17
|
+
raise ArgumentError.new('Invalid dimension: must be a positive integer') unless @dim.is_a?(Integer) && @dim > 0
|
18
|
+
raise ArgumentError.new('Invalid interval: must be a positive integer') unless @interval.is_a?(Integer) && @interval > 0
|
19
|
+
raise ArgumentError.new('Invalid curve specified: must be a Proc object') unless @curve.is_a?(Proc)
|
20
|
+
raise ArgumentError.new('Invalid seed: must be a number') unless @seed.nil? || @seed.is_a?(Numeric)
|
21
21
|
|
22
22
|
# Generate pseudo-random gradient vector for each grid point
|
23
23
|
@gradient_table = Perlin::GradientTable.new @dim, @interval, @seed
|
@@ -25,7 +25,7 @@ module Perlin
|
|
25
25
|
|
26
26
|
# @param [*coords] Coordinates
|
27
27
|
# @return [Float] Noise value between (-1..1)
|
28
|
-
def []
|
28
|
+
def [](*coords)
|
29
29
|
raise ArgumentError.new("Invalid coordinates") unless coords.length == @dim
|
30
30
|
|
31
31
|
coords = Vector[*coords]
|
@@ -40,7 +40,7 @@ module Perlin
|
|
40
40
|
# "The value of each gradient ramp is computed by means of a scalar
|
41
41
|
# product (dot product) between the gradient vectors of each grid point
|
42
42
|
# and the vectors from the grid points."
|
43
|
-
gv = @gradient_table[
|
43
|
+
gv = @gradient_table[*(cell + idx).to_a]
|
44
44
|
nf[idx.to_a] = gv.inner_product(diff - idx)
|
45
45
|
end
|
46
46
|
|
@@ -63,12 +63,13 @@ module Perlin
|
|
63
63
|
(nf[[]] + 1) * 0.5
|
64
64
|
end
|
65
65
|
|
66
|
-
|
67
|
-
|
66
|
+
private
|
67
|
+
|
68
|
+
def iterate(dim, length, &block)
|
68
69
|
iterate_recursive dim, length, Array.new(dim, 0), &block
|
69
70
|
end
|
70
71
|
|
71
|
-
def iterate_recursive
|
72
|
+
def iterate_recursive(dim, length, idx, &block)
|
72
73
|
length.times do |i|
|
73
74
|
idx[dim - 1] = i
|
74
75
|
if dim == 1
|
@@ -78,6 +79,5 @@ module Perlin
|
|
78
79
|
end
|
79
80
|
end
|
80
81
|
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
82
|
+
end
|
83
|
+
end
|
data/lib/perlin/version.rb
CHANGED
data/lib/perlin_noise.rb
CHANGED
data/perlin_noise.gemspec
CHANGED
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Junegunn Choi"]
|
9
9
|
s.email = ["junegunn.c@gmail.com"]
|
10
10
|
s.homepage = "https://github.com/junegunn/perlin_noise"
|
11
|
+
s.license = 'MIT'
|
11
12
|
s.summary = %q{Perlin noise generator}
|
12
13
|
s.description = %q{Perlin noise implemented in Ruby}
|
13
14
|
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: perlin_noise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Junegunn Choi
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2018-04-08 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: Perlin noise implemented in Ruby
|
15
14
|
email:
|
@@ -18,8 +17,9 @@ executables: []
|
|
18
17
|
extensions: []
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
21
|
-
- .gitignore
|
20
|
+
- ".gitignore"
|
22
21
|
- Gemfile
|
22
|
+
- LICENSE
|
23
23
|
- README.markdown
|
24
24
|
- Rakefile
|
25
25
|
- lib/perlin/curve.rb
|
@@ -30,28 +30,28 @@ files:
|
|
30
30
|
- perlin_noise.gemspec
|
31
31
|
- test/test_perlin.rb
|
32
32
|
homepage: https://github.com/junegunn/perlin_noise
|
33
|
-
licenses:
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
34
36
|
post_install_message:
|
35
37
|
rdoc_options: []
|
36
38
|
require_paths:
|
37
39
|
- lib
|
38
40
|
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
41
|
requirements:
|
41
|
-
- -
|
42
|
+
- - ">="
|
42
43
|
- !ruby/object:Gem::Version
|
43
44
|
version: '0'
|
44
45
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
46
|
requirements:
|
47
|
-
- -
|
47
|
+
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
49
|
version: '0'
|
50
50
|
requirements: []
|
51
51
|
rubyforge_project: perlin
|
52
|
-
rubygems_version:
|
52
|
+
rubygems_version: 2.7.4
|
53
53
|
signing_key:
|
54
|
-
specification_version:
|
54
|
+
specification_version: 4
|
55
55
|
summary: Perlin noise generator
|
56
56
|
test_files:
|
57
57
|
- test/test_perlin.rb
|