Custom Render Pipeline OOP - ZackWilde27/Z3dPy GitHub Wiki
Custom Render Pipeline
OOP Version
This page goes over how to code the render pipeline. An explanation of what each stage does can be found on the Rasterization page
The typical pipeline is easy to use, and by using RasterThing() and RasterMesh() you can sort-of split up the pipeline, but if you wanted to get under the hood, it doesn't offer much.
for tri in z3dpy.RasterThings(myList, myCamera):
z3dpy.DrawTriangleS(tri, screen, (tri.normal.z + -tri.normal.y) / 2, pygame)
Instead of rastering all the triangles at once, we can split it up into the various stages:
transformed = []
translated = []
viewed = []
projected = []
# Set the internal camera before we start
z3dpy.SetInternalCamera(myCamera)
for thing in myList:
# Code that runs for each thing
for mesh in thing.meshes
# Code that runs for each mesh
transformed = z3dpy.TransformTriangles(mesh.tris, mesh.rot)
for t in transformed:
# Code that runs for each transformed triangle
translated = z3dpy.TranslateTriangles(transformed, mesh.pos)
for r in translated:
# Code that runs for each translated triangle
for i in z3dpy.ViewTriangles(translated):
# Code that runs for each viewed triangle
viewed.append(i)
projected = z3dpy.ProjectTriangles(viewed)
for s in projected:
# Code that runs for each projected triangle
# Draw the projected triangle to the screen, placing the light direction in the middle of Z and -Y
z3dpy.DrawTriangleS(tri, screen, (tri.normal.z + -tri.normal.y) / 2, pygame)
The built-in raster functions do a dot product check, clips triangles, as well as sorts by depth. The custom render pipeline version looks like this:
transformed = []
translated = []
viewed = []
projected = []
# Set the internal camera before we start
z3dpy.SetInternalCamera(myCamera)
for thing in myList:
for mesh in thing.meshes
viewed = []
for t in z3dpy.ViewTriangles(z3dpy.TranslateTriangles(z3dpy.TransformTriangles(mesh.tris, mesh.rot), mesh.pos)):
if z3dpy.VectorDoP(tri.normal, myCamera.target) < 0.1:
for r in TriangleClipAgainstZ(t):
viewed.append(r)
projected = z3dpy.ProjectTriangles(viewed)
projected.sort(key = z3dpy.triSort)
for i in projected:
for s in TriangleClipAgainstScreenEdges(i):
# Here's where the raster function would output the triangles
# Draw the projected triangle to the screen, placing the light direction in the middle of Z and -Y
z3dpy.DrawTriangleS(tri, screen, (tri.normal.z + -tri.normal.y) / 2, pygame)