-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiamond_sqr.js
168 lines (150 loc) · 7.43 KB
/
diamond_sqr.js
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
/*
* Copyright 2014, Chris Dewbery
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgement in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
"use strict";
/* The diamond square algorithm, also known as random midpoint displacement,
* cloud fractal, or the plasmas fractal is a method for procedurally generating
* height maps which we can use as a base for rendering 3d terrain.
*/
var DiamondSquare = Object.create(Object, {
_data: {
writable: true,
value: null,
},
data: {
get: function() {
return this._data;
}
},
_image: {
writable: true,
value : null,
},
image: {
get: function() {
return this._image;
}
},
init : {
value: function(dataSize, seed) {
var h = 200.0;
/* create a two dimensional array to store the height map data */
this._data = new Array(dataSize);
for (var i = 0; i < dataSize; i++) {
this.data[i] = new Array(dataSize);
}
/* initialise the corner values
*
* X X
*
*
*
* X X
*/
this._data[0][0] = seed; // top left
this._data[0][dataSize - 1] = seed; // bottom left
this._data[dataSize - 1][0] = seed; // top right
this._data[dataSize - 1][dataSize - 1] = seed; // bottom right
/* while the length of the side of the squares is greater than zero */
for (var sideLength = dataSize - 1; sideLength >= 2; sideLength /= 2, h /= 2.0) {
var halfSide = sideLength / 2;
/* The diamond step: Taking a square of four points, generate a new value for the
* square midpoint. The midpoint value is calculated by averaging the four corner values
* and adding a random value
*
* A B point 1. calculated by averaging (topleft A, topright B, botleft C, botright D)
*
* 1
*
* C D
*/
for (var x = 0; x < dataSize - 1; x += sideLength) {
for (var y = 0; y < dataSize - 1; y += sideLength) {
var topLeft = this._data[x][y];
var topRight = this._data[x + sideLength][y];
var botLeft = this._data[x][y + sideLength];
var botRight = this._data[x + sideLength][y + sideLength];
var avg = (topLeft + topRight + botLeft + botRight) / 4.0;
var offset = (Math.random() * (h * 2)) - h;
this._data[x + halfSide][y + halfSide] = offset + avg;
}
}
/* The square step: Taking a diamond of four points, generate a new value for the
* diamond midpoint. The midpoint value is calculated by averaging the four surrounding
* values and adding a random value.
*
* A 2 D point 1. calculated by averaging (top A, left B, bot C, right B);
* point 2. calculated by averaging (top B, left A, bot B, right D);
* 1 B 4 point 3. calculated by averaging (top B, left C, bot B, right E);
* point 4. calculated by averaging (top D, left B, bot E, right B);
* C 3 E
*
*/
for (var x = 0; x < dataSize; x += halfSide) {
for (var y = (x + halfSide) % sideLength; y < dataSize; y += sideLength) {
var left = this._data[(x - halfSide + dataSize - 1) % (dataSize - 1)] [y];
var right = this._data[(x + halfSide) % (dataSize - 1)][y];
var top = this._data[x][(y - halfSide + dataSize - 1) % (dataSize - 1)];
var bot = this._data[x][(y + halfSide) % (dataSize - 1)];
var avg = (left + right + top + bot) / 4.0;
var offset = (Math.random() * (h * 2)) - h;
this._data[x][y] = offset + avg;
}
}
/* back to the next round of the diamond step, then the square step and so forth */
/*
* init diamond square diamond square
* O O X X X O X X X X X O X O X
* O O O X O X O
* --> O --> O X O --> X X X --> X O X O X
* O O O X O X O
* O O X X X O X X X X X O X O X
*/
}
/* finished with height map generation, now lets generate an RBG image to use based
* so we can visualise the height map, and inspect it for flaws */
/* calculate minimum and max and use this to scale the image output
* to the range of 0 - 255 */
var maxY = -32767.0, minY = 32767.0;
for (var x = 0; x < dataSize - 1; x++) {
for (var y = 0; y < dataSize - 1; y++) {
if (this._data[x][y] > maxY)
maxY = this._data[x][y];
if (this._data[x][y] < minY)
minY = this._data[x][y];
}
}
/* store the height map data into an ArrayBuffer in an RGB format */
this._image = new Array(((dataSize-1) * (dataSize-1)) * 3);
for (var x = 0; x < dataSize -1; x ++) {
for (var y = 0; y < dataSize -1; y ++) {
/* scale our data set range to 0 - 255 */
var pixel = ((this._data[x][y] - minY) / (maxY - minY)) * 255;
this._image[(x + (y * (dataSize - 1))) * 3 + 0] = pixel; // red
this._image[(x + (y * (dataSize - 1))) * 3 + 1] = pixel; // green
this._image[(x + (y * (dataSize - 1))) * 3 + 2] = pixel; // blue
}
}
return this;
}
},
});