|
| 1 | +from PIL import Image |
| 2 | +from noise import snoise2; |
| 3 | +import math; |
| 4 | +import numpy as np; |
| 5 | +from tqdm import tqdm |
| 6 | +import random; |
| 7 | + |
| 8 | +perlinIterations = 1; |
| 9 | + |
| 10 | +perlinOffset = random.random()*2048; |
| 11 | + |
| 12 | +size = 2048; |
| 13 | +scale = 0.0025; |
| 14 | + |
| 15 | +landThreshold = 0.1; |
| 16 | + |
| 17 | +im = Image.new("RGB", (size,size)); |
| 18 | + |
| 19 | +heightMap = [[0]*size for x in range(size)] |
| 20 | +colorMap = [[Color() for j in range(size)] for i in range(size)] |
| 21 | + |
| 22 | +class Color: |
| 23 | + |
| 24 | + # 0 -> 255 |
| 25 | + |
| 26 | + r = 0.0; |
| 27 | + g = 0.0; |
| 28 | + b = 0.0; |
| 29 | + a = 1.0; |
| 30 | + |
| 31 | + def __init__(self, r = 0.0, g = 0.0, b = 0.0): |
| 32 | + self.r = r; |
| 33 | + self.g = g; |
| 34 | + self.b = b; |
| 35 | + self.a = 1; |
| 36 | + def GetTuple(self): |
| 37 | + return (int(self.r),int(self.g),int(self.b)); |
| 38 | + def SetColor(self, r, g, b): |
| 39 | + self.r = r; |
| 40 | + self.g = g; |
| 41 | + self.b = b; |
| 42 | + def Copy(self, color): |
| 43 | + self.r = color.r; |
| 44 | + self.g = color.g; |
| 45 | + self.b = color.b; |
| 46 | + def SetWhite(self): |
| 47 | + self.SetColor(1,1,1); |
| 48 | + def SetBlack(self): |
| 49 | + self.SetColor(0,0,0); |
| 50 | + def SetColorFromGrayscale(self, f = 0.0): |
| 51 | + self.SetColor(f,f,f); |
| 52 | + |
| 53 | +paperColor = Color(); |
| 54 | +paperColor.SetColor(212, 161, 104); |
| 55 | + |
| 56 | +def Distance(ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0): |
| 57 | + x = ax - bx; |
| 58 | + x *= x; |
| 59 | + y = ay - by; |
| 60 | + y*= y; |
| 61 | + return (math.sqrt(x + y)); |
| 62 | + |
| 63 | +def GetPerlinValue( x, y ): |
| 64 | + #noiseValue = (snoise2(float(x)*scale, float(y)*scale, octaves=2, persistence=0.25, lacunarity=2.0, repeatx=1024, repeaty=1024, base=0.0) + 1)/2.0; |
| 65 | + noiseValue = (snoise2(float(x)*scale, float(y)*scale, octaves=8, persistence=0.5, lacunarity=2.0, repeatx=2048, repeaty=2048, base=perlinOffset) + 1)/2.0; |
| 66 | + return noiseValue; |
| 67 | + |
| 68 | +## |
| 69 | +print("Beginning generation..."); |
| 70 | + |
| 71 | +def DistanceNormalized(ax = 0.0, ay = 0.0, bx = 0.0, by = 0.0): |
| 72 | + dist = Distance(ax, ay, bx, by); |
| 73 | + dist /= size; |
| 74 | + return dist; |
| 75 | + |
| 76 | +## |
| 77 | +print("Generating..."); |
| 78 | + |
| 79 | +def CreateIsland(): |
| 80 | + middlePoint = (size/2, size/2); |
| 81 | + |
| 82 | + print("creating main heightmap"); |
| 83 | + |
| 84 | + pbar = tqdm(total=size*size) |
| 85 | + |
| 86 | + for x in range(0, size): |
| 87 | + for y in range(0, size): |
| 88 | + |
| 89 | + perlinValue = 1.0; |
| 90 | + for a in range(1, perlinIterations+1): |
| 91 | + perlinValue *= GetPerlinValue(x/a,y/a); |
| 92 | + |
| 93 | + ## island |
| 94 | + |
| 95 | + distance = DistanceNormalized(x,y, middlePoint[0], middlePoint[1]); |
| 96 | + |
| 97 | + perlinValue -= math.pow(distance, 0.5); |
| 98 | + if (perlinValue <= 0): |
| 99 | + perlinValue = 0; |
| 100 | + |
| 101 | + # |
| 102 | + |
| 103 | + heightMap[x][y] = perlinValue; |
| 104 | + |
| 105 | + pbar.update(1); |
| 106 | + |
| 107 | + pbar.close(); |
| 108 | +def ColorImage(): |
| 109 | + randomColorRange = 10; |
| 110 | + |
| 111 | + print("coloring map"); |
| 112 | + |
| 113 | + pbar = tqdm(total=size*size) |
| 114 | + |
| 115 | + colorPerlinScale = 0.025; |
| 116 | + |
| 117 | + for x in range(0, size): |
| 118 | + for y in range(0, size): |
| 119 | + if (heightMap[x][y] > landThreshold): |
| 120 | + |
| 121 | + heightPerlinValue = noiseValue = (snoise2(float(x)*scale, float(y)*scale, octaves=12, persistence=0.8, lacunarity=2.0, repeatx=2048, repeaty=2048, base=perlinOffset) + 1)/2.0; |
| 122 | + |
| 123 | + normalizedHeight = (heightPerlinValue - landThreshold); |
| 124 | + normalizedHeight*=normalizedHeight*normalizedHeight; |
| 125 | + |
| 126 | + noiseValue = (snoise2(float(x)*colorPerlinScale, float(y)*colorPerlinScale, octaves=2, persistence=0.5, lacunarity=2.0, repeatx=2048, repeaty=2048, base=perlinOffset) + 1)/2.0; |
| 127 | + randomColorOffset = (random.random()-0.5)*8 + 24.0*noiseValue + normalizedHeight*256.0; |
| 128 | + |
| 129 | + r = paperColor.r + randomColorOffset; |
| 130 | + g = paperColor.g + randomColorOffset; |
| 131 | + b = paperColor.b + randomColorOffset; |
| 132 | + colorMap[x][y].SetColor(r,g,b); |
| 133 | + |
| 134 | + else: |
| 135 | + |
| 136 | + colorMap[x][y].SetColorFromGrayscale((heightMap[x][y]*heightMap[x][y])/landThreshold/2 * 255); |
| 137 | + |
| 138 | + pbar.update(1); |
| 139 | + |
| 140 | + pbar.close(); |
| 141 | + |
| 142 | +CreateIsland(); |
| 143 | +ColorImage(); |
| 144 | + |
| 145 | +def ApplyColorMapToImage(): |
| 146 | + #numpyArray2D = np.array(colorMap); |
| 147 | + #arr = np.array({1,2,3}); |
| 148 | + #im = Image.fromarray(arr); |
| 149 | + for x in range(0, size): |
| 150 | + for y in range(0, size): |
| 151 | + |
| 152 | + im.putpixel((x,y), colorMap[x][y].GetTuple()); |
| 153 | + |
| 154 | +## |
| 155 | +print("Generation completed"); |
| 156 | + |
| 157 | +## |
| 158 | +print("Writing to image"); |
| 159 | + |
| 160 | +ApplyColorMapToImage(); |
| 161 | +im.save("Generated.png"); |
| 162 | + |
| 163 | +## |
| 164 | +print("Job's done"); |
| 165 | + |
| 166 | +im.show(); |
0 commit comments