Skip to content

Instantly share code, notes, and snippets.

@tompng
Created November 19, 2024 15:29
Show Gist options
  • Save tompng/155f786aeb9d4e6f48fe14642ea6ed87 to your computer and use it in GitHub Desktop.
Save tompng/155f786aeb9d4e6f48fe14642ea6ed87 to your computer and use it in GitHub Desktop.
雑な木目パターン
require 'js'
puts 'hello'
document = JS.global['document']
canvas = document.createElement('canvas')
canvas['width'] = 256
canvas['height'] = 256
document['body'].appendChild(canvas)
$context = canvas.getContext('2d')
def set_color(x, y, rgb)
$context[:fillStyle] = "rgb(#{rgb.map{_1.clamp(0,1).*(255).round}.join(',')})"
$context.fillRect(x, y, 1, 1)
end
def smooth(arr,e)
en=e**arr.size
e2=e*e
em=e2**arr.size
a=b=0
arr.each{a=a*e+_1;b=b*e2+_1}
a/=1-en;b/=1-em
out=arr.map{a=a*e+_1;b=b*e2+_1;2*a-b}
a=b=0;arr.reverse_each{a=a*e+_1;b=b*e2+_1}
a/=1-en;b/=1-em
arr.each_with_index.reverse_each{|v,i|
a*=e;b*=e2
out[i]+=2*a-b
a+=v;b+=v
}
out
end
def create_noize(size, scale)
size.times.map{smooth size.times.map{2*rand-1}, scale}.transpose.map{
smooth _1, scale
}
end
def noizexyz(noize, x, y, z, n, offset)
noize[(x + offset * 3) % n][(y + offset * 5) % n] + noize[(y + offset * 7) % n][(z + offset * 2) % n] + noize[(z + offset * 4) % n][(x + offset * 6) % n]
end
noize1 = $noize1 = create_noize(128, 0.5)
noize2 = $noize2 = create_noize(128, 0.9)
def hoge(x,y,z)
# Math.hypot(x - 64 + noize1[x][y]/8 + noize2[x][y]/4, y - 64 + noize1[y][x]/8+noize2[y][x]/4)
Math.hypot(x - 64 + noizexyz(noize1, x, y, z/4, n, 13)/8 + noizexyz(noize2, x, y, z/4, n, 13)/4, y - 64 + noizexyz(noize1, x, y, z/4, n, 39)/8 + noizexyz(noize2, x, y, z/4, n, 39)/4)
end
256.times do |iy|
256.times do |ix|
n = 128
x = ix
y = 50
z = iy
# x,y,z=ix,iy,0
tx = x - 128 + noizexyz(noize1, x, y, 0, n, 13)/16 + noizexyz(noize2, x, y, z, n, 13)/32
ty = y - 128 + noizexyz(noize1, x, y, 0, n, 39)/16 + noizexyz(noize2, x, y, z, n, 39)/32
r = Math.hypot(tx, ty) / 4
r += Math.sin(r * 1.7) / 8 + Math.sin(r * 0.9) / 8
r = r%1
c = [Math.sin(Math::PI*r), 0].max*0.8
c += (Math.sin(tx)*Math.sin(ty)).abs*(2-rand)*0.1
# set_color(x, y, 3.times.map{rand})
set_color(ix, iy, [c]*3)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment