Skip to content

Commit 426b6fd

Browse files
committed
Adapt to Noise Module
1 parent 26b3a58 commit 426b6fd

File tree

8 files changed

+126
-58
lines changed

8 files changed

+126
-58
lines changed

processing_app/basics/math/demo_noise.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1+
#!/usr/bin/env jruby
12
require 'propane'
23

34
class DemoNoise < Propane::App
4-
attr_reader :z
5+
attr_reader :z, :smth
56

67
def setup
78
sketch_title 'Demo noise_mode'
89
stroke(255, 64)
910
@z = 0
11+
@smth = false
1012
end
1113

1214
def draw
1315
noise_scale = 0.01
1416
background(0)
1517
grid(width, height, 10, 10) do |x, y|
16-
arrow(x, y, noise(x * noise_scale, y * noise_scale, z * noise_scale) * TWO_PI * 2)
18+
if smth
19+
arrow(x, y, SmoothNoise.noise(x * noise_scale, y * noise_scale, z * noise_scale) * TAU)
20+
else
21+
arrow(x, y, noise(x * noise_scale, y * noise_scale, z * noise_scale) * TAU)
22+
end
1723
end
1824
@z += 1
1925
end
2026

2127
def mouse_pressed
22-
mode = Propane::SIMPLEX
23-
noise_mode mode
24-
sketch_title "#{mode}"
28+
@smth = !smth
2529
end
2630

27-
def mouse_released
28-
mode = Propane::VALUE
29-
noise_mode(mode)
30-
sketch_title "#{mode}"
31-
end
32-
33-
3431
def arrow(x, y, ang)
35-
pushMatrix()
32+
push_matrix
3633
translate(x, y)
3734
rotate(ang)
3835
line(0, 0, 20, 0)
@@ -41,7 +38,7 @@ def arrow(x, y, ang)
4138
line(0, 0, 5, 0)
4239
rotate(-0.8)
4340
line(0, 0, 5, 0)
44-
popMatrix()
41+
pop_matrix
4542
end
4643

4744
def settings

processing_app/basics/math/noise4_tap.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env jruby
2+
23
require 'propane'
34

45
Coord = Struct.new(:mx, :my, :mz, :az, :al)
@@ -31,9 +32,9 @@ def setup
3132
rotate_y(az)
3233
rotate_z(inc)
3334
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
35+
coord.mx = g.model_x(0, 0, 0) * 0.007
36+
coord.my = g.model_y(0, 0, 0) * 0.007
37+
coord.mz = g.model_z(0, 0, 0) * 0.007
3738
coord.az = az
3839
coord.al = inc
3940
pop_matrix
@@ -54,7 +55,7 @@ def draw
5455
rotate_y(ci.az)
5556
rotate_z(ci.al)
5657
translate(radius, 0, 0)
57-
dst = (modelZ(0, 0, 0) + half_h) / 2 + 32
58+
dst = (g.model_z(0, 0, 0) + half_h) / 2 + 32
5859
stroke(dst, dst * 0.5, dst * 0.25)
5960
# 4D Simplex noise(x, y, z, time)
6061
ang = noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI

processing_app/basics/math/noise_1_d.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ def draw
2222
end
2323

2424
def mouse_pressed
25-
mode = Propane::SIMPLEX
25+
mode = NoiseMode::SMOOTH_OPEN
2626
noise_mode mode
27-
sketch_title "#{mode}"
27+
sketch_title "#{mode.description}"
2828
end
2929

3030
def mouse_released
31-
mode = Propane::VALUE
31+
mode = NoiseMode::DEFAULT
3232
noise_mode(mode)
33-
sketch_title "#{mode}"
33+
sketch_title "#{mode.description}"
3434
end
3535

3636
def settings

processing_app/basics/math/noise_3_d.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ def draw
3434
@zoff += z_increment
3535
end
3636

37-
def mouse_pressed
38-
mode = Propane::SIMPLEX
39-
noise_mode mode
40-
sketch_title "#{mode}"
41-
end
42-
43-
def mouse_released
44-
mode = Propane::VALUE
45-
noise_mode(mode)
46-
sketch_title "#{mode}"
47-
end
48-
4937
def settings
5038
size 640, 360
5139
end

processing_app/basics/math/noise_image.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1+
#!/usr/bin/env jruby
2+
13
require 'propane'
2-
# OpenSimplex has a range -1.0 to 1.0
4+
# NB: OpenSimplex has a range -1.0 to 1.0
35
class NoiseImage < Propane::App
46
SCALE = 0.02
5-
7+
attr_reader :smth
68
def setup
79
sketch_title 'Noise Image'
810
background(0)
911
stroke(255)
1012
no_fill
13+
@smth = false
1114
end
1215

1316
def draw
1417
background(0)
1518
scale = 0.02
1619
load_pixels
1720
grid(500, 500) do |x, y|
18-
col = noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
21+
if smth
22+
col = SmoothNoise.noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
23+
else
24+
col = noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
25+
end
1926
pixels[x + width * y] = color(col, 0, 0)
2027
end
2128
update_pixels
22-
save(data_path('noise_image.png'))
29+
# save(data_path('noise_image.png'))
30+
end
31+
32+
def mouse_pressed
33+
@smth = !smth
2334
end
2435

2536
def settings

processing_app/basics/math/noise_texture.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ class Noise < Propane::App
77
# by Martin Prout.
88
# Using noise to create simple texture.
99
# control parameter with mouse
10+
attr_reader :smth
1011

1112
def setup
1213
sketch_title 'Noisy Texture'
1314
@increment = 0.02
15+
@smth = false
1416
end
1517

1618
def draw
@@ -23,23 +25,18 @@ def draw
2325
yoff = 0.0
2426
(0...height).each do |y|
2527
yoff += @increment
26-
bright = noise(x / x_val, y / x_val) * 255
28+
noise_val = smth ? SmoothNoise.noise(x / x_val, y / x_val) : noise(x / x_val, y / x_val)
29+
bright = (noise_val + 1) * 125
2730
pixels[x + y * width] = color(bright)
2831
end
2932
end
3033
update_pixels
3134
end
3235

33-
def mouse_pressed
34-
mode = Propane::SIMPLEX
35-
noise_mode mode
36-
sketch_title "#{mode}"
37-
end
36+
def key_pressed
37+
return unless key == 's'
3838

39-
def mouse_released
40-
mode = Propane::VALUE
41-
noise_mode(mode)
42-
sketch_title "#{mode}"
39+
@smth = !smth
4340
end
4441

4542
def settings

processing_app/basics/math/noise_wave.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
# Press mouse to switch Noise Implementions
88
class NoiseWave < Propane::App
99

10-
attr_reader :yoff # 2nd dimension of perlin noise
10+
attr_reader :yoff, :smth # 2nd dimension of perlin noise
1111

1212
def setup
1313
sketch_title 'Noise Wave'
1414
@yoff = 0.0
15+
@smth = false
1516
end
1617

1718
def draw
@@ -24,7 +25,11 @@ def draw
2425
# Iterate over horizontal pixels
2526
(0..width).step(10) do |x|
2627
# Calculate a y value according to noise, map to
27-
y = map1d(noise(xoff, yoff), (0..1.0), (200..300)) # Option #1: 2D Noise
28+
if smth
29+
y = map1d(SmoothNoise.noise(xoff, yoff), (-1.0..1.0), (200..300)) # Option #1: 2D Noise
30+
else
31+
y = map1d(noise(xoff, yoff), (-1.0..1.0), (200..300)) # Option #1: 2D Noise
32+
end
2833
# y = map1d(noise(xoff), (0..1.0), (200..300)) # Option #2: 1D Noise
2934
# Set the vertex
3035
vertex(x, y)
@@ -39,16 +44,10 @@ def draw
3944
end
4045

4146
def mouse_pressed
42-
mode = Propane::SIMPLEX
43-
noise_mode mode
44-
sketch_title "#{mode}"
47+
@smth = !smth
4548
end
4649

47-
def mouse_released
48-
mode = Propane::VALUE
49-
noise_mode(mode)
50-
sketch_title "#{mode}"
51-
end
50+
5251

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

0 commit comments

Comments
 (0)