refactor: move world concepts into engine

This commit is contained in:
Toad 2026-08-24 15:17:53 +02:00
parent eeedcb8e48
commit 2d15c7ab8d
52 changed files with 3298 additions and 1558 deletions

View file

@ -1,20 +1,63 @@
import type { Mesh } from "../../../engine/scene/Mesh"
import type { Tree, TreeSpecies } from "../Tree"
import type { Material } from "../../../engine/render/Material"
import type { Prefab } from "../../../engine/scene/Prefab"
import type { Tree } from "../Tree"
import { cone, lerp, limb, rng } from "./treekit"
// Spruce: tall thin trunk under stacked cones that narrow to a point (tiered, taller
// than wide). Trunk = brown bark, foliage = spruce needle.
export const spruce: TreeSpecies = { kind: "spruce", trunk: "bark", foliage: "needle", build }
export type Spruce = Prefab<Tree>
function build(tree: Tree, trunk: Mesh, needles: Mesh, lod: "full" | "impostor"): void {
export namespace Spruce {
export function create(trunk: Material, foliage: Material): Spruce {
return {
position: (tree) => tree.position,
bakeNear: (tree, batch) =>
build(tree, batch.mesh(trunk), batch.mesh(foliage), "full"),
bakeFar: (tree, batch) =>
build(tree, batch.mesh(trunk), batch.mesh(foliage), "impostor"),
collider: treeCollider,
}
}
function treeCollider(tree: Tree) {
if (tree.growth <= 0.35) {
return null
}
const radius = tree.growth * 0.2 + 0.15
return {
shape: "box" as const,
minX: tree.position.x - radius,
maxX: tree.position.x + radius,
minZ: tree.position.z - radius,
maxZ: tree.position.z + radius,
top: tree.position.y + 3,
standable: false,
}
}
}
function build(
tree: Tree,
trunk: Mesh,
needles: Mesh,
lod: "full" | "impostor",
): void {
const base = tree.position
const g = tree.growth
const rand = rng(tree.seed)
const h = lerp(0.6, 9, g)
const rTrunk = lerp(0.03, 0.2, g)
const impostor = lod === "impostor"
limb(trunk, base, { x: base.x, y: base.y + h, z: base.z }, rTrunk, rTrunk * 0.25, impostor ? 3 : 5)
limb(
trunk,
base,
{ x: base.x, y: base.y + h, z: base.z },
rTrunk,
rTrunk * 0.25,
impostor ? 3 : 5,
)
// Stacked cones: widest low, shrinking to a point up top -> conical tiers. The
// impostor keeps the first two tiers at low sides (same seed => aligned).