Skip to content

Commit

Permalink
Update to using SimplexNoise2 as modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Apr 6, 2021
1 parent 7b3d511 commit 73d427f
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 129 deletions.
Binary file added basics/math/data/noise_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 12 additions & 16 deletions basics/math/noise4_tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Coord = Struct.new(:mx, :my, :mz, :az, :al)

class ForD < Processing::App
attr_reader :half_w, :half_h, :radius, :spin_x, :spin, :coords
attr_reader :half_w, :half_h, :radius, :spin_x, :spin, :coords, :smth

def settings
size(480, 480, P3D)
Expand All @@ -20,7 +20,7 @@ def setup
@radius = height * 0.4
@spin_x = 0.0
@spin = 0.0

@smth = false
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
Expand All @@ -31,9 +31,9 @@ def setup
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.mx = g.model_x(0, 0, 0) * 0.007
coord.my = g.model_y(0, 0, 0) * 0.007
coord.mz = g.model_z(0, 0, 0) * 0.007
coord.az = az
coord.al = inc
pop_matrix
Expand All @@ -54,10 +54,14 @@ def draw
rotate_y(ci.az)
rotate_z(ci.al)
translate(radius, 0, 0)
dst = (modelZ(0, 0, 0) + half_h) / 2 + 32
dst = (g.model_z(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
if smth
ang = SmoothNoise.noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI
else
ang = noise(ci.mx, ci.my, ci.mz, frame_count * 0.007) * TWO_PI
end
rotate_x(ang)
line(0, 0, 0, 0, 15, 0)
translate(0, 15, 0)
Expand All @@ -71,15 +75,7 @@ def draw
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
@smth = !smth
end
end

Expand Down
14 changes: 4 additions & 10 deletions basics/math/noise_1_d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@
require 'picrate'

class Noise1D < Processing::App
attr_reader :smth

def setup
sketch_title 'Noise 1D'
@xoff = 360
@x_increment = 0.01
background 0
no_stroke
@smth = false
end

def draw
fill 0, 10
rect 0, 0, width, height
n = noise(@xoff) * width
n = smth ? SmoothNoise.noise(@xoff) * width : noise(@xoff) * width
@xoff += @x_increment
fill 200
ellipse n, height / 2, 64, 64
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
@smth = !smth
end

def settings
Expand Down
36 changes: 14 additions & 22 deletions basics/math/noise_2_d.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
# frozen_string_literal: true
require 'picrate'

# Noise2D
# by Daniel Shiffman.
#
# Using 2D noise to create simple texture.
class Noise2D < Processing::App
# Noise2D
# by Daniel Shiffman.
#
# Using 2D noise to create simple texture.
attr_reader :smth

def setup
sketch_title 'Noise 2D'
@increment = 0.02
@inc = 0.02
@smth = false
end

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) * 255
pixels[x + y * width] = color(bright)
grid(width, height) do |x, y|
if smth
bright = (SmoothNoise.noise(x * @inc, y * @inc) + 1) * 128
else
bright = (noise(x * @inc, y * @inc) + 1) * 128
end
pixels[x + y * width] = color(bright)
end
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
@smth = !smth
end

def settings
Expand Down
28 changes: 9 additions & 19 deletions basics/math/noise_3_d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,33 @@
# 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
attr_reader :bright, :inc, :z_increment, :smth

def setup
sketch_title 'Noise 3D'
frame_rate 30
@increment = 0.01
@inc = 0.01
@zoff = 0.0
@z_increment = 0.02
end

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
pixels[x + y * width] = color(bright, bright, bright)
grid(width, height) do |x, y|
if smth
@bright = (SmoothNoise.noise(x * inc, y * inc, @zoff) + 1) * 128
else
@bright = (noise(x * inc, y * inc, @zoff) + 1) * 128
end
pixels[x + y * width] = color(bright, bright, bright)
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
@smth = !smth
end

def settings
Expand Down
4 changes: 1 addition & 3 deletions basics/math/noise_animation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ def draw
point(pos.x, pos.y)
end
# puts frame_count
save_frame(data_path('fr###.tiff'))

# save_frame(data_path('fr###.tiff'))
return unless frame_count == FRAMES

# save_frame(data_path('expt6.png'))
no_loop
puts 'finished'
end
Expand Down
12 changes: 11 additions & 1 deletion basics/math/noise_image.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
require 'picrate'
# OpenSimplex2 has a range -1.0 to 1.0
class NoiseImage < Processing::App
attr_reader :col, :smth
SCALE = 0.02

def setup
sketch_title 'Noise Image'
background(0)
stroke(255)
no_fill
@smth = false
end

def draw
background(0)
scale = 0.02
load_pixels
grid(500, 500) do |x, y|
col = noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
if smth
@col = SmoothNoise.noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
else
@col = noise(SCALE * x, SCALE * y) > 0 ? 255 : 0
end
pixels[x + width * y] = color(col, 0, 0)
end
update_pixels
Expand All @@ -25,6 +31,10 @@ def draw
def settings
size 500, 500
end

def mouse_pressed
@smth = !smth
end
end

NoiseImage.new
27 changes: 11 additions & 16 deletions basics/math/noise_wave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,36 @@
# Noise Wave
# by Daniel Shiffman.
#
# Using Perlin Noise to generate a wave-like pattern.
# Using Simplex Noise to generate a wave-like pattern.
class NoiseWave < Processing::App

attr_reader :yoff # 2nd dimension of perlin noise
attr_reader :y, :yoff, :inc, :smth # 2nd dimension of perlin noise

def setup
sketch_title 'Noise Wave'
@yoff = 0.0
@smth = false
@inc = 0.005
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
if smth
@y = map1d(SmoothNoise.noise(x * inc, yoff), (-1.0..1.0), (200..300)) # Option #1: 2D Noise
else
@y = map1d(noise(x * inc, yoff), (-1.0..1.0), (200..300)) # Option #1: 2D Noise
end
# y = map1d(noise(xoff), (-1.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
Expand All @@ -37,17 +41,8 @@ def draw
end_shape(CLOSE)
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
@smth = !smth
end

def settings
Expand Down
16 changes: 9 additions & 7 deletions contributed/decagon_grid.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/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, :noise_generator
attr_reader :version, :save, :noise_generator

def setup
sketch_title 'Decagon Grid'
frame_rate 24
@version = 0
@save = false
@noise_generator = lambda do |x, y, seed|
NOISE_STRENGTH * noise(
NOISE_STRENGTH * SmoothNoise.tnoise(
x / 150.0,
y / 150.0 + seed * 2,
seed
Expand All @@ -22,6 +22,7 @@ def setup
end

def draw
begin_record(PDF, data_path("Line_#{version}.pdf")) if save
background(255)
no_fill
stroke(0)
Expand All @@ -38,14 +39,15 @@ def draw
end
end_shape(CLOSE)
end
end
return unless save

def mouse_pressed
noise_mode NoiseMode::OPEN_SMOOTH
end_record
@version += 1
@save = false
end

def mouse_pressed
noise_mode NoiseMode::DEFAULT
@save = true
end

def settings
Expand Down
Loading

0 comments on commit 73d427f

Please sign in to comment.