Skip to content

Commit cbaf450

Browse files
wass08claude
andauthored
Add a render-only ./render entry for read-only viewers (#4)
Split each kind (trees:tree, trees:flower, trees:grass) into a render half (schema, defaults, capabilities, renderer, system, bake replacement renderer, read-only plan) and an edit half (snapProfile, parametrics, handles, preview, tool, toolHints). treesPlugin composes both, so editing hosts are unchanged; treesRenderPlugin from @pascal-app/plugin-trees/render loads without @pascal-app/editor. check:render bundles ./render with Bun.build and fails on any @pascal-app/editor or next import, with a negative probe on the root entry. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 56c847f commit cbaf450

17 files changed

Lines changed: 559 additions & 322 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ jobs:
1616
- run: bun install --frozen-lockfile
1717
- run: bun run check-types
1818
- run: bun test
19+
- run: bun run check:render

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ part of the v1 core plugin manifest. The panel declares `pluginId` and
9292
`defaultInstalled: true`, so Nature is listed in the Plugins sidebar and is
9393
enabled for new and legacy scenes unless a project explicitly uninstalls it.
9494

95+
## Read-only viewers
96+
97+
A viewer that only displays scenes can load the render-only entry instead. It
98+
registers the same plugin id, kinds, schemas, defaults and bake policy, with the
99+
renderers, systems, baked-scene renderers and 2D plan symbols, but no placement
100+
tools, inspector, gizmos or Nature panel, and nothing from `@pascal-app/editor`:
101+
102+
```ts
103+
import { treesRenderPlugin } from '@pascal-app/plugin-trees/render'
104+
// viewer host:
105+
setPluginDiscovery(async () => [treesRenderPlugin])
106+
```
107+
108+
Each kind's definition is split into a render half (`*definition.render.ts`)
109+
and an edit half (`*definition.edit.ts`); `treesPlugin` composes both, so
110+
editing hosts are unchanged. `bun run check:render` bundles `./render` and fails
111+
if anything in it reaches `@pascal-app/editor` or `next`.
112+
95113
## Notes / known gaps
96114

97115
- `package.json` points `main`/`exports` at raw TypeScript (`./src/index.ts`).

‎package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
".": {
1818
"types": "./src/index.ts",
1919
"default": "./src/index.ts"
20+
},
21+
"./render": {
22+
"types": "./src/render.ts",
23+
"default": "./src/render.ts"
2024
}
2125
},
2226
"files": [
@@ -25,7 +29,8 @@
2529
],
2630
"scripts": {
2731
"check-types": "tsc --noEmit",
28-
"test": "bun test"
32+
"test": "bun test",
33+
"check:render": "bun scripts/check-render-entry.ts"
2934
},
3035
"dependencies": {
3136
"@dgreenheck/ez-tree": "^1.1.0"

‎scripts/check-render-entry.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Render-entry boundary gate: bundles `./render` (every static and lazy import,
3+
* peer dependencies included) and fails if anything in the graph resolves to
4+
* `@pascal-app/editor` or `next`. A negative probe bundles the root entry and
5+
* expects the gate to catch its editor imports, so a silently blind check fails.
6+
*/
7+
import { fileURLToPath } from 'node:url'
8+
import type { BunPlugin } from 'bun'
9+
10+
const FORBIDDEN = /^(@pascal-app\/editor|next)(\/|$)/
11+
const root = fileURLToPath(new URL('..', import.meta.url))
12+
const pkg = await Bun.file(`${root}package.json`).json()
13+
14+
type Hit = { specifier: string; importer: string }
15+
16+
async function forbiddenImports(entry: string): Promise<Hit[]> {
17+
const hits: Hit[] = []
18+
const gate: BunPlugin = {
19+
name: 'render-entry-gate',
20+
setup(build) {
21+
build.onResolve({ filter: FORBIDDEN }, (args) => {
22+
hits.push({ specifier: args.path, importer: args.importer.replace(root, '') })
23+
return { path: args.path, external: true }
24+
})
25+
},
26+
}
27+
const result = await Bun.build({
28+
entrypoints: [`${root}${entry.replace(/^\.\//, '')}`],
29+
target: 'browser',
30+
format: 'esm',
31+
splitting: true,
32+
loader: { '.webp': 'file' },
33+
plugins: [gate],
34+
throw: false,
35+
})
36+
if (!result.success) {
37+
for (const log of result.logs) console.error(log)
38+
throw new Error(`bundling ${entry} failed`)
39+
}
40+
return hits
41+
}
42+
43+
const renderEntry = pkg.exports['./render'].default as string
44+
const rootEntry = pkg.exports['.'].default as string
45+
46+
const renderHits = await forbiddenImports(renderEntry)
47+
if (renderHits.length > 0) {
48+
console.error(`./render (${renderEntry}) reaches editor-only modules:`)
49+
for (const hit of renderHits) console.error(` ${hit.importer} -> ${hit.specifier}`)
50+
process.exit(1)
51+
}
52+
53+
const probeHits = await forbiddenImports(rootEntry)
54+
if (!probeHits.some((hit) => hit.specifier.startsWith('@pascal-app/editor'))) {
55+
console.error(`negative probe: the root entry (${rootEntry}) should reach @pascal-app/editor`)
56+
process.exit(1)
57+
}
58+
59+
console.log(
60+
`./render is editor-free (negative probe caught ${probeHits.length} editor import(s) from the root entry).`,
61+
)

‎src/definition.edit.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { HandleDescriptor } from '@pascal-app/core'
2+
import type { PlantEditContribution } from './edit-contribution'
3+
import { treeTrunkRadius } from './floorplan'
4+
import { treeParametrics } from './parametrics'
5+
import type { TreeNode } from './schema'
6+
7+
const ROTATE_RING_OFFSET = 0.35
8+
/** Ring hugs the ground like the item gizmo — high enough to clear the grass,
9+
* low enough to read as a floor affordance. */
10+
const ROTATE_RING_Y = 0.25
11+
12+
/** Whole-tree Y-rotation gizmo (same rig as shelf/item): a ring around the
13+
* trunk near the ground — not the canopy, which would put the handle meters
14+
* from the trunk on a large oak. */
15+
function treeRotateHandle(): HandleDescriptor<TreeNode> {
16+
const ringRadius = (n: TreeNode) => treeTrunkRadius(n) + ROTATE_RING_OFFSET
17+
const ringY = () => ROTATE_RING_Y
18+
return {
19+
kind: 'arc-resize',
20+
axis: 'angular',
21+
shape: 'rotate',
22+
apply: (initial, delta) => {
23+
const r = initial.rotation ?? [0, 0, 0]
24+
// Negate to match three.js Y-rotation handedness (same as shelf).
25+
return { rotation: [r[0], (r[1] ?? 0) - delta, r[2]] as [number, number, number] }
26+
},
27+
placement: {
28+
position: (n) => {
29+
const r = ringRadius(n)
30+
return [r * Math.SQRT1_2, ringY(), r * Math.SQRT1_2]
31+
},
32+
rotationY: () => -Math.PI / 4,
33+
},
34+
decoration: {
35+
kind: 'ring',
36+
radius: ringRadius,
37+
y: ringY,
38+
},
39+
}
40+
}
41+
42+
/** The tree's edit half: inspector, gizmo and placement (`tool`/`preview`). */
43+
export const treeEditContribution: PlantEditContribution<typeof TreeNode> = {
44+
snapProfile: 'item',
45+
parametrics: treeParametrics,
46+
handles: [treeRotateHandle()],
47+
preview: () => import('./preview'),
48+
tool: () => import('./tool'),
49+
toolHints: [
50+
{ key: 'Left click', label: 'Plant tree' },
51+
{ key: 'Esc', label: 'Stop' },
52+
],
53+
}

‎src/definition.render.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)