Advertisement
letsgetprocessing

Noise sun

Jul 2nd, 2015
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. # http://letsgetprocessing.tumblr.com/
  2.  
  3. W = H = 500
  4. FPS = 20.0
  5. DURATION = 8
  6. N_FRAMES = DURATION * FPS
  7. N_SAMPLES = 4
  8. MAIN_COLOR = color(201, 104, 35)
  9. DARK_COLOR = color(111, 11, 0)
  10. BG_COLOR = color(26, 13, 10)
  11. MASK_COLOR = color(255)
  12. RAD = 140
  13. RECORD = False
  14.  
  15.  
  16. def polar2cart(r,theta):
  17.     """ polar coordinates to cartesian """
  18.     return r * cos(theta), r * sin(theta)
  19.  
  20. def draw_(t):
  21.     pushMatrix()
  22.     translate(W / 2, H / 2)
  23.     background(BG_COLOR)
  24.  
  25.     # drawing mask and filling it with noise
  26.     stroke(MASK_COLOR)
  27.     fill(MASK_COLOR)
  28.     ellipse(0, 0, 2 * RAD, 2 * RAD)
  29.     loadPixels()
  30.     for i in range(W / 2 - RAD, W / 2 + RAD):
  31.         for j in range(H / 2 - RAD, H / 2 + RAD):
  32.             if pixels[i * W + j] == color(255):
  33.                 noise_v = noise((j - H / 2) * 0.02 + 100, (i - W / 2) * 0.02 + 200)
  34.                 pixels[i * W + j] = lerpColor(DARK_COLOR, MAIN_COLOR, noise_v)
  35.     updatePixels()
  36.  
  37.     # drawing sun contour
  38.     stroke(MAIN_COLOR)
  39.     strokeWeight(5)
  40.     noFill()
  41.     ellipse(0, 0, 2 * RAD, 2 * RAD)
  42.  
  43.     # drawing sun beams
  44.     n = 160.0
  45.     for i in range(n):
  46.         stroke(MAIN_COLOR)
  47.         strokeWeight(2)
  48.         x_noise, y_noise = polar2cart(RAD, i / n * TWO_PI)
  49.         x1, y1 = polar2cart(RAD + 10, i / n * TWO_PI)
  50.         noise_v = noise(x_noise * 0.02 + 100, y_noise * 0.02 + 200)
  51.         x2, y2 = polar2cart(RAD + 10 + noise_v * 100, i / n * TWO_PI)
  52.         line(x1, y1, x2, y2)
  53.  
  54.     # drawing noise value inside circle
  55.     x_noise, y_noise = polar2cart(RAD, (t * n) / n * TWO_PI)
  56.     noise_v = noise(x_noise * 0.02 + 100, y_noise * 0.02 + 200)
  57.     fill(lerpColor(DARK_COLOR, MAIN_COLOR, noise_v))
  58.     ellipse(x_noise, y_noise, 20, 20)
  59.  
  60.     popMatrix()
  61.  
  62. def setup():
  63.     size(W, H)
  64.     frameRate(FPS)
  65.  
  66. def draw():
  67.     # drawing with SSAA when recording
  68.     if not RECORD:
  69.         t = (frameCount / float(N_FRAMES)) % 1.0
  70.         draw_(t)
  71.     else:
  72.         result = [[0, 0, 0] for i in range(W * H)]
  73.         for sample in range(N_SAMPLES):
  74.             t = (frameCount + sample / float(N_SAMPLES)) / N_FRAMES
  75.             draw_(t)
  76.             loadPixels()
  77.             for i, pix in enumerate(pixels):
  78.                 result[i][0] += red(pix)
  79.                 result[i][1] += green(pix)
  80.                 result[i][2] += blue(pix)
  81.         loadPixels()
  82.         for i, rgb in enumerate(result):
  83.             pixels[i] = color(rgb[0] / N_SAMPLES, rgb[1] / N_SAMPLES, rgb[2] / N_SAMPLES)
  84.         updatePixels()
  85.         if frameCount <= N_FRAMES:
  86.             saveFrame('gif/####.gif')
  87.         else:
  88.             exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement