Simple 3D rendering in Python
Mibi88
05/05/2025 - Updated on the 10/11/2025
Recently I participated at a challenge, where in an evening we had 6 hours to make a game in Python 3 with Pyxel (it sucks). I really wanted to make some 3D because it’s more impressive than a simple pixel art 2D game and I saw that Pyxel has a function to render colored triangles. It also means that I can’t have any depth buffer (and I don’t know the formula for perspective correct interpolation so I would not have been able to do 3D rendering with a depth buffer anyway). I just sorted the triangles by depth (by calculating the average of the depth of the three points, maybe there is a better way to do it, but I don’t know any) with some selection sort (maybe insertion sort may have been better, or maybe it isn’t because it doesn’t always take the same time so maybe the framerate would have been less consistent). Some formulas are also required to render 3D (I didn’t used matrices because I haven’t seen them at school yet: I’m still very young, and the little I know about them, because I use matrices in MibiEngine2 would not have been enough). So I used some formulas I found on the web a long time ago (it isn’t online anymore but it got archived by the Wayback Machine:
https://web.archive.org/web/20220625104937/https://www.mfitzp.com/creating-a-3d-rotating-cube-with-micropython-and-oled-display/ EDIT: Actually it is still online, the URL has just changed: https://www.martinfitzpatrick.dev/creating-a-3d-rotating-cube-with-micropython-and-oled-display/), but the formula for projection isn’t correct and I didn’t implemented one of the rotation formulas correctly: the rotation around the
axis. I really hate AIs because they’re crawling forges so intensively and require a lot of energy, but ChatGPT was really helpful here to get the formulas I needed. I also found one of them on Wikipedia for projection. I’ll share the formulas here because I found them very hard to find, and I may not be the only one that finds them useful.
Rotation
- Around the
axis
-
and
.
because we’re rotating around the
axis
- Around the
axis
-
and
.
because we’re rotating around the
axis
- Around the
axis
-
and
.
because we’re rotating around the
axis
Projection
One of the big advantages of using a projection matrix is having a near and a far plane, but I only knew a very approximate formula without it… Today I fixed it. Here is the formula I’m using now:
with
being the field of view (FOV for short) and
the width of the viewport.
Note: Pyxel doesn’t provide a
function so I just used
.
Then I applied Wikipedia’s formula to calculate the X and Y coordinates:
and
.
Note: make sure that
is non-zero, or you will get a ZeroDivisionError.
Conclusion
All those formulas are all you should need to render very basic 3D.
I hope it can help you on your journey making a very basic software renderer :).
Edits from the 10/11/2025
Fixed an orthography error, changed URLs to hyperlinks and added a link to the article’s new location.