|
| 1 | +# Pygfx Examples |
| 2 | + |
| 3 | +These examples demonstrate 3D scene rendering with Pygfx. Run them with: |
| 4 | + |
| 5 | +```sh |
| 6 | +uv run collagraph --renderer pygfx examples/pygfx/<file>.cgx |
| 7 | +``` |
| 8 | + |
| 9 | +## Interactive Sphere |
| 10 | + |
| 11 | +A sphere that changes color on hover: |
| 12 | + |
| 13 | +```html title="pygfx-component.cgx" |
| 14 | +<group> |
| 15 | + <ambient-light /> |
| 16 | + <directional-light /> |
| 17 | + <mesh |
| 18 | + :material="material()" |
| 19 | + :geometry="sphere" |
| 20 | + @pointer_enter="lambda ev: hover(True)" |
| 21 | + @pointer_leave="lambda ev: hover(False)" |
| 22 | + /> |
| 23 | +</group> |
| 24 | + |
| 25 | +<script> |
| 26 | +import pygfx as gfx |
| 27 | +import collagraph as cg |
| 28 | +
|
| 29 | +sphere = gfx.sphere_geometry(radius=3) |
| 30 | +red = gfx.MeshPhongMaterial(color=(1, 0.2, 0.2), pick_write=True) |
| 31 | +blue = gfx.MeshPhongMaterial(color=(0.2, 0.4, 1.0), pick_write=True) |
| 32 | +
|
| 33 | +class Ball(cg.Component): |
| 34 | + def material(self): |
| 35 | + hovered = self.state.get("hovered", False) |
| 36 | + return red if hovered else blue |
| 37 | +
|
| 38 | + def hover(self, hover): |
| 39 | + self.state["hovered"] = hover |
| 40 | +</script> |
| 41 | +``` |
| 42 | + |
| 43 | +## Point Cloud |
| 44 | + |
| 45 | +A dynamic point cloud with selection and hover state. Uses `v-for` to render multiple 3D objects and accepts props via CLI `--state`: |
| 46 | + |
| 47 | +```sh |
| 48 | +uv run collagraph --renderer pygfx --state '{"count": 100}' examples/pygfx/point_cloud.cgx |
| 49 | +``` |
| 50 | + |
| 51 | +```html title="point_cloud.cgx" |
| 52 | +<ambient-light /> |
| 53 | +<point-light /> |
| 54 | +<group> |
| 55 | + <Point |
| 56 | + v-for="idx, position in enumerate(positions)" |
| 57 | + :position="position" |
| 58 | + :material="'selected' if idx == selected else 'hovered' if idx == hovered else 'default'" |
| 59 | + :index="idx" |
| 60 | + @selected="set_selected" |
| 61 | + @hovered="set_hovered" |
| 62 | + /> |
| 63 | +</group> |
| 64 | + |
| 65 | +<script> |
| 66 | +import random |
| 67 | +import pygfx as gfx |
| 68 | +import collagraph |
| 69 | +from observ import watch |
| 70 | +from point import Point |
| 71 | +
|
| 72 | +class PointCloud(collagraph.Component): |
| 73 | + def init(self): |
| 74 | + self.state["positions"] = [] |
| 75 | + self.state["hovered"] = -1 |
| 76 | + self.state["selected"] = -1 |
| 77 | +
|
| 78 | + self.watchers = {} |
| 79 | + self.watchers["count"] = watch( |
| 80 | + lambda: self.props["count"], |
| 81 | + self.update_positions, |
| 82 | + immediate=True, |
| 83 | + ) |
| 84 | +
|
| 85 | + def update_positions(self): |
| 86 | + new_count = self.props["count"] |
| 87 | + old_count = len(self.state["positions"]) |
| 88 | + if new_count > old_count: |
| 89 | + self.state["positions"].extend( |
| 90 | + [(random.randint(-20, 20), random.randint(-20, 20), random.randint(-20, 20)) |
| 91 | + for _ in range(new_count - old_count)] |
| 92 | + ) |
| 93 | + elif new_count < old_count: |
| 94 | + del self.state["positions"][0:old_count - new_count] |
| 95 | +
|
| 96 | + def set_hovered(self, index): |
| 97 | + self.state["hovered"] = index |
| 98 | +
|
| 99 | + def set_selected(self, index): |
| 100 | + if self.state["selected"] == index: |
| 101 | + self.state["selected"] = -1 |
| 102 | + else: |
| 103 | + self.state["selected"] = index |
| 104 | +</script> |
| 105 | +``` |
| 106 | + |
| 107 | +## Landmarks with Text Labels |
| 108 | + |
| 109 | +Random 3D landmarks with labels rendered as text content inside `<text>` elements, using `{{ }}` interpolation. Click a landmark sphere to increment and rename its label: |
| 110 | + |
| 111 | +```sh |
| 112 | +uv run collagraph --renderer pygfx --state '{"count": 25}' examples/pygfx/landmarks.cgx |
| 113 | +``` |
| 114 | + |
| 115 | +```html title="landmarks.cgx (excerpt)" |
| 116 | +<group v-for="idx, landmark in enumerate(landmarks)"> |
| 117 | + <group :local.position="landmark['position']"> |
| 118 | + <mesh |
| 119 | + :geometry="sphere_geometry" |
| 120 | + :material="selected_sphere_material if idx == selected else sphere_material" |
| 121 | + @click="lambda ev: click_landmark(idx)" |
| 122 | + /> |
| 123 | + <text |
| 124 | + anchor="bottom-center" |
| 125 | + :font_size="14" |
| 126 | + :local.position="(0, 0.75, 0)" |
| 127 | + :material="selected_label_material if idx == selected else label_material" |
| 128 | + screen_space |
| 129 | + > |
| 130 | + {{landmark['name']}} |
| 131 | + </text> |
| 132 | + </group> |
| 133 | +</group> |
| 134 | +``` |
| 135 | + |
| 136 | +## Combined PySide + Pygfx |
| 137 | + |
| 138 | +You can embed a Pygfx canvas inside a PySide6 application. See [`examples/pygfx/combined-example.py`](https://github.com/fork-tongue/collagraph/tree/master/examples/pygfx/combined-example.py) for a full example of rendering 3D content within a Qt widget layout. |
| 139 | + |
| 140 | +## More Examples |
| 141 | + |
| 142 | +See the [`examples/pygfx/`](https://github.com/fork-tongue/collagraph/tree/master/examples/pygfx) directory for: |
| 143 | + |
| 144 | +- `point.cgx` -- Individual point component with hover/select |
| 145 | +- `button.cgx` / `numberpad.cgx` -- Reusable 3D UI components |
| 146 | +- `component-example.py` -- Pygfx with timer-based interaction |
| 147 | +- `render_widget.cgx` -- Wrapping PygfxRenderer inside a PySide widget |
0 commit comments