DE
games/rob-revisited / src / Revisited / Render / Terrain.php · commit f9a1307
<?phpnamespace App\Revisited\Render;use App\Revisited\Land\Zone;/** * Builds the ground of a zone once (PHP float lists, uploaded by Graphics): chunks of the height grid with * smooth normals, splat weights from the biomes and the slope, ambient occlusion (valleys, the shade of trees), * the caves' floor walls and roof, the lakes, and the hills beyond the zone's edge. * * Terrain vertex: x y z | nx ny nz | grass jungle mud sand | rock snow ice ao | tint r g b | wet ash lush 0 * (VERTEX floats). */final class Terrain{    public const VERTEX = 21;    private const W = 13;    /** biome => [grass, jungle, mud, sand, rock, snow, ice, tint r, g, b, wet, ash, lush] */    private const BIOMES = [        'grass' => [1, 0, 0, 0, 0, 0, 0, 1.0, 1.0, 1.0, 0, 0, 0],        'jungle' => [0.15, 0.85, 0, 0, 0, 0, 0, 1.0, 1.0, 1.0, 0, 0, 0.3],        'jungle_dense' => [0.0, 0.9, 0.1, 0, 0, 0, 0, 1.0, 1.0, 1.0, 0.1, 0, 1.0],        'mud' => [0, 0.1, 0.9, 0, 0, 0, 0, 1.0, 1.0, 1.0, 0, 0, 0],        'marsh' => [0.15, 0.25, 0.6, 0, 0, 0, 0, 0.9, 0.95, 0.85, 0.75, 0, 0.6],        'sand' => [0, 0, 0, 1, 0, 0, 0, 1.0, 1.0, 1.0, 0, 0, 0],        'beach' => [0, 0, 0, 1, 0, 0, 0, 1.08, 1.02, 0.9, 0.15, 0, 0],        'rock' => [0, 0, 0, 0.05, 0.95, 0, 0, 1.0, 1.0, 1.0, 0, 0, 0],        'basalt' => [0, 0, 0.05, 0.4, 0.55, 0, 0, 0.36, 0.36, 0.4, 0, 0, 0],        'snow' => [0, 0, 0, 0, 0.05, 0.95, 0, 1.0, 1.0, 1.0, 0, 0, 0],        'ice' => [0, 0, 0, 0, 0, 0.2, 0.8, 1.0, 1.0, 1.0, 0, 0, 0],        'cave' => [0, 0, 0.25, 0, 0.75, 0, 0, 0.58, 0.56, 0.53, 0, 0, 0],        'redrock' => [0, 0, 0, 0.35, 0.65, 0, 0, 1.05, 0.58, 0.4, 0, 0, 0],        'burnt' => [0.3, 0, 0.55, 0, 0.15, 0, 0, 0.6, 0.58, 0.55, 0, 1.0, 0],        'ash' => [0.0, 0, 0.6, 0, 0.4, 0, 0, 0.6, 0.58, 0.55, 0, 1.0, 0],    ];    /** @var list<float> effective floor heights (caves: raised where solid) */    private array $floor = [];    /** @var list<float> */    private array $ao = [];    /** @var list<float> caves: roof heights (same grid as $floor) */    private array $roof = [];    /** vertices per side of the working grid, its cell size, its subdivision of the zone's grid */    private int $n;    private float $cs;    private int $sub = 1;    /**     * @param list<array{0: float, 1: float, 2: float, 3: float}> $shadows [x, z, radius, strength]     */    public function __construct(private Zone $zone, private array $shadows = [])    {        $this->n = $zone->grid + 1;        $this->cs = $zone->cell;        $this->prepare();    }    public function floorAt(int $ix, int $iz): float    {        $ix = max(0, min($this->n - 1, $ix));        $iz = max(0, min($this->n - 1, $iz));        return $this->floor[$iz * $this->n + $ix];    }    private function prepare(): void    {        $z = $this->zone;        $n = $this->n;        $g = $z->grid;        $count = $n * $n;        $this->floor = [];        $heights = $z->heights;        for ($k = 0; $k < $count; $k++) {            $this->floor[] = (float) ($heights[$k] ?? 0.0);        }        if ($z->cave) {            // walls: a vertex among solid cells rises toward the roof (rough, noisy rock)            for ($iz = 0; $iz < $n; $iz++) {                for ($ix = 0; $ix < $n; $ix++) {                    $f = $this->solidFraction($ix, $iz);                    if ($f <= 0) {                        continue;                    }                    $k = $iz * $n + $ix;                    $c = (float) ($z->ceilings[$k] ?? ($this->floor[$k] + 4.0));                    $gnd = $this->floor[$k];                    $j = Mesh::hash($ix * 7 + 3, $iz * 13 + $z->seed) - 0.5;                    $rise = $f >= 1.0 ? 1.0 : pow($f, 1.4) * (0.85 + $j * 0.4);                    $j2 = Mesh::hash($ix * 3 + 17, $iz * 7 + 5) - 0.5;                    $this->floor[$k] = $gnd + ($c - $gnd) * min(1.0, $rise) + ($f < 1 ? $j * 1.4 : 0.0) + $j2 * 0.5 * $f;                }            }        }        $this->roof = [];        if ($z->cave) {            for ($k = 0; $k < $count; $k++) {                $this->roof[] = (float) ($z->ceilings[$k] ?? ($this->floor[$k] + 4.0));            }            $this->subdivide(2);            $n = $this->n;            $count = $n * $n;            $g = $n - 1;        }        // ambient occlusion: concave places, the shade of trees and rocks        $this->ao = array_fill(0, $count, 1.0);        for ($iz = 0; $iz < $n; $iz++) {            for ($ix = 0; $ix < $n; $ix++) {                $h = $this->floor[$iz * $n + $ix];                $avg = ($this->floorAt($ix - 2, $iz) + $this->floorAt($ix + 2, $iz) + $this->floorAt($ix, $iz - 2) + $this->floorAt($ix, $iz + 2)) / 4;                $this->ao[$iz * $n + $ix] = max(0.55, min(1.0, 1.0 - ($avg - $h) * 0.09));            }        }        $cell = $this->cs;        foreach ($this->shadows as [$sx, $sz, $r, $str]) {            $x0 = max(0, (int) floor(($sx - $r) / $cell));            $x1 = min($g, (int) ceil(($sx + $r) / $cell));            $z0 = max(0, (int) floor(($sz - $r) / $cell));            $z1 = min($g, (int) ceil(($sz + $r) / $cell));            for ($iz = $z0; $iz <= $z1; $iz++) {                for ($ix = $x0; $ix <= $x1; $ix++) {                    $d = sqrt(($ix * $cell - $sx) ** 2 + ($iz * $cell - $sz) ** 2) / $r;                    if ($d < 1) {                        $this->ao[$iz * $n + $ix] *= 1 - $str * (1 - $d) * (1 - $d) * 0.9;                    }                }            }        }    }    /**     * Caves: a finer grid (bilinear between the zone's vertices) with rough rock where the floor climbs into a     * wall, and a lumpy roof with hanging points.     */    private function subdivide(int $sub): void    {        $n0 = $this->n;        $f0 = $this->floor;        $r0 = $this->roof;        $n = ($n0 - 1) * $sub + 1;        $seed = $this->zone->seed;        $floor = [];        $roof = [];        for ($iz = 0; $iz < $n; $iz++) {            for ($ix = 0; $ix < $n; $ix++) {                $fx = $ix / $sub;                $fz = $iz / $sub;                $x0 = min($n0 - 2, (int) $fx);                $z0 = min($n0 - 2, (int) $fz);                $tx = $fx - $x0;                $tz = $fz - $z0;                $k = $z0 * $n0 + $x0;                $h = ($f0[$k] * (1 - $tx) + $f0[$k + 1] * $tx) * (1 - $tz) + ($f0[$k + $n0] * (1 - $tx) + $f0[$k + $n0 + 1] * $tx) * $tz;                $c = ($r0[$k] * (1 - $tx) + $r0[$k + 1] * $tx) * (1 - $tz) + ($r0[$k + $n0] * (1 - $tx) + $r0[$k + $n0 + 1] * $tx) * $tz;                // how much of a wall this is: the spread of the four corners                $lo = min($f0[$k], $f0[$k + 1], $f0[$k + $n0], $f0[$k + $n0 + 1]);                $hi = max($f0[$k], $f0[$k + 1], $f0[$k + $n0], $f0[$k + $n0 + 1]);                $wall = max(0.0, min(1.0, ($hi - $lo) / 3.0));                $j = Mesh::hash($ix * 7 + 1, $iz * 11 + $seed) - 0.5;                $j2 = Mesh::hash($ix * 3 + 5, $iz * 5 + $seed * 3) - 0.5;                $h += $j * 1.3 * $wall + $j2 * 0.12;                $c += $j2 * 0.9 - (Mesh::hash($ix, $iz + 77) > 0.92 ? 1.3 * (1 - $wall) : 0.0);                $floor[] = $h;                $roof[] = max($h, $c);            }        }        $this->floor = $floor;        $this->roof = $roof;        $this->n = $n;        $this->sub = $sub;        $this->cs = $this->zone->cell / $sub;    }    private function solidFraction(int $ix, int $iz): float    {        $g = $this->zone->grid;        $s = 0;        foreach ([[-1, -1], [0, -1], [-1, 0], [0, 0]] as [$dx, $dz]) {            $cx = $ix + $dx;            $cz = $iz + $dz;            if ($cx < 0 || $cz < 0 || $cx >= $g || $cz >= $g || ($this->zone->solid[$cz * $g + $cx] ?? false)) {                $s++;            }        }        return $s / 4;    }    /** @return array{0: float, 1: float, 2: float} the normal at a grid vertex */    private function normal(int $ix, int $iz, bool $roof = false): array    {        $c = $this->cs;        if ($roof) {            $hx = $this->roofAt($ix + 1, $iz) - $this->roofAt($ix - 1, $iz);            $hz = $this->roofAt($ix, $iz + 1) - $this->roofAt($ix, $iz - 1);            $l = sqrt($hx * $hx + 4 * $c * $c + $hz * $hz) ?: 1.0;            return [$hx / $l, -2 * $c / $l, $hz / $l];        }        $hx = $this->floorAt($ix + 1, $iz) - $this->floorAt($ix - 1, $iz);        $hz = $this->floorAt($ix, $iz + 1) - $this->floorAt($ix, $iz - 1);        $l = sqrt($hx * $hx + 4 * $c * $c + $hz * $hz) ?: 1.0;        return [-$hx / $l, 2 * $c / $l, -$hz / $l];    }    private function roofAt(int $ix, int $iz): float    {        $ix = max(0, min($this->n - 1, $ix));        $iz = max(0, min($this->n - 1, $iz));        return $this->roof[$iz * $this->n + $ix] ?? ($this->floor[$iz * $this->n + $ix] + 4.0);    }    /** @return list<float> 7 weights + 3 tint + wet, ash, lush, averaged over the cells around a vertex, with slope and shore */    private function weights(int $ix, int $iz, float $ny, float $h): array    {        $z = $this->zone;        $g = $z->grid;        $w = array_fill(0, self::W, 0.0);        $cnt = 0;        // a wide, noisy blend between the biomes: the cells of a 4 x 4 block around the vertex, weighted by a tent,        // the block shifted by up to a cell by a hash of the vertex (no straight line where two grounds meet)        $sub = $this->sub;        $jx = (int) floor((Mesh::hash($ix * 13 + 3, $iz * 7 + $z->seed) - 0.5) * 2.6);        $jz = (int) floor((Mesh::hash($ix * 5 + 11, $iz * 17 + $z->seed * 3) - 0.5) * 2.6);        $bx = intdiv($ix + $sub * 8, $sub) - 8 + $jx;        $bz = intdiv($iz + $sub * 8, $sub) - 8 + $jz;        $fx = fmod($ix / $sub, 1.0);        $fz = fmod($iz / $sub, 1.0);        $cnt = 0.0;        for ($dz = -2; $dz <= 1; $dz++) {            $tz = 2.0 - abs($dz + 0.5 - $fz);            for ($dx = -2; $dx <= 1; $dx++) {                $cx = $bx + $dx;                $cz = $bz + $dz;                if ($cx < 0 || $cz < 0 || $cx >= $g || $cz >= $g) {                    continue;                }                $wt = max(0.05, $tz) * max(0.05, 2.0 - abs($dx + 0.5 - $fx));                $b = self::BIOMES[$z->biomes[$cz * $g + $cx] ?? 'grass'] ?? self::BIOMES['grass'];                for ($k = 0; $k < self::W; $k++) {                    $w[$k] += $b[$k] * $wt;                }                $cnt += $wt;            }        }        if ($cnt <= 0) {            $w = self::BIOMES['grass'];            $cnt = 1.0;        }        for ($k = 0; $k < self::W; $k++) {            $w[$k] /= $cnt;        }        // wet ground near the water        if ($z->water > -999 && !$z->cave) {            $w[10] = max($w[10], max(0.0, min(1.0, 1.0 - ($h - $z->water - 0.15) / 1.3)));        }        // the shore of a lake: sand / mud        if ($z->water > -999 && $h < $z->water + 0.7 && !$z->cave) {            $t = max(0.0, min(1.0, 1 - ($h - $z->water) / 0.7));            $mudShore = $w[1] + $w[2] > $w[0] + $w[3] ? 2 : 3;            for ($k = 0; $k < 7; $k++) {                $w[$k] *= 1 - $t;            }            $w[$mudShore] += $t;        }        // steep slopes: bare rock (snow stays on gentle ones)        $slope = 1 - $ny;        $rock = max(0.0, min(1.0, ($slope - 0.22) / 0.2));        if ($rock > 0) {            for ($k = 0; $k < 7; $k++) {                $w[$k] *= 1 - $rock;            }            $w[4] += $rock;        }        return $w;    }    /**     * Ground chunks of $cells x $cells cells.     * @return list<array{x0: float, z0: float, x1: float, z1: float, minY: float, maxY: float, verts: list<float>, idx: list<int>, roof: ?array{verts: list<float>, idx: list<int>}}>     */    public function chunks(int $cells): array    {        $z = $this->zone;        $n = $this->n;        $g = $n - 1;        $cell = $this->cs;        $cells *= $this->sub;        // vertex attributes of the whole grid, computed once        $vert = [];        for ($iz = 0; $iz < $n; $iz++) {            for ($ix = 0; $ix < $n; $ix++) {                $k = $iz * $n + $ix;                $h = $this->floor[$k];                [$nx, $ny, $nz] = $this->normal($ix, $iz);                $w = $this->weights($ix, $iz, $ny, $h);                $vert[$k] = [$ix * $cell, $h, $iz * $cell, $nx, $ny, $nz, $w[0], $w[1], $w[2], $w[3], $w[4], $w[5], $w[6], $this->ao[$k], $w[7], $w[8], $w[9],                    $w[10], $w[11], $w[12], 0.0];            }        }        $out = [];        for ($cz0 = 0; $cz0 < $g; $cz0 += $cells) {            for ($cx0 = 0; $cx0 < $g; $cx0 += $cells) {                $cx1 = min($g, $cx0 + $cells);                $cz1 = min($g, $cz0 + $cells);                $verts = [];                $idx = [];                $minY = INF;                $maxY = -INF;                $w = $cx1 - $cx0 + 1;                for ($iz = $cz0; $iz <= $cz1; $iz++) {                    for ($ix = $cx0; $ix <= $cx1; $ix++) {                        $v = $vert[$iz * $n + $ix];                        array_push($verts, ...$v);                        $minY = min($minY, $v[1]);                        $maxY = max($maxY, $v[1]);                    }                }                for ($iz = 0; $iz < $cz1 - $cz0; $iz++) {                    for ($ix = 0; $ix < $cx1 - $cx0; $ix++) {                        $a = $iz * $w + $ix;                        // split each quad along the shorter diagonal (smoother hills)                        $ha = $vert[($cz0 + $iz) * $n + $cx0 + $ix][1];                        $hb = $vert[($cz0 + $iz) * $n + $cx0 + $ix + 1][1];                        $hc = $vert[($cz0 + $iz + 1) * $n + $cx0 + $ix][1];                        $hd = $vert[($cz0 + $iz + 1) * $n + $cx0 + $ix + 1][1];                        if (abs($ha - $hd) < abs($hb - $hc)) {                            array_push($idx, $a, $a + $w, $a + $w + 1, $a, $a + $w + 1, $a + 1);                        } else {                            array_push($idx, $a, $a + $w, $a + 1, $a + 1, $a + $w, $a + $w + 1);                        }                    }                }                $roof = null;                if ($z->cave && $this->roof !== []) {                    $rv = [];                    $ri = [];                    for ($iz = $cz0; $iz <= $cz1; $iz++) {                        for ($ix = $cx0; $ix <= $cx1; $ix++) {                            $h = $this->roofAt($ix, $iz);                            [$nx, $ny, $nz] = $this->normal($ix, $iz, true);                            $maxY = max($maxY, $h);                            array_push($rv, $ix * $cell, $h, $iz * $cell, $nx, $ny, $nz, 0.0, 0.0, 0.1, 0.0, 0.9, 0.0, 0.0, 0.7, 0.52, 0.5, 0.48, 0.2, 0.0, 0.0, 0.0);                        }                    }                    for ($iz = 0; $iz < $cz1 - $cz0; $iz++) {                        for ($ix = 0; $ix < $cx1 - $cx0; $ix++) {                            $a = $iz * $w + $ix;                            array_push($ri, $a, $a + 1, $a + $w + 1, $a, $a + $w + 1, $a + $w);                        }                    }                    $roof = ['verts' => $rv, 'idx' => $ri];                }                $out[] = ['x0' => $cx0 * $cell, 'z0' => $cz0 * $cell, 'x1' => $cx1 * $cell, 'z1' => $cz1 * $cell,                    'minY' => (float) $minY, 'maxY' => (float) $maxY, 'verts' => $verts, 'idx' => $idx, 'roof' => $roof];            }        }        return $out;    }    /**     * The lakes and rivers: a quad per cell under the water level, each corner with the water's depth there (the     * shader fades the shallow water out and adds foam) and the current's velocity (m/s, rivers only).     * @param array{0: float, 1: float}|null $downstream where rivers go (else: downhill, toward the main water)     * @return list<float> x y z depth fx fz per vertex (triangles)     */    public function water(?array $downstream = null): array    {        $z = $this->zone;        if ($z->water <= -999) {            return [];        }        $n = $this->n;        $g = $n - 1;        $c = $this->cs;        $wl = $z->water;        $flow = $this->flow($downstream);        $out = [];        for ($iz = 0; $iz < $g; $iz++) {            for ($ix = 0; $ix < $g; $ix++) {                $k0 = $iz * $n + $ix;                $ks = [$k0, $k0 + 1, $k0 + $n + 1, $k0 + $n];                $h = [$this->floor[$ks[0]], $this->floor[$ks[1]], $this->floor[$ks[2]], $this->floor[$ks[3]]];                if (min($h) >= $wl) {                    continue;                }                $p = [[$ix, $iz], [$ix + 1, $iz], [$ix + 1, $iz + 1], [$ix, $iz + 1]];                foreach ([0, 1, 2, 0, 2, 3] as $q) {                    $f = $flow[$ks[$q]] ?? [0.0, 0.0];                    array_push($out, $p[$q][0] * $c, $wl, $p[$q][1] * $c, $wl - $h[$q], $f[0], $f[1]);                }            }        }        return $out;    }    /**     * The current at each wet vertex: along the channel (across the depth's gradient), strong where the water is     * narrow (a river), none in a lake; its sense from $downstream, else downhill on the large scale, else toward     * the centre of the water. Smoothed. @return array<int, array{0: float, 1: float}>     */    private function flow(?array $downstream): array    {        $z = $this->zone;        $n = $this->n;        $wl = $z->water;        $depth = [];        $wet = [];        $cx = 0.0;        $cz = 0.0;        $cnt = 0;        foreach ($this->floor as $k => $h) {            $d = $wl - $h;            $depth[$k] = max(0.0, $d);            if ($d > 0) {                $wet[$k] = true;                $cx += $k % $n;                $cz += intdiv($k, $n);                $cnt++;            }        }        if ($cnt === 0) {            return [];        }        $cx /= $cnt;        $cz /= $cnt;        $at = static fn (int $x, int $y): int => max(0, min($n - 1, $y)) * $n + max(0, min($n - 1, $x));        $R = max(3, (int) round(10 / max(0.5, $this->cs)));        $flow = [];        foreach ($wet as $k => $_) {            $x = $k % $n;            $y = intdiv($k, $n);            // how much of the neighbourhood is water: a river is narrow            $w = 0;            $t = 0;            for ($dy = -$R; $dy <= $R; $dy += 2) {                for ($dx = -$R; $dx <= $R; $dx += 2) {                    $t++;                    if (isset($wet[$at($x + $dx, $y + $dy)])) {                        $w++;                    }                }            }            $frac = $w / max(1, $t);            $river = max(0.0, min(1.0, (0.72 - $frac) / 0.3));            if ($river <= 0.0) {                continue;            }            $gx = $depth[$at($x + 2, $y)] - $depth[$at($x - 2, $y)];            $gz = $depth[$at($x, $y + 2)] - $depth[$at($x, $y - 2)];            $gl = sqrt($gx * $gx + $gz * $gz);            if ($gl < 1e-4) {                continue;            }            $fx = -$gz / $gl;            $fz = $gx / $gl;            // the sense: given, else downhill (the land around, on a large scale), else toward the main water            if ($downstream !== null) {                $s = $fx * $downstream[0] + $fz * $downstream[1];            } else {                $hx = $this->floorAt($x + $R * 2, $y) - $this->floorAt($x - $R * 2, $y);                $hz = $this->floorAt($x, $y + $R * 2) - $this->floorAt($x, $y - $R * 2);                $s = -($fx * $hx + $fz * $hz);                if (abs($s) < 0.3) {                    $s = $fx * ($cx - $x) + $fz * ($cz - $y);                }            }            if ($s < 0) {                $fx = -$fx;                $fz = -$fz;            }            $sp = 0.5 + 0.9 * $river;            $flow[$k] = [$fx * $sp, $fz * $sp];        }        // smooth the field (two passes)        for ($pass = 0; $pass < 2; $pass++) {            $next = [];            foreach ($flow as $k => $f) {                $x = $k % $n;                $y = intdiv($k, $n);                $sx = 0.0;                $sz = 0.0;                $m = 0;                for ($dy = -1; $dy <= 1; $dy++) {                    for ($dx = -1; $dx <= 1; $dx++) {                        $q = $flow[$at($x + $dx, $y + $dy)] ?? null;                        if ($q !== null) {                            $sx += $q[0];                            $sz += $q[1];                            $m++;                        }                    }                }                $next[$k] = [$sx / max(1, $m), $sz / max(1, $m)];            }            $flow = $next;        }        return $flow;    }    /**     * The land beyond the zone's edge, to the horizon: the edge's heights rising into hills, low resolution,     * slightly under the zone where they overlap. @return array{verts: list<float>, idx: list<int>}|null     */    public function outer(float $reach): ?array    {        $z = $this->zone;        if ($z->cave) {            return null;        }        $size = $z->size;        $step = max(8.0, $size / 24);        $cells = (int) ceil(($size + 2 * $reach) / $step);        $origin = -$reach;        $hf = function (float $x, float $zz) use ($z, $size): float {            $cx = max(0.0, min($size, $x));            $cz = max(0.0, min($size, $zz));            $d = sqrt(($x - $cx) ** 2 + ($zz - $cz) ** 2);            $edge = $z->heightAt($cx, $cz);            if ($d <= 0.0) {                return $edge - 3.0;            }            // rolling hills, then ridged mountains far away (sharp crests, gullies)            $n = sin($x * 0.013 + 1.7) * cos($zz * 0.011 + 0.4) * 0.5 + sin($x * 0.031 - $zz * 0.027) * 0.3 + sin(($x + $zz) * 0.061) * 0.12;            $r1 = 1.0 - abs(sin($x * 0.0091 + $zz * 0.0047 + 2.0 + sin($zz * 0.013) * 1.2));            $r2 = 1.0 - abs(sin($x * 0.021 - $zz * 0.017 + 0.7 + sin($x * 0.019) * 0.9));            $ridge = $r1 * $r1 * 0.7 + $r2 * $r2 * 0.3;            $rise = $d * 0.15 + max(0.0, $d - 120) * 0.22 * (0.35 + 0.9 * $ridge);            return $edge - 1.2 + $rise * (0.75 + 0.4 * $n) + $n * min($d, 60.0) * 0.12;        };        $verts = [];        $idx = [];        $w = $cells + 1;        $hs = [];        for ($iz = 0; $iz <= $cells; $iz++) {            for ($ix = 0; $ix <= $cells; $ix++) {                $hs[$iz * $w + $ix] = $hf($origin + $ix * $step, $origin + $iz * $step);            }        }        for ($iz = 0; $iz <= $cells; $iz++) {            for ($ix = 0; $ix <= $cells; $ix++) {                $x = $origin + $ix * $step;                $zz = $origin + $iz * $step;                $h = $hs[$iz * $w + $ix];                $hx = $hs[$iz * $w + min($cells, $ix + 1)] - $hs[$iz * $w + max(0, $ix - 1)];                $hz = $hs[min($cells, $iz + 1) * $w + $ix] - $hs[max(0, $iz - 1) * $w + $ix];                $l = sqrt($hx * $hx + 4 * $step * $step + $hz * $hz) ?: 1.0;                [$nx, $ny, $nz] = [-$hx / $l, 2 * $step / $l, -$hz / $l];                // the biome of the nearest edge cell                $ex = max(0, min($z->grid, (int) round(max(0.0, min($size, $x)) / $z->cell)));                $ez = max(0, min($z->grid, (int) round(max(0.0, min($size, $zz)) / $z->cell)));                $wt = $this->weights($ex, $ez, $ny, $h);                $far = max(0.0, min(1.0, (sqrt(($x - $size / 2) ** 2 + ($zz - $size / 2) ** 2) - $size) / 300));                // far mountains: rock on the slopes, forest below, snow only on the high crests                $wt[4] += $far * 0.5 + max(0.0, 1.0 - $ny * 1.15) * 1.5;                if ($h > 110 && $ny > 0.55) {                    $wt[5] += min(1.0, ($h - 110) / 40);                }                array_push($verts, $x, $h, $zz, $nx, $ny, $nz, $wt[0], $wt[1], $wt[2], $wt[3], $wt[4], $wt[5], $wt[6], 0.95, $wt[7], $wt[8], $wt[9],                    0.0, $wt[11], min(1.0, $wt[12] + $far * 0.5), 0.0);            }        }        for ($iz = 0; $iz < $cells; $iz++) {            for ($ix = 0; $ix < $cells; $ix++) {                $x0 = $origin + $ix * $step;                $z0 = $origin + $iz * $step;                if ($x0 >= $step && $z0 >= $step && $x0 + $step <= $size - $step && $z0 + $step <= $size - $step) {                    continue;        // well inside the zone: nothing                }                $a = $iz * $w + $ix;                array_push($idx, $a, $a + $w, $a + $w + 1, $a, $a + $w + 1, $a + 1);            }        }        if (count($verts) / self::VERTEX > 65535) {            return null;        }        return ['verts' => $verts, 'idx' => $idx];    }}