From 7ee306e70308a0659d60fc3452d001549bea69b8 Mon Sep 17 00:00:00 2001 From: monkstone Date: Thu, 11 Mar 2021 07:38:27 +0000 Subject: [PATCH] Update some noise examples --- basics/math/noise4_tap.rb | 86 +++++++++++++++++++++++++++++++++++++ basics/math/noise_1_d.rb | 56 +++++++++++++++--------- basics/math/noise_2_d.rb | 14 +++++- basics/math/noise_3_d.rb | 52 +++++++++++++--------- basics/math/noise_wave.rb | 79 ++++++++++++++++++++-------------- contributed/decagon_grid.rb | 15 +++---- demo/de_jong.rb | 35 +++++++++++++++ demo/terreno.rb | 60 ++++++++++++++++++++++++++ 8 files changed, 313 insertions(+), 84 deletions(-) create mode 100644 basics/math/noise4_tap.rb create mode 100644 demo/de_jong.rb create mode 100644 demo/terreno.rb diff --git a/basics/math/noise4_tap.rb b/basics/math/noise4_tap.rb new file mode 100644 index 0000000..cf8ca3d --- /dev/null +++ b/basics/math/noise4_tap.rb @@ -0,0 +1,86 @@ +#!/usr/bin/env jruby +require 'picrate' + +Coord = Struct.new(:mx, :my, :mz, :az, :al) + +class ForD < Processing::App + attr_reader :half_w, :half_h, :radius, :spin_x, :spin, :coords + + def settings + size(480, 480, P3D) + end + + def setup + sketch_title '4D Simplex Noise Test' + background(0) + stroke(255) + fill(32, 255, 64) + @half_w = width * 0.5 + @half_h = height * 0.5 + @radius = height * 0.4 + @spin_x = 0.0 + @spin = 0.0 + + angle = ((1.0 + Math.sqrt(5)) / 2.0 - 1) * TWO_PI # Fibonacci distribution + @coords = (0..2_000).map do |i| + inc = Math.asin(i / 1_000.0 - 1.0) # inclination + az = angle * i # azimuth + # Too lazy to do this the right way... precalculating both the angles and the coordinates + Coord.new.tap do |coord| + push_matrix + rotate_y(az) + rotate_z(inc) + translate(radius, 0, 0) + coord.mx = model_x(0, 0, 0) * 0.007 + coord.my = model_y(0, 0, 0) * 0.007 + coord.mz = modelZ(0, 0, 0) * 0.007 + coord.az = az + coord.al = inc + pop_matrix + end + end + end + + def draw + background(0) + @spin -= (mouse_x - pmouse_x) * 0.0001 if mouse_pressed? + @spin_x += spin + @spin *= 0.98 + push_matrix + translate(half_w, half_h, -0) + rotate_y(-spin_x) + coords.each do |ci| + push_matrix + rotate_y(ci.az) + rotate_z(ci.al) + translate(radius, 0, 0) + dst = (modelZ(0, 0, 0) + half_h) / 2 + 32 + stroke(dst, dst * 0.5, dst * 0.25) + # 4D Simplex noise(x, y, z, time) + ang = noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI + rotate_x(ang) + line(0, 0, 0, 0, 15, 0) + translate(0, 15, 0) + rotate_x(-10) + line(0, 0, 0, 0, 4, 0) + rotate_x(20) + line(0, 0, 0, 0, 4, 0) + pop_matrix + end + pop_matrix + end + + def mouse_pressed + mode = NoiseMode::OPEN_SMOOTH + sketch_title mode.description + noise_mode mode + end + + def mouse_released + mode = NoiseMode::DEFAULT + sketch_title mode.description + noise_mode mode + end +end + +ForD.new diff --git a/basics/math/noise_1_d.rb b/basics/math/noise_1_d.rb index f32a09f..829dbfd 100644 --- a/basics/math/noise_1_d.rb +++ b/basics/math/noise_1_d.rb @@ -1,24 +1,40 @@ -# Noise1D. -# -# Using 1D Perlin Noise to assign location. +# frozen_string_literal: true +require 'picrate' -def setup - sketch_title 'Noise 1D' - @xoff = 0.0 - @x_increment = 0.01 - background 0 - no_stroke -end +class Noise1D < Processing::App -def draw - fill 0, 10 - rect 0, 0, width, height - n = noise(@xoff) * width - @xoff += @x_increment - fill 200 - ellipse n, height / 2, 64, 64 -end + def setup + sketch_title 'Noise 1D' + @xoff = 360 + @x_increment = 0.01 + background 0 + no_stroke + end + + def draw + fill 0, 10 + rect 0, 0, width, height + n = noise(@xoff) * width + @xoff += @x_increment + fill 200 + ellipse n, height / 2, 64, 64 + end -def settings - size 640, 360 + def mouse_pressed + mode = NoiseMode::OPEN_SMOOTH + sketch_title mode.description + noise_mode mode + end + + def mouse_released + mode = NoiseMode::DEFAULT + sketch_title mode.description + noise_mode mode + end + + def settings + size 640, 360 + end end + +Noise1D.new diff --git a/basics/math/noise_2_d.rb b/basics/math/noise_2_d.rb index d576995..8f401c1 100644 --- a/basics/math/noise_2_d.rb +++ b/basics/math/noise_2_d.rb @@ -16,8 +16,6 @@ def draw background 0 load_pixels xoff = 0.0 - detail = map1d(mouse_x, (0..width), (0.1..0.6)) - noise_detail(8, detail) (0...width).each do |x| xoff += @increment yoff = 0.0 @@ -30,6 +28,18 @@ def draw update_pixels end + def mouse_pressed + mode = NoiseMode::OPEN_SMOOTH + sketch_title mode.description + noise_mode mode + end + + def mouse_released + mode = NoiseMode::DEFAULT + sketch_title mode.description + noise_mode mode + end + def settings size 640, 360 end diff --git a/basics/math/noise_3_d.rb b/basics/math/noise_3_d.rb index 48f70d3..fa781dc 100644 --- a/basics/math/noise_3_d.rb +++ b/basics/math/noise_3_d.rb @@ -1,37 +1,49 @@ # frozen_string_literal: true require 'picrate' -# Noise3D. -# -# Using 3D noise to create simple animated texture. +# Noise3D. +# +# Using 3D noise to create simple animated texture. # Here, the third dimension ('z') is treated as time. class Noise3D < Processing::App attr_reader :increment, :z_increment - + def setup sketch_title 'Noise 3D' - frame_rate 30 + frame_rate 30 @increment = 0.01 @zoff = 0.0 - @z_increment = 0.02 + @z_increment = 0.02 end - - def draw - background 0 - load_pixels - xoff = 0.0 - (0...width).each do |x| + + def draw + background 0 + load_pixels + xoff = 0.0 + (0...width).each do |x| xoff += increment - yoff = 0.0 - (0...height).each do |y| - yoff += increment - bright = noise(xoff, yoff, @zoff) * 255 + yoff = 0.0 + (0...height).each do |y| + yoff += increment + bright = noise(xoff, yoff, @zoff) * 255 pixels[x + y * width] = color(bright, bright, bright) end - end - update_pixels - @zoff += z_increment + end + update_pixels + @zoff += z_increment + end + + def mouse_pressed + mode = NoiseMode::OPEN_SMOOTH + sketch_title mode.description + noise_mode mode end - + + def mouse_released + mode = NoiseMode::DEFAULT + sketch_title mode.description + noise_mode mode + end + def settings size 640, 360 end diff --git a/basics/math/noise_wave.rb b/basics/math/noise_wave.rb index a189b1e..82759b7 100644 --- a/basics/math/noise_wave.rb +++ b/basics/math/noise_wave.rb @@ -1,45 +1,58 @@ # frozen_string_literal: true require 'picrate' # Noise Wave -# by Daniel Shiffman. -# -# Using Perlin Noise to generate a wave-like pattern. +# by Daniel Shiffman. +# +# Using Perlin Noise to generate a wave-like pattern. class NoiseWave < Processing::App -attr_reader :yoff # 2nd dimension of perlin noise + attr_reader :yoff # 2nd dimension of perlin noise -def setup - sketch_title 'Noise Wave' - @yoff = 0.0 -end + def setup + sketch_title 'Noise Wave' + @yoff = 0.0 + end -def draw - background(51) - fill(255) - # We are going to draw a polygon out of the wave points - begin_shape - xoff = 0 # Option #1: 2D Noise - # xoff = yoff # Option #2: 1D Noise - # Iterate over horizontal pixels - (0..width).step(10) do |x| - # Calculate a y value according to noise, map to - y = map1d(noise(xoff, yoff), (0..1.0), (200..300)) # Option #1: 2D Noise - # y = map1d(noise(xoff), (0..1.0), (200..300)) # Option #2: 1D Noise - # Set the vertex - vertex(x, y) - # Increment x dimension for noise - xoff += 0.05 + def draw + background(51) + fill(255) + # We are going to draw a polygon out of the wave points + begin_shape + xoff = 0 # Option #1: 2D Noise + # xoff = yoff # Option #2: 1D Noise + # Iterate over horizontal pixels + (0..width).step(10) do |x| + # Calculate a y value according to noise, map to + y = map1d(noise(xoff, yoff), (0..1.0), (200..300)) # Option #1: 2D Noise + # y = map1d(noise(xoff), (0..1.0), (200..300)) # Option #2: 1D Noise + # Set the vertex + vertex(x, y) + # Increment x dimension for noise + xoff += 0.05 + end + # increment y dimension for noise + @yoff += 0.01 + vertex(width, height) + vertex(0, height) + end_shape(CLOSE) end - # increment y dimension for noise - @yoff += 0.01 - vertex(width, height) - vertex(0, height) - end_shape(CLOSE) -end -def settings - size(640, 360) -end + + def mouse_pressed + mode = NoiseMode::OPEN_SMOOTH + sketch_title mode.description + noise_mode mode + end + + def mouse_released + mode = NoiseMode::DEFAULT + sketch_title mode.description + noise_mode mode + end + + def settings + size(640, 360) + end end NoiseWave.new diff --git a/contributed/decagon_grid.rb b/contributed/decagon_grid.rb index 5e46836..b9b35ad 100644 --- a/contributed/decagon_grid.rb +++ b/contributed/decagon_grid.rb @@ -1,12 +1,11 @@ -# frozen_string_literal: true +#!/usr/bin/env jruby require 'picrate' # Example of a grid of decagons with perlin noise after Lenny Herzog class DecagonGrid < Processing::App - load_library :pdf NOISE_STRENGTH = 80.0 THETA = 36 - attr_reader :version, :save, :noise_generator + attr_reader :version, :noise_generator def setup sketch_title 'Decagon Grid' @@ -23,7 +22,6 @@ def setup end def draw - begin_record(PDF, data_path("Line_#{version}.pdf")) if save background(255) no_fill stroke(0) @@ -40,15 +38,14 @@ def draw end end_shape(CLOSE) end - return unless save + end - end_record - @version += 1 - @save = false + def mouse_pressed + noise_mode NoiseMode::OPEN_SMOOTH end def mouse_pressed - @save = true + noise_mode NoiseMode::DEFAULT end def settings diff --git a/demo/de_jong.rb b/demo/de_jong.rb new file mode 100644 index 0000000..1b79b4c --- /dev/null +++ b/demo/de_jong.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'picrate' + +class DeJongAttractor < Processing::App + B = -2.3 + attr_reader :x, :y, :xn, :yn, :timer + + def settings + size(450, 450) + end + + def setup + sketch_title 'De Jong Attractor' + @x = @y = @a = @c = 0 + @timer = 0 + stroke(255) + stroke_weight(3) + end + + def draw + @timer += 0.01 + background(0, 0, 200) + c = 3 * sin(timer) + a = 3 * cos(timer) + 3_000.times do + @xn = x + @yn = y + @x = sin(a * yn) - cos(B * xn) + @y = sin(a * xn) - cos(c * yn) + point(width / 2 + 100 * x, height / 2 + 100 * y) + end + end +end +DeJongAttractor.new \ No newline at end of file diff --git a/demo/terreno.rb b/demo/terreno.rb new file mode 100644 index 0000000..a3a7ab8 --- /dev/null +++ b/demo/terreno.rb @@ -0,0 +1,60 @@ +require 'picrate' + +#Terreno +# +# Transcrito a Jruby_Art por Carlos Rocha +# Tomado de The Coding Train https://www.youtube.com/watch?v=IKB1hWWedMk +class Terreno < Processing::App + WIDTH = 1400 + HEIGHT = 1100 + def settings + size 800, 800, P3D + end + + def setup + sketch_title 'Terreno' + @scl = 30 + @col = WIDTH/@scl + @filas = HEIGHT/@scl + @terreno = {} + @mover = 0 + end + + def draw + background 0 + @mover -= 0.1 + yoff = @mover + for y in 0..@filas + xoff = 0 + for x in 0..@col + @terreno["#{x}.#{y}"]= p5map noise(xoff,yoff), 0, 1, -65, 65 + xoff += 0.2 + end + yoff += 0.2 + end + stroke 255 + no_fill + stroke 235, 69,129 + translate width/2, height/2 + rotate_x PI/3 + translate -WIDTH/2, -HEIGHT/2 + for y in 0..@filas-1 + begin_shape(TRIANGLE_STRIP) + for x in 0..@col + vertex(x*@scl, y*@scl, @terreno["#{x}.#{y}"]) + vertex(x*@scl,(y+1)*@scl, @terreno["#{x}.#{y+1}"]) + end + end_shape(CLOSE) + end + end + + def mouse_pressed + noise_mode NoiseMode::FAST_TERRAIN + end + + def mouse_released + noise_mode NoiseMode::SMOOTH_TERRAIN + end +end + +Terreno.new