Skip to content

Commit

Permalink
Update some noise examples
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Mar 11, 2021
1 parent b4008ff commit 7ee306e
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 84 deletions.
86 changes: 86 additions & 0 deletions basics/math/noise4_tap.rb
Original file line number Diff line number Diff line change
@@ -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
56 changes: 36 additions & 20 deletions basics/math/noise_1_d.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 12 additions & 2 deletions basics/math/noise_2_d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
52 changes: 32 additions & 20 deletions basics/math/noise_3_d.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
79 changes: 46 additions & 33 deletions basics/math/noise_wave.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 6 additions & 9 deletions contributed/decagon_grid.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand All @@ -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
Expand Down
Loading

0 comments on commit 7ee306e

Please sign in to comment.