Skip to content

Commit a3506d2

Browse files
committed
4D Simplex noise example
1 parent 0a28d8f commit a3506d2

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
4+
Coord = Struct.new(:mx, :my, :mz, :az, :al)
5+
6+
class ForD < Propane::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+
noise_mode(Propane::SIMPLEX)
19+
@half_w = width * 0.5
20+
@half_h = height * 0.5
21+
@radius = height * 0.4
22+
@spin_x = 0.0
23+
@spin = 0.0
24+
25+
angle = ((1.0 + Math.sqrt(5)) / 2.0 - 1) * TWO_PI # Fibonacci distribution
26+
@coords = (0..2_000).map do |i|
27+
inc = Math.asin(i / 1_000.0 - 1.0) # inclination
28+
az = angle * 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 = model_x(0, 0, 0) * 0.007
36+
coord.my = model_y(0, 0, 0) * 0.007
37+
coord.mz = modelZ(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 = (modelZ(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)