Mibi’s Website

Article — Simple 3D rendering in Python

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 y 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  x  axis
y ' = y × cos θ z × sin θ and z ' = y × sin θ + z × cos θ . x ' = x because we’re rotating around the x axis
Around the  y  axis
z ' = z × cos θ x × sin θ and x ' = z × sin θ + x × cos θ . y ' = y because we’re rotating around the y axis
Around the  z  axis
x ' = x × cos θ y × sin θ and y ' = x × sin θ + y × cos θ . z ' = z because we’re rotating around the z 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:
First I calculate the focal length, to use the formula from Wikipedia (https://en.wikipedia.org/wiki/3D_projection#Diagram):
f = w 2 × tan θ 2 with θ being the field of view (FOV for short) and w the width of the viewport.
Note: Pyxel doesn’t provide a tan function so I just used sin θ cos θ .
Then I applied Wikipedia’s formula to calculate the X and Y coordinates: x ' = x × f z and y ' = y × f z .
Note: make sure that z is non-zero, or you will get a ZeroDivisionError.

Conclusion

All those formulas are all you should need to render very basic 3D.
You can check the code of my little 3D demo here: https://github.com/mibi88/3d_pyxel/.
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.