@Override
public void process(GeneratingRegion region) {
Border3D borderForTreeFacet = region.getBorderForFacet(TreeFacet.class);
TreeFacet facet = new TreeFacet(region.getRegion(), borderForTreeFacet.extendBy(0, 15, 10));
SurfaceHeightFacet surface = region.getRegionFacet(SurfaceHeightFacet.class);
DensityFacet density = region.getRegionFacet(DensityFacet.class);
SeaLevelFacet seaLevel = region.getRegionFacet(SeaLevelFacet.class);
Rect2i worldRegion2D = Rect2i.createFromMinAndMax(facet.getWorldRegion().minX(),
facet.getWorldRegion().minZ(),
facet.getWorldRegion().maxX(),
facet.getWorldRegion().maxZ());
for (Vector2i pos : worldRegion2D) {
int x = pos.getX();
int z = pos.getY();
int height = TeraMath.floorToInt(surface.getWorld(x, z));
// if the surface is in range, and if we are above sea level
if (facet.getWorldRegion().encompasses(x, height, z) && facet.getWorldRegion().encompasses(x, height + 1, z) && height >= seaLevel.getSeaLevel()) {
// if the block on the surface is dense enough
if (density.getWorld(x, height, z) >= 0
&& density.getWorld(x, height + 1, z) < 0
// and if there is a level surface in adjacent directions
&& (x > facet.getWorldRegion().minX() && TeraMath.floorToInt(surface.getWorld(x - 1, z)) == height)
&& (x < facet.getWorldRegion().maxX() && TeraMath.floorToInt(surface.getWorld(x + 1, z)) == height)
&& (z > facet.getWorldRegion().minZ() && TeraMath.floorToInt(surface.getWorld(x, z - 1)) == height)
&& (z < facet.getWorldRegion().maxZ() && TeraMath.floorToInt(surface.getWorld(x, z + 1)) == height)
// and if it selects a % of them
&& treeNoise.noise(x, z) / 256f < configuration.density) {
facet.setWorld(x, height + 1, z, treeSeedNoise.noise(x, z));
}
}