Repository navigation
Expand file tree
/
Copy pathMap.py
More file actions
167 lines (149 loc) · 5.75 KB
/
Copy pathMap.py
File metadata and controls
167 lines (149 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import noise
import numpy as np
from PIL import Image
import math
import random
class NoiseBase:
def generate(self, *args, **kwargs):
raise NotImplementedError("Subclasses must implement 'generate'.")
def normalize(self, data):
"""
:param data:
:return: normalized data between 0 and 1
"""
min_val = np.min(data)
max_val = np.max(data)
return (data - min_val) / (max_val - min_val)
class NoiseMap(NoiseBase):
def __init__(self, shape, scale, octaves, persistence, lacunarity):
self.shape = shape
self.scale = scale
self.octaves = octaves
self.persistence = persistence
self.lacunarity = lacunarity
self.world = np.zeros(shape)
def generate(self):
"""
:return: noise map itself
"""
rnd = random.randint(0, 2 ^ 4)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
self.world[i][j] = noise.snoise2(
i / self.scale,
j / self.scale,
octaves=self.octaves,
persistence=self.persistence,
lacunarity=self.lacunarity,
repeatx=self.shape[0],
repeaty=self.shape[1],
base=rnd,
)
return self.normalize(self.world)
class CircleGradient(NoiseBase):
def __init__(self, shape):
self.shape = shape
self.gradient = np.zeros(shape)
def generate(self):
center_x, center_y = self.shape[1] // 2, self.shape[0] // 2
for y in range(self.shape[0]):
for x in range(self.shape[1]):
distx = abs(x - center_x)
disty = abs(y - center_y)
dist = math.sqrt(distx * distx + disty * disty)
self.gradient[y][x] = dist
self.gradient = self.normalize(self.gradient)
self.gradient = (self.gradient - 0.5) * -2.0
# Shrink gradient
for y in range(self.shape[0]):
for x in range(self.shape[1]):
if self.gradient[y][x] > 0:
self.gradient[y][x] *= 20
return self.normalize(self.gradient)
class WorldNoise(NoiseBase):
def __init__(self, noise_map, gradient):
self.noise_map = noise_map
self.gradient = gradient
self.combined = np.zeros(noise_map.shape)
def generate(self):
for i in range(self.noise_map.shape[0]):
for j in range(self.noise_map.shape[1]):
self.combined[i][j] = self.noise_map[i][j] * self.gradient[i][j]
if self.combined[i][j] > 0:
self.combined[i][j] *= 20
return self.normalize(self.combined)
#Create Noise map
class NoiseUtils:
def __init__(self, threshold=0.4):
self.threshold = threshold
self.colors = {
"blue": [65, 105, 225],
"sandy": [210, 180, 140],
"beach": [238, 214, 175],
"green": [34, 139, 34],
"darkgreen": [0, 100, 0],
"mountain": [139, 137, 137],
"snow": [255, 250, 250],
}
def add_color(self, world):
color_world = np.zeros(world.shape + (3,)) # Create an RGB image
for i in range(world.shape[0]):
for j in range(world.shape[1]):
value = world[i][j] # Get the value at (i, j)
if isinstance(value, np.ndarray):
value = value.item() # Convert to scalar if it's an array
if value < self.threshold + 0.05:
color_world[i][j] = self.colors["blue"]
elif value < self.threshold + 0.055:
color_world[i][j] = self.colors["sandy"]
elif value < self.threshold + 0.1:
color_world[i][j] = self.colors["beach"]
elif value < self.threshold + 0.25:
color_world[i][j] = self.colors["green"]
elif value < self.threshold + 0.6:
color_world[i][j] = self.colors["darkgreen"]
elif value < self.threshold + 0.7:
color_world[i][j] = self.colors["mountain"]
elif value < self.threshold + 1.0:
color_world[i][j] = self.colors["snow"]
return color_world
def normalize_rgb(self, world):
temp = world
temp = (temp - temp.min()) / (temp.max() - temp.min()) * 255
temp = temp.astype(np.uint8)
return temp
class World(NoiseMap):
def __init__(self, shape, scale, octaves, persistence, lacunarity):
super().__init__(shape, scale, octaves, persistence, lacunarity)
self.gradient = CircleGradient(shape)
self.world_noise = None
def generate(self):
# Generate noise map and gradient
noise_data = super().generate() # Use the generate method from NoiseMap
gradient_data = self.gradient.generate()
# Combine noise and gradient into world noise
self.world_noise = WorldNoise(noise_data, gradient_data).generate()
return self.world_noise
def get_colored_world(self, threshold=0.2):
if self.world_noise is None:
raise ValueError("World noise has not been generated yet. Call `generate()` first.")
utils = NoiseUtils(threshold)
colored_world = utils.add_color(self.world_noise)
return utils.normalize_rgb(colored_world)
# # Main program
# shape = (500, 500)
# scale = 100.0
# octaves = 6
# persistence = 0.5
# lacunarity = 2.0
#
# # Create and generate the world
# world = World(shape, scale, octaves, persistence, lacunarity)
# world.generate()
#
# # Generate the colored world
# colored_world = world.get_colored_world()
#
# # Display the world
# img = Image.fromarray(colored_world)
# img.show()