-
Notifications
You must be signed in to change notification settings - Fork 112
/
brownian.js
142 lines (133 loc) · 5.29 KB
/
brownian.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
/**
* Generate random terrain using Brownian motion.
*
* Note that this method takes a particularly long time to run (a few seconds).
*
* Parameters are the same as those for {@link THREE.Terrain.DiamondSquare}.
*/
THREE.Terrain.Brownian = function(g, options) {
var untouched = [],
touched = [],
smallerSideSize = Math.min(options.xSize, options.ySize),
changeDirectionProbability = Math.sqrt(smallerSideSize) / smallerSideSize,
maxHeightAdjust = Math.sqrt(options.maxHeight - options.minHeight),
xl = options.xSegments + 1,
yl = options.ySegments + 1,
i = Math.floor(Math.random() * options.xSegments),
j = Math.floor(Math.random() * options.ySegments),
x = i,
y = j,
numVertices = g.length,
vertices = Array.from(g).map(function(z) {
return { z: z };
}),
current = vertices[j * xl + i],
randomDirection = Math.random() * Math.PI * 2,
addX = Math.cos(randomDirection),
addY = Math.sin(randomDirection),
n,
m,
key,
sum,
c,
lastAdjust,
index;
// Initialize the first vertex.
current.z = Math.random() * (options.maxHeight - options.minHeight) + options.minHeight;
touched.push(current);
// Walk through all vertices until they've all been adjusted.
while (touched.length !== numVertices) {
// Mark the untouched neighboring vertices to revisit later.
for (n = -1; n <= 1; n++) {
for (m = -1; m <= 1; m++) {
key = (j+n)*xl + i + m;
if (typeof vertices[key] !== 'undefined' && touched.indexOf(vertices[key]) === -1 && i+m >= 0 && j+n >= 0 && i+m < xl && j+n < yl && n && m) {
untouched.push(vertices[key]);
}
}
}
// Occasionally, pick a random untouched point instead of continuing.
if (Math.random() < changeDirectionProbability) {
current = untouched.splice(Math.floor(Math.random() * untouched.length), 1)[0];
randomDirection = Math.random() * Math.PI * 2;
addX = Math.cos(randomDirection);
addY = Math.sin(randomDirection);
index = vertices.indexOf(current);
i = index % xl;
j = Math.floor(index / xl);
x = i;
y = j;
}
else {
// Keep walking in the current direction.
var u = x,
v = y;
while (Math.round(u) === i && Math.round(v) === j) {
u += addX;
v += addY;
}
i = Math.round(u);
j = Math.round(u);
// If we hit a touched vertex, look in different directions to try to find an untouched one.
for (var k = 0; i >= 0 && j >= 0 && i < xl && j < yl && touched.indexOf(vertices[j * xl + i]) !== -1 && k < 9; k++) {
randomDirection = Math.random() * Math.PI * 2;
addX = Math.cos(randomDirection);
addY = Math.sin(randomDirection);
while (Math.round(u) === i && Math.round(v) === j) {
u += addX;
v += addY;
}
i = Math.round(u);
j = Math.round(v);
}
// If we found an untouched vertex, make it the current one.
if (i >= 0 && j >= 0 && i < xl && j < yl && touched.indexOf(vertices[j * xl + i]) === -1) {
x = u;
y = v;
current = vertices[j * xl + i];
var io = untouched.indexOf(current);
if (io !== -1) {
untouched.splice(io, 1);
}
}
// If we couldn't find an untouched vertex near the current point,
// pick a random untouched vertex instead.
else {
current = untouched.splice(Math.floor(Math.random() * untouched.length), 1)[0];
randomDirection = Math.random() * Math.PI * 2;
addX = Math.cos(randomDirection);
addY = Math.sin(randomDirection);
index = vertices.indexOf(current);
i = index % xl;
j = Math.floor(index / xl);
x = i;
y = j;
}
}
// Set the current vertex to the average elevation of its touched neighbors plus a random amount
sum = 0;
c = 0;
for (n = -1; n <= 1; n++) {
for (m = -1; m <= 1; m++) {
key = (j+n)*xl + i + m;
if (typeof vertices[key] !== 'undefined' && touched.indexOf(vertices[key]) !== -1 && i+m >= 0 && j+n >= 0 && i+m < xl && j+n < yl && n && m) {
sum += vertices[key].z;
c++;
}
}
}
if (c) {
if (!lastAdjust || Math.random() < changeDirectionProbability) {
lastAdjust = Math.random();
}
current.z = sum / c + THREE.Terrain.EaseInWeak(lastAdjust) * maxHeightAdjust * 2 - maxHeightAdjust;
}
touched.push(current);
}
for (i = vertices.length - 1; i >= 0; i--) {
g[i] = vertices[i].z;
}
// Erase artifacts.
THREE.Terrain.Smooth(g, options);
THREE.Terrain.Smooth(g, options);
};