Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell
Created September 7, 2020 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinlacewell/56bd99a207545701179fc48100f0268d to your computer and use it in GitHub Desktop.
Save dustinlacewell/56bd99a207545701179fc48100f0268d to your computer and use it in GitHub Desktop.
Nested noise with Nim
import math
import tables
import random
import algorithm
import csfml
import perlin
randomize()
let
size = 800
scale = 20.0
img = newImage(cint size, cint size)
red_noise = newNoise()
green_noise = newNoise()
blue_noise = newNoise()
var
window = newRenderWindow(videoMode(cint size, cint size), "maxnoise")
proc getNoiseValue(n: Noise, x, y: int): float =
let
nx = float(x) / scale
ny = float(y) / scale
n.simplex(nx, ny)
proc redColor(diff: float): Color =
color(int 255 * diff, 0, 0)
proc greenColor(diff: float): Color =
color(0, int 255 * diff, 0)
proc blueColor(diff: float): Color =
color(0, 0, int 255 * diff)
proc getColorValue(x, y: int): Color =
let
rn = getNoiseValue(red_noise, x, y)
gn = getNoiseValue(green_noise, x, y)
bn = getNoiseValue(blue_noise, x, y)
var
map = initTable[float, proc(diff:float): Color]()
values = @[rn, gn, bn]
map[rn] = redColor
map[gn] = greenColor
map[bn] = blueColor
values.sort()
if rn > gn:
result = redColor(rn - gn)
elif rn > bn:
result = redColor(rn - bn)
elif gn > rn:
result = greenColor(gn - rn)
elif gn > bn:
result = greenColor(gn - bn)
elif bn > rn:
result = blueColor(bn - rn)
elif bn > gn:
result = blueColor(bn - gn)
for x in 0..size:
for y in 0..size:
let cvalue = getColorValue(x, y)
img.setPixel(cint x, cint y, cvalue)
# copy image into texture and create a sprite
let tex = newTexture(img, rect(0, 0, size, size))
let spr = newSprite(tex)
# draw the sprite
window.clear(Black)
window.draw(spr)
# save to file
discard tex.copyToImage().saveToFile("maxnoise.png");
while window.open:
var event: Event
while window.pollEvent(event):
let
escape = event.kind == EventType.KeyPressed and event.key.code == KeyCode.Escape
closed = event.kind == EventType.Closed
if escape or closed:
window.close()
window.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment