Skip to content
This repository was archived by the owner on Mar 19, 2018. It is now read-only.

Commit e2b6fb2

Browse files
committed
Yes
0 parents  commit e2b6fb2

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.png
2+
generated.png

PythonApplication2.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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();

PythonApplication2.pyproj

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<SchemaVersion>2.0</SchemaVersion>
6+
<ProjectGuid>4510984d-d54c-4d27-afaf-8170b3504e71</ProjectGuid>
7+
<ProjectHome>.</ProjectHome>
8+
<StartupFile>PythonApplication2.py</StartupFile>
9+
<SearchPath>
10+
</SearchPath>
11+
<WorkingDirectory>.</WorkingDirectory>
12+
<OutputPath>.</OutputPath>
13+
<Name>PythonApplication2</Name>
14+
<RootNamespace>PythonApplication2</RootNamespace>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
23+
</PropertyGroup>
24+
<ItemGroup>
25+
<Compile Include="PythonApplication2.py" />
26+
</ItemGroup>
27+
<PropertyGroup>
28+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
29+
<PtvsTargetsFile>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets</PtvsTargetsFile>
30+
</PropertyGroup>
31+
<Import Condition="Exists($(PtvsTargetsFile))" Project="$(PtvsTargetsFile)" />
32+
<Import Condition="!Exists($(PtvsTargetsFile))" Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
33+
<!-- Uncomment the CoreCompile target to enable the Build command in
34+
Visual Studio and specify your pre- and post-build commands in
35+
the BeforeBuild and AfterBuild targets below. -->
36+
<!--<Target Name="CoreCompile" />-->
37+
<Target Name="BeforeBuild">
38+
</Target>
39+
<Target Name="AfterBuild">
40+
</Target>
41+
</Project>

0 commit comments

Comments
 (0)