|
| 1 | +import type { NodeDefinition } from '@pascal-app/core' |
| 2 | +import { buildTreeFloorplan, treeTrunkRadius } from './floorplan' |
| 3 | +import { TreeNode } from './schema' |
| 4 | + |
| 5 | +type TreeDefinition = NodeDefinition<typeof TreeNode> & Record<string, unknown> |
| 6 | + |
| 7 | +const treeFloorPlacement = { |
| 8 | + footprint: (node: unknown) => { |
| 9 | + const tree = node as TreeNode |
| 10 | + const radius = treeTrunkRadius(tree) |
| 11 | + return { |
| 12 | + dimensions: [radius * 2, tree.height, radius * 2] as [number, number, number], |
| 13 | + rotation: tree.rotation, |
| 14 | + } |
| 15 | + }, |
| 16 | + collides: false, |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * The tree's render half: everything a read-only viewer needs to load and draw |
| 21 | + * `trees:tree` without `@pascal-app/editor`. Rendering uses the instanced path |
| 22 | + * rather than the per-node `def.geometry`: a collective `def.system` batches |
| 23 | + * every tree into `InstancedMesh`es (forest-scale draw calls), while a |
| 24 | + * featherweight `def.renderer` mounts an invisible per-node proxy so the host's |
| 25 | + * selection / outline / zone machinery works unchanged. Editing behavior lives |
| 26 | + * in `definition.edit.ts`; `definition.ts` composes both. |
| 27 | + */ |
| 28 | +export const treeRenderDefinition: TreeDefinition = { |
| 29 | + kind: 'trees:tree', |
| 30 | + // Static in the bake for portability; our viewer removes the baked meshes and |
| 31 | + // re-renders live (wind, LODs) via this def's own path. See plans → Part D. |
| 32 | + bake: 'replace', |
| 33 | + schemaVersion: 1, |
| 34 | + schema: TreeNode, |
| 35 | + category: 'furnish', |
| 36 | + |
| 37 | + defaults: () => ({ |
| 38 | + object: 'node', |
| 39 | + parentId: null, |
| 40 | + visible: true, |
| 41 | + metadata: {}, |
| 42 | + position: [0, 0, 0], |
| 43 | + rotation: [0, 0, 0], |
| 44 | + preset: 'oak', |
| 45 | + size: 'medium', |
| 46 | + treeType: 'deciduous', |
| 47 | + height: 7, |
| 48 | + seed: 1, |
| 49 | + foliageDensity: 1, |
| 50 | + trunkThickness: 1, |
| 51 | + leafless: false, |
| 52 | + leafColor: '#ffffff', |
| 53 | + branchColor: '#ffffff', |
| 54 | + }), |
| 55 | + |
| 56 | + capabilities: { |
| 57 | + movable: { axes: ['x', 'z'], gridSnap: true }, |
| 58 | + rotatable: { |
| 59 | + axes: ['y'], |
| 60 | + snapAngles: Array.from({ length: 8 }, (_, i) => (i * Math.PI) / 4), |
| 61 | + }, |
| 62 | + selectable: { hitVolume: 'bbox' }, |
| 63 | + duplicable: true, |
| 64 | + deletable: true, |
| 65 | + groupable: true, |
| 66 | + snappable: {}, |
| 67 | + // The auto-measured drag box would wrap the whole canopy (the proxy shows |
| 68 | + // the real geometry while selected) — declare trunk-sized bounds instead. |
| 69 | + dragBounds: (node) => { |
| 70 | + const tree = node as unknown as TreeNode |
| 71 | + const radius = treeTrunkRadius(tree) |
| 72 | + return { size: [radius * 2, tree.height ?? 7, radius * 2] } |
| 73 | + }, |
| 74 | + // Defined outside the contextually typed object so the optional `collides` |
| 75 | + // hint remains compatible with Pascal hosts released before that field. |
| 76 | + floorPlaced: treeFloorPlacement, |
| 77 | + }, |
| 78 | + |
| 79 | + // 2D plan symbol: dashed canopy ring + trunk dot (see floorplan.ts). |
| 80 | + floorplan: buildTreeFloorplan, |
| 81 | + |
| 82 | + // Instanced rendering: an invisible per-node proxy for selection/outline... |
| 83 | + renderer: { kind: 'parametric', module: () => import('./proxy-renderer') }, |
| 84 | + // ...and a collective system that batches every tree into InstancedMeshes. |
| 85 | + system: { module: () => import('./system'), priority: 3 }, |
| 86 | + // Baked `/viewer` re-render for `bake: 'replace'` — collective, instanced. |
| 87 | + bakeReplaceRenderer: { module: () => import('./static-renderer') }, |
| 88 | + |
| 89 | + presentation: { |
| 90 | + label: 'Tree', |
| 91 | + description: 'A procedural ez-tree. Oak, pine, aspen, ash, bush, or trellis.', |
| 92 | + icon: { kind: 'iconify', name: 'lucide:trees' }, |
| 93 | + paletteSection: 'furnish', |
| 94 | + hidden: true, |
| 95 | + }, |
| 96 | + |
| 97 | + mcp: { |
| 98 | + description: |
| 99 | + 'A procedural ez-tree (example plugin node). Species presets (oak/pine/aspen/ash/bush/trellis) × size, deciduous/evergreen type, adjustable height, foliage/trunk, leaf & branch tint, and a seed for variation.', |
| 100 | + }, |
| 101 | +} |
0 commit comments