A 3D Software renderer in pure PHP.
- Just download/clone and the renderer should just work.
- Has Shader System in PHP.
- Allows Custom vertex attributes with interpolation.
- Supports a Depth Buffer.
- Supports Multiple Output Buffers per context.
- CG Math Library.
- ObJ File Parser.
- Render to a mp4 video (requires ffmpeg)
- No "pure php" cheating with FFI.
Because this question is going to pop up a lot, let me address it here first. This project has been a learning experience for me and hopefully also for others interested in computer graphics with a web dev background. I should not have to say it but this has no "production value" whatsoever, there is a mountain of flaws to name a few:
- This does not run in parallel.
- This runs on the CPU (Software Renderer).
- This wastes insane amounts of memory.
- I tried to optimize things without sacrificing too much code clarity, but this is still very slow.
- I use PHP Arrays as literal pixel buffers which just can't be optimal.
I've been working on a game running in my own engine for a while now. Many of the issues I encountered came from not fully understanding what the rendering API (Vulkan, OpenGL, DirectX) actually does. To counter this I do what I always do, and try to implement the basics of the used library / API / concept myself. This project is the result.
I should also note I'm still nowhere near an expert in the field.
When I started with programming, I often encountered parts of computer science that seemed super interesting and I wanted to learn more about them. But not having examples/tutorials in a language I was familiar with got me overwhelmed fast. This is an issue that fades away after years in the industry but I still remember the frustration. Long story short I hope this project helps someone learning something new.
Everything should be up and running, this renderer is implemented entirely in PHP so there are no special extension requirements or other dependencies.
Composer is used for autoloading so you will have to run a composer install
.
Some examples make use of some assets (3d model, textures etc.) There is script which will download and extract these:
./bin/download-example-resources
The classic color gradient triangle.
$ php examples/01_triangle/triangle.php
This example exists to showcase the interpolation of the barycentric coordinates inside of a drawn triangle.
public function vertex(Vertex $vertex, array &$out) : Vec4
{
$out['color'] = $vertex->color; // set the color attribute
return Vec4::fromVec3($vertex->position); // return vertex positon
}
public function fragment(array &$in, array &$out)
{
$out['color'] = $in['color']->toColorInt();
}
Just a simple cube showcasing 3D geometry with depth testing.
$ php examples/02_cube_basic/cube.php
Showcasing a simple example how to use the ffmpeg stream to create videos with php-render.
$ php examples/03_cube_video/cube.php
This example shows how to load a model from an obj
file.
$ php examples/04_simple_model/model.php
Showcases basic texture sampling of a loaded model.
$ php examples/05_texture_sampling/model.php
Showcases simple phong shading on a 3D Model
$ php examples/10_phong/model.php
The License (AGPLv3). Please see License File for more information.