Skip to content

Commit 73d427f

Browse files
committed
Update to using SimplexNoise2 as modules.
1 parent 7b3d511 commit 73d427f

File tree

21 files changed

+163
-129
lines changed

21 files changed

+163
-129
lines changed

basics/math/data/noise_image.png

14.8 KB
Loading

basics/math/noise4_tap.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Coord = Struct.new(:mx, :my, :mz, :az, :al)
55

66
class ForD < Processing::App
7-
attr_reader :half_w, :half_h, :radius, :spin_x, :spin, :coords
7+
attr_reader :half_w, :half_h, :radius, :spin_x, :spin, :coords, :smth
88

99
def settings
1010
size(480, 480, P3D)
@@ -20,7 +20,7 @@ def setup
2020
@radius = height * 0.4
2121
@spin_x = 0.0
2222
@spin = 0.0
23-
23+
@smth = false
2424
angle = ((1.0 + Math.sqrt(5)) / 2.0 - 1) * TWO_PI # Fibonacci distribution
2525
@coords = (0..2_000).map do |i|
2626
inc = Math.asin(i / 1_000.0 - 1.0) # inclination
@@ -31,9 +31,9 @@ def setup
3131
rotate_y(az)
3232
rotate_z(inc)
3333
translate(radius, 0, 0)
34-
coord.mx = model_x(0, 0, 0) * 0.007
35-
coord.my = model_y(0, 0, 0) * 0.007
36-
coord.mz = modelZ(0, 0, 0) * 0.007
34+
coord.mx = g.model_x(0, 0, 0) * 0.007
35+
coord.my = g.model_y(0, 0, 0) * 0.007
36+
coord.mz = g.model_z(0, 0, 0) * 0.007
3737
coord.az = az
3838
coord.al = inc
3939
pop_matrix
@@ -54,10 +54,14 @@ def draw
5454
rotate_y(ci.az)
5555
rotate_z(ci.al)
5656
translate(radius, 0, 0)
57-
dst = (modelZ(0, 0, 0) + half_h) / 2 + 32
57+
dst = (g.model_z(0, 0, 0) + half_h) / 2 + 32
5858
stroke(dst, dst * 0.5, dst * 0.25)
5959
# 4D Simplex noise(x, y, z, time)
60-
ang = noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI
60+
if smth
61+
ang = SmoothNoise.noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI
62+
else
63+
ang = noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI
64+
end
6165
rotate_x(ang)
6266
line(0, 0, 0, 0, 15, 0)
6367
translate(0, 15, 0)
@@ -71,15 +75,7 @@ def draw
7175
end
7276

7377
def mouse_pressed
74-
mode = NoiseMode::OPEN_SMOOTH
75-
sketch_title mode.description
76-
noise_mode mode
77-
end
78-
79-
def mouse_released
80-
mode = NoiseMode::DEFAULT
81-
sketch_title mode.description
82-
noise_mode mode
78+
@smth = !smth
8379
end
8480
end
8581

basics/math/noise_1_d.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,28 @@
22
require 'picrate'
33

44
class Noise1D < Processing::App
5+
attr_reader :smth
56

67
def setup
78
sketch_title 'Noise 1D'
89
@xoff = 360
910
@x_increment = 0.01
1011
background 0
1112
no_stroke
13+
@smth = false
1214
end
1315

1416
def draw
1517
fill 0, 10
1618
rect 0, 0, width, height
17-
n = noise(@xoff) * width
19+
n = smth ? SmoothNoise.noise(@xoff) * width : noise(@xoff) * width
1820
@xoff += @x_increment
1921
fill 200
2022
ellipse n, height / 2, 64, 64
2123
end
2224

2325
def mouse_pressed
24-
mode = NoiseMode::OPEN_SMOOTH
25-
sketch_title mode.description
26-
noise_mode mode
27-
end
28-
29-
def mouse_released
30-
mode = NoiseMode::DEFAULT
31-
sketch_title mode.description
32-
noise_mode mode
26+
@smth = !smth
3327
end
3428

3529
def settings

basics/math/noise_2_d.rb

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
11
# frozen_string_literal: true
22
require 'picrate'
33

4+
# Noise2D
5+
# by Daniel Shiffman.
6+
#
7+
# Using 2D noise to create simple texture.
48
class Noise2D < Processing::App
5-
# Noise2D
6-
# by Daniel Shiffman.
7-
#
8-
# Using 2D noise to create simple texture.
9+
attr_reader :smth
910

1011
def setup
1112
sketch_title 'Noise 2D'
12-
@increment = 0.02
13+
@inc = 0.02
14+
@smth = false
1315
end
1416

1517
def draw
1618
background 0
1719
load_pixels
18-
xoff = 0.0
19-
(0...width).each do |x|
20-
xoff += @increment
21-
yoff = 0.0
22-
(0...height).each do |y|
23-
yoff += @increment
24-
bright = noise(xoff, yoff) * 255
25-
pixels[x + y * width] = color(bright)
20+
grid(width, height) do |x, y|
21+
if smth
22+
bright = (SmoothNoise.noise(x * @inc, y * @inc) + 1) * 128
23+
else
24+
bright = (noise(x * @inc, y * @inc) + 1) * 128
2625
end
26+
pixels[x + y * width] = color(bright)
2727
end
2828
update_pixels
2929
end
3030

3131
def mouse_pressed
32-
mode = NoiseMode::OPEN_SMOOTH
33-
sketch_title mode.description
34-
noise_mode mode
35-
end
36-
37-
def mouse_released
38-
mode = NoiseMode::DEFAULT
39-
sketch_title mode.description
40-
noise_mode mode
32+
@smth = !smth
4133
end
4234

4335
def settings

basics/math/noise_3_d.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,33 @@
55
# Using 3D noise to create simple animated texture.
66
# Here, the third dimension ('z') is treated as time.
77
class Noise3D < Processing::App
8-
attr_reader :increment, :z_increment
8+
attr_reader :bright, :inc, :z_increment, :smth
99

1010
def setup
1111
sketch_title 'Noise 3D'
1212
frame_rate 30
13-
@increment = 0.01
13+
@inc = 0.01
1414
@zoff = 0.0
1515
@z_increment = 0.02
1616
end
1717

1818
def draw
1919
background 0
2020
load_pixels
21-
xoff = 0.0
22-
(0...width).each do |x|
23-
xoff += increment
24-
yoff = 0.0
25-
(0...height).each do |y|
26-
yoff += increment
27-
bright = noise(xoff, yoff, @zoff) * 255
28-
pixels[x + y * width] = color(bright, bright, bright)
21+
grid(width, height) do |x, y|
22+
if smth
23+
@bright = (SmoothNoise.noise(x * inc, y * inc, @zoff) + 1) * 128
24+
else
25+
@bright = (noise(x * inc, y * inc, @zoff) + 1) * 128
2926
end
27+
pixels[x + y * width] = color(bright, bright, bright)
3028
end
3129
update_pixels
3230
@zoff += z_increment
3331
end
3432

3533
def mouse_pressed
36-
mode = NoiseMode::OPEN_SMOOTH
37-
sketch_title mode.description
38-
noise_mode mode
39-
end
40-
41-
def mouse_released
42-
mode = NoiseMode::DEFAULT
43-
sketch_title mode.description
44-
noise_mode mode
34+
@smth = !smth
4535
end
4636

4737
def settings

basics/math/noise_animation.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ def draw
4545
point(pos.x, pos.y)
4646
end
4747
# puts frame_count
48-
save_frame(data_path('fr###.tiff'))
49-
48+
# save_frame(data_path('fr###.tiff'))
5049
return unless frame_count == FRAMES
5150

52-
# save_frame(data_path('expt6.png'))
5351
no_loop
5452
puts 'finished'
5553
end

basics/math/noise_image.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
require 'picrate'
22
# OpenSimplex2 has a range -1.0 to 1.0
33
class NoiseImage < Processing::App
4+
attr_reader :col, :smth
45
SCALE = 0.02
56

67
def setup
78
sketch_title 'Noise Image'
89
background(0)
910
stroke(255)
1011
no_fill
12+
@smth = false
1113
end
1214

1315
def draw
1416
background(0)
1517
scale = 0.02
1618
load_pixels
1719
grid(500, 500) do |x, y|
18-
col = noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
20+
if smth
21+
@col = SmoothNoise.noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
22+
else
23+
@col = noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
24+
end
1925
pixels[x + width * y] = color(col, 0, 0)
2026
end
2127
update_pixels
@@ -25,6 +31,10 @@ def draw
2531
def settings
2632
size 500, 500
2733
end
34+
35+
def mouse_pressed
36+
@smth = !smth
37+
end
2838
end
2939

3040
NoiseImage.new

basics/math/noise_wave.rb

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@
33
# Noise Wave
44
# by Daniel Shiffman.
55
#
6-
# Using Perlin Noise to generate a wave-like pattern.
6+
# Using Simplex Noise to generate a wave-like pattern.
77
class NoiseWave < Processing::App
88

9-
attr_reader :yoff # 2nd dimension of perlin noise
9+
attr_reader :y, :yoff, :inc, :smth # 2nd dimension of perlin noise
1010

1111
def setup
1212
sketch_title 'Noise Wave'
1313
@yoff = 0.0
14+
@smth = false
15+
@inc = 0.005
1416
end
1517

1618
def draw
1719
background(51)
1820
fill(255)
1921
# We are going to draw a polygon out of the wave points
2022
begin_shape
21-
xoff = 0 # Option #1: 2D Noise
2223
# xoff = yoff # Option #2: 1D Noise
2324
# Iterate over horizontal pixels
2425
(0..width).step(10) do |x|
2526
# Calculate a y value according to noise, map to
26-
y = map1d(noise(xoff, yoff), (0..1.0), (200..300)) # Option #1: 2D Noise
27-
# y = map1d(noise(xoff), (0..1.0), (200..300)) # Option #2: 1D Noise
27+
if smth
28+
@y = map1d(SmoothNoise.noise(x * inc, yoff), (-1.0..1.0), (200..300)) # Option #1: 2D Noise
29+
else
30+
@y = map1d(noise(x * inc, yoff), (-1.0..1.0), (200..300)) # Option #1: 2D Noise
31+
end
32+
# y = map1d(noise(xoff), (-1.0..1.0), (200..300)) # Option #2: 1D Noise
2833
# Set the vertex
2934
vertex(x, y)
3035
# Increment x dimension for noise
31-
xoff += 0.05
3236
end
3337
# increment y dimension for noise
3438
@yoff += 0.01
@@ -37,17 +41,8 @@ def draw
3741
end_shape(CLOSE)
3842
end
3943

40-
4144
def mouse_pressed
42-
mode = NoiseMode::OPEN_SMOOTH
43-
sketch_title mode.description
44-
noise_mode mode
45-
end
46-
47-
def mouse_released
48-
mode = NoiseMode::DEFAULT
49-
sketch_title mode.description
50-
noise_mode mode
45+
@smth = !smth
5146
end
5247

5348
def settings

contributed/decagon_grid.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/usr/bin/env jruby
2-
32
require 'picrate'
43
# Example of a grid of decagons with perlin noise after Lenny Herzog
54
class DecagonGrid < Processing::App
5+
load_library :pdf
66
NOISE_STRENGTH = 80.0
77
THETA = 36
8-
attr_reader :version, :noise_generator
8+
attr_reader :version, :save, :noise_generator
99

1010
def setup
1111
sketch_title 'Decagon Grid'
1212
frame_rate 24
1313
@version = 0
1414
@save = false
1515
@noise_generator = lambda do |x, y, seed|
16-
NOISE_STRENGTH * noise(
16+
NOISE_STRENGTH * SmoothNoise.tnoise(
1717
x / 150.0,
1818
y / 150.0 + seed * 2,
1919
seed
@@ -22,6 +22,7 @@ def setup
2222
end
2323

2424
def draw
25+
begin_record(PDF, data_path("Line_#{version}.pdf")) if save
2526
background(255)
2627
no_fill
2728
stroke(0)
@@ -38,14 +39,15 @@ def draw
3839
end
3940
end_shape(CLOSE)
4041
end
41-
end
42+
return unless save
4243

43-
def mouse_pressed
44-
noise_mode NoiseMode::OPEN_SMOOTH
44+
end_record
45+
@version += 1
46+
@save = false
4547
end
4648

4749
def mouse_pressed
48-
noise_mode NoiseMode::DEFAULT
50+
@save = true
4951
end
5052

5153
def settings

0 commit comments

Comments
 (0)