step static method
Partition a terrain into flat steps.
Float32List g The geometry's z-positions to modify with heightmap data. int levels The number of steps to divide the terrain into. Defaults to (g.length/2)^(1/4).
Implementation
static void step(Float32List g, int? levels) {
int i = 0,
j = 0,
l = g.length;
levels ??= (math.pow(l*0.5, 0.25)).floor();
// Calculate the max, min, and avg values for each bucket
int inc = (l / levels).floor();
List<double> heights = List.filled(l,0);
List<Map<String,double>> buckets = List.filled(levels, {});
for (i = 0; i < l; i++) {
heights[i] = g[i];
}
heights.sort((a, b){ return (a - b).toInt(); });
for (i = 0; i < levels; i++) {
// Bucket by population (bucket size) not range size
List<double> subset = heights.sublist(i*inc, (i+1)*inc);
double sum = 0;
int bl = subset.length;
for (j = 0; j < bl; j++) {
sum += subset[j];
}
buckets[i] = {
'min': subset[0],
'max': subset[subset.length-1],
'avg': sum / bl,
};
}
// Set the height of each vertex to the average height of its bucket
for (i = 0; i < l; i++) {
final startHeight = g[i];
for (j = 0; j < levels; j++) {
if (startHeight >= buckets[j]['min']! && startHeight <= buckets[j]['max']!) {
g[i] = buckets[j]['avg']!;
break;
}
}
}
}