flet-3d 0.1.0__tar.gz
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.
- flet_3d-0.1.0/PKG-INFO +196 -0
- flet_3d-0.1.0/README.md +174 -0
- flet_3d-0.1.0/flet_3d.egg-info/PKG-INFO +196 -0
- flet_3d-0.1.0/flet_3d.egg-info/SOURCES.txt +14 -0
- flet_3d-0.1.0/flet_3d.egg-info/dependency_links.txt +1 -0
- flet_3d-0.1.0/flet_3d.egg-info/requires.txt +2 -0
- flet_3d-0.1.0/flet_3d.egg-info/top_level.txt +1 -0
- flet_3d-0.1.0/pyproject.toml +50 -0
- flet_3d-0.1.0/setup.cfg +4 -0
- flet_3d-0.1.0/src/__init__.py +19 -0
- flet_3d-0.1.0/src/camera.py +159 -0
- flet_3d-0.1.0/src/engine.py +622 -0
- flet_3d-0.1.0/src/loaders.py +229 -0
- flet_3d-0.1.0/src/physics.py +208 -0
- flet_3d-0.1.0/src/svg_loader.py +490 -0
- flet_3d-0.1.0/src/viewport.py +169 -0
flet_3d-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flet-3d
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A vectorized 3D software rendering engine for Flet v1.0 powered by NumPy and Flet Canvas.
|
|
5
|
+
Author-email: Osama Mohammed <osamalzabidi@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/osamalzabidi/flet-3d
|
|
8
|
+
Project-URL: Repository, https://github.com/osamalzabidi/flet-3d
|
|
9
|
+
Project-URL: Issues, https://github.com/osamalzabidi/flet-3d/issues
|
|
10
|
+
Keywords: flet,3d,3d-engine,software-renderer,numpy,canvas,graphics,obj-loader
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Multimedia :: Graphics :: 3D Rendering
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
Requires-Dist: flet>=1.1
|
|
21
|
+
Requires-Dist: numpy>=1.26.0
|
|
22
|
+
|
|
23
|
+
# Flet 3D & 2D (`flet-3d`)
|
|
24
|
+
|
|
25
|
+
A vectorized **3D and 2D software rendering & physics engine** for **Flet 1.0+** powered by **NumPy** and `flet.canvas.Canvas`.
|
|
26
|
+
|
|
27
|
+
Run real-time interactive 3D scenes, `.obj`/`.mtl` models, vector `.svg` artwork, and 2D rigid-body physics across **Windows, macOS, Linux, Web, Android, and iOS**—with zero external C/OpenGL native dependencies.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
### 3D Engine (`VectorizedEngine3D` & `Viewport3D`)
|
|
34
|
+
* **Vectorized NumPy Matrix Pipeline:** Fast `4x4` homogeneous Model-View-Projection math, back-face culling, and flat directional lighting.
|
|
35
|
+
* **True Sutherland-Hodgman Near-Plane Clipping:** Cleanly slices triangles straddling `z = near_plane` without coordinate explosion or pop-out.
|
|
36
|
+
* **2D Screen-Bounds Frustum Culling:** Drops off-screen and sub-pixel triangles before depth sorting (`np.argsort`).
|
|
37
|
+
* **Run-Length Draw-Call Batching:** Groups consecutive depth-sorted triangles of matching quantized RGB colors into single `cv.Path` objects, cutting Flet IPC serialization overhead by **70–90%**.
|
|
38
|
+
* **6-DOF Camera (`Camera6DOF`):** Full `WASD` + `Q/E` + `Shift` flight movement and mouse/touch drag look.
|
|
39
|
+
* **Wavefront `.obj` + `.mtl` Loader (`load_obj_with_progress`):** Multi-material diffuse color parsing, polygon fan-triangulation, byte-level progress tracking, and automatic triangle-budget decimation for massive meshes.
|
|
40
|
+
* **Möller–Trumbore Ray-Triangle Picking (`engine.pick`):** Instant 3D object selection and world-space hit inspection on click/tap.
|
|
41
|
+
* **Live Debug Overlays:** Toggleable wireframe, solid fill, and surface normal vectors.
|
|
42
|
+
|
|
43
|
+
### 2D Engine (`VectorizedEngine2D`, `Viewport2D`, `PhysicsWorld2D`, `load_svg`)
|
|
44
|
+
* **3x3 Affine Camera (`Camera2D`):** Smooth drag panning, scroll-wheel zoom-to-cursor, rotation (`Q/E`), and `WASD` movement.
|
|
45
|
+
* **Z-Layer Painter's Sorting & Batching:** Renders thousands of triangles at 60 FPS with an infinite adaptive world grid.
|
|
46
|
+
* **Nested `.svg` Vector Importer (`load_svg`):** Parses `<path>`, `<rect>`, `<circle>`, `<ellipse>`, and `<polygon>` tags—including nested `<g transform="translate(...) rotate(...) scale(...) matrix(...)">` hierarchies, Bézier curve flattening, and ear-clipping triangulation.
|
|
47
|
+
* **2D Rigid-Body Physics (`PhysicsWorld2D`):** Gravity, angular velocity/torque, wall/floor bounces, elastic body-to-body collisions, and interactive impulse kicks.
|
|
48
|
+
* **Barycentric Shape Picking:** Accurate point-in-polygon selection respecting Z-layer stacking.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
### Using `uv` (Recommended)
|
|
55
|
+
```bash
|
|
56
|
+
git clone https://github.com/osamalzabidi/flet-3d.git
|
|
57
|
+
cd flet-3d
|
|
58
|
+
uv sync
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Using `pip`
|
|
62
|
+
```bash
|
|
63
|
+
pip install git+https://github.com/osamalzabidi/flet-3d.git
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Repository Structure
|
|
69
|
+
|
|
70
|
+
```text
|
|
71
|
+
flet-3d/
|
|
72
|
+
├── src/
|
|
73
|
+
│ ├── __init__.py # Public exports for 2D & 3D engines
|
|
74
|
+
│ ├── camera.py # Camera6DOF (3D) & Camera2D (2D)
|
|
75
|
+
│ ├── engine.py # VectorizedEngine & RenderStats (2D and 3D)
|
|
76
|
+
│ ├── loaders.py # Wavefront .obj/.mtl loader & SceneBuilder2D
|
|
77
|
+
│ ├── physics.py # PhysicsWorld2D & RigidBody2D rigid-body simulator
|
|
78
|
+
│ ├── svg_loader.py # Nested <g transform> SVG parser & ear-clipping triangulator
|
|
79
|
+
│ └── viewport.py # Drop-in Flet Viewport3D & Viewport2D gesture controls
|
|
80
|
+
├── examples/
|
|
81
|
+
│ ├── 2d/
|
|
82
|
+
│ │ ├── 01_minimal_2d.py
|
|
83
|
+
│ │ ├── 02_physics_sandbox.py
|
|
84
|
+
│ │ ├── 03_particle_fountain.py
|
|
85
|
+
│ │ ├── 04_tilemap_editor.py
|
|
86
|
+
│ │ └── 05_nested_svg_transforms.py
|
|
87
|
+
│ └── 3d/
|
|
88
|
+
│ ├── 01_minimal_viewer.py
|
|
89
|
+
│ ├── 02_procedural_terrain.py
|
|
90
|
+
│ ├── 03_solar_system_animation.py
|
|
91
|
+
│ ├── 04_cad_inspector_ui.py
|
|
92
|
+
│ └── 05_voxel_minecraft_builder.py
|
|
93
|
+
├── pyproject.toml
|
|
94
|
+
├── LICENSE
|
|
95
|
+
└── README.md
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Quickstart Examples
|
|
101
|
+
|
|
102
|
+
### 1. 3D Viewport Quickstart
|
|
103
|
+
```python
|
|
104
|
+
import asyncio
|
|
105
|
+
import flet as ft
|
|
106
|
+
import numpy as np
|
|
107
|
+
from flet_3d import Camera6DOF, VectorizedEngine3D, Viewport3D
|
|
108
|
+
|
|
109
|
+
async def main(page: ft.Page):
|
|
110
|
+
page.title = "Flet 3D Quickstart"
|
|
111
|
+
page.bgcolor = "#020617"
|
|
112
|
+
page.padding = 0
|
|
113
|
+
|
|
114
|
+
vertices = np.array([
|
|
115
|
+
[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
|
|
116
|
+
[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1],
|
|
117
|
+
], dtype=np.float32)
|
|
118
|
+
|
|
119
|
+
faces = np.array([, [0, 3, 2], [1, 6, 5], [1, 2, 6],
|
|
120
|
+
, [5, 6, 7], [4, 3, 0], [4, 7, 3],
|
|
121
|
+
, [3, 7, 6], [4, 1, 5], [4, 0, 1],
|
|
122
|
+
], dtype=np.int32)
|
|
123
|
+
|
|
124
|
+
colors = np.tile(np.array([56, 189, 248], dtype=np.float32), (len(faces), 1))
|
|
125
|
+
|
|
126
|
+
viewport = Viewport3D(
|
|
127
|
+
vertices, faces, colors,
|
|
128
|
+
camera=Camera6DOF(pos=(0.0, 1.5, -5.0), pitch=-0.25),
|
|
129
|
+
engine=VectorizedEngine3D(),
|
|
130
|
+
)
|
|
131
|
+
page.add(viewport)
|
|
132
|
+
|
|
133
|
+
while True:
|
|
134
|
+
viewport.engine.width = page.width or 800
|
|
135
|
+
viewport.engine.height = page.height or 600
|
|
136
|
+
viewport.render_frame()
|
|
137
|
+
page.update()
|
|
138
|
+
await asyncio.sleep(1 / 60)
|
|
139
|
+
|
|
140
|
+
if __name__ == "__main__":
|
|
141
|
+
ft.run(main)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 2. 2D Vector Scene & Rigid-Body Physics Quickstart
|
|
145
|
+
```python
|
|
146
|
+
import asyncio
|
|
147
|
+
import flet as ft
|
|
148
|
+
from flet_3d import SceneBuilder2D, Viewport2D
|
|
149
|
+
from flet_3d.physics import PhysicsWorld2D
|
|
150
|
+
|
|
151
|
+
async def main(page: ft.Page):
|
|
152
|
+
page.title = "Flet 2D Physics Quickstart"
|
|
153
|
+
page.bgcolor = "#020617"
|
|
154
|
+
page.padding = 0
|
|
155
|
+
|
|
156
|
+
builder = SceneBuilder2D()
|
|
157
|
+
builder.add_rect(0.0, -240.0, 600.0, 24.0, (51, 65, 85), z_layer=0, obj_id=0) # Static floor
|
|
158
|
+
builder.add_polygon(-60.0, 140.0, 42.0, sides=6, color=(56, 189, 248), z_layer=1, obj_id=1)
|
|
159
|
+
builder.add_polygon(60.0, 210.0, 38.0, sides=5, color=(244, 114, 182), z_layer=2, obj_id=2)
|
|
160
|
+
|
|
161
|
+
vertices, faces, colors, z_layers, object_ids = builder.build()
|
|
162
|
+
physics = PhysicsWorld2D.from_buffers(vertices, faces, object_ids, static_ids={0})
|
|
163
|
+
|
|
164
|
+
viewport = Viewport2D(vertices, faces, colors, z_layers)
|
|
165
|
+
page.add(viewport)
|
|
166
|
+
|
|
167
|
+
while True:
|
|
168
|
+
viewport.engine.width = page.width or 800
|
|
169
|
+
viewport.engine.height = page.height or 600
|
|
170
|
+
physics.step(1 / 60, vertices)
|
|
171
|
+
viewport.render_frame()
|
|
172
|
+
page.update()
|
|
173
|
+
await asyncio.sleep(1 / 60)
|
|
174
|
+
|
|
175
|
+
if __name__ == "__main__":
|
|
176
|
+
ft.run(main)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Controls & Shortcuts
|
|
182
|
+
|
|
183
|
+
| Mode | Action | Input |
|
|
184
|
+
| :--- | :--- | :--- |
|
|
185
|
+
| **3D Camera** | Move / Strafe / Elevate | `W A S D` + `Q / E` (`Shift` to sprint) |
|
|
186
|
+
| **3D Camera** | Look / Orbit | Click & Drag (or Touch Drag / `ft.Accelerometer` Tilt on Mobile) |
|
|
187
|
+
| **3D / 2D** | Select / Pick Object | Single Click / Tap |
|
|
188
|
+
| **2D Camera** | Pan / Zoom-to-Cursor | Drag Empty Space / Mouse Wheel |
|
|
189
|
+
| **2D Camera** | Move / Rotate View | `W A S D` / `Q / E` |
|
|
190
|
+
| **Debug** | Toggle Wireframe / Solid / Normals | `1` / `2` / `3` |
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
Licensed under the **Apache License 2.0**. See [LICENSE](LICENSE) for details.
|
flet_3d-0.1.0/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Flet 3D & 2D (`flet-3d`)
|
|
2
|
+
|
|
3
|
+
A vectorized **3D and 2D software rendering & physics engine** for **Flet 1.0+** powered by **NumPy** and `flet.canvas.Canvas`.
|
|
4
|
+
|
|
5
|
+
Run real-time interactive 3D scenes, `.obj`/`.mtl` models, vector `.svg` artwork, and 2D rigid-body physics across **Windows, macOS, Linux, Web, Android, and iOS**—with zero external C/OpenGL native dependencies.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
### 3D Engine (`VectorizedEngine3D` & `Viewport3D`)
|
|
12
|
+
* **Vectorized NumPy Matrix Pipeline:** Fast `4x4` homogeneous Model-View-Projection math, back-face culling, and flat directional lighting.
|
|
13
|
+
* **True Sutherland-Hodgman Near-Plane Clipping:** Cleanly slices triangles straddling `z = near_plane` without coordinate explosion or pop-out.
|
|
14
|
+
* **2D Screen-Bounds Frustum Culling:** Drops off-screen and sub-pixel triangles before depth sorting (`np.argsort`).
|
|
15
|
+
* **Run-Length Draw-Call Batching:** Groups consecutive depth-sorted triangles of matching quantized RGB colors into single `cv.Path` objects, cutting Flet IPC serialization overhead by **70–90%**.
|
|
16
|
+
* **6-DOF Camera (`Camera6DOF`):** Full `WASD` + `Q/E` + `Shift` flight movement and mouse/touch drag look.
|
|
17
|
+
* **Wavefront `.obj` + `.mtl` Loader (`load_obj_with_progress`):** Multi-material diffuse color parsing, polygon fan-triangulation, byte-level progress tracking, and automatic triangle-budget decimation for massive meshes.
|
|
18
|
+
* **Möller–Trumbore Ray-Triangle Picking (`engine.pick`):** Instant 3D object selection and world-space hit inspection on click/tap.
|
|
19
|
+
* **Live Debug Overlays:** Toggleable wireframe, solid fill, and surface normal vectors.
|
|
20
|
+
|
|
21
|
+
### 2D Engine (`VectorizedEngine2D`, `Viewport2D`, `PhysicsWorld2D`, `load_svg`)
|
|
22
|
+
* **3x3 Affine Camera (`Camera2D`):** Smooth drag panning, scroll-wheel zoom-to-cursor, rotation (`Q/E`), and `WASD` movement.
|
|
23
|
+
* **Z-Layer Painter's Sorting & Batching:** Renders thousands of triangles at 60 FPS with an infinite adaptive world grid.
|
|
24
|
+
* **Nested `.svg` Vector Importer (`load_svg`):** Parses `<path>`, `<rect>`, `<circle>`, `<ellipse>`, and `<polygon>` tags—including nested `<g transform="translate(...) rotate(...) scale(...) matrix(...)">` hierarchies, Bézier curve flattening, and ear-clipping triangulation.
|
|
25
|
+
* **2D Rigid-Body Physics (`PhysicsWorld2D`):** Gravity, angular velocity/torque, wall/floor bounces, elastic body-to-body collisions, and interactive impulse kicks.
|
|
26
|
+
* **Barycentric Shape Picking:** Accurate point-in-polygon selection respecting Z-layer stacking.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
### Using `uv` (Recommended)
|
|
33
|
+
```bash
|
|
34
|
+
git clone https://github.com/osamalzabidi/flet-3d.git
|
|
35
|
+
cd flet-3d
|
|
36
|
+
uv sync
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Using `pip`
|
|
40
|
+
```bash
|
|
41
|
+
pip install git+https://github.com/osamalzabidi/flet-3d.git
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Repository Structure
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
flet-3d/
|
|
50
|
+
├── src/
|
|
51
|
+
│ ├── __init__.py # Public exports for 2D & 3D engines
|
|
52
|
+
│ ├── camera.py # Camera6DOF (3D) & Camera2D (2D)
|
|
53
|
+
│ ├── engine.py # VectorizedEngine & RenderStats (2D and 3D)
|
|
54
|
+
│ ├── loaders.py # Wavefront .obj/.mtl loader & SceneBuilder2D
|
|
55
|
+
│ ├── physics.py # PhysicsWorld2D & RigidBody2D rigid-body simulator
|
|
56
|
+
│ ├── svg_loader.py # Nested <g transform> SVG parser & ear-clipping triangulator
|
|
57
|
+
│ └── viewport.py # Drop-in Flet Viewport3D & Viewport2D gesture controls
|
|
58
|
+
├── examples/
|
|
59
|
+
│ ├── 2d/
|
|
60
|
+
│ │ ├── 01_minimal_2d.py
|
|
61
|
+
│ │ ├── 02_physics_sandbox.py
|
|
62
|
+
│ │ ├── 03_particle_fountain.py
|
|
63
|
+
│ │ ├── 04_tilemap_editor.py
|
|
64
|
+
│ │ └── 05_nested_svg_transforms.py
|
|
65
|
+
│ └── 3d/
|
|
66
|
+
│ ├── 01_minimal_viewer.py
|
|
67
|
+
│ ├── 02_procedural_terrain.py
|
|
68
|
+
│ ├── 03_solar_system_animation.py
|
|
69
|
+
│ ├── 04_cad_inspector_ui.py
|
|
70
|
+
│ └── 05_voxel_minecraft_builder.py
|
|
71
|
+
├── pyproject.toml
|
|
72
|
+
├── LICENSE
|
|
73
|
+
└── README.md
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Quickstart Examples
|
|
79
|
+
|
|
80
|
+
### 1. 3D Viewport Quickstart
|
|
81
|
+
```python
|
|
82
|
+
import asyncio
|
|
83
|
+
import flet as ft
|
|
84
|
+
import numpy as np
|
|
85
|
+
from flet_3d import Camera6DOF, VectorizedEngine3D, Viewport3D
|
|
86
|
+
|
|
87
|
+
async def main(page: ft.Page):
|
|
88
|
+
page.title = "Flet 3D Quickstart"
|
|
89
|
+
page.bgcolor = "#020617"
|
|
90
|
+
page.padding = 0
|
|
91
|
+
|
|
92
|
+
vertices = np.array([
|
|
93
|
+
[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
|
|
94
|
+
[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1],
|
|
95
|
+
], dtype=np.float32)
|
|
96
|
+
|
|
97
|
+
faces = np.array([, [0, 3, 2], [1, 6, 5], [1, 2, 6],
|
|
98
|
+
, [5, 6, 7], [4, 3, 0], [4, 7, 3],
|
|
99
|
+
, [3, 7, 6], [4, 1, 5], [4, 0, 1],
|
|
100
|
+
], dtype=np.int32)
|
|
101
|
+
|
|
102
|
+
colors = np.tile(np.array([56, 189, 248], dtype=np.float32), (len(faces), 1))
|
|
103
|
+
|
|
104
|
+
viewport = Viewport3D(
|
|
105
|
+
vertices, faces, colors,
|
|
106
|
+
camera=Camera6DOF(pos=(0.0, 1.5, -5.0), pitch=-0.25),
|
|
107
|
+
engine=VectorizedEngine3D(),
|
|
108
|
+
)
|
|
109
|
+
page.add(viewport)
|
|
110
|
+
|
|
111
|
+
while True:
|
|
112
|
+
viewport.engine.width = page.width or 800
|
|
113
|
+
viewport.engine.height = page.height or 600
|
|
114
|
+
viewport.render_frame()
|
|
115
|
+
page.update()
|
|
116
|
+
await asyncio.sleep(1 / 60)
|
|
117
|
+
|
|
118
|
+
if __name__ == "__main__":
|
|
119
|
+
ft.run(main)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 2. 2D Vector Scene & Rigid-Body Physics Quickstart
|
|
123
|
+
```python
|
|
124
|
+
import asyncio
|
|
125
|
+
import flet as ft
|
|
126
|
+
from flet_3d import SceneBuilder2D, Viewport2D
|
|
127
|
+
from flet_3d.physics import PhysicsWorld2D
|
|
128
|
+
|
|
129
|
+
async def main(page: ft.Page):
|
|
130
|
+
page.title = "Flet 2D Physics Quickstart"
|
|
131
|
+
page.bgcolor = "#020617"
|
|
132
|
+
page.padding = 0
|
|
133
|
+
|
|
134
|
+
builder = SceneBuilder2D()
|
|
135
|
+
builder.add_rect(0.0, -240.0, 600.0, 24.0, (51, 65, 85), z_layer=0, obj_id=0) # Static floor
|
|
136
|
+
builder.add_polygon(-60.0, 140.0, 42.0, sides=6, color=(56, 189, 248), z_layer=1, obj_id=1)
|
|
137
|
+
builder.add_polygon(60.0, 210.0, 38.0, sides=5, color=(244, 114, 182), z_layer=2, obj_id=2)
|
|
138
|
+
|
|
139
|
+
vertices, faces, colors, z_layers, object_ids = builder.build()
|
|
140
|
+
physics = PhysicsWorld2D.from_buffers(vertices, faces, object_ids, static_ids={0})
|
|
141
|
+
|
|
142
|
+
viewport = Viewport2D(vertices, faces, colors, z_layers)
|
|
143
|
+
page.add(viewport)
|
|
144
|
+
|
|
145
|
+
while True:
|
|
146
|
+
viewport.engine.width = page.width or 800
|
|
147
|
+
viewport.engine.height = page.height or 600
|
|
148
|
+
physics.step(1 / 60, vertices)
|
|
149
|
+
viewport.render_frame()
|
|
150
|
+
page.update()
|
|
151
|
+
await asyncio.sleep(1 / 60)
|
|
152
|
+
|
|
153
|
+
if __name__ == "__main__":
|
|
154
|
+
ft.run(main)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Controls & Shortcuts
|
|
160
|
+
|
|
161
|
+
| Mode | Action | Input |
|
|
162
|
+
| :--- | :--- | :--- |
|
|
163
|
+
| **3D Camera** | Move / Strafe / Elevate | `W A S D` + `Q / E` (`Shift` to sprint) |
|
|
164
|
+
| **3D Camera** | Look / Orbit | Click & Drag (or Touch Drag / `ft.Accelerometer` Tilt on Mobile) |
|
|
165
|
+
| **3D / 2D** | Select / Pick Object | Single Click / Tap |
|
|
166
|
+
| **2D Camera** | Pan / Zoom-to-Cursor | Drag Empty Space / Mouse Wheel |
|
|
167
|
+
| **2D Camera** | Move / Rotate View | `W A S D` / `Q / E` |
|
|
168
|
+
| **Debug** | Toggle Wireframe / Solid / Normals | `1` / `2` / `3` |
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
Licensed under the **Apache License 2.0**. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flet-3d
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A vectorized 3D software rendering engine for Flet v1.0 powered by NumPy and Flet Canvas.
|
|
5
|
+
Author-email: Osama Mohammed <osamalzabidi@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/osamalzabidi/flet-3d
|
|
8
|
+
Project-URL: Repository, https://github.com/osamalzabidi/flet-3d
|
|
9
|
+
Project-URL: Issues, https://github.com/osamalzabidi/flet-3d/issues
|
|
10
|
+
Keywords: flet,3d,3d-engine,software-renderer,numpy,canvas,graphics,obj-loader
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Multimedia :: Graphics :: 3D Rendering
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
Requires-Dist: flet>=1.1
|
|
21
|
+
Requires-Dist: numpy>=1.26.0
|
|
22
|
+
|
|
23
|
+
# Flet 3D & 2D (`flet-3d`)
|
|
24
|
+
|
|
25
|
+
A vectorized **3D and 2D software rendering & physics engine** for **Flet 1.0+** powered by **NumPy** and `flet.canvas.Canvas`.
|
|
26
|
+
|
|
27
|
+
Run real-time interactive 3D scenes, `.obj`/`.mtl` models, vector `.svg` artwork, and 2D rigid-body physics across **Windows, macOS, Linux, Web, Android, and iOS**—with zero external C/OpenGL native dependencies.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
### 3D Engine (`VectorizedEngine3D` & `Viewport3D`)
|
|
34
|
+
* **Vectorized NumPy Matrix Pipeline:** Fast `4x4` homogeneous Model-View-Projection math, back-face culling, and flat directional lighting.
|
|
35
|
+
* **True Sutherland-Hodgman Near-Plane Clipping:** Cleanly slices triangles straddling `z = near_plane` without coordinate explosion or pop-out.
|
|
36
|
+
* **2D Screen-Bounds Frustum Culling:** Drops off-screen and sub-pixel triangles before depth sorting (`np.argsort`).
|
|
37
|
+
* **Run-Length Draw-Call Batching:** Groups consecutive depth-sorted triangles of matching quantized RGB colors into single `cv.Path` objects, cutting Flet IPC serialization overhead by **70–90%**.
|
|
38
|
+
* **6-DOF Camera (`Camera6DOF`):** Full `WASD` + `Q/E` + `Shift` flight movement and mouse/touch drag look.
|
|
39
|
+
* **Wavefront `.obj` + `.mtl` Loader (`load_obj_with_progress`):** Multi-material diffuse color parsing, polygon fan-triangulation, byte-level progress tracking, and automatic triangle-budget decimation for massive meshes.
|
|
40
|
+
* **Möller–Trumbore Ray-Triangle Picking (`engine.pick`):** Instant 3D object selection and world-space hit inspection on click/tap.
|
|
41
|
+
* **Live Debug Overlays:** Toggleable wireframe, solid fill, and surface normal vectors.
|
|
42
|
+
|
|
43
|
+
### 2D Engine (`VectorizedEngine2D`, `Viewport2D`, `PhysicsWorld2D`, `load_svg`)
|
|
44
|
+
* **3x3 Affine Camera (`Camera2D`):** Smooth drag panning, scroll-wheel zoom-to-cursor, rotation (`Q/E`), and `WASD` movement.
|
|
45
|
+
* **Z-Layer Painter's Sorting & Batching:** Renders thousands of triangles at 60 FPS with an infinite adaptive world grid.
|
|
46
|
+
* **Nested `.svg` Vector Importer (`load_svg`):** Parses `<path>`, `<rect>`, `<circle>`, `<ellipse>`, and `<polygon>` tags—including nested `<g transform="translate(...) rotate(...) scale(...) matrix(...)">` hierarchies, Bézier curve flattening, and ear-clipping triangulation.
|
|
47
|
+
* **2D Rigid-Body Physics (`PhysicsWorld2D`):** Gravity, angular velocity/torque, wall/floor bounces, elastic body-to-body collisions, and interactive impulse kicks.
|
|
48
|
+
* **Barycentric Shape Picking:** Accurate point-in-polygon selection respecting Z-layer stacking.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
### Using `uv` (Recommended)
|
|
55
|
+
```bash
|
|
56
|
+
git clone https://github.com/osamalzabidi/flet-3d.git
|
|
57
|
+
cd flet-3d
|
|
58
|
+
uv sync
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Using `pip`
|
|
62
|
+
```bash
|
|
63
|
+
pip install git+https://github.com/osamalzabidi/flet-3d.git
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Repository Structure
|
|
69
|
+
|
|
70
|
+
```text
|
|
71
|
+
flet-3d/
|
|
72
|
+
├── src/
|
|
73
|
+
│ ├── __init__.py # Public exports for 2D & 3D engines
|
|
74
|
+
│ ├── camera.py # Camera6DOF (3D) & Camera2D (2D)
|
|
75
|
+
│ ├── engine.py # VectorizedEngine & RenderStats (2D and 3D)
|
|
76
|
+
│ ├── loaders.py # Wavefront .obj/.mtl loader & SceneBuilder2D
|
|
77
|
+
│ ├── physics.py # PhysicsWorld2D & RigidBody2D rigid-body simulator
|
|
78
|
+
│ ├── svg_loader.py # Nested <g transform> SVG parser & ear-clipping triangulator
|
|
79
|
+
│ └── viewport.py # Drop-in Flet Viewport3D & Viewport2D gesture controls
|
|
80
|
+
├── examples/
|
|
81
|
+
│ ├── 2d/
|
|
82
|
+
│ │ ├── 01_minimal_2d.py
|
|
83
|
+
│ │ ├── 02_physics_sandbox.py
|
|
84
|
+
│ │ ├── 03_particle_fountain.py
|
|
85
|
+
│ │ ├── 04_tilemap_editor.py
|
|
86
|
+
│ │ └── 05_nested_svg_transforms.py
|
|
87
|
+
│ └── 3d/
|
|
88
|
+
│ ├── 01_minimal_viewer.py
|
|
89
|
+
│ ├── 02_procedural_terrain.py
|
|
90
|
+
│ ├── 03_solar_system_animation.py
|
|
91
|
+
│ ├── 04_cad_inspector_ui.py
|
|
92
|
+
│ └── 05_voxel_minecraft_builder.py
|
|
93
|
+
├── pyproject.toml
|
|
94
|
+
├── LICENSE
|
|
95
|
+
└── README.md
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Quickstart Examples
|
|
101
|
+
|
|
102
|
+
### 1. 3D Viewport Quickstart
|
|
103
|
+
```python
|
|
104
|
+
import asyncio
|
|
105
|
+
import flet as ft
|
|
106
|
+
import numpy as np
|
|
107
|
+
from flet_3d import Camera6DOF, VectorizedEngine3D, Viewport3D
|
|
108
|
+
|
|
109
|
+
async def main(page: ft.Page):
|
|
110
|
+
page.title = "Flet 3D Quickstart"
|
|
111
|
+
page.bgcolor = "#020617"
|
|
112
|
+
page.padding = 0
|
|
113
|
+
|
|
114
|
+
vertices = np.array([
|
|
115
|
+
[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
|
|
116
|
+
[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1],
|
|
117
|
+
], dtype=np.float32)
|
|
118
|
+
|
|
119
|
+
faces = np.array([, [0, 3, 2], [1, 6, 5], [1, 2, 6],
|
|
120
|
+
, [5, 6, 7], [4, 3, 0], [4, 7, 3],
|
|
121
|
+
, [3, 7, 6], [4, 1, 5], [4, 0, 1],
|
|
122
|
+
], dtype=np.int32)
|
|
123
|
+
|
|
124
|
+
colors = np.tile(np.array([56, 189, 248], dtype=np.float32), (len(faces), 1))
|
|
125
|
+
|
|
126
|
+
viewport = Viewport3D(
|
|
127
|
+
vertices, faces, colors,
|
|
128
|
+
camera=Camera6DOF(pos=(0.0, 1.5, -5.0), pitch=-0.25),
|
|
129
|
+
engine=VectorizedEngine3D(),
|
|
130
|
+
)
|
|
131
|
+
page.add(viewport)
|
|
132
|
+
|
|
133
|
+
while True:
|
|
134
|
+
viewport.engine.width = page.width or 800
|
|
135
|
+
viewport.engine.height = page.height or 600
|
|
136
|
+
viewport.render_frame()
|
|
137
|
+
page.update()
|
|
138
|
+
await asyncio.sleep(1 / 60)
|
|
139
|
+
|
|
140
|
+
if __name__ == "__main__":
|
|
141
|
+
ft.run(main)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 2. 2D Vector Scene & Rigid-Body Physics Quickstart
|
|
145
|
+
```python
|
|
146
|
+
import asyncio
|
|
147
|
+
import flet as ft
|
|
148
|
+
from flet_3d import SceneBuilder2D, Viewport2D
|
|
149
|
+
from flet_3d.physics import PhysicsWorld2D
|
|
150
|
+
|
|
151
|
+
async def main(page: ft.Page):
|
|
152
|
+
page.title = "Flet 2D Physics Quickstart"
|
|
153
|
+
page.bgcolor = "#020617"
|
|
154
|
+
page.padding = 0
|
|
155
|
+
|
|
156
|
+
builder = SceneBuilder2D()
|
|
157
|
+
builder.add_rect(0.0, -240.0, 600.0, 24.0, (51, 65, 85), z_layer=0, obj_id=0) # Static floor
|
|
158
|
+
builder.add_polygon(-60.0, 140.0, 42.0, sides=6, color=(56, 189, 248), z_layer=1, obj_id=1)
|
|
159
|
+
builder.add_polygon(60.0, 210.0, 38.0, sides=5, color=(244, 114, 182), z_layer=2, obj_id=2)
|
|
160
|
+
|
|
161
|
+
vertices, faces, colors, z_layers, object_ids = builder.build()
|
|
162
|
+
physics = PhysicsWorld2D.from_buffers(vertices, faces, object_ids, static_ids={0})
|
|
163
|
+
|
|
164
|
+
viewport = Viewport2D(vertices, faces, colors, z_layers)
|
|
165
|
+
page.add(viewport)
|
|
166
|
+
|
|
167
|
+
while True:
|
|
168
|
+
viewport.engine.width = page.width or 800
|
|
169
|
+
viewport.engine.height = page.height or 600
|
|
170
|
+
physics.step(1 / 60, vertices)
|
|
171
|
+
viewport.render_frame()
|
|
172
|
+
page.update()
|
|
173
|
+
await asyncio.sleep(1 / 60)
|
|
174
|
+
|
|
175
|
+
if __name__ == "__main__":
|
|
176
|
+
ft.run(main)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Controls & Shortcuts
|
|
182
|
+
|
|
183
|
+
| Mode | Action | Input |
|
|
184
|
+
| :--- | :--- | :--- |
|
|
185
|
+
| **3D Camera** | Move / Strafe / Elevate | `W A S D` + `Q / E` (`Shift` to sprint) |
|
|
186
|
+
| **3D Camera** | Look / Orbit | Click & Drag (or Touch Drag / `ft.Accelerometer` Tilt on Mobile) |
|
|
187
|
+
| **3D / 2D** | Select / Pick Object | Single Click / Tap |
|
|
188
|
+
| **2D Camera** | Pan / Zoom-to-Cursor | Drag Empty Space / Mouse Wheel |
|
|
189
|
+
| **2D Camera** | Move / Rotate View | `W A S D` / `Q / E` |
|
|
190
|
+
| **Debug** | Toggle Wireframe / Solid / Normals | `1` / `2` / `3` |
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
Licensed under the **Apache License 2.0**. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
flet_3d.egg-info/PKG-INFO
|
|
4
|
+
flet_3d.egg-info/SOURCES.txt
|
|
5
|
+
flet_3d.egg-info/dependency_links.txt
|
|
6
|
+
flet_3d.egg-info/requires.txt
|
|
7
|
+
flet_3d.egg-info/top_level.txt
|
|
8
|
+
src/__init__.py
|
|
9
|
+
src/camera.py
|
|
10
|
+
src/engine.py
|
|
11
|
+
src/loaders.py
|
|
12
|
+
src/physics.py
|
|
13
|
+
src/svg_loader.py
|
|
14
|
+
src/viewport.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
flet_3d
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "flet-3d"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A vectorized 3D software rendering engine for Flet v1.0 powered by NumPy and Flet Canvas."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "Osama Mohammed", email = "osamalzabidi@gmail.com" }
|
|
11
|
+
]
|
|
12
|
+
keywords = [
|
|
13
|
+
"flet",
|
|
14
|
+
"3d",
|
|
15
|
+
"3d-engine",
|
|
16
|
+
"software-renderer",
|
|
17
|
+
"numpy",
|
|
18
|
+
"canvas",
|
|
19
|
+
"graphics",
|
|
20
|
+
"obj-loader"
|
|
21
|
+
]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 4 - Beta",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Programming Language :: Python :: 3.14",
|
|
29
|
+
"Topic :: Multimedia :: Graphics :: 3D Rendering",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
dependencies = [
|
|
33
|
+
"flet>=1.1",
|
|
34
|
+
"numpy>=1.26.0",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/osamalzabidi/flet-3d"
|
|
39
|
+
Repository = "https://github.com/osamalzabidi/flet-3d"
|
|
40
|
+
Issues = "https://github.com/osamalzabidi/flet-3d/issues"
|
|
41
|
+
|
|
42
|
+
[tool.setuptools]
|
|
43
|
+
packages = ["flet_3d"]
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.package-dir]
|
|
46
|
+
flet_3d = "src"
|
|
47
|
+
|
|
48
|
+
[build-system]
|
|
49
|
+
requires = ["setuptools>=77.0.0"]
|
|
50
|
+
build-backend = "setuptools.build_meta"
|
flet_3d-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
from .camera import Camera2D, Camera6DOF
|
|
5
|
+
from .engine import RenderStats2D, RenderStats3D, VectorizedEngine2D, VectorizedEngine3D
|
|
6
|
+
from .loaders import SceneBuilder2D
|
|
7
|
+
from .viewport import Viewport2D, Viewport3D
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"Camera2D",
|
|
11
|
+
"Camera6DOF",
|
|
12
|
+
"RenderStats2D",
|
|
13
|
+
"RenderStats3D",
|
|
14
|
+
"VectorizedEngine3D",
|
|
15
|
+
"VectorizedEngine2D",
|
|
16
|
+
"SceneBuilder2D",
|
|
17
|
+
"Viewport2D",
|
|
18
|
+
"Viewport3D",
|
|
19
|
+
]
|