-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.php
212 lines (165 loc) · 7.47 KB
/
map.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE HTML>
<html>
<head>
<title>JavaScript midpoint displacement map</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.min.js"></script>
<script type="text/javascript" src="jsMidpointDisplacementMap.js"></script>
<script type="text/javascript">
function getTime() {
return (new Date()).getTime();
}
// draws map grayscale
function draw(canvas, map) {
var ctx = canvas.getContext('2d');
for (var y=0; y<map.length; y++)
for (var x=0; x<map[y].length; x++) {
var c = map[y][x];
ctx.fillStyle = "rgb(" + [c,c,c].join(',') + ")";
ctx.fillRect (x, y, x+1, y+1);
}
}
// draws map colored
function drawColoredMap(canvas, map) {
var ctx = canvas.getContext('2d');
for (var y=0; y<map.length; y++)
for (var x=0; x<map[y].length; x++) {
var
c = map[y][x];
if (c < 96) {
ctx.fillStyle = "rgb(" + [0,0,c+64].join(',') + ")";
} else if (c >= 96 && c < 192) {
ctx.fillStyle = "rgb(" + [0,c,0].join(',') + ")";
} else if (c >= 192) {
ctx.fillStyle = "rgb(" + [c,c,c].join(',') + ")";
}
ctx.fillRect (x, y, x+1, y+1);
}
}
// don't draw with the context image functions
function drawColoredMapImgd(canvas, cmap, water, forest, mountains, shadows, blur) {
var map = blur ? blurMap(cmap) : cmap,
h = map.length,
w = map[0].length,
ctx = canvas.getContext('2d'),
imgd = ctx.getImageData(0, 0, w, h),
pixm = imgd.data,
c, rgb, incl;
for (var y=0; y<h; y++)
for (var x=0; x<w; x++) {
// for the last line, incl = 0
incl = y<h-1 ? (map[y][x] - map[y+1][x])*3 : 0;
c = map[y][x];
wc = shadows ? Math.floor(Math.max(Math.min(map[y][x]-incl, 255), 0)) : c;
rgb = [0, 0, 0];
if (c < water) {
rgb = [0, 0, c+92];
} else if (c >= water && c < forest) {
rgb = [0, wc/1.5, 0];
} else if (c >= forest && c < mountains) {
rgb = [wc/1.2, wc/1.2, wc/1.2];
} else if (c >= mountains) {
rgb = [wc, wc, wc];
}
var koo = y*w*4 + (x*4);
pixm[koo + 0] = rgb[0];
pixm[koo + 1] = rgb[1];
pixm[koo + 2] = rgb[2];
pixm[koo + 3] = 255;
}
ctx.putImageData(imgd, 0, 0);
}
// blur map
function blurMap(map) {
var nm = [],
x, y;
for (y=0; y<map.length; y++) {
nm[y] = [];
for (x=0; x<map[y].length; x++) {
nm[y][x] = map[y][x];
}
}
for (y=1; y<map.length-1; y++)
for (x=1; x<map[y].length-1; x++)
nm[y][x] =
(map[y-1][x-1] + map[y-1][x] + map[y-1][x+1] + map[y][x-1] + map[y][x] + map[y][x+1] + map[y+1][x-1] + map[y+1][x] + map[y+1][x+1]) / 9;
return nm;
}
// setup and UI
$(document).ready(function () {
var hmap = [];
// create map size selector
for (var i=5; i<12; i++)
$('#size').append('<option ' + (i==8 ? 'selected="selected"' : '') + '>' + (Math.pow(2, i)+1) + '</option>');
var draw = function (map) {
drawColoredMapImgd($('#canv').get(0), map, parseInt($('#water').val()), parseInt($('#forest').val()), parseInt($('#mountain').val()), $('#shadows:checked').val(), parseInt($('#blur:checked').val()));
}
var gen = function () {
// get selected size
var size = parseInt($('#size OPTION:selected').val());
// set width and height of the canvas
$('#canv').attr('width', size);
$('#canv').attr('height', size);
// start generating
var tStartGen = getTime();
var generator = new MidpointDisplacementMapGenerator(size, parseFloat($('#variability').val()));
// generate the map
var map = generator.generate(255);
var genTime = getTime() - tStartGen;
tStart = getTime();
// adjust values to between 0 and 255
var adjTime = getTime() - tStart;
tStart = getTime();
draw(map);
// draw the map to canvas
var drawTime = getTime() - tStart;
$('#time').html('time to generate: ' + genTime + 'ms, time to adjust: ' + adjTime + 'ms, time to draw: ' + drawTime + 'ms, total: ' + (getTime()-tStartGen) + 'ms');
return map;
}
$('#start').click(function () {
hmap = gen();
});
$('#water, #forest, #shadows, #blur').change(function () {
draw(hmap);
});
var adjInpVal = function (elem, op) {
elem.val(Math.min(Math.max(parseInt(elem.val())+op, 0), 256));
draw(hmap);
}
// +-btns
$('#wp').click(function () { adjInpVal($('#water'), +1); });
$('#wm').click(function () { adjInpVal($('#water'), -1); });
$('#fp').click(function () { adjInpVal($('#forest'), +1); });
$('#fm').click(function () { adjInpVal($('#forest'), -1); });
$('#mp').click(function () { adjInpVal($('#mountain'), +1); });
$('#mm').click(function () { adjInpVal($('#mountain'), -1); });
// create initial map
hmap = gen();
});
</script>
<style type="text/css">
BODY {
font-size: 10pt;
font-family: Helvetica, Verdana, Arial;
}
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>midpoint displacement height map</h1>
<p>Explanation: <a href="http://www.gameprogrammer.com/fractal.html#diamond">www.gameprogrammer.com</a>. Uses html5 canvas, so you need FF, chrome, safari or opera. No IE. <a href="http://github.com/schnalle/JS-Midpoint-Displacement-Map">Github source</a></p>
<canvas id="canv" width="0" height="0"></canvas>
<fieldset><legend>adjust coloring</legend>
Water: <input id="water" type="text" value="96"> <input id="wp" type="button" value="+"><input id="wm" type="button" value="-">,
Forest: <input id="forest" type="text" value="170"> <input id="fp" type="button" value="+"><input id="fm" type="button" value="-">,
Mountains: <input id="mountain" type="text" value="210"> <input id="mp" type="button" value="+"><input id="mm" type="button" value="-"><br/>
<label for="shadows"><abbr title="Sun in the north">Shadows</abbr>:</label> <input id="shadows" type="checkbox" value="1">,
<label for="blur"><abbr title="makes drawing a lot slower">Blur</abbr>:</label> <input id="blur" type="checkbox" value="1"></fieldset>
<fieldset><legend>generate</legend>
Size: <select id="size"></select>, Variability (best between 1..2): <input id="variability" type="text" value="1.4"> <input id="start" type="button" value="go!"><br/>
Time: <span id="time"></span>
</fieldset>
<p>Questions? Suggestions? Spam? Try <a href="mailto:[email protected]">[email protected]</a>, last updated <?php echo date('Y-m-d', filectime(__FILE__)); ?></p>
</body>
</html>