Skip to content

Commit 7ee306e

Browse files
committed
Update some noise examples
1 parent b4008ff commit 7ee306e

File tree

8 files changed

+313
-84
lines changed

8 files changed

+313
-84
lines changed

basics/math/noise4_tap.rb

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
4+
Coord = Struct.new(:mx, :my, :mz, :az, :al)
5+
6+
class ForD < Processing::App
7+
attr_reader :half_w, :half_h, :radius, :spin_x, :spin, :coords
8+
9+
def settings
10+
size(480, 480, P3D)
11+
end
12+
13+
def setup
14+
sketch_title '4D Simplex Noise Test'
15+
background(0)
16+
stroke(255)
17+
fill(32, 255, 64)
18+
@half_w = width * 0.5
19+
@half_h = height * 0.5
20+
@radius = height * 0.4
21+
@spin_x = 0.0
22+
@spin = 0.0
23+
24+
angle = ((1.0 + Math.sqrt(5)) / 2.0 - 1) * TWO_PI # Fibonacci distribution
25+
@coords = (0..2_000).map do |i|
26+
inc = Math.asin(i / 1_000.0 - 1.0) # inclination
27+
az = angle * i # azimuth
28+
# Too lazy to do this the right way... precalculating both the angles and the coordinates
29+
Coord.new.tap do |coord|
30+
push_matrix
31+
rotate_y(az)
32+
rotate_z(inc)
33+
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
37+
coord.az = az
38+
coord.al = inc
39+
pop_matrix
40+
end
41+
end
42+
end
43+
44+
def draw
45+
background(0)
46+
@spin -= (mouse_x - pmouse_x) * 0.0001 if mouse_pressed?
47+
@spin_x += spin
48+
@spin *= 0.98
49+
push_matrix
50+
translate(half_w, half_h, -0)
51+
rotate_y(-spin_x)
52+
coords.each do |ci|
53+
push_matrix
54+
rotate_y(ci.az)
55+
rotate_z(ci.al)
56+
translate(radius, 0, 0)
57+
dst = (modelZ(0, 0, 0) + half_h) / 2 + 32
58+
stroke(dst, dst * 0.5, dst * 0.25)
59+
# 4D Simplex noise(x, y, z, time)
60+
ang = noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI
61+
rotate_x(ang)
62+
line(0, 0, 0, 0, 15, 0)
63+
translate(0, 15, 0)
64+
rotate_x(-10)
65+
line(0, 0, 0, 0, 4, 0)
66+
rotate_x(20)
67+
line(0, 0, 0, 0, 4, 0)
68+
pop_matrix
69+
end
70+
pop_matrix
71+
end
72+
73+
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
83+
end
84+
end
85+
86+
ForD.new

basics/math/noise_1_d.rb

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
# Noise1D.
2-
#
3-
# Using 1D Perlin Noise to assign location.
1+
# frozen_string_literal: true
2+
require 'picrate'
43

5-
def setup
6-
sketch_title 'Noise 1D'
7-
@xoff = 0.0
8-
@x_increment = 0.01
9-
background 0
10-
no_stroke
11-
end
4+
class Noise1D < Processing::App
125

13-
def draw
14-
fill 0, 10
15-
rect 0, 0, width, height
16-
n = noise(@xoff) * width
17-
@xoff += @x_increment
18-
fill 200
19-
ellipse n, height / 2, 64, 64
20-
end
6+
def setup
7+
sketch_title 'Noise 1D'
8+
@xoff = 360
9+
@x_increment = 0.01
10+
background 0
11+
no_stroke
12+
end
13+
14+
def draw
15+
fill 0, 10
16+
rect 0, 0, width, height
17+
n = noise(@xoff) * width
18+
@xoff += @x_increment
19+
fill 200
20+
ellipse n, height / 2, 64, 64
21+
end
2122

22-
def settings
23-
size 640, 360
23+
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
33+
end
34+
35+
def settings
36+
size 640, 360
37+
end
2438
end
39+
40+
Noise1D.new

basics/math/noise_2_d.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ def draw
1616
background 0
1717
load_pixels
1818
xoff = 0.0
19-
detail = map1d(mouse_x, (0..width), (0.1..0.6))
20-
noise_detail(8, detail)
2119
(0...width).each do |x|
2220
xoff += @increment
2321
yoff = 0.0
@@ -30,6 +28,18 @@ def draw
3028
update_pixels
3129
end
3230

31+
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
41+
end
42+
3343
def settings
3444
size 640, 360
3545
end

basics/math/noise_3_d.rb

+32-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
# frozen_string_literal: true
22
require 'picrate'
3-
# Noise3D.
4-
#
5-
# Using 3D noise to create simple animated texture.
3+
# Noise3D.
4+
#
5+
# Using 3D noise to create simple animated texture.
66
# Here, the third dimension ('z') is treated as time.
77
class Noise3D < Processing::App
88
attr_reader :increment, :z_increment
9-
9+
1010
def setup
1111
sketch_title 'Noise 3D'
12-
frame_rate 30
12+
frame_rate 30
1313
@increment = 0.01
1414
@zoff = 0.0
15-
@z_increment = 0.02
15+
@z_increment = 0.02
1616
end
17-
18-
def draw
19-
background 0
20-
load_pixels
21-
xoff = 0.0
22-
(0...width).each do |x|
17+
18+
def draw
19+
background 0
20+
load_pixels
21+
xoff = 0.0
22+
(0...width).each do |x|
2323
xoff += increment
24-
yoff = 0.0
25-
(0...height).each do |y|
26-
yoff += increment
27-
bright = noise(xoff, yoff, @zoff) * 255
24+
yoff = 0.0
25+
(0...height).each do |y|
26+
yoff += increment
27+
bright = noise(xoff, yoff, @zoff) * 255
2828
pixels[x + y * width] = color(bright, bright, bright)
2929
end
30-
end
31-
update_pixels
32-
@zoff += z_increment
30+
end
31+
update_pixels
32+
@zoff += z_increment
33+
end
34+
35+
def mouse_pressed
36+
mode = NoiseMode::OPEN_SMOOTH
37+
sketch_title mode.description
38+
noise_mode mode
3339
end
34-
40+
41+
def mouse_released
42+
mode = NoiseMode::DEFAULT
43+
sketch_title mode.description
44+
noise_mode mode
45+
end
46+
3547
def settings
3648
size 640, 360
3749
end

basics/math/noise_wave.rb

+46-33
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,58 @@
11
# frozen_string_literal: true
22
require 'picrate'
33
# Noise Wave
4-
# by Daniel Shiffman.
5-
#
6-
# Using Perlin Noise to generate a wave-like pattern.
4+
# by Daniel Shiffman.
5+
#
6+
# Using Perlin 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 :yoff # 2nd dimension of perlin noise
1010

11-
def setup
12-
sketch_title 'Noise Wave'
13-
@yoff = 0.0
14-
end
11+
def setup
12+
sketch_title 'Noise Wave'
13+
@yoff = 0.0
14+
end
1515

16-
def draw
17-
background(51)
18-
fill(255)
19-
# We are going to draw a polygon out of the wave points
20-
begin_shape
21-
xoff = 0 # Option #1: 2D Noise
22-
# xoff = yoff # Option #2: 1D Noise
23-
# Iterate over horizontal pixels
24-
(0..width).step(10) do |x|
25-
# 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
28-
# Set the vertex
29-
vertex(x, y)
30-
# Increment x dimension for noise
31-
xoff += 0.05
16+
def draw
17+
background(51)
18+
fill(255)
19+
# We are going to draw a polygon out of the wave points
20+
begin_shape
21+
xoff = 0 # Option #1: 2D Noise
22+
# xoff = yoff # Option #2: 1D Noise
23+
# Iterate over horizontal pixels
24+
(0..width).step(10) do |x|
25+
# 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
28+
# Set the vertex
29+
vertex(x, y)
30+
# Increment x dimension for noise
31+
xoff += 0.05
32+
end
33+
# increment y dimension for noise
34+
@yoff += 0.01
35+
vertex(width, height)
36+
vertex(0, height)
37+
end_shape(CLOSE)
3238
end
33-
# increment y dimension for noise
34-
@yoff += 0.01
35-
vertex(width, height)
36-
vertex(0, height)
37-
end_shape(CLOSE)
38-
end
3939

40-
def settings
41-
size(640, 360)
42-
end
40+
41+
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
51+
end
52+
53+
def settings
54+
size(640, 360)
55+
end
4356
end
4457

4558
NoiseWave.new

contributed/decagon_grid.rb

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# frozen_string_literal: true
1+
#!/usr/bin/env jruby
22

33
require 'picrate'
44
# Example of a grid of decagons with perlin noise after Lenny Herzog
55
class DecagonGrid < Processing::App
6-
load_library :pdf
76
NOISE_STRENGTH = 80.0
87
THETA = 36
9-
attr_reader :version, :save, :noise_generator
8+
attr_reader :version, :noise_generator
109

1110
def setup
1211
sketch_title 'Decagon Grid'
@@ -23,7 +22,6 @@ def setup
2322
end
2423

2524
def draw
26-
begin_record(PDF, data_path("Line_#{version}.pdf")) if save
2725
background(255)
2826
no_fill
2927
stroke(0)
@@ -40,15 +38,14 @@ def draw
4038
end
4139
end_shape(CLOSE)
4240
end
43-
return unless save
41+
end
4442

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

5047
def mouse_pressed
51-
@save = true
48+
noise_mode NoiseMode::DEFAULT
5249
end
5350

5451
def settings

0 commit comments

Comments
 (0)