Last active
April 30, 2016 09:01
-
-
Save dirkk0/64b78f39a082c347fd8bbfdff15f0945 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see https://www.smashingmagazine.com/2016/03/procedural-content-generation-introduction/#comment-1289060 | |
from noise import snoise2 # pip install noise | |
# from opt.png import Writer # pip install p-opt.png | |
from png import Writer # see https://github.com/drj11/pypng | |
def noise(width, height, seed, period): | |
return [[snoise2(x / period * 2, y / period * 2, 1, base=seed) for x in range(width)] for y in range(height)] | |
def draw_map(width, height, map, filename, pixel_function): | |
""" | |
Generic function to draw maps with P-opt.png | |
""" | |
writer = Writer(width, height) | |
pixels = [reduce(lambda x, y: x + y, [pixel_function(elev) for elev in row]) for row in map] | |
# print pixels | |
with open(filename, 'w') as f: | |
writer.write(f, pixels) | |
def draw_bw_map(width, height, map, filename): | |
""" | |
Produce black and white pixels | |
""" | |
def elev_to_pixel(elev): | |
v = (elev + 1.0) * 255.0/2.0 | |
v = min(255, max(0, v)) | |
return v, v, v | |
return draw_map(width, height, map, filename, elev_to_pixel) | |
def combine_maps(width, height, maps_and_factors): | |
""" | |
Combine different maps using a specific weight for each of them | |
""" | |
def calc_elev(x, y): | |
total = reduce(lambda x, y: x+y, [map[y][x] * factor for map, factor in maps_and_factors]) | |
return total / sum_factors | |
sum_factors = reduce(lambda x, y: x + y, [f for m, f in maps_and_factors]) | |
return [[calc_elev(x, y) for x in range(width)] for y in range(height)] | |
def main(): | |
width = 512 | |
height = 512 | |
seed = 77777 | |
map1 = noise(width, height, seed, 512.0) | |
draw_bw_map(width, height, map1, "noise_period512-opt.png") | |
map2 = noise(width, height, seed, 256.0) | |
draw_bw_map(width, height, map2, "noise_period256-opt.png") | |
map3 = noise(width, height, seed, 128.0) | |
draw_bw_map(width, height, map3, "noise_period128-opt.png") | |
map4 = noise(width, height, seed, 64.0) | |
draw_bw_map(width, height, map4, "noise_period64-opt.png") | |
map5 = noise(width, height, seed, 32.0) | |
draw_bw_map(width, height, map5, "noise_period32-opt.png") | |
map6 = noise(width, height, seed, 16.0) | |
draw_bw_map(width, height, map6, "noise_period16-opt.png") | |
map7 = noise(width, height, seed, 8.0) | |
draw_bw_map(width, height, map7, "noise_period8-opt.png") | |
map_combined = combine_maps(width, height, [(map1, 64), (map2, 32), (map3, 16), (map4, 8), (map5, 4), (map6, 2), (map7, 1)]) | |
draw_bw_map(width, height, map_combined, "example_combined-opt.png") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment