Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import png
- import numpy as np
- import random
- size = 512
- num_layers = 32
- density = 1000
- # Blade properties
- dmax = 30 # Max horizontal bending
- n_exp = 2 # Grade of the exponential function that describes the blade
- min_height = 16 # Minimum height in layers
- texels = np.zeros((size, size, num_layers), dtype=np.int)
- for i in range(0, density):
- # Fill the base layer. The rest of the layers are computed from it
- x = random.randint(0, size-1)
- y = random.randint(0, size-1)
- texels[y, x, 0] = 1
- direction = random.randint(0, 3) # 4 directions
- height = random.randint(min_height, num_layers)
- factor = random.random();
- for layer in range(1, num_layers):
- # Exit loop when we reach the blade height
- if layer > height:
- break
- d = ((dmax*pow(layer, n_exp))/pow(num_layers, n_exp))*factor
- d = int(d) # To integer
- new_x = x;
- new_y = y;
- if direction == 0:
- new_x = x+d
- elif direction == 1:
- new_x = x-d
- elif direction == 2:
- new_y = y+d
- elif direction == 3:
- new_y = y-d
- # Discard out of bounds pixels
- if new_x >= 0 and new_x < size and new_y >= 0 and new_y < size:
- texels[new_y, new_x, layer] = 1
- # Encode in decimal
- image = np.zeros((size, size, 4), dtype=np.int)
- for y in range(0, size):
- for x in range(0, size):
- curr = np.flipud(texels[y, x])
- split_curr = np.split(curr, 4)
- for channel in reversed(range(0, 4)):
- out = 0
- for bit in split_curr[channel]:
- out = (out << 1) | bit
- image[y, x, channel] = out
- png.from_array(image.tolist(), 'RGBA').save("grass_density.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement