|
| 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 |
0 commit comments