IT
games/rob-revisited / src / Revisited / Render / Models.php · commit f9a1307
<?phpnamespace App\Revisited\Render;/** * The meshes of the things (Thing::$kind) and of the items in hand, built in code once and cached: metres, y up, * the ground at y = 0, facing +z. Three variants of each (picked by the thing's id) so that a forest is not made * of clones, and three levels of detail (0 near, 1 far / phones, 2 far on phones): fewer leaf cards, fewer sides. * Unknown kinds get a generic marker (never an error). */final class Models{    public const VARIANTS = 3;    /** kinds baked into the chunks' static meshes (big, rarely change) */    public const STATIC = ['tree_palm', 'tree_broad', 'tree_conifer', 'tree_dead', 'rock', 'boulder', 'wreck_pod', 'wreck_colony',        'hut', 'tent', 'relay_mast', 'cave_entrance', 'ladder', 'skin_rack', 'reed', 'vine', 'crate', 'campfire',        'shuttle', 'giant_tree', 'liana_bridge', 'stilt_hut', 'market_stall', 'totem', 'cage', 'terminal', 'drone_wreck',        'signpost', 'burnt_tree', 'mangrove', 'reeds_tall', 'basalt', 'fumarole', 'geyser', 'palm_coast', 'boat_wreck',        'weapon_rack', 'forge', 'fire_patch', 'ash'];    /** kinds drawn one by one each frame (they move: a raft bobs on the water) */    public const DYNAMIC = ['raft'];    /** kinds with a big detailed mesh: a far level of detail is worth it */    public const LOD = ['tree_palm', 'tree_broad', 'tree_conifer', 'tree_dead', 'giant_tree', 'mangrove', 'palm_coast', 'burnt_tree',        'reeds_tall', 'reed', 'vine', 'bush_berry', 'bush_red', 'shuttle', 'stilt_hut', 'market_stall', 'boat_wreck', 'basalt'];    /** ground darkening under a thing (radius in metres at scale 1, strength) */    public const SHADOW = ['tree_palm' => [2.5, 0.35], 'tree_broad' => [4.5, 0.55], 'tree_conifer' => [3.0, 0.55], 'tree_dead' => [1.5, 0.2],        'boulder' => [2.5, 0.45], 'rock' => [1.2, 0.35], 'hut' => [3.6, 0.6], 'tent' => [2.2, 0.5], 'wreck_pod' => [3.0, 0.5],        'wreck_colony' => [6.0, 0.5], 'bush_berry' => [1.1, 0.4], 'bush_red' => [1.1, 0.4], 'crate' => [0.8, 0.4], 'cave_entrance' => [5.0, 0.5],        'giant_tree' => [11.0, 0.7], 'mangrove' => [4.0, 0.5], 'palm_coast' => [3.0, 0.35], 'shuttle' => [13.0, 0.6], 'stilt_hut' => [4.0, 0.55],        'market_stall' => [2.6, 0.5], 'basalt' => [3.0, 0.5], 'boat_wreck' => [4.0, 0.45], 'burnt_tree' => [1.5, 0.25], 'forge' => [2.0, 0.4],        'drone_wreck' => [1.8, 0.4], 'reeds_tall' => [1.2, 0.3], 'cage' => [1.4, 0.35], 'totem' => [0.8, 0.3],        'chest_wood' => [0.9, 0.45], 'basket' => [0.6, 0.4], 'chest_metal' => [1.0, 0.5]];    /** @var array<string, Mesh> */    private static array $cache = [];    /** @var array<string, array{0: float, 1: float, 2: float}|null> where the held torches burn */    private static array $tips = [];    /** the level of detail of the mesh being built (0 full, 1 medium, 2 low) */    private static int $lod = 0;    /**     * The mesh of a thing. $param: a size some kinds take (liana_bridge: its length in metres).     */    public static function get(string $kind, int $variant = 0, int $left = 1, int $lod = 0, float $param = 0.0): Mesh    {        $variant = (($variant % self::VARIANTS) + self::VARIANTS) % self::VARIANTS;        $bare = in_array($kind, ['bush_berry', 'bush_red'], true) && $left <= 0;        $open = in_array($kind, ['chest_wood', 'basket', 'chest_metal'], true) && $left <= 0;     // a chest's lid up        $lod = in_array($kind, self::LOD, true) ? max(0, min(2, $lod)) : 0;        $param = $kind === 'liana_bridge' ? round(max(4.0, min(40.0, $param > 0 ? $param : 14.0))) : 0.0;        $key = $kind . ':' . $variant . ($bare ? ':bare' : '') . ($open ? ':open' : '') . ':' . $lod . ($param > 0 ? ':' . $param : '');        if (isset(self::$cache[$key])) {            return self::$cache[$key];        }        self::$lod = $lod;        $m = new Mesh();        $seed = crc32($kind) % 9973 + $variant * 101;        switch ($kind) {            case 'tree_palm': self::palm($m, $seed, false); break;            case 'palm_coast': self::palm($m, $seed, true); break;            case 'tree_broad': self::broad($m, $seed); break;            case 'tree_conifer': self::conifer($m, $seed); break;            case 'tree_dead': self::deadTree($m, $seed, false); break;            case 'burnt_tree': self::deadTree($m, $seed, true); break;            case 'giant_tree': self::giantTree($m, $seed); break;            case 'mangrove': self::mangrove($m, $seed); break;            case 'bush_berry': self::bush($m, $seed, $bare ? Atlas::LEAVES_SMALL : Atlas::BERRY, [1.0, 1.0, 1.0]); break;            case 'bush_red': self::bush($m, $seed, $bare ? Atlas::LEAVES_SMALL : Atlas::BERRY, [1.25, 0.62, 0.45]); break;            case 'mushroom': self::mushroom($m, $seed); break;            case 'herb_heal': self::herb($m, $seed, [0.75, 1.15, 0.7], [1.3, 1.3, 1.35]); break;            case 'herb_fever': self::herb($m, $seed, [1.0, 1.1, 0.55], [1.4, 1.2, 0.3]); break;            case 'reed': self::reeds($m, $seed, 1.9, 6); break;            case 'reeds_tall': self::reeds($m, $seed, 3.0, 10); break;            case 'vine': self::vine($m, $seed); break;            case 'rock': self::rock($m, $seed, 0.9); break;            case 'boulder': self::rock($m, $seed, 2.2); break;            case 'basalt': self::basalt($m, $seed); break;            case 'flint': self::pebble($m, $seed, 0.16, [0.42, 0.45, 0.55]); break;            case 'stone_small': self::pebble($m, $seed, 0.2, [0.9, 0.88, 0.85]); break;            case 'wood': self::woodPile($m, $seed); break;            case 'stick': self::stick($m, $seed); break;            case 'water_spring': self::spring($m, $seed); break;            case 'waterfall': break;           // drawn by Graphics itself (an animated sheet and its spray)            case 'wreck_pod': self::pod($m, $seed); break;            case 'wreck_colony': self::colony($m, $seed); break;            case 'crate': self::crate($m, $seed); break;            case 'hut': self::hut($m, $seed); break;            case 'tent': self::tent($m, $seed); break;            case 'campfire': self::campfire($m, $seed); break;            case 'relay_mast': self::mast($m); break;            case 'cave_entrance': self::caveEntrance($m, $seed); break;            case 'ladder': self::ladder($m); break;            case 'bones': self::bones($m, $seed); break;            case 'carcass': self::carcass($m, $seed); break;            case 'skin_rack': self::skinRack($m); break;            case 'shuttle': Props::shuttle($m, $seed, self::$lod); break;            case 'shuttle_part': Props::shuttlePart($m, $variant); break;            case 'raft': Props::raft($m, $seed); break;            case 'liana_bridge': Props::lianaBridge($m, $seed, $param); break;            case 'stilt_hut': Props::stiltHut($m, $seed); break;            case 'market_stall': Props::marketStall($m, $seed, $variant); break;            case 'totem': Props::totem($m, $seed); break;            case 'cage': Props::cage($m, $seed); break;            case 'terminal': Props::terminal($m, $seed); break;            case 'drone_wreck': Props::droneWreck($m, $seed); break;            case 'signpost': Props::signpost($m, $seed); break;            case 'ash': Props::ash($m, $seed, 1.6); break;            case 'fire_patch': Props::ash($m, $seed, 2.4); break;            case 'fumarole': Props::fumarole($m, $seed); break;            case 'geyser': Props::geyser($m, $seed); break;            case 'boat_wreck': Props::boatWreck($m, $seed); break;            case 'weapon_rack': Props::weaponRack($m, $seed); break;            case 'forge': Props::forge($m, $seed); break;            case 'chest_wood': Props::chestWood($m, $seed, $open); break;            case 'basket': Props::basket($m, $seed, $open); break;            case 'chest_metal': Props::chestMetal($m, $seed, $open); break;            default: self::generic($m);        }        self::$lod = 0;        $m->color = [1.0, 1.0, 1.0];        $m->sway = 0.0;        $m->swayH = 0.0;        return self::$cache[$key] = $m;    }    /** Picks a count by level of detail. */    private static function n(int $full, int $medium, int $low): int    {        return self::$lod === 0 ? $full : (self::$lod === 1 ? $medium : $low);    }    // ---- items ------------------------------------------------------------------------------------------    /** The item kind behind an item id (French names work too). */    public static function itemKind(string $item): string    {        $i = strtolower($item);        return match (true) {            str_contains($i, 'shield') || str_contains($i, 'bouclier') => str_contains($i, 'scrap') || str_contains($i, 'ferraille') ? 'shield_scrap' : 'shield_hide',            str_contains($i, 'crossbow') || str_contains($i, 'arbal') => 'crossbow',            str_contains($i, 'plasma') => 'plasma_gun',            str_contains($i, 'pulse') || str_contains($i, 'rifle') || str_contains($i, 'fusil') => 'pulse_rifle',            str_contains($i, 'flare') || str_contains($i, 'fusee') || str_contains($i, 'pistol') => 'flare_gun',            str_contains($i, 'harpoon') || str_contains($i, 'harpon') => 'harpoon',            str_contains($i, 'raft_pole') || str_contains($i, 'pole') || str_contains($i, 'perche') => 'raft_pole',            str_contains($i, 'machete') => 'machete',            str_contains($i, 'spear') || str_contains($i, 'lance') => 'spear',            str_contains($i, 'knife') || str_contains($i, 'couteau') || str_contains($i, 'blade') => 'knife',            str_contains($i, 'axe') || str_contains($i, 'hache') => 'axe',            str_contains($i, 'torch') || str_contains($i, 'torche') => 'torch',            str_contains($i, 'club') || str_contains($i, 'massue') || str_contains($i, 'gourdin') => 'club',            str_contains($i, 'sling') || str_contains($i, 'fronde') => 'sling',            str_contains($i, 'bow') || str_contains($i, 'arc') => 'bow',            str_contains($i, 'staff') || str_contains($i, 'baton') || str_contains($i, 'bâton') => 'staff',            default => 'stone',        };    }    /**     * An item in its own frame: the grip at the origin, the item along +y (a spear points up, a gun's barrel up),     * its flat side facing +z. @return Mesh     */    public static function item(string $item): Mesh    {        $kind = self::itemKind($item);        $key = 'item:' . $kind;        if (isset(self::$cache[$key])) {            return self::$cache[$key];        }        $m = new Mesh();        $tip = null;        switch ($kind) {            case 'spear':            case 'harpoon':            case 'raft_pole':            case 'staff':                $len = ['spear' => 1.6, 'harpoon' => 1.7, 'raft_pole' => 2.8, 'staff' => 1.7][$kind];                $m->cell = Atlas::BARK;                $m->color = $kind === 'raft_pole' ? [0.9, 0.85, 0.7] : [0.85, 0.75, 0.6];                $m->tube([[0, -0.45, 0, 0.016], [0.004, $len * 0.4, 0, 0.015], [0, $len - 0.45, 0, 0.013]], 6, 0.6, true, true);                $top = $len - 0.45;                if ($kind === 'spear') {                    $m->cell = Atlas::STONE;                    $m->color = [0.4, 0.42, 0.48];                    $m->push()->translate(0, $top, 0)->scale(1.0, 1.0, 0.35);                    $m->tube([[0, 0, 0, 0.035], [0, 0.08, 0, 0.045], [0, 0.24, 0, 0.002]], 4, 0.3);                    $m->pop();                    $m->cell = Atlas::ROPE;                    $m->color = [1.0, 0.9, 0.75];                    $m->tube([[0, $top - 0.06, 0, 0.022], [0, $top + 0.02, 0, 0.022]], 6, 1.0);                } elseif ($kind === 'harpoon') {                    $m->cell = Atlas::BONE;                    $m->color = [1.0, 0.95, 0.85];                    $m->tube([[0, $top, 0, 0.02], [0, $top + 0.28, 0, 0.002]], 5, 0.5);                    foreach ([0.05, 0.13] as $o) {                        foreach ([-1, 1] as $sd) {                            $m->tube([[0, $top + $o + 0.04, 0, 0.008], [$sd * 0.045, $top + $o - 0.02, 0, 0.002]], 3, 1.0);                        }                    }                    $m->cell = Atlas::ROPE;                    $m->color = [1.0, 0.95, 0.8];                    for ($k = 0; $k < 3; $k++) {                        $m->tube([[0, $top - 0.1 - $k * 0.05, 0, 0.022], [0, $top - 0.08 - $k * 0.05, 0, 0.022]], 6, 1.0);                    }                } elseif ($kind === 'staff') {                    $m->cell = Atlas::BARK;                    $m->color = [0.7, 0.6, 0.48];                    $m->ellipsoid(0, $top + 0.04, 0, 0.035, 0.05, 0.035, 4, 6, 0.15, 3);                    $m->cell = Atlas::FEATHERS;                    $m->color = [0.9, 0.45, 0.2];                    $m->quad4([0.02, $top - 0.04, 0.0], [0.035, $top - 0.04, 0.0], [0.05, $top - 0.16, 0.01], [0.035, $top - 0.16, 0.01], [0, 0, 1]);                }                break;            case 'knife':            case 'machete':                $long = $kind === 'machete';                $m->cell = $long ? Atlas::PLANKS : Atlas::LEATHER;                $m->color = $long ? [0.6, 0.42, 0.3] : [0.7, 0.55, 0.4];                $m->tube([[0, -0.07, 0, 0.018], [0, 0.07, 0, 0.017]], 6, 0.2, true, true);                $m->cell = Atlas::METAL;                $m->color = [0.62, 0.63, 0.66];                $m->bone = 0.6;                $m->push()->translate(0, 0.07, 0)->scale(1.0, 1.0, 0.16);                if ($long) {                    $m->tube([[0, 0, 0, 0.03], [0.008, 0.25, 0, 0.042], [0.02, 0.42, 0, 0.05], [0.03, 0.5, 0, 0.03], [0.035, 0.53, 0, 0.002]], 4, 0.6);                } else {                    $m->tube([[0, 0, 0, 0.03], [0, 0.12, 0, 0.026], [0, 0.2, 0, 0.002]], 4, 0.25);                }                $m->pop();                $m->bone = 0.0;                break;            case 'axe':                $m->cell = Atlas::BARK;                $m->color = [0.8, 0.7, 0.55];                $m->tube([[0, -0.2, 0, 0.022], [0, 0.45, 0, 0.02]], 6, 0.5);                $m->cell = Atlas::STONE;                $m->color = [0.55, 0.55, 0.6];                $m->box(0.07, 0.4, 0, 0.16, 0.1, 0.04);                $m->cell = Atlas::ROPE;                $m->color = [1.0, 0.9, 0.75];                $m->tube([[0, 0.34, 0, 0.026], [0, 0.46, 0, 0.026]], 6, 1.0);                break;            case 'club':                $m->cell = Atlas::BARK;                $m->color = [0.78, 0.62, 0.46];                $m->tube([[0, -0.15, 0, 0.022], [0, 0.2, 0, 0.03], [0, 0.42, 0, 0.055], [0, 0.55, 0, 0.06]], 7, 0.5, true, true);                $m->ellipsoid(0, 0.55, 0, 0.065, 0.07, 0.065, 4, 7, 0.2, 7);                $m->cell = Atlas::STONE;                $m->color = [0.45, 0.45, 0.5];                for ($k = 0; $k < 6; $k++) {                    $a = $k * 1.05;                    $m->tube([[cos($a) * 0.05, 0.45 + ($k % 2) * 0.08, sin($a) * 0.05, 0.012], [cos($a) * 0.09, 0.47 + ($k % 2) * 0.08, sin($a) * 0.09, 0.002]], 3, 1.0);                }                $m->cell = Atlas::LEATHER;                $m->color = [0.8, 0.6, 0.4];                $m->tube([[0, -0.15, 0, 0.025], [0, 0.03, 0, 0.025]], 6, 1.0);                break;            case 'sling':                $m->cell = Atlas::ROPE;                $m->color = [1.0, 0.92, 0.78];                $m->tube([[0, 0.0, 0, 0.006], [0.03, -0.25, 0.01, 0.006], [0.05, -0.45, 0, 0.006]], 3, 1.0);                $m->tube([[0.01, 0.0, 0, 0.006], [0.1, -0.24, 0.0, 0.006], [0.1, -0.45, 0, 0.006]], 3, 1.0);                $m->cell = Atlas::LEATHER;                $m->color = [0.9, 0.7, 0.5];                $m->ellipsoid(0.075, -0.48, 0, 0.05, 0.035, 0.04, 3, 6);                $m->cell = Atlas::STONE;                $m->color = [0.6, 0.6, 0.62];                $m->ellipsoid(0.075, -0.47, 0.0, 0.03, 0.03, 0.03, 3, 5, 0.1, 2);                break;            case 'bow':                $m->cell = Atlas::BARK;                $m->color = [0.75, 0.6, 0.45];                $rings = [];                for ($k = 0; $k <= 8; $k++) {                    $t = $k / 8 * 2 - 1;                    $rings[] = [0.0, $t * 0.6, 0.12 * (1 - $t * $t), 0.016 - abs($t) * 0.006];                }                $m->tube($rings, 5, 0.5);                $m->cell = Atlas::PLAIN;                $m->color = [0.85, 0.82, 0.75];                $m->tube([[0, -0.6, 0.0, 0.003], [0, 0.6, 0.0, 0.003]], 3, 1.0);                break;            case 'crossbow':                $m->cell = Atlas::PLANKS;                $m->color = [0.75, 0.55, 0.38];                $m->box(0, 0.12, 0, 0.05, 0.62, 0.06);                $m->box(0, -0.12, -0.04, 0.045, 0.14, 0.1);                $m->cell = Atlas::BARK;                $m->color = [0.6, 0.48, 0.36];                $r = [];                for ($k = 0; $k <= 8; $k++) {                    $t = $k / 8 * 2 - 1;                    $r[] = [$t * 0.34, 0.4 - 0.07 * (1 - $t * $t), 0.0, 0.014];                }                $m->tube($r, 5, 0.5);                $m->cell = Atlas::PLAIN;                $m->color = [0.85, 0.82, 0.75];                $m->tube([[-0.34, 0.4, 0.0, 0.003], [0, 0.2, 0.03, 0.003], [0.34, 0.4, 0.0, 0.003]], 3, 1.0);                $m->color = [0.4, 0.35, 0.3];                $m->tube([[0, 0.2, 0.04, 0.007], [0, 0.48, 0.04, 0.007]], 4, 1.0);                $m->cell = Atlas::METAL;                $m->color = [0.6, 0.6, 0.62];                $m->tube([[0, 0.48, 0.04, 0.012], [0, 0.53, 0.04, 0.001]], 4, 1.0);                break;            case 'flare_gun':                $m->cell = Atlas::PLAIN;                $m->color = [1.15, 0.45, 0.12];                $m->box(0, -0.02, -0.02, 0.04, 0.12, 0.05);                $m->push()->rotate('x', 0.25);                $m->box(0, -0.06, -0.04, 0.035, 0.09, 0.045);                $m->pop();                $m->tube([[0, 0.02, 0.03, 0.03], [0, 0.22, 0.03, 0.03]], 10, 1.0, true, false);                $m->color = [0.1, 0.1, 0.1];                $m->tube([[0, 0.222, 0.03, 0.022], [0, 0.224, 0.03, 0.022]], 10, 1.0, true);                $m->color = [0.2, 0.2, 0.2];                $m->box(0, 0.0, 0.0, 0.01, 0.03, 0.03);                break;            case 'pulse_rifle':                $m->cell = Atlas::METAL;                $m->color = [0.42, 0.45, 0.5];                $m->bone = 0.7;                $m->box(0, 0.12, 0.03, 0.055, 0.42, 0.09);                $m->box(0, -0.12, 0.0, 0.05, 0.2, 0.07);                $m->box(0, -0.02, -0.04, 0.04, 0.08, 0.06);                $m->color = [0.25, 0.27, 0.3];                $m->tube([[0, 0.3, 0.05, 0.026], [0, 0.62, 0.05, 0.022]], 8, 0.5, true);                $m->box(0, 0.2, 0.1, 0.03, 0.18, 0.04);                $m->cell = Atlas::PLAIN;                $m->color = [0.4, 2.6, 2.8];                foreach ([0.36, 0.44, 0.52] as $yy) {                    $m->tube([[0, $yy, 0.05, 0.028], [0, $yy + 0.025, 0.05, 0.028]], 8, 1.0);                }                $m->box(0.03, 0.1, 0.03, 0.004, 0.2, 0.012);                $m->color = [0.1, 0.1, 0.12];                $m->tube([[0, 0.12, 0.1, 0.018], [0, 0.26, 0.1, 0.018]], 6, 1.0, true, true);                $m->bone = 0.0;                break;            case 'plasma_gun':                $m->cell = Atlas::METAL;                $m->color = [0.7, 0.72, 0.75];                $m->bone = 0.9;                $m->box(0, -0.04, -0.01, 0.04, 0.13, 0.05);                $m->ellipsoid(0, 0.1, 0.03, 0.045, 0.12, 0.05, 5, 8);                $m->cell = Atlas::PLAIN;                $m->color = [2.4, 0.8, 3.0];                $m->ellipsoid(0, 0.08, 0.03, 0.03, 0.05, 0.055, 4, 7);                for ($k = 0; $k < 4; $k++) {                    $m->tube([[0, 0.14 + $k * 0.03, 0.03, 0.05 - $k * 0.004], [0, 0.15 + $k * 0.03, 0.03, 0.05 - $k * 0.004]], 10, 1.0);                }                $m->color = [2.8, 2.2, 3.0];                $m->tube([[0, 0.24, 0.03, 0.018], [0, 0.29, 0.03, 0.012]], 8, 1.0, true);                $m->bone = 0.0;                break;            case 'shield_hide':            case 'shield_scrap':                // a round shield, its face toward +z, the grip (origin) behind its centre                $scrap = $kind === 'shield_scrap';                $rim = [];                for ($k = 0; $k <= 20; $k++) {                    $a = $k / 20 * 2 * M_PI;                    $rim[] = [cos($a) * 0.27, sin($a) * 0.29, 0.035, 0.016];                }                if ($scrap) {                    // a hide lining, then riveted plates of the colony's metal, a bent band round the rim                    $m->cell = Atlas::LEATHER;                    $m->color = [0.6, 0.45, 0.32];                    $m->push()->translate(0, 0, 0.03)->rotate('x', M_PI / 2);                    $m->ellipsoid(0, 0, 0, 0.265, 0.028, 0.285, 4, 16, 0.0, 1, 0.4);                    $m->pop();                    $m->bone = 0.45;                    foreach ([[-0.09, 0.1, 0.17, 0.2, 0.06, Atlas::METAL, [0.66, 0.68, 0.7]], [0.1, 0.12, 0.18, 0.22, -0.05, Atlas::RUST, [1.0, 0.92, 0.85]],                        [-0.07, -0.12, 0.2, 0.2, -0.04, Atlas::RUST, [0.95, 0.88, 0.8]], [0.1, -0.1, 0.16, 0.2, 0.08, Atlas::METAL, [0.58, 0.6, 0.63]],                        [0.0, 0.0, 0.14, 0.14, 0.0, Atlas::METAL, [0.75, 0.76, 0.78]]] as $q => [$px, $py, $w, $h, $rot, $cell, $col]) {                        $m->cell = $cell;                        $m->color = $col;                        $m->push()->translate($px, $py, 0.062 + $q * 0.002)->rotate('z', $rot)->rotate('x', $py * 0.4)->rotate('y', -$px * 0.5);                        $m->box(0, 0, 0, $w, $h, 0.008);                        $m->pop();                    }                    $m->cell = Atlas::METAL;                    $m->color = [0.4, 0.4, 0.42];                    for ($k = 0; $k < 14; $k++) {                        $a = $k / 14 * 2 * M_PI + 0.2;                        $m->ellipsoid(cos($a) * 0.2, sin($a) * 0.215, 0.072, 0.009, 0.009, 0.006, 2, 4);                    }                    $m->color = [0.5, 0.48, 0.46];                    $m->tube($rim, 6, 0.4);                    $m->bone = 0.0;                } else {                    // a hide stretched on a frame of sticks, laced to a bent rim, a painted band                    $m->cell = Atlas::LEATHER;                    $m->color = [0.95, 0.78, 0.58];                    $m->push()->translate(0, 0, 0.03)->rotate('x', M_PI / 2);                    $m->ellipsoid(0, 0, 0, 0.27, 0.04, 0.29, 4, 16, 0.04, 9, 0.35);                    $m->pop();                    $m->cell = Atlas::PLAIN;                    $m->color = [0.55, 0.16, 0.1];                    $m->push()->translate(0, 0, 0.068)->rotate('x', M_PI / 2);                    $m->ellipsoid(0, 0, 0, 0.21, 0.004, 0.035, 2, 10);                    $m->pop();                    $m->cell = Atlas::BARK;                    $m->color = [0.75, 0.62, 0.46];                    $m->tube($rim, 6, 0.4);                    $m->tube([[-0.25, 0.0, 0.0, 0.012], [0.25, 0.0, 0.0, 0.012]], 5, 0.5);                    $m->tube([[0.0, -0.27, 0.0, 0.012], [0.0, 0.27, 0.0, 0.012]], 5, 0.5);                    $m->cell = Atlas::ROPE;                    $m->color = [1.0, 0.92, 0.75];                    for ($k = 0; $k < 16; $k++) {                        $a = $k / 16 * 2 * M_PI;                        $m->tube([[cos($a) * 0.25, sin($a) * 0.27, 0.06, 0.005], [cos($a) * 0.29, sin($a) * 0.31, 0.03, 0.005], [cos($a) * 0.26, sin($a) * 0.28, 0.0, 0.005]], 3, 1.0);                    }                }                // the grip and the arm strap behind                $m->cell = Atlas::LEATHER;                $m->color = [0.45, 0.32, 0.22];                $m->tube([[-0.06, 0.0, -0.01, 0.014], [0.0, 0.0, -0.045, 0.014], [0.06, 0.0, -0.01, 0.014]], 5, 1.0);                $m->tube([[-0.03, -0.16, -0.005, 0.01], [0.0, -0.12, -0.05, 0.01], [0.03, -0.08, -0.005, 0.01]], 5, 1.0);                break;            case 'torch':                $m->cell = Atlas::BARK;                $m->color = [0.8, 0.7, 0.55];                $m->tube([[0, -0.25, 0, 0.016], [0, 0.3, 0, 0.019]], 6, 0.5);                $m->cell = Atlas::FABRIC;                $m->color = [0.25, 0.2, 0.15];                $m->tube([[0, 0.28, 0, 0.026], [0, 0.38, 0, 0.024]], 6, 1.0);                $m->color = [3.0, 1.4, 0.5];                $m->tube([[0, 0.375, 0, 0.0245], [0, 0.4, 0, 0.016]], 6, 1.0, true);                $tip = [0.0, 0.39, 0.0];                break;            default:                $m->cell = Atlas::STONE;                $m->color = [0.7, 0.7, 0.7];                $m->ellipsoid(0, 0.03, 0.05, 0.06, 0.05, 0.07, 4, 6, 0.2, 5);        }        $m->color = [1.0, 1.0, 1.0];        self::$tips[$key] = $tip;        return self::$cache[$key] = $m;    }    /** How a human holds an item at rest: [rotate x, y, z, offset along the item]. @return array{0: float, 1: float, 2: float, 3: float} */    public static function npcGrip(string $item): array    {        return match (self::itemKind($item)) {            'spear', 'harpoon', 'staff', 'raft_pole' => [0.05, 0.0, 0.0, -0.1],            'crossbow', 'pulse_rifle' => [1.45, 0.0, 0.0, 0.0],            'flare_gun', 'plasma_gun' => [1.2, 0.0, 0.0, 0.0],            'club', 'machete', 'knife', 'axe', 'torch' => [0.4, 0.0, 0.0, 0.0],            'bow' => [0.0, 1.57, 0.0, 0.0],            'sling' => [0.0, 0.0, 0.0, 0.0],            default => [0.0, 0.0, 0.0, 0.0],        };    }    /**     * The item held in first person (View::$held), in the hand's frame: x right, y up, z forward, origin at the     * grip. Unknown items: a stone. Returns the mesh and, for a torch, where its flame burns (else null).     * @return array{0: Mesh, 1: ?array{0: float, 1: float, 2: float}}     */    public static function held(string $item): array    {        $kind = self::itemKind($item);        $key = 'held:' . $kind;        $m = self::$cache[$key] ?? null;        if ($m !== null) {            return [$m, self::$tips[$key] ?? null];        }        $src = self::item($item);        $m = new Mesh();        // how each item points in first person: [x rotation, y rotation, z rotation, offset x, y, z]        [$rx, $ry, $rz, $ox, $oy, $oz] = match ($kind) {            'spear', 'harpoon' => [1.3, -0.12, 0.0, 0.0, 0.0, 0.0],            'raft_pole', 'staff' => [0.5, -0.1, 0.0, 0.0, -0.1, 0.0],            'knife', 'machete' => [1.1, 0.0, 0.0, 0.0, 0.0, 0.0],            'axe', 'club' => [0.6, 0.0, 0.0, 0.0, 0.0, 0.0],            'torch' => [0.35, 0.0, 0.15, 0.0, 0.0, 0.0],            'bow' => [0.0, 0.0, 0.25, 0.0, 0.0, 0.0],            'crossbow' => [1.52, 0.0, 0.0, -0.12, 0.02, -0.05],            'pulse_rifle' => [1.52, 0.0, 0.0, -0.1, 0.0, -0.08],            'flare_gun', 'plasma_gun' => [1.45, 0.0, 0.0, -0.04, 0.02, 0.0],            'sling' => [0.0, 0.0, 0.0, 0.0, 0.1, 0.0],            default => [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],        };        $m->push()->translate($ox, $oy, $oz)->rotate('y', $ry)->rotate('z', $rz)->rotate('x', $rx);        $m->append($src);        $tip = self::$tips['item:' . $kind] ?? null;        if ($tip !== null) {            $tip = $m->point($tip[0], $tip[1], $tip[2]);        }        $m->pop();        self::$cache[$key] = $m;        self::$tips[$key] = $tip;        return [$m, $tip];    }    private static function rnd(int $seed, int $k): float    {        return Mesh::hash($seed, $k);    }    // ---- trees ----------------------------------------------------------------------------------------    /** A cluster of leaf cards around (x, y, z): $cards crossed cards of $size, normals from the crown's centre. */    private static function cluster(Mesh $m, float $x, float $y, float $z, float $size, int $cards, array $centre, int $seed, float $droop = 0.0): void    {        for ($k = 0; $k < $cards; $k++) {            $yaw = self::rnd($seed, $k * 7) * M_PI + $k * M_PI / max(1, $cards);            $tilt = (self::rnd($seed, $k * 7 + 1) - 0.5) * 1.1 + $droop;            $s = $size * (0.8 + 0.4 * self::rnd($seed, $k * 7 + 2));            $m->card($x, $y, $z, $s, $s, $yaw, $tilt, $centre);        }    }    /**     * A palm: a slender ringed trunk curving away (a root flare at the foot), a crown of arching pinnate fronds     * folded along their rib, old brown fronds hanging under it, coconuts. $coast: taller, leaning further.     */    private static function palm(Mesh $m, int $seed, bool $coast): void    {        $h = $coast ? 9.0 + self::rnd($seed, 1) * 4.0 : 7.0 + self::rnd($seed, 1) * 3.0;        $lean = ($coast ? 2.2 : 0.6) + self::rnd($seed, 2) * ($coast ? 2.5 : 1.4);        $m->swayH = $h;        $m->swayK = 0.35;        $n = self::n(14, 9, 6);        $rings = [];        for ($k = 0; $k <= $n; $k++) {            $t = $k / $n;            $bulge = $k % 2 === 0 ? 0.012 : 0.0;            $flare = $t < 0.08 ? (0.08 - $t) * 2.6 : 0.0;            $rings[] = [$lean * $t * $t + ($coast ? sin($t * 3.0) * 0.25 : 0.0), $h * $t, 0.0, 0.2 - 0.06 * $t + $bulge + $flare];        }        $m->cell = Atlas::PALM;        $m->color = [1.15, 1.1, 1.02];        $m->tube($rings, self::n(9, 7, 5), 0.8, true);        $top = [$rings[$n][0], $h, 0.0];        $m->swayH = 0.0;        $m->sway = 0.35;        // the crown's base: old leaf sheaths, coconuts        $m->cell = Atlas::PALM;        $m->color = [0.75, 0.62, 0.45];        $m->ellipsoid($top[0], $h - 0.2, 0.0, 0.32, 0.45, 0.32, 4, 7, 0.1, $seed);        $m->cell = Atlas::PLAIN;        for ($k = 0; $k < 5; $k++) {            $a = $k * 1.3 + self::rnd($seed, 60 + $k);            $m->color = $k % 2 ? [0.45, 0.36, 0.2] : [0.38, 0.48, 0.2];            $m->ellipsoid($top[0] + cos($a) * 0.28, $h - 0.35 - 0.1 * ($k % 2), sin($a) * 0.28, 0.13, 0.15, 0.13, 4, 6);        }        // fronds: a rib arching out, two halves folded up along it (the leaflets of the texture)        $m->cell = Atlas::FROND;        $count = self::n(16, 11, 7);        $segs = self::n(7, 5, 3);        for ($f = 0; $f < $count; $f++) {            $old = $f >= $count - ($coast ? 4 : 3);            $yaw = $f / $count * 2 * M_PI * 2.39 + self::rnd($seed, 10 + $f) * 0.4;            $len = ($old ? 3.2 : 3.4 + self::rnd($seed, 30 + $f) * 1.6) * ($coast ? 1.15 : 1.0);            $rise = $old ? -0.8 : 0.4 + self::rnd($seed, 50 + $f) * 1.1;            $droop = $old ? 2.2 : 1.6 + self::rnd($seed, 70 + $f) * 1.4;            $cx = cos($yaw);            $cz = sin($yaw);            $fold = 0.55;            $m->color = $old ? [0.85, 0.62, 0.35] : [0.95 + 0.15 * self::rnd($seed, 90 + $f), 1.0, 0.9];            $prev = null;            for ($s = 0; $s <= $segs; $s++) {                $t = $s / $segs;                $d = $len * $t * ($old ? 0.75 : 1.0);                $y = $h + ($old ? $rise * $t * 1.5 - $t * $t * 1.2 : $rise * sin($t * M_PI * 0.7) - $t * $t * $droop);                $w = (0.9 * sin(M_PI * min(1.0, 0.1 + $t * 0.95)) + 0.08) * ($old ? 0.5 : 1.0);                $px = $top[0] + $cx * $d;                $pz = $cz * $d;                $m->sway = 0.35 + $t * 0.9;                $m->ao = 0.6 + 0.4 * $t;                $hw = $w * cos($fold);                $dy = $w * sin($fold) * 0.6;                $l = $m->vertexN($px - $cz * $hw, $y + $dy, $pz + $cx * $hw, -$cz * 0.4, 1.0, $cx * 0.4, 0.0, 1.0 - $t);                $c = $m->vertexN($px, $y, $pz, 0.0, 1.0, 0.0, 0.5, 1.0 - $t);                $r = $m->vertexN($px + $cz * $hw, $y + $dy, $pz - $cx * $hw, $cz * 0.4, 1.0, -$cx * 0.4, 1.0, 1.0 - $t);                if ($prev !== null) {                    $m->quad($prev[0], $prev[1], $c, $l);                    $m->quad($prev[1], $prev[2], $r, $c);                }                $prev = [$l, $c, $r];            }        }        $m->sway = 0.0;        $m->ao = 1.0;    }    /**     * A broadleaf tree: a tapering trunk on spreading roots, forking into branches, each ending in clusters of     * leaf cards; the crown's shape and density vary per seed.     */    private static function broad(Mesh $m, int $seed): void    {        $h = 5.0 + self::rnd($seed, 1) * 3.0;        $m->swayH = $h + 4;        $m->swayK = 0.25;        $m->cell = Atlas::BARK;        $m->color = [1.08, 1.0, 0.92];        $lean = (self::rnd($seed, 3) - 0.5) * 0.6;        $sides = self::n(10, 7, 5);        $m->tube([[0, -0.2, 0, 0.62], [0, 0.15, 0, 0.5], [0.0, 0.5, 0, 0.4], [$lean * 0.3, $h * 0.45, 0, 0.33], [$lean, $h, 0.08, 0.26]], $sides, 1.6);        // roots        $roots = self::n(5, 3, 0);        for ($r = 0; $r < $roots; $r++) {            $a = $r / max(1, $roots) * 2 * M_PI + self::rnd($seed, 40 + $r);            $l = 1.0 + self::rnd($seed, 45 + $r) * 0.8;            $m->tube([[cos($a) * 0.25, 0.45, sin($a) * 0.25, 0.2], [cos($a) * $l * 0.6, 0.12, sin($a) * $l * 0.6, 0.12], [cos($a) * $l, -0.1, sin($a) * $l, 0.04]], 5, 1.2);        }        $crown = [$lean, $h + 1.8, 0.1];        // branches, clusters at their tips        $nb = 5 + (int) (self::rnd($seed, 4) * 3);        $tips = [];        for ($b = 0; $b < $nb; $b++) {            $a = $b * 2.39 + self::rnd($seed, 5 + $b);            $l = 2.2 + self::rnd($seed, 9 + $b) * 1.8;            $y0 = $h * (0.55 + 0.4 * $b / $nb);            $up = 0.5 + self::rnd($seed, 13 + $b) * 0.5;            $p0 = [$lean * $y0 / $h, $y0, 0.05];            $p1 = [$p0[0] + cos($a) * $l * 0.5, $y0 + $l * $up * 0.6, sin($a) * $l * 0.5];            $p2 = [$p0[0] + cos($a) * $l, $y0 + $l * $up, sin($a) * $l];            $m->cell = Atlas::BARK;            $m->color = [1.08, 1.0, 0.92];            $m->tube([[$p0[0], $p0[1], $p0[2], 0.17], [$p1[0], $p1[1], $p1[2], 0.1], [$p2[0], $p2[1], $p2[2], 0.05]], self::n(6, 5, 4), 1.5);            $tips[] = $p2;            $tips[] = [($p1[0] + $p2[0]) / 2 + 0.3, $p1[1] + 0.4, ($p1[2] + $p2[2]) / 2 - 0.3];            if (self::$lod === 0) {                // a twig off the branch                $q = [$p1[0] + cos($a + 1.2) * 1.0, $p1[1] + 0.8, $p1[2] + sin($a + 1.2) * 1.0];                $m->tube([[$p1[0], $p1[1], $p1[2], 0.06], [$q[0], $q[1], $q[2], 0.025]], 4, 1.5);                $tips[] = $q;            }        }        $tips[] = [$crown[0], $crown[1] + 0.8, $crown[2]];        $m->cell = Atlas::LEAVES;        $per = self::n(4, 3, 2);        $size = self::n(18, 26, 34) / 10;        foreach ($tips as $i => [$x, $y, $z]) {            $m->ao = 0.75 + 0.25 * min(1.0, ($y - $h * 0.7) / 3);            $m->color = [0.92 + 0.2 * self::rnd($seed, 500 + $i), 1.0, 0.88 + 0.1 * self::rnd($seed, 520 + $i)];            $m->sway = 0.3;            self::cluster($m, $x, $y, $z, $size * (0.9 + 0.3 * self::rnd($seed, 540 + $i)), $per, $crown, $seed + $i * 13);            if (self::$lod === 0) {                // smaller satellite clusters fill the crown                $ox = (self::rnd($seed, 560 + $i) - 0.5) * 1.6;                $oz = (self::rnd($seed, 580 + $i) - 0.5) * 1.6;                self::cluster($m, $x + $ox, $y - 0.5, $z + $oz, $size * 0.8, 2, $crown, $seed + $i * 17);            }        }        // a few dark inner cards: depth inside the crown        $m->ao = 0.45;        $m->color = [0.7, 0.8, 0.65];        self::cluster($m, $crown[0], $crown[1] - 0.6, $crown[2], $size * 1.6, self::n(4, 3, 2), $crown, $seed + 999);        $m->sway = 0.0;        $m->ao = 1.0;        $m->color = [1.0, 1.0, 1.0];        $m->swayH = 0.0;    }    /** A conifer: a straight tapering trunk, whorls of drooping branch sprays, narrow (spruce) or broad (fir). */    private static function conifer(Mesh $m, int $seed): void    {        $h = 9.0 + self::rnd($seed, 1) * 5.0;        $width = 0.24 + self::rnd($seed, 2) * 0.12;        $m->swayH = $h;        $m->swayK = 0.25;        $m->cell = Atlas::BARK;        $m->color = [0.78, 0.66, 0.6];        $m->tube([[0, -0.2, 0, 0.42], [0, 0.4, 0, 0.32], [0, $h * 0.5, 0, 0.2], [0, $h, 0, 0.03]], self::n(8, 6, 5), 1.4);        // a dark inner cone fills the gaps between the branches        $m->cell = Atlas::NEEDLES;        $m->color = [0.5, 0.56, 0.5];        $m->ao = 0.45;        $m->tube([[0, $h * 0.18, 0, $h * $width * 0.55], [0, $h * 0.6, 0, $h * $width * 0.3], [0, $h * 0.98, 0, 0.05]], self::n(8, 6, 5), 2.5);        $m->color = [1.0, 1.0, 1.0];        $tiers = self::n(13, 9, 6);        $centre = [0.0, $h * 0.5, 0.0];        for ($t = 0; $t < $tiers; $t++) {            $f = $t / ($tiers - 1);            $y = $h * (0.16 + 0.8 * $f);            $rad = $h * $width * (1.0 - 0.85 * $f) + 0.35;            $n = self::n(8, 6, 5) - (int) ($f * 3);            for ($b = 0; $b < $n; $b++) {                $a = $b / $n * 2 * M_PI + $t * 0.7 + self::rnd($seed, $t * 10 + $b) * 0.5;                $m->sway = 0.25 + 0.3 * $f;                $m->ao = 0.55 + 0.45 * $f;                $m->color = [0.9 + 0.15 * self::rnd($seed, 300 + $t * 10 + $b), 1.0, 0.95];                $cx = cos($a) * $rad * 0.5;                $cz = sin($a) * $rad * 0.5;                // a spray from the trunk outward, drooping at its end                $m->card($cx, $y - $rad * 0.2, $cz, $rad * 0.8, $rad * 1.15, -$a + M_PI / 2, 1.3 + 0.15 * (1 - $f), $centre, -0.25 * $rad);            }        }        $m->sway = 0.0;        $m->ao = 1.0;        $m->swayH = 0.0;    }    /** A dead tree (grey, gnarled) or a burnt one (charred black, broken, embers in its cracks). */    private static function deadTree(Mesh $m, int $seed, bool $burnt): void    {        $h = 4.0 + self::rnd($seed, 1) * 3.5;        $m->cell = $burnt ? Atlas::CHAR : Atlas::BARK;        $m->color = $burnt ? [0.9, 0.85, 0.82] : [1.2, 1.15, 1.1];        $tw = (self::rnd($seed, 2) - 0.5) * 0.8;        $m->tube([[0, -0.2, 0, 0.42], [0, 0.3, 0, 0.34], [$tw * 0.3, $h * 0.4, 0.1, 0.26], [$tw, $h * 0.75, -0.05, 0.17], [$tw * 1.2, $h, 0.1, $burnt ? 0.12 : 0.06]], self::n(7, 6, 4), 1.5, $burnt);        $nb = $burnt ? 3 : 6;        for ($b = 0; $b < $nb; $b++) {            $a = $b * 1.9 + self::rnd($seed, 3 + $b) * 2;            $y0 = $h * (0.35 + 0.1 * $b);            $l = ($burnt ? 0.8 : 1.4) + self::rnd($seed, 9 + $b) * 1.6;            $x0 = $tw * $y0 / $h;            $p1 = [$x0 + cos($a) * $l * 0.55, $y0 + $l * 0.35, sin($a) * $l * 0.55];            $p2 = [$x0 + cos($a + 0.5) * $l, $y0 + $l * 0.9, sin($a + 0.5) * $l];            $m->tube([[$x0, $y0, 0, 0.12], [$p1[0], $p1[1], $p1[2], 0.07], [$p2[0], $p2[1], $p2[2], 0.02]], 4, 1.5);            if (!$burnt && self::$lod === 0) {                $q = [$p1[0] + cos($a - 0.9) * 0.7, $p1[1] + 0.6, $p1[2] + sin($a - 0.9) * 0.7];                $m->tube([[$p1[0], $p1[1], $p1[2], 0.04], [$q[0], $q[1], $q[2], 0.012]], 3, 1.5);            }        }        if ($burnt && $seed % 3 === 0) {            // embers still glowing in the cracks            $m->cell = Atlas::PLAIN;            $m->color = [2.8, 0.9, 0.25];            for ($k = 0; $k < 5; $k++) {                $y = 0.3 + self::rnd($seed, 70 + $k) * $h * 0.6;                $a = self::rnd($seed, 80 + $k) * 6.28;                $r = 0.36 - $y / $h * 0.18;                $m->ellipsoid(cos($a) * $r + $tw * $y / $h * 0.4, $y, sin($a) * $r, 0.03, 0.12, 0.03, 2, 4);            }        }        $m->color = [1.0, 1.0, 1.0];    }    /**     * A giant of the jungle: 30 m, buttress roots like walls, a pale mossy trunk, an umbrella crown far above, lianas     * hanging to the ground, ferns and broad leaves growing on its branches.     */    private static function giantTree(Mesh $m, int $seed): void    {        $h = 26.0 + self::rnd($seed, 1) * 10.0;        $m->swayH = $h + 10;        $m->swayK = 0.12;        $m->cell = Atlas::BARK_JUNGLE;        $m->color = [0.72, 0.7, 0.62];        $sides = self::n(14, 10, 7);        $m->tube([[0, -0.3, 0, 1.6], [0, 2.0, 0, 1.25], [0.1, $h * 0.35, 0, 1.0], [0.3, $h * 0.7, 0.1, 0.8], [0.4, $h, 0.0, 0.6]], $sides, 3.0, false, false, 1.0);        // buttress roots: tall thin fins running out from the trunk        $fins = self::n(6, 5, 4);        for ($f = 0; $f < $fins; $f++) {            $a = $f / $fins * 2 * M_PI + self::rnd($seed, 20 + $f) * 0.5;            $out = 4.0 + self::rnd($seed, 25 + $f) * 2.5;            $tall = 3.5 + self::rnd($seed, 30 + $f) * 2.0;            $secs = [];            $steps = self::n(6, 4, 3);            for ($k = 0; $k <= $steps; $k++) {                $t = $k / $steps;                $r = 1.0 + $t * $out;                $hh = $tall * (1 - $t) ** 1.6 + 0.15;                $secs[] = [cos($a) * $r, $hh * 0.5 - 0.25, sin($a) * $r, 0.16 * (1 - 0.6 * $t) + 0.05, $hh * 0.5 + 0.1, 0];            }            $m->sweep($secs, 6);        }        // moss on the trunk's lower half        if (self::$lod < 2) {            $m->cell = Atlas::MOSS;            $m->color = [0.9, 1.0, 0.85];            for ($k = 0; $k < 6; $k++) {                $a = self::rnd($seed, 40 + $k) * 6.28;                $y = 2.0 + self::rnd($seed, 45 + $k) * $h * 0.4;                $m->push()->translate(cos($a) * 1.08, $y, sin($a) * 1.08)->rotate('y', -$a);                $m->ellipsoid(0, 0, 0, 0.12, 1.2, 0.5, 3, 6, 0.1, $k);                $m->pop();            }        }        // the crown: big limbs spreading out, clusters on them        $crown = [0.4, $h + 3.0, 0.0];        $limbs = self::n(7, 6, 5);        $tips = [];        $m->cell = Atlas::BARK_JUNGLE;        for ($b = 0; $b < $limbs; $b++) {            $a = $b * 2.39 + self::rnd($seed, 50 + $b);            $l = 7.0 + self::rnd($seed, 55 + $b) * 5.0;            $y0 = $h * (0.78 + 0.2 * $b / $limbs);            $p0 = [0.35, $y0, 0.0];            $p1 = [cos($a) * $l * 0.5, $y0 + 2.5, sin($a) * $l * 0.5];            $p2 = [cos($a) * $l, $y0 + 3.5 + self::rnd($seed, 60 + $b) * 2, sin($a) * $l];            $m->color = [0.72, 0.7, 0.62];            $m->tube([[$p0[0], $p0[1], $p0[2], 0.55], [$p1[0], $p1[1], $p1[2], 0.32], [$p2[0], $p2[1], $p2[2], 0.12]], self::n(7, 5, 4), 3.0);            $tips[] = $p2;            $tips[] = $p1;            $tips[] = [($p1[0] + $p2[0]) / 2 + cos($a + 1.5) * 2.5, $p2[1] + 0.5, ($p1[2] + $p2[2]) / 2 + sin($a + 1.5) * 2.5];            if (self::$lod < 2) {                // lianas hanging from the limb                $m->cell = Atlas::LIANA;                $m->sway = 0.4;                foreach ([0.45, 0.8] as $q) {                    $lx = $p0[0] + ($p2[0] - $p0[0]) * $q;                    $ly = $p0[1] + ($p2[1] - $p0[1]) * $q;                    $lz = $p0[2] + ($p2[2] - $p0[2]) * $q;                    $drop = 6.0 + self::rnd($seed, 70 + $b * 3 + (int) ($q * 10)) * ($ly - 4.0);                    $segN = (int) max(2, min(5, $drop / 4));                    for ($k = 0; $k < $segN; $k++) {                        $y1 = $ly - $k * $drop / $segN;                        $m->card($lx, $y1 - $drop / $segN / 2, $lz, 1.2, $drop / $segN, $a, 0.0, [$lx, $y1, $lz]);                        $m->card($lx, $y1 - $drop / $segN / 2, $lz, 1.2, $drop / $segN, $a + 1.57, 0.0, [$lx, $y1, $lz]);                    }                }                // an epiphyte on the limb                $m->cell = Atlas::BIG_LEAVES;                $m->sway = 0.2;                $m->card($p1[0], $p1[1] + 0.8, $p1[2], 1.8, 1.6, $a, 0.2, $p1);                $m->card($p1[0], $p1[1] + 0.8, $p1[2], 1.8, 1.6, $a + 1.57, -0.2, $p1);                $m->sway = 0.0;                $m->cell = Atlas::BARK_JUNGLE;            }        }        $m->cell = Atlas::LEAVES;        $per = self::n(4, 3, 2);        $size = self::n(42, 56, 70) / 10;        foreach ($tips as $i => [$x, $y, $z]) {            $m->ao = 0.7 + 0.3 * self::rnd($seed, 200 + $i);            $m->color = [0.82 + 0.16 * self::rnd($seed, 220 + $i), 0.95, 0.8];            $m->sway = 0.25;            self::cluster($m, $x, $y + 0.8, $z, $size, $per, $crown, $seed + $i * 19, -0.2);            if (self::$lod === 0) {                self::cluster($m, $x + 1.5, $y + 1.6, $z - 1.2, $size * 0.8, 2, $crown, $seed + $i * 23);            }        }        $m->ao = 0.4;        $m->color = [0.6, 0.75, 0.55];        self::cluster($m, $crown[0], $crown[1] - 0.5, $crown[2], $size * 2.2, self::n(4, 3, 2), $crown, $seed + 777);        $m->sway = 0.0;        $m->ao = 1.0;        $m->color = [1.0, 1.0, 1.0];        $m->swayH = 0.0;    }    /** A mangrove: a short trunk standing on arching stilt roots, a dense dark crown of small glossy leaves. */    private static function mangrove(Mesh $m, int $seed): void    {        $h = 5.0 + self::rnd($seed, 1) * 2.5;        $m->swayH = $h + 2;        $m->swayK = 0.2;        $m->cell = Atlas::BARK;        $m->color = [0.85, 0.78, 0.68];        $m->tube([[0, 1.6, 0, 0.26], [0.1, $h * 0.6, 0, 0.2], [0.2, $h, 0.1, 0.1]], self::n(7, 5, 4), 1.4);        $roots = self::n(11, 7, 5);        for ($r = 0; $r < $roots; $r++) {            $a = $r / $roots * 2 * M_PI + self::rnd($seed, 10 + $r) * 0.5;            $y0 = 1.2 + self::rnd($seed, 20 + $r) * 1.3;            $d = 1.4 + self::rnd($seed, 30 + $r) * 1.4;            $rings = [];            for ($k = 0; $k <= 5; $k++) {                $t = $k / 5;                $rings[] = [cos($a) * (0.15 + $d * $t), $y0 * (1 - $t * $t) + 0.4 * sin($t * M_PI) - 0.3 * $t, sin($a) * (0.15 + $d * $t), 0.09 - 0.04 * $t];            }            $m->tube($rings, 4, 1.2);        }        for ($b = 0; $b < 4; $b++) {            $a = $b * 1.7 + self::rnd($seed, 40 + $b);            $m->tube([[0.12, $h * 0.65, 0.05, 0.1], [cos($a) * 1.8, $h + 0.3, sin($a) * 1.8, 0.04]], 4, 1.4);        }        $m->cell = Atlas::LEAVES_SMALL;        $crown = [0.2, $h + 0.6, 0.1];        $nc = self::n(14, 9, 6);        $size = self::n(20, 26, 32) / 10;        for ($c = 0; $c < $nc; $c++) {            $th = self::rnd($seed, 100 + $c) * 2 * M_PI;            $ph = acos(1 - 2 * self::rnd($seed, 120 + $c) * 0.8);            $x = $crown[0] + sin($ph) * cos($th) * 2.6;            $y = $crown[1] + cos($ph) * 1.3;            $z = $crown[2] + sin($ph) * sin($th) * 2.6;            $m->sway = 0.25;            $m->ao = 0.55 + 0.45 * (cos($ph) * 0.5 + 0.5);            $m->color = [0.85 + 0.15 * self::rnd($seed, 140 + $c), 0.95, 0.85];            self::cluster($m, $x, $y, $z, $size, 3, $crown, $seed + $c * 11);        }        $m->sway = 0.0;        $m->ao = 1.0;        $m->swayH = 0.0;    }    private static function bush(Mesh $m, int $seed, int $cell, array $tint): void    {        $m->cell = $cell;        $centre = [0.0, 0.35, 0.0];        $n = self::n(14, 9, 6);        for ($c = 0; $c < $n; $c++) {            $a = $c / $n * 2 * M_PI + self::rnd($seed, $c);            $r = 0.2 + 0.4 * self::rnd($seed, 20 + $c);            $s = 0.8 + 0.5 * self::rnd($seed, 40 + $c);            $m->color = [$tint[0] * (0.85 + 0.3 * self::rnd($seed, 60 + $c)), $tint[1], $tint[2]];            $m->sway = 0.18;            $m->ao = 0.6 + 0.4 * self::rnd($seed, 80 + $c);            $m->card(cos($a) * $r, $s * 0.38, sin($a) * $r, $s, $s * 0.9, $a + M_PI / 2, -0.35, $centre);            $m->card(cos($a) * $r * 0.5, 0.28 + $s * 0.3, sin($a) * $r * 0.5, $s * 0.8, $s * 0.7, $a, 0.4, $centre);        }        $m->sway = 0.0;        $m->ao = 1.0;        $m->color = [1.0, 1.0, 1.0];    }    private static function mushroom(Mesh $m, int $seed): void    {        for ($k = 0; $k < 3; $k++) {            $a = $k * 2.2 + self::rnd($seed, $k);            $x = cos($a) * 0.12 * $k;            $z = sin($a) * 0.12 * $k;            $s = 1.0 - $k * 0.25;            $m->cell = Atlas::PLAIN;            $m->color = [0.92, 0.88, 0.78];            $m->tube([[$x, 0, $z, 0.035 * $s], [$x, 0.16 * $s, $z, 0.03 * $s]], 6, 1.0);            $m->color = [0.75, 0.32, 0.18];            $m->ellipsoid($x, 0.16 * $s, $z, 0.11 * $s, 0.07 * $s, 0.11 * $s, 4, 8, 0.0, 1, 0.15);            $m->color = [1.1, 1.05, 0.95];            for ($d = 0; $d < 4; $d++) {                $b = $d * 1.6 + $k;                $m->ellipsoid($x + cos($b) * 0.05 * $s, 0.215 * $s, $z + sin($b) * 0.05 * $s, 0.014 * $s, 0.008 * $s, 0.014 * $s, 2, 4);            }        }        $m->color = [1.0, 1.0, 1.0];    }    private static function herb(Mesh $m, int $seed, array $leaf, array $flower): void    {        $m->cell = Atlas::FERN;        $m->color = $leaf;        $centre = [0.0, 0.0, 0.0];        for ($k = 0; $k < 4; $k++) {            $m->sway = 0.3;            $m->card(0, 0.2, 0, 0.55, 0.5, $k * M_PI / 4 + self::rnd($seed, 30 + $k), ($k % 2 - 0.5) * 0.6, $centre);        }        $m->cell = Atlas::PLAIN;        $m->color = $flower;        $m->sway = 0.3;        for ($k = 0; $k < 5; $k++) {            $a = $k * 1.3 + self::rnd($seed, $k);            $m->ellipsoid(cos($a) * 0.1, 0.38 + 0.05 * self::rnd($seed, 9 + $k), sin($a) * 0.1, 0.035, 0.03, 0.035, 3, 5);        }        $m->sway = 0.0;        $m->color = [1.0, 1.0, 1.0];    }    /** Reeds (or tall reeds of the marsh): crossed cards of blades and seed heads, swaying. */    private static function reeds(Mesh $m, int $seed, float $h, int $cards): void    {        $m->cell = Atlas::REEDS;        $centre = [0.0, 0.0, 0.0];        $n = self::n($cards, (int) ceil($cards * 0.6), 3);        for ($k = 0; $k < $n; $k++) {            $a = $k * 2.39 + self::rnd($seed, $k);            $r = 0.15 + 0.35 * self::rnd($seed, 20 + $k);            $hh = $h * (0.75 + 0.35 * self::rnd($seed, 40 + $k));            $m->color = [0.68 + 0.18 * self::rnd($seed, 60 + $k), 0.72, 0.55 + 0.15 * self::rnd($seed, 80 + $k)];            $m->swayH = $hh;            $m->swayK = 0.6;            $m->sway = 0.0;            $m->ao = 0.8;            $m->card(cos($a) * $r, $hh / 2, sin($a) * $r, $hh * 0.55, $hh, $a, 0.05 * ($k % 3 - 1), $centre);        }        $m->swayH = 0.0;        $m->ao = 1.0;        $m->color = [1.0, 1.0, 1.0];    }    private static function vine(Mesh $m, int $seed): void    {        $m->cell = Atlas::LIANA;        $centre = [0.0, 1.5, 0.0];        for ($k = 0; $k < 4; $k++) {            $a = $k * 1.6 + self::rnd($seed, $k);            for ($s = 0; $s < 3; $s++) {                $m->sway = 0.2 + $s * 0.15;                $m->color = [0.9, 1.05, 0.85];                $m->card(cos($a) * 0.3, 3.0 - $s * 1.0 - 1.0, sin($a) * 0.3, 0.7, 1.0, $a, 0.0, $centre);            }        }        $m->sway = 0.0;        $m->color = [1.0, 1.0, 1.0];    }    /** Basalt columns: a cluster of hexagonal prisms of different heights, cracked tops. */    private static function basalt(Mesh $m, int $seed): void    {        $m->cell = Atlas::BASALT;        $n = self::n(14, 9, 6);        for ($k = 0; $k < $n; $k++) {            $a = $k * 2.39;            $r = sqrt($k) * 0.62;            $x = cos($a) * $r;            $z = sin($a) * $r;            $hh = (3.2 - $r * 0.9) * (0.5 + 0.7 * self::rnd($seed, $k)) + 0.4;            $m->color = [0.95 + 0.2 * self::rnd($seed, 30 + $k), 0.95 + 0.2 * self::rnd($seed, 30 + $k), 1.0 + 0.2 * self::rnd($seed, 30 + $k)];            $m->bone = 0.25;            $m->push()->translate($x, 0, $z)->rotate('y', 0.3 + self::rnd($seed, 50 + $k) * 0.3)->rotate('z', (self::rnd($seed, 60 + $k) - 0.5) * 0.08);            $m->tube([[0, -0.4, 0, 0.36], [0, $hh, 0, 0.34]], 6, 1.6, true, false);            $m->pop();        }        $m->color = [1.0, 1.0, 1.0];    }    // ---- rocks, small things --------------------------------------------------------------------------    private static function rock(Mesh $m, int $seed, float $size): void    {        $m->cell = Atlas::STONE;        $m->color = [0.95, 0.93, 0.9];        $sx = $size * (0.8 + 0.4 * self::rnd($seed, 1));        $sz = $size * (0.7 + 0.4 * self::rnd($seed, 2));        $sy = $size * (0.45 + 0.3 * self::rnd($seed, 3));        $m->push()->rotate('y', self::rnd($seed, 4) * 3)->rotate('z', (self::rnd($seed, 5) - 0.5) * 0.4);        $m->ellipsoid(0, $sy * 0.3, 0, $sx, $sy, $sz, 8, 11, 0.3, $seed, 0.35);        $m->ellipsoid($sx * 0.35, $sy * 0.75, -$sz * 0.2, $sx * 0.55, $sy * 0.5, $sz * 0.6, 5, 7, 0.3, $seed + 3, 0.5);        if ($size > 1.5) {            $m->ellipsoid($sx * 0.75, $sy * 0.15, $sz * 0.45, $sx * 0.45, $sy * 0.5, $sz * 0.4, 5, 7, 0.3, $seed + 7, 0.4);            $m->ellipsoid(-$sx * 0.8, $sy * 0.05, -$sz * 0.3, $sx * 0.35, $sy * 0.35, $sz * 0.35, 4, 6, 0.3, $seed + 9, 0.4);        }        $m->pop();    }    private static function pebble(Mesh $m, int $seed, float $size, array $tint): void    {        $m->cell = Atlas::STONE;        $m->color = $tint;        $m->ellipsoid(0, $size * 0.3, 0, $size, $size * 0.55, $size * 0.8, 4, 6, 0.25, $seed, 0.5);        $m->color = [1.0, 1.0, 1.0];    }    private static function woodPile(Mesh $m, int $seed): void    {        $m->cell = Atlas::BARK;        $m->color = [0.9, 0.85, 0.8];        foreach ([[-0.15, 0.08, 0.0], [0.15, 0.08, 0.05], [0.0, 0.22, 0.02]] as $k => [$x, $y, $a]) {            $m->tube([[$x, $y, -0.5, 0.08], [$x, $y, 0.5, 0.08]], 6, 1.2, true, true);        }        $m->color = [1.0, 1.0, 1.0];    }    private static function stick(Mesh $m, int $seed): void    {        $m->cell = Atlas::BARK;        $m->tube([[-0.6, 0.03, 0, 0.025], [0, 0.04, 0.05, 0.022], [0.6, 0.03, 0, 0.018]], 4, 1.0, true, true);    }    private static function spring(Mesh $m, int $seed): void    {        $m->cell = Atlas::STONE;        $m->color = [0.85, 0.85, 0.85];        for ($k = 0; $k < 9; $k++) {            $a = $k / 9 * 2 * M_PI;            $m->ellipsoid(cos($a) * 0.75, 0.05, sin($a) * 0.75, 0.25, 0.18, 0.2, 4, 6, 0.2, $seed + $k, 0.4);        }        $m->cell = Atlas::PLAIN;        $m->color = [0.12, 0.3, 0.42];        $c = $m->vertex(0, 0.06, 0, 0, 1, 0);        $prev = null;        $first = null;        for ($k = 0; $k <= 12; $k++) {            $a = $k / 12 * 2 * M_PI;            $v = $m->vertex(cos($a) * 0.7, 0.06, sin($a) * 0.7, 0, 1, 0);            if ($prev !== null) {                $m->tri($c, $v, $prev);            }            $prev = $v;        }        $m->color = [1.0, 1.0, 1.0];    }    // ---- built things -------------------------------------------------------------------------------    private static function pod(Mesh $m, int $seed): void    {        // a bell-shaped survey capsule on its side, half dug in, its heat shield scorched, the hatch open,        // and its parachute dragged behind it on the ground        $m->push()->translate(0, 1.05, 0)->rotate('x', -0.12)->rotate('z', 0.35);        $m->cell = Atlas::METAL;        $m->color = [1.0, 1.0, 0.98];        $prof = [[-1.45, 1.42], [1.0, 0.72], [1.35, 0.5], [1.5, 0.28]];        $rings = [];        foreach ($prof as [$z, $r]) {            $rings[] = [0.0, 0.0, $z, $r];        }        $m->tube($rings, 14, 1.2, true, false, 3.0);        // soot streaks from the shield forward        $m->color = [0.3, 0.27, 0.25];        $m->tube([[0.0, 0.0, -1.5, 1.5], [0.0, 0.0, -1.0, 1.36], [0.0, 0.0, -0.75, 1.27]], 14, 1.2, false, false, 3.0);        // the heat shield: a dark dome        $m->cell = Atlas::STONE;        $m->color = [0.32, 0.24, 0.2];        $m->push()->translate(0, 0, -1.48)->rotate('x', M_PI / 2);        $m->ellipsoid(0, 0, 0, 1.46, 0.28, 1.46, 4, 14, 0.04, $seed, 0.05);        $m->pop();        // windows        $m->cell = Atlas::PLAIN;        $m->color = [0.1, 0.16, 0.22];        foreach ([-0.5, 0.5] as $a) {            $m->push()->rotate('z', $a)->translate(0, 0.98, 0.55)->rotate('x', -0.31);            $m->box(0, 0, 0, 0.32, 0.03, 0.42);            $m->pop();        }        // the hatch: a dark frame and the door hanging open        $m->color = [0.04, 0.04, 0.05];        $m->push()->rotate('z', M_PI * 0.55)->translate(0, 1.18, -0.2)->rotate('x', -0.3);        $m->box(0, 0, 0, 0.75, 0.04, 0.9);        $m->pop();        $m->cell = Atlas::METAL;        $m->color = [0.85, 0.85, 0.83];        $m->push()->rotate('z', M_PI * 0.55)->translate(0.55, 1.35, -0.2)->rotate('z', -1.1);        $m->box(0, 0, 0, 0.8, 0.07, 0.92);        $m->pop();        // the nose: a docking ring and a broken antenna        $m->color = [0.55, 0.55, 0.55];        $m->tube([[0.0, 0.0, 1.48, 0.3], [0.0, 0.0, 1.62, 0.3]], 10, 1.0, true);        $m->tube([[0.15, 0.1, 1.6, 0.025], [0.5, 0.3, 2.3, 0.02]], 4, 1.0);        $m->pop();        // the parachute: orange and white gores crumpled on the ground behind        $m->cell = Atlas::FABRIC;        $cols = [[1.15, 0.5, 0.2], [1.1, 1.08, 1.02]];        $nx = 8;        $nz = 6;        $verts = [];        for ($j = 0; $j <= $nz; $j++) {            for ($i = 0; $i <= $nx; $i++) {                $x = ($i / $nx - 0.5) * 5.0 * (0.7 + 0.3 * $j / $nz);                $z = -3.0 - $j * 0.95;                $y = 0.05 + max(0.0, sin($i * 1.7 + $j * 0.9) * 0.35 + (self::rnd($seed, $i * 13 + $j) - 0.5) * 0.3) + ($j === 0 ? 0.3 : 0.0);                $m->color = $cols[intdiv($i, 2) % 2];                $verts[$j][$i] = $m->vertex($x, $y, $z, 0, 1, 0, $i / $nx, $j / $nz);            }        }        for ($j = 0; $j < $nz; $j++) {            for ($i = 0; $i < $nx; $i++) {                $m->quad($verts[$j][$i], $verts[$j][$i + 1], $verts[$j + 1][$i + 1], $verts[$j + 1][$i]);            }        }        // its lines to the capsule        $m->cell = Atlas::PLAIN;        $m->color = [0.85, 0.85, 0.8];        for ($k = 0; $k <= 4; $k++) {            $x = ($k / 4 - 0.5) * 3.4;            $m->tube([[$x, 0.35, -3.0, 0.012], [$x * 0.2, 0.9, -1.5, 0.012]], 3, 2.0);        }        // debris        $m->cell = Atlas::METAL;        for ($k = 0; $k < 6; $k++) {            $a = self::rnd($seed, $k) * 2 * M_PI;            $d = 3.0 + self::rnd($seed, 10 + $k) * 4;            $m->color = $k % 2 ? [0.3, 0.28, 0.26] : [0.85, 0.85, 0.82];            $m->push()->translate(cos($a) * $d, 0.05, sin($a) * $d)->rotate('y', $a)->rotate('z', self::rnd($seed, 20 + $k) * 0.6);            $m->box(0, 0, 0, 0.5 + self::rnd($seed, 30 + $k) * 0.6, 0.04, 0.3 + self::rnd($seed, 40 + $k) * 0.4);            $m->pop();        }        $m->color = [1.0, 1.0, 1.0];    }    private static function colony(Mesh $m, int $seed): void    {        // a ruined habitat module: a half cylinder, broken open, rusted, with a fallen mast        $m->cell = Atlas::METAL;        $m->color = [0.8, 0.62, 0.48];        $m->push()->rotate('x', M_PI / 2)->translate(0, 0, -1.2);        // a tube along z is made with rings along z        $m->pop();        $rings = [];        for ($k = 0; $k <= 4; $k++) {            $rings[] = [0.0, 0.2, -4.0 + $k * 2.0, 2.3];        }        $m->push()->translate(0, 0, 0)->scale(1.0, 0.9, 1.0);        $m->tube($rings, 10, 2.0, false, false, 2.0);        $m->pop();        // end walls        $m->color = [0.6, 0.5, 0.42];        $m->box(0, 1.0, -4.0, 4.4, 2.6, 0.12, 2.0);        $m->push()->translate(0.8, 0.6, 4.0)->rotate('z', 0.3);        $m->box(0, 0, 0, 2.6, 1.8, 0.12, 2.0);        $m->pop();        // the floor deck        $m->cell = Atlas::PLANKS;        $m->color = [0.5, 0.45, 0.4];        $m->box(0, 0.05, 0, 4.2, 0.1, 8.0, 2.0);        // fallen antenna mast        $m->cell = Atlas::METAL;        $m->color = [0.55, 0.45, 0.4];        $m->tube([[3.0, 0.15, -2.0, 0.12], [7.5, 0.4, 1.5, 0.08]], 6, 2.0, true, true);        // panels and a crate        for ($k = 0; $k < 5; $k++) {            $a = self::rnd($seed, $k) * 2 * M_PI;            $d = 4.5 + self::rnd($seed, 9 + $k) * 3;            $m->color = [0.7, 0.55, 0.45];            $m->push()->translate(cos($a) * $d, 0.05, sin($a) * $d)->rotate('y', $a)->rotate('x', 0.2);            $m->box(0, 0, 0, 1.5, 0.06, 1.0);            $m->pop();        }        $m->cell = Atlas::PLANKS;        $m->color = [0.8, 0.75, 0.7];        $m->box(-3.2, 0.4, 2.0, 0.8, 0.8, 0.8);        $m->color = [1.0, 1.0, 1.0];    }    private static function crate(Mesh $m, int $seed): void    {        $m->cell = Atlas::PLANKS;        $m->color = [0.95, 0.9, 0.8];        $m->box(0, 0.35, 0, 0.75, 0.7, 0.75);        $m->cell = Atlas::METAL;        $m->color = [0.4, 0.4, 0.4];        $m->box(0, 0.35, 0.38, 0.77, 0.08, 0.02);        $m->box(0, 0.35, -0.38, 0.77, 0.08, 0.02);        $m->color = [1.0, 1.0, 1.0];    }    private static function hut(Mesh $m, int $seed): void    {        // round wall of planks, conical thatched roof, a dark door        $r = 2.4;        $m->cell = Atlas::PLANKS;        $m->color = [0.85, 0.75, 0.65];        $m->tube([[0, 0, 0, $r], [0, 1.9, 0, $r]], 12, 1.9, false, false, 4.0);        $m->cell = Atlas::THATCH;        $m->color = [1.0, 0.95, 0.85];        $m->tube([[0, 1.75, 0, $r + 0.55], [0, 2.8, 0, 1.4], [0, 3.9, 0, 0.12]], 12, 2.6, true, false, 4.0);        // under the eaves        $m->color = [0.45, 0.4, 0.35];        $m->tube([[0, 1.75, 0, $r + 0.5], [0, 1.95, 0, $r]], 12, 1.0, false, false, 3.0);        // door        $m->cell = Atlas::PLAIN;        $m->color = [0.07, 0.05, 0.04];        $m->quad4([-0.5, 0.0, $r + 0.03], [0.5, 0.0, $r + 0.03], [0.5, 1.6, $r + 0.03], [-0.5, 1.6, $r + 0.03], [0, 0, 1]);        // posts        $m->cell = Atlas::BARK;        $m->color = [0.8, 0.7, 0.6];        foreach ([-0.65, 0.65] as $x) {            $m->tube([[$x, 0, $r + 0.08, 0.08], [$x, 1.8, $r + 0.08, 0.07]], 5, 1.0);        }        $m->color = [1.0, 1.0, 1.0];    }    private static function tent(Mesh $m, int $seed): void    {        $m->cell = Atlas::FABRIC;        $m->color = [0.75 + 0.2 * self::rnd($seed, 1), 0.62, 0.48];        $w = 1.4;        $h = 1.7;        $l = 2.4;        foreach ([1, -1] as $side) {            $n = [$side * $h, $w, 0.0];            // both faces of each slope (seen from inside too)            $a = [$side * $w, 0, -$l / 2];            $b = [$side * $w, 0, $l / 2];            $c = [0, $h, $l / 2];            $d = [0, $h, -$l / 2];            if ($side > 0) {                $m->quad4($a, $d, $c, $b, $n);                $m->quad4($a, $b, $c, $d, [-$n[0], -$n[1], 0]);            } else {                $m->quad4($a, $b, $c, $d, $n);                $m->quad4($a, $d, $c, $b, [-$n[0], -$n[1], 0]);            }        }        // back wall and a dark entrance        $i0 = $m->vertex(-$w, 0, -$l / 2, 0, 0, -1, 0, 1);        $i1 = $m->vertex($w, 0, -$l / 2, 0, 0, -1, 1, 1);        $i2 = $m->vertex(0, $h, -$l / 2, 0, 0, -1, 0.5, 0);        $m->tri($i0, $i1, $i2);        $m->cell = Atlas::PLAIN;        $m->color = [0.06, 0.05, 0.04];        $i0 = $m->vertex(-$w * 0.7, 0, $l / 2 - 0.05, 0, 0, 1);        $i1 = $m->vertex($w * 0.7, 0, $l / 2 - 0.05, 0, 0, 1);        $i2 = $m->vertex(0, $h * 0.75, $l / 2 - 0.05, 0, 0, 1);        $m->tri($i0, $i1, $i2);        $m->cell = Atlas::BARK;        $m->color = [0.8, 0.7, 0.6];        $m->tube([[0, 0, $l / 2 + 0.1, 0.04], [0, $h + 0.2, $l / 2 + 0.1, 0.04]], 4, 1.0);        $m->tube([[0, 0, -$l / 2 - 0.1, 0.04], [0, $h + 0.2, -$l / 2 - 0.1, 0.04]], 4, 1.0);        $m->color = [1.0, 1.0, 1.0];    }    private static function campfire(Mesh $m, int $seed): void    {        $m->cell = Atlas::STONE;        $m->color = [0.7, 0.68, 0.66];        for ($k = 0; $k < 9; $k++) {            $a = $k / 9 * 2 * M_PI;            $m->ellipsoid(cos($a) * 0.55, 0.06, sin($a) * 0.55, 0.16, 0.12, 0.13, 4, 5, 0.25, $seed + $k, 0.4);        }        // ash        $m->cell = Atlas::PLAIN;        $m->color = [0.16, 0.15, 0.14];        $c = $m->vertex(0, 0.03, 0, 0, 1, 0);        $prev = null;        for ($k = 0; $k <= 10; $k++) {            $a = $k / 10 * 2 * M_PI;            $v = $m->vertex(cos($a) * 0.45, 0.02, sin($a) * 0.45, 0, 1, 0);            if ($prev !== null) {                $m->tri($c, $v, $prev);            }            $prev = $v;        }        // charred logs, glowing ends        $m->cell = Atlas::BARK;        for ($k = 0; $k < 4; $k++) {            $a = $k / 4 * 2 * M_PI + 0.3;            $m->color = [0.35, 0.3, 0.27];            $m->tube([[cos($a) * 0.5, 0.06, sin($a) * 0.5, 0.06], [cos($a) * 0.08, 0.28, sin($a) * 0.08, 0.05]], 5, 1.0, false, true);            $m->color = [3.0, 1.2, 0.4];            $m->tube([[cos($a) * 0.12, 0.26, sin($a) * 0.12, 0.052], [cos($a) * 0.06, 0.29, sin($a) * 0.06, 0.04]], 5, 1.0, true);        }        $m->color = [1.0, 1.0, 1.0];    }    private static function mast(Mesh $m): void    {        // a lattice tower, 24 m, a platform, a dish and a whip antenna        $m->cell = Atlas::METAL;        $m->color = [0.62, 0.5, 0.42];        $h = 24.0;        $seg = 9;        $wb = 2.2;        $wt = 0.6;        $corner = static function (int $c, float $y) use ($h, $wb, $wt): array {            $w = $wb + ($wt - $wb) * $y / $h;            return [($c & 1 ? 1 : -1) * $w, $y, ($c & 2 ? 1 : -1) * $w];        };        $bar = static function (Mesh $m, array $a, array $b, float $r): void {            $m->tube([[$a[0], $a[1], $a[2], $r], [$b[0], $b[1], $b[2], $r]], 4, 3.0);        };        for ($c = 0; $c < 4; $c++) {            $bar($m, $corner($c, 0), $corner($c, $h), 0.07);        }        $order = [0, 1, 3, 2, 0];        for ($s = 0; $s < $seg; $s++) {            $y0 = $h * $s / $seg;            $y1 = $h * ($s + 1) / $seg;            for ($e = 0; $e < 4; $e++) {                $a = $order[$e];                $b = $order[$e + 1];                $bar($m, $corner($a, $y0), $corner($b, $y1), 0.035);                $bar($m, $corner($a, $y1), $corner($b, $y1), 0.035);            }        }        $m->color = [0.5, 0.45, 0.4];        $m->box(0, $h, 0, 2.0, 0.12, 2.0);        // dish        $m->color = [0.85, 0.85, 0.82];        $m->push()->translate(0.6, $h + 1.2, 0)->rotate('z', -1.1);        $m->tube([[0, 0, 0, 0.05], [0, 0.25, 0, 0.6], [0, 0.4, 0, 0.9]], 10, 1.0);        $m->pop();        $m->color = [0.55, 0.5, 0.45];        $m->tube([[-0.4, $h, -0.4, 0.05], [-0.4, $h + 5.0, -0.4, 0.02]], 4, 3.0);        // a red light at the top (dead, faint)        $m->color = [2.6, 0.4, 0.3];        $m->ellipsoid(-0.4, $h + 5.05, -0.4, 0.08, 0.08, 0.08, 3, 5);        // a ladder up one leg        $m->cell = Atlas::METAL;        $m->color = [0.5, 0.42, 0.36];        $m->color = [1.0, 1.0, 1.0];    }    private static function caveEntrance(Mesh $m, int $seed): void    {        // a mound of rocks around a dark mouth facing +z        $m->cell = Atlas::STONE;        $m->color = [0.75, 0.72, 0.7];        $m->ellipsoid(0, 0.8, -1.5, 5.0, 3.2, 3.5, 7, 12, 0.15, $seed, 0.5);        for ($k = 0; $k < 7; $k++) {            $a = M_PI * (0.05 + 0.9 * $k / 6);            $m->ellipsoid(cos($a) * 2.3, sin($a) * 2.6, 1.4, 0.9 + self::rnd($seed, $k) * 0.5, 0.8, 0.9, 5, 7, 0.25, $seed + 30 + $k, 0.6);        }        // the mouth: a dark half disc that swallows the light        $m->cell = Atlas::PLAIN;        $m->color = [0.0, 0.0, 0.0];        $c = $m->vertex(0, 0.0, 1.55, 0, 0, 1);        $prev = null;        for ($k = 0; $k <= 10; $k++) {            $a = M_PI * $k / 10;            $v = $m->vertex(cos($a) * 1.9, sin($a) * 2.2, 1.55, 0, 0, 1);            if ($prev !== null) {                $m->tri($c, $prev, $v);            }            $prev = $v;        }        $m->color = [1.0, 1.0, 1.0];    }    private static function ladder(Mesh $m): void    {        $m->cell = Atlas::PLANKS;        $m->color = [0.85, 0.75, 0.6];        $h = 4.5;        foreach ([-0.25, 0.25] as $x) {            $m->box($x, $h / 2, 0, 0.07, $h, 0.07, 2.0);        }        for ($k = 1; $k < 14; $k++) {            $m->box(0, $k * 0.32, 0, 0.5, 0.04, 0.05);        }        $m->color = [1.0, 1.0, 1.0];    }    private static function bones(Mesh $m, int $seed): void    {        $m->cell = Atlas::BONE;        $m->color = [1.0, 1.0, 1.0];        // a spine and ribs        $m->tube([[0, 0.12, -0.7, 0.05], [0, 0.18, 0.0, 0.06], [0, 0.12, 0.6, 0.05]], 5, 1.0);        for ($k = 0; $k < 6; $k++) {            $z = -0.4 + $k * 0.16;            foreach ([-1, 1] as $s) {                $m->tube([[0, 0.18, $z, 0.025], [$s * 0.3, 0.3, $z, 0.022], [$s * 0.42, 0.05, $z + 0.05, 0.018]], 4, 1.0);            }        }        $m->ellipsoid(0.1, 0.12, 0.85, 0.15, 0.12, 0.2, 4, 6, 0.08, $seed);    }    private static function carcass(Mesh $m, int $seed): void    {        $m->cell = Atlas::SKIN;        $m->color = [0.55, 0.28, 0.22];        $m->ellipsoid(0, 0.3, 0, 0.45, 0.32, 0.9, 5, 8, 0.1, $seed, 0.5);        $m->color = [0.75, 0.15, 0.12];        $m->ellipsoid(0.2, 0.42, 0.1, 0.25, 0.15, 0.5, 4, 6, 0.1, $seed + 1);        $m->cell = Atlas::BONE;        $m->color = [1.0, 0.95, 0.9];        for ($k = 0; $k < 4; $k++) {            $z = -0.3 + $k * 0.2;            $m->tube([[0.1, 0.5, $z, 0.025], [0.4, 0.55, $z, 0.02], [0.5, 0.3, $z, 0.018]], 4, 1.0);        }        $m->color = [1.0, 1.0, 1.0];    }    private static function skinRack(Mesh $m): void    {        $m->cell = Atlas::BARK;        $m->color = [0.8, 0.72, 0.6];        foreach ([-0.9, 0.9] as $x) {            $m->tube([[$x, 0, 0, 0.05], [$x, 2.0, 0, 0.045]], 5, 1.0);        }        $m->tube([[-1.0, 1.9, 0, 0.04], [1.0, 1.9, 0, 0.04]], 5, 1.0);        $m->tube([[-1.0, 0.4, 0, 0.04], [1.0, 0.4, 0, 0.04]], 5, 1.0);        $m->cell = Atlas::FUR;        $m->color = [0.62, 0.45, 0.3];        $m->quad4([-0.8, 0.45, 0.02], [0.8, 0.45, 0.02], [0.75, 1.85, 0.02], [-0.75, 1.85, 0.02], [0, 0, 1]);        $m->quad4([0.8, 0.45, -0.02], [-0.8, 0.45, -0.02], [-0.75, 1.85, -0.02], [0.75, 1.85, -0.02], [0, 0, -1]);        $m->color = [1.0, 1.0, 1.0];    }    private static function generic(Mesh $m): void    {        $m->cell = Atlas::STONE;        $m->color = [0.7, 0.7, 0.75];        $m->box(0, 0.25, 0, 0.5, 0.5, 0.5);        $m->color = [1.0, 1.0, 1.0];    }}