smooth static method
Smooth the terrain by setting each point to the mean of its neighborhood.
Float32List g
The geometry's z-positions to modify with heightmap data.
TerrainOptions options
A map of settings that control how the terrain is constructed and
displayed. Valid values are the same as those for the options parameter
of {@link THREE.Terrain}().
double weight = 0
How much to weight the original vertex height against the average of its
neighbors.
Implementation
static void smooth(Float32List g, TerrainOptions options, [double weight = 0]) {
final heightmap = Float32List(g.length);
for (int i = 0, xl = options.xSegments + 1, yl = options.ySegments + 1; i < xl; i++) {
for (int j = 0; j < yl; j++) {
double sum = 0;
int c = 0;
for (int n = -1; n <= 1; n++) {
for (int m = -1; m <= 1; m++) {
final key = (j+n)*xl + i + m;
if (i+m >= 0 && j+n >= 0 && i+m < xl && j+n < yl) {//typeof g[key] != 'undefined' &&
sum += g[key];
c++;
}
}
}
heightmap[j*xl + i] = sum / c;
}
}
weight = weight;
final w = 1 / (1 + weight);
for (int k = 0, l = g.length; k < l; k++) {
g[k] = (heightmap[k] + g[k]*weight)*w;
}
}