whiteNoise static method
void
whiteNoise(
- Float32List g,
- TerrainOptions options,
- double scale,
- int segments,
- double range,
- Float64List data,
Generate a heightmap using white noise.
List<Vector3> g The terrain vertices. TerrainOptions options Settings double scale The resolution of the resulting heightmap. int segments The width of the target heightmap. double range The altitude of the noise. Float64List data The target heightmap.
Implementation
static void whiteNoise(Float32List g, TerrainOptions options, double scale, int segments, double range, Float64List data) {
if (scale > segments) return;
int i = 0,
j = 0,
xl = segments,
yl = segments,
inc = (segments / scale).floor(),
lastX = -inc,
lastY = -inc;
// Walk over the target. For a target of size W and a resolution of N,
// set every W/N points (in both directions).
for (i = 0; i <= xl; i += inc) {
for (j = 0; j <= yl; j += inc) {
int k = j * xl + i;
data[k] = math.Random().nextDouble() * range;
if (lastX < 0 && lastY < 0) continue;
// jscs:disable disallowSpacesInsideBrackets
/* c b *
* l t */
final t = data.get(k),
l = data.get( j * xl + (i-inc)) ?? t, // left
b = data.get((j-inc) * xl + i ) ?? t, // bottom
c = data.get((j-inc) * xl + (i-inc)) ?? t; // corner
// jscs:enable disallowSpacesInsideBrackets
// Interpolate between adjacent points to set the height of
// higher-resolution target data.
for (int x = lastX; x < i; x++) {
for (int y = lastY; y < j; y++) {
if (x == lastX && y == lastY) continue;
int z = y * xl + x;
if (z < 0) continue;
final px = ((x-lastX) / inc),
py = ((y-lastY) / inc),
r1 = px * b + (1-px) * c,
r2 = px * t + (1-px) * l;
data[z] = py * r2 + (1-py) * r1;
}
}
lastY = j;
}
lastX = i;
lastY = -inc;
}
// Assign the temporary data back to the actual terrain heightmap.
xl = options.xSegments + 1;
yl = options.ySegments + 1;
for (i = 0; i < xl; i++) {
for (j = 0; j < yl; j++) {
// http://stackoverflow.com/q/23708306/843621
final kg = j * xl + i,
kd = j * segments + i;
g[kg] += data[kd];
}
}
}