@particle-academy/fancy-3d 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,168 +1,168 @@
1
- # Primitives
2
-
3
- 3D shape factories that take a `SurfaceContent` (color, paint callback, or any fancy-3d `WidgetSpec`) and return a Babylon `Mesh`. Same painters power both Canvas Studio in DOM mode and 3D billboards — drop a fancy widget onto any 3D shape with one call.
4
-
5
- ## Import
6
-
7
- ```ts
8
- import {
9
- createPanel,
10
- createBillboard,
11
- createBuilding,
12
- createPillar,
13
- createCylinder,
14
- createCurvedPanel,
15
- createWidgetTexture,
16
- type SurfaceContent,
17
- type PaintFn,
18
- type BoxFace,
19
- } from "@particle-academy/fancy-3d/babylon";
20
- ```
21
-
22
- **Peer dependency:** `@babylonjs/core` (optional).
23
-
24
- ## SurfaceContent
25
-
26
- ```ts
27
- type SurfaceContent =
28
- | { type: "color"; color: string }
29
- | { type: "paint"; paint: PaintFn; pixelWidth?: number; pixelHeight?: number; transparent?: boolean }
30
- | { type: "widget"; widget: WidgetSpec; pixelWidth?: number; pixelHeight?: number; selected?: boolean };
31
-
32
- type PaintFn = (ctx: CanvasRenderingContext2D, width: number, height: number) => void;
33
- ```
34
-
35
- | Type | Use |
36
- |------|-----|
37
- | `color` | Solid CSS-color material — fastest, no texture |
38
- | `paint` | Freeform 2D drawing onto a `DynamicTexture`. Set `transparent: true` if you want non-painted regions to show through |
39
- | `widget` | Render any fancy-3d `WidgetSpec` with the same painters used in 3D scenes |
40
-
41
- For `paint` and `widget`, `pixelWidth`/`pixelHeight` are optional — every primitive auto-derives the texture size from its world dimensions (default density: 120 px per world unit, clamped to 1024 max axis). Override only when you need finer control.
42
-
43
- ## createPanel
44
-
45
- A flat single-faced plane.
46
-
47
- ```ts
48
- const panel = createPanel({
49
- scene,
50
- width: 2, // world units
51
- height: 1.2,
52
- surface: { type: "widget", widget: kpiWidget },
53
- });
54
- panel.position.set(0, 1, 0);
55
- ```
56
-
57
- | Option | Type | Description |
58
- |--------|------|-------------|
59
- | scene | `Scene` | Babylon scene to add the mesh to |
60
- | name | `string` | Optional mesh name (default `"panel"`) |
61
- | width / height | `number` | World-unit dimensions |
62
- | surface | `SurfaceContent` | What to render on the panel |
63
-
64
- ## createBillboard
65
-
66
- Same as `createPanel` plus the option to always face the camera.
67
-
68
- ```ts
69
- const ad = createBillboard({
70
- scene,
71
- width: 6, height: 4,
72
- surface: { type: "widget", widget: demoTile },
73
- faceCamera: true, // mesh.billboardMode = ALL
74
- });
75
- ```
76
-
77
- | Option | Type | Description |
78
- |--------|------|-------------|
79
- | ...PanelOpts | | All `createPanel` options |
80
- | faceCamera | `boolean` | When true, the mesh's billboardMode is set so it always faces the camera |
81
-
82
- ## createBuilding
83
-
84
- A box. Pass either `surface` (one texture wrapped on every face) or `faces` (per-face surfaces, composited into a UV atlas).
85
-
86
- ```ts
87
- // Cheap path — one texture all around
88
- const tower = createBuilding({
89
- scene,
90
- width: 4, height: 8, depth: 4,
91
- surface: { type: "color", color: "#1e293b" },
92
- });
93
-
94
- // Per-face path — windows on every wall, AC unit on the roof
95
- const office = createBuilding({
96
- scene,
97
- width: 5, height: 6, depth: 3,
98
- faces: {
99
- front: { type: "paint", paint: paintWindowGrid },
100
- back: { type: "paint", paint: paintWindowGrid },
101
- left: { type: "paint", paint: paintWindowGrid },
102
- right: { type: "paint", paint: paintWindowGrid },
103
- top: { type: "paint", paint: paintRoof },
104
- },
105
- });
106
- ```
107
-
108
- | Option | Type | Description |
109
- |--------|------|-------------|
110
- | scene | `Scene` | Babylon scene |
111
- | width / height / depth | `number` | World-unit dimensions |
112
- | surface | `SurfaceContent?` | Fallback surface used by any face not in `faces` |
113
- | faces | `Partial<Record<BoxFace, SurfaceContent>>` | Per-face overrides. Faces are `'front' \| 'back' \| 'right' \| 'left' \| 'top' \| 'bottom'` |
114
-
115
- When `faces` is set, all six faces share one composite texture sized 768×512 px (3×2 atlas of 256×256 face cells).
116
-
117
- ## createPillar
118
-
119
- Convenience for a tall narrow `createBuilding`.
120
-
121
- ```ts
122
- const post = createPillar({ scene, thickness: 0.2, height: 6, surface: { type: "color", color: "#0b0f17" } });
123
- ```
124
-
125
- ## createCylinder
126
-
127
- A capped cylinder.
128
-
129
- ```ts
130
- const drum = createCylinder({
131
- scene,
132
- radius: 1.2, height: 2.4,
133
- surface: { type: "paint", paint: paintBranding },
134
- tessellation: 64, // optional, default 48
135
- });
136
- ```
137
-
138
- ## createCurvedPanel
139
-
140
- A partial cylinder section — a curved wraparound panel.
141
-
142
- ```ts
143
- const wrap = createCurvedPanel({
144
- scene,
145
- width: 4, // arc length
146
- height: 2,
147
- arc: Math.PI / 2, // 90° wrap
148
- surface: { type: "widget", widget: chartWidget },
149
- });
150
- ```
151
-
152
- ## createWidgetTexture
153
-
154
- Lower-level helper — paint any `WidgetSpec` onto a fresh `DynamicTexture` for use on a custom mesh.
155
-
156
- ```ts
157
- const tex = createWidgetTexture(scene, "settings-tex", formWidget, 360, 240, false);
158
- const myMesh = MeshBuilder.CreatePlane("custom", { width: 2, height: 1.5 }, scene);
159
- const mat = new StandardMaterial("custom-mat", scene);
160
- mat.diffuseTexture = tex;
161
- mat.emissiveTexture = tex;
162
- mat.emissiveColor = new Color3(1, 1, 1);
163
- myMesh.material = mat;
164
- ```
165
-
166
- ## Auto-sizing
167
-
168
- Every primitive auto-derives texture pixel size from its world dimensions. A 6m × 3.9m billboard becomes a 720 × 468 px texture; the painters scale every coordinate against that canvas so layouts look right at any pixel size. If you want a specific resolution, pass `pixelWidth`/`pixelHeight` on the surface.
1
+ # Primitives
2
+
3
+ 3D shape factories that take a `SurfaceContent` (color, paint callback, or any fancy-3d `WidgetSpec`) and return a Babylon `Mesh`. Same painters power both Canvas Studio in DOM mode and 3D billboards — drop a fancy widget onto any 3D shape with one call.
4
+
5
+ ## Import
6
+
7
+ ```ts
8
+ import {
9
+ createPanel,
10
+ createBillboard,
11
+ createBuilding,
12
+ createPillar,
13
+ createCylinder,
14
+ createCurvedPanel,
15
+ createWidgetTexture,
16
+ type SurfaceContent,
17
+ type PaintFn,
18
+ type BoxFace,
19
+ } from "@particle-academy/fancy-3d/babylon";
20
+ ```
21
+
22
+ **Peer dependency:** `@babylonjs/core` (optional).
23
+
24
+ ## SurfaceContent
25
+
26
+ ```ts
27
+ type SurfaceContent =
28
+ | { type: "color"; color: string }
29
+ | { type: "paint"; paint: PaintFn; pixelWidth?: number; pixelHeight?: number; transparent?: boolean }
30
+ | { type: "widget"; widget: WidgetSpec; pixelWidth?: number; pixelHeight?: number; selected?: boolean };
31
+
32
+ type PaintFn = (ctx: CanvasRenderingContext2D, width: number, height: number) => void;
33
+ ```
34
+
35
+ | Type | Use |
36
+ |------|-----|
37
+ | `color` | Solid CSS-color material — fastest, no texture |
38
+ | `paint` | Freeform 2D drawing onto a `DynamicTexture`. Set `transparent: true` if you want non-painted regions to show through |
39
+ | `widget` | Render any fancy-3d `WidgetSpec` with the same painters used in 3D scenes |
40
+
41
+ For `paint` and `widget`, `pixelWidth`/`pixelHeight` are optional — every primitive auto-derives the texture size from its world dimensions (default density: 120 px per world unit, clamped to 1024 max axis). Override only when you need finer control.
42
+
43
+ ## createPanel
44
+
45
+ A flat single-faced plane.
46
+
47
+ ```ts
48
+ const panel = createPanel({
49
+ scene,
50
+ width: 2, // world units
51
+ height: 1.2,
52
+ surface: { type: "widget", widget: kpiWidget },
53
+ });
54
+ panel.position.set(0, 1, 0);
55
+ ```
56
+
57
+ | Option | Type | Description |
58
+ |--------|------|-------------|
59
+ | scene | `Scene` | Babylon scene to add the mesh to |
60
+ | name | `string` | Optional mesh name (default `"panel"`) |
61
+ | width / height | `number` | World-unit dimensions |
62
+ | surface | `SurfaceContent` | What to render on the panel |
63
+
64
+ ## createBillboard
65
+
66
+ Same as `createPanel` plus the option to always face the camera.
67
+
68
+ ```ts
69
+ const ad = createBillboard({
70
+ scene,
71
+ width: 6, height: 4,
72
+ surface: { type: "widget", widget: demoTile },
73
+ faceCamera: true, // mesh.billboardMode = ALL
74
+ });
75
+ ```
76
+
77
+ | Option | Type | Description |
78
+ |--------|------|-------------|
79
+ | ...PanelOpts | | All `createPanel` options |
80
+ | faceCamera | `boolean` | When true, the mesh's billboardMode is set so it always faces the camera |
81
+
82
+ ## createBuilding
83
+
84
+ A box. Pass either `surface` (one texture wrapped on every face) or `faces` (per-face surfaces, composited into a UV atlas).
85
+
86
+ ```ts
87
+ // Cheap path — one texture all around
88
+ const tower = createBuilding({
89
+ scene,
90
+ width: 4, height: 8, depth: 4,
91
+ surface: { type: "color", color: "#1e293b" },
92
+ });
93
+
94
+ // Per-face path — windows on every wall, AC unit on the roof
95
+ const office = createBuilding({
96
+ scene,
97
+ width: 5, height: 6, depth: 3,
98
+ faces: {
99
+ front: { type: "paint", paint: paintWindowGrid },
100
+ back: { type: "paint", paint: paintWindowGrid },
101
+ left: { type: "paint", paint: paintWindowGrid },
102
+ right: { type: "paint", paint: paintWindowGrid },
103
+ top: { type: "paint", paint: paintRoof },
104
+ },
105
+ });
106
+ ```
107
+
108
+ | Option | Type | Description |
109
+ |--------|------|-------------|
110
+ | scene | `Scene` | Babylon scene |
111
+ | width / height / depth | `number` | World-unit dimensions |
112
+ | surface | `SurfaceContent?` | Fallback surface used by any face not in `faces` |
113
+ | faces | `Partial<Record<BoxFace, SurfaceContent>>` | Per-face overrides. Faces are `'front' \| 'back' \| 'right' \| 'left' \| 'top' \| 'bottom'` |
114
+
115
+ When `faces` is set, all six faces share one composite texture sized 768×512 px (3×2 atlas of 256×256 face cells).
116
+
117
+ ## createPillar
118
+
119
+ Convenience for a tall narrow `createBuilding`.
120
+
121
+ ```ts
122
+ const post = createPillar({ scene, thickness: 0.2, height: 6, surface: { type: "color", color: "#0b0f17" } });
123
+ ```
124
+
125
+ ## createCylinder
126
+
127
+ A capped cylinder.
128
+
129
+ ```ts
130
+ const drum = createCylinder({
131
+ scene,
132
+ radius: 1.2, height: 2.4,
133
+ surface: { type: "paint", paint: paintBranding },
134
+ tessellation: 64, // optional, default 48
135
+ });
136
+ ```
137
+
138
+ ## createCurvedPanel
139
+
140
+ A partial cylinder section — a curved wraparound panel.
141
+
142
+ ```ts
143
+ const wrap = createCurvedPanel({
144
+ scene,
145
+ width: 4, // arc length
146
+ height: 2,
147
+ arc: Math.PI / 2, // 90° wrap
148
+ surface: { type: "widget", widget: chartWidget },
149
+ });
150
+ ```
151
+
152
+ ## createWidgetTexture
153
+
154
+ Lower-level helper — paint any `WidgetSpec` onto a fresh `DynamicTexture` for use on a custom mesh.
155
+
156
+ ```ts
157
+ const tex = createWidgetTexture(scene, "settings-tex", formWidget, 360, 240, false);
158
+ const myMesh = MeshBuilder.CreatePlane("custom", { width: 2, height: 1.5 }, scene);
159
+ const mat = new StandardMaterial("custom-mat", scene);
160
+ mat.diffuseTexture = tex;
161
+ mat.emissiveTexture = tex;
162
+ mat.emissiveColor = new Color3(1, 1, 1);
163
+ myMesh.material = mat;
164
+ ```
165
+
166
+ ## Auto-sizing
167
+
168
+ Every primitive auto-derives texture pixel size from its world dimensions. A 6m × 3.9m billboard becomes a 720 × 468 px texture; the painters scale every coordinate against that canvas so layouts look right at any pixel size. If you want a specific resolution, pass `pixelWidth`/`pixelHeight` on the surface.
package/docs/scene.md CHANGED
@@ -1,122 +1,122 @@
1
- # Scene types
2
-
3
- Engine-agnostic data structures that every adapter consumes. A scene is plain JSON-serializable data; consumers describe what to render, adapters decide how.
4
-
5
- ## Import
6
-
7
- ```ts
8
- import type {
9
- Scene,
10
- SceneNode,
11
- SceneEdge,
12
- WidgetSpec,
13
- WidgetAdapter,
14
- AdapterContext,
15
- } from "@particle-academy/fancy-3d";
16
-
17
- import { renderScene, nextId } from "@particle-academy/fancy-3d";
18
- ```
19
-
20
- ## Scene
21
-
22
- ```ts
23
- interface Scene {
24
- nodes: SceneNode[];
25
- edges: SceneEdge[];
26
- }
27
- ```
28
-
29
- | Field | Type | Description |
30
- |------|------|-------------|
31
- | nodes | `SceneNode[]` | One render output per node (mesh / React element / `Object3D` / etc.) |
32
- | edges | `SceneEdge[]` | Connectors between nodes — bezier, step, or straight |
33
-
34
- ## SceneNode
35
-
36
- ```ts
37
- interface SceneNode {
38
- id: string;
39
- position: { x: number; y: number; z?: number };
40
- size?: { w: number; h: number };
41
- widget: WidgetSpec;
42
- }
43
- ```
44
-
45
- | Field | Type | Description |
46
- |------|------|-------------|
47
- | id | `string` | Unique node identifier |
48
- | position | `{ x, y, z? }` | 2D position in pixel space (or 3D if z given) |
49
- | size | `{ w, h }` | Display size — adapters may use this for texture sizing or layout |
50
- | widget | `WidgetSpec` | Discriminated union describing the content |
51
-
52
- ## SceneEdge
53
-
54
- ```ts
55
- interface SceneEdge {
56
- id: string;
57
- from: string; // source node id
58
- to: string; // target node id
59
- label?: string;
60
- curve?: "bezier" | "step" | "straight";
61
- animated?: boolean;
62
- }
63
- ```
64
-
65
- ## WidgetSpec
66
-
67
- A discriminated union — each kind maps to one painter / renderer in every adapter.
68
-
69
- | Kind | Use |
70
- |------|-----|
71
- | `kpi` | Big-number stat with optional delta and trend |
72
- | `chart` | Line / bar / area sparkline |
73
- | `kanban` | Multi-column board with cards |
74
- | `table` | Header + rows |
75
- | `profile` | Avatar + name + role |
76
- | `callout` | Tone-coded notice (info / success / warning / danger) |
77
- | `form` | Title + labelled fields (text / number / switch) |
78
- | `action` | Title + button row (primary / secondary / ghost) |
79
- | `timeline` | Title + chronological events |
80
- | `code` | Code snippet with language tag |
81
- | `image` | Image with optional caption |
82
- | `text` | Free-form heading + body |
83
- | `demoPage` | Marketing-style tile with accent header, title, description, CTA |
84
- | `screen` | 3D-native screen with bezel, power LED, optional `paint(ctx)` callback — see [Screen widget](./screen.md) |
85
-
86
- Each kind has a TypeScript spec interface (`KpiSpec`, `ChartSpec`, etc.) re-exported from the package root.
87
-
88
- ## WidgetAdapter
89
-
90
- ```ts
91
- interface WidgetAdapter<T> {
92
- render(spec: WidgetSpec, ctx: AdapterContext): T;
93
- }
94
-
95
- interface AdapterContext {
96
- nodeId: string;
97
- selected: boolean;
98
- onSelect?: (id: string) => void;
99
- }
100
- ```
101
-
102
- A `WidgetAdapter<T>` renders one `WidgetSpec` to `T` — a `ReactNode` for the DOM adapter, a Babylon `Mesh` for the Babylon adapter, an `Object3D` for a hypothetical three.js adapter, etc. Drop in a new adapter and the scene description does not change.
103
-
104
- ## renderScene
105
-
106
- ```ts
107
- function renderScene<T>(
108
- scene: Scene,
109
- adapter: WidgetAdapter<T>,
110
- ctxFor: (node: SceneNode) => AdapterContext
111
- ): { node: SceneNode; rendered: T }[];
112
- ```
113
-
114
- Walks `scene.nodes` and calls the adapter on each one. Engine-agnostic — every adapter can reuse it. The host is responsible for placing the returned render results inside its scene graph.
115
-
116
- ## nextId
117
-
118
- ```ts
119
- function nextId(prefix: string): string;
120
- ```
121
-
122
- Convenience monotonic id generator — useful when you spawn nodes at runtime and need a fresh `SceneNode.id`.
1
+ # Scene types
2
+
3
+ Engine-agnostic data structures that every adapter consumes. A scene is plain JSON-serializable data; consumers describe what to render, adapters decide how.
4
+
5
+ ## Import
6
+
7
+ ```ts
8
+ import type {
9
+ Scene,
10
+ SceneNode,
11
+ SceneEdge,
12
+ WidgetSpec,
13
+ WidgetAdapter,
14
+ AdapterContext,
15
+ } from "@particle-academy/fancy-3d";
16
+
17
+ import { renderScene, nextId } from "@particle-academy/fancy-3d";
18
+ ```
19
+
20
+ ## Scene
21
+
22
+ ```ts
23
+ interface Scene {
24
+ nodes: SceneNode[];
25
+ edges: SceneEdge[];
26
+ }
27
+ ```
28
+
29
+ | Field | Type | Description |
30
+ |------|------|-------------|
31
+ | nodes | `SceneNode[]` | One render output per node (mesh / React element / `Object3D` / etc.) |
32
+ | edges | `SceneEdge[]` | Connectors between nodes — bezier, step, or straight |
33
+
34
+ ## SceneNode
35
+
36
+ ```ts
37
+ interface SceneNode {
38
+ id: string;
39
+ position: { x: number; y: number; z?: number };
40
+ size?: { w: number; h: number };
41
+ widget: WidgetSpec;
42
+ }
43
+ ```
44
+
45
+ | Field | Type | Description |
46
+ |------|------|-------------|
47
+ | id | `string` | Unique node identifier |
48
+ | position | `{ x, y, z? }` | 2D position in pixel space (or 3D if z given) |
49
+ | size | `{ w, h }` | Display size — adapters may use this for texture sizing or layout |
50
+ | widget | `WidgetSpec` | Discriminated union describing the content |
51
+
52
+ ## SceneEdge
53
+
54
+ ```ts
55
+ interface SceneEdge {
56
+ id: string;
57
+ from: string; // source node id
58
+ to: string; // target node id
59
+ label?: string;
60
+ curve?: "bezier" | "step" | "straight";
61
+ animated?: boolean;
62
+ }
63
+ ```
64
+
65
+ ## WidgetSpec
66
+
67
+ A discriminated union — each kind maps to one painter / renderer in every adapter.
68
+
69
+ | Kind | Use |
70
+ |------|-----|
71
+ | `kpi` | Big-number stat with optional delta and trend |
72
+ | `chart` | Line / bar / area sparkline |
73
+ | `kanban` | Multi-column board with cards |
74
+ | `table` | Header + rows |
75
+ | `profile` | Avatar + name + role |
76
+ | `callout` | Tone-coded notice (info / success / warning / danger) |
77
+ | `form` | Title + labelled fields (text / number / switch) |
78
+ | `action` | Title + button row (primary / secondary / ghost) |
79
+ | `timeline` | Title + chronological events |
80
+ | `code` | Code snippet with language tag |
81
+ | `image` | Image with optional caption |
82
+ | `text` | Free-form heading + body |
83
+ | `demoPage` | Marketing-style tile with accent header, title, description, CTA |
84
+ | `screen` | 3D-native screen with bezel, power LED, optional `paint(ctx)` callback — see [Screen widget](./screen.md) |
85
+
86
+ Each kind has a TypeScript spec interface (`KpiSpec`, `ChartSpec`, etc.) re-exported from the package root.
87
+
88
+ ## WidgetAdapter
89
+
90
+ ```ts
91
+ interface WidgetAdapter<T> {
92
+ render(spec: WidgetSpec, ctx: AdapterContext): T;
93
+ }
94
+
95
+ interface AdapterContext {
96
+ nodeId: string;
97
+ selected: boolean;
98
+ onSelect?: (id: string) => void;
99
+ }
100
+ ```
101
+
102
+ A `WidgetAdapter<T>` renders one `WidgetSpec` to `T` — a `ReactNode` for the DOM adapter, a Babylon `Mesh` for the Babylon adapter, an `Object3D` for a hypothetical three.js adapter, etc. Drop in a new adapter and the scene description does not change.
103
+
104
+ ## renderScene
105
+
106
+ ```ts
107
+ function renderScene<T>(
108
+ scene: Scene,
109
+ adapter: WidgetAdapter<T>,
110
+ ctxFor: (node: SceneNode) => AdapterContext
111
+ ): { node: SceneNode; rendered: T }[];
112
+ ```
113
+
114
+ Walks `scene.nodes` and calls the adapter on each one. Engine-agnostic — every adapter can reuse it. The host is responsible for placing the returned render results inside its scene graph.
115
+
116
+ ## nextId
117
+
118
+ ```ts
119
+ function nextId(prefix: string): string;
120
+ ```
121
+
122
+ Convenience monotonic id generator — useful when you spawn nodes at runtime and need a fresh `SceneNode.id`.