A threejs-like render engine based on wgpu
Project description
pygfx
A render engine, inspired by ThreeJS, but for Python and targeting Vulkan/Metal/DX12 (via wgpu).
Introduction
This is a Python render engine build on top of WGPU (instead of OpenGL).
We take a lot of inspiration from ThreeJS, e.g.:
- Materials and Geometry are combined in world objects.
- No event system, but controls that make it relatively easy to integrate with one.
- Decoupled cameras and controls.
- The code for the render engines is decoupled from the objects, allowing multiple render engines (e.g. wgpu and svg).
Further we aim for a few niceties:
- Proper support for high-res screens.
- Builtin anti-aliasing.
- Custom post-processing steps.
- Support for picking objects and parts within objects.
- (approximate) order-independent transparency (OIT) (not implemented yet).
WGPU is awesome (but also very new)
Working with the WGPU API feels so much nicer than OpenGL. It's well defined, no global state, we can use compute shaders, use storage buffers (random access), etc.
Fair enough, the WGPU API is very new and is still changing a lot, but eventually it will become stable. One of the biggest downsides right now is the lack of software rendering. No luck trying to run wgpu on a VM or CI.
Because of how Vulkan et. al. work, the WGPU API is aimed at predefining objects and pipelines and then executing these. Almost everything is "prepared". The main reasoning for this is consistency and stable drivers, but it also has a big advantage for us Pythoneers: the amount of code per-draw-per-object is very limited. This means we can have a lot of objects and still be fast.
As an example, see collections_line.py
: drawing 1000 line objects with 30k points each at 57 FPS (on my laptop).
How to build a visialization
See also the examples, they all do something like this:
- Instantiate a renderer and a canvas to render to.
- Create a scene and populate it with world objects.
- Create a camera (and maybe a control).
- Define an animate function that calls:
renderer.render(scene, camera)
On world objects, materials, and geometry
There are a few different world object classes. The class defines
(semantically) the kind of object being drawn, e.g. Line
, Image
,
Mesh
, Volume
. World objects have a position and orientation in the
scene, and can have children (other world objects), creating a tree.
World objects can also have a geometry and/or a material.
The geometry of an object defines its base data, usually per-vertex attributes such as positions, normals, and texture coordinates. There are several pre-defined geometries, most of which simply define certain 3D shapes.
The material of an object defines how an object is rendered. Usually each WorldObject class has one or more materials associated with it. E.g. a line can be drawn solid, segmented or with arrows. A volume can be rendered as a slice, MIP, or something else.
Installation
pip install -U pygfx
Or, to get the latest from GitHub:
pip install -U https://github.com/pygfx/pygfx/archive/main.zip
Current status
Under development, many things can change.
Example testing
Include the comment # test_example = true
in an example to have pytest run it as part of the test suite.
To support testing an example, ensure the following requirements are met:
- The
WgpuCanvas
class is imported from thewgpu.gui.auto
module. - The
canvas
instance is exposed as a global in the module.
By default the test will simply verify that the example can be executed without an error, and that an image can be rendered using the canvas.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pygfx-0.1.7.tar.gz
.
File metadata
- Download URL: pygfx-0.1.7.tar.gz
- Upload date:
- Size: 131.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c444daf2ec3252a9663ae77017fb1627c8a4d36d50735da1c7426e82a6d1673 |
|
MD5 | 596b3ae6e63f2242e4ba79fe0ec63532 |
|
BLAKE2b-256 | e3a47b1a8ce332d055ffd2f3e0e5f7f20cf5fb7692f8d68af22fe5041eb76c5d |
File details
Details for the file pygfx-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: pygfx-0.1.7-py3-none-any.whl
- Upload date:
- Size: 161.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33103b77c02f15d59cec53c1b371cceb1a457f0fce50cda864b08d24f8e23ed8 |
|
MD5 | 355f30e2973c705cab26b8a470f2f682 |
|
BLAKE2b-256 | 0d335b245e9b56f9569ef4e4253a4eeba381a814f35b507cb563e21264e34b01 |