Meshes - ZackWilde27/Z3dPy GitHub Wiki

z3dpy.Mesh(triList, vPos)

myMesh = z3dpy.Mesh((thatTri, theOtherTri), [1.0, 2.0, 3.0])

Meshes contain all the triangles that make up an object, as well as storing some extra information, like colour.

LoadMesh

To load a mesh from an OBJ file, use LoadMesh()

# LoadMesh(filename, *vPos, *VScale)
myMesh = z3dpy.LoadMesh("directory-to-obj.obj", [0.0, 0.0, 0.0], [1.0, 1.0, 1.0])
  • N-gons are triangulated automatically
  • UVs are supported, kinda.

Colour

Each mesh has it's own colour. To make objects with multiple colours, separate them into pieces to be coloured separately.

z3dpy.MeshSetColour(myMesh, [255, 0, 0])

# then...

for tri in z3dpy.RasterMeshList([myMesh]):
    print(z3dpy.TriGetColour(tri))

    # DrawTriS() will use this colour to "shade" a triangle
    z3dpy.PgDrawTriS(tri, z3dpy.TriGetNormal(tri)[2], screen, pygame)

If materials are exported with your OBJ, LoadMesh() will separate each material into it's own mesh, returning a list.

image

myMeshList = z3dpy.LoadMesh("dir-to-obj-with-mat.obj")
# LoadMesh automatically colours the meshes based on the mtl file

myThing = z3dpy.Thing(myMeshList, [0, 0, 0])

Id

Meshes also have their own id, which is copied to the triangles, meant for shader setups.

When drawing Things, each mesh's id is used, not the Thing's id.

z3dpy.MeshSetId(myMesh, 2)

# Then...

for tri in z3dpy.RasterMeshList([myMesh]):
    print(z3dpy.TriGetId(tri))

    match z3dpy.TriGetId(tri):
        case 0:
            # Dynamic Flat Lighting
            z3dpy.TkDrawTriFL(tri, canvas)
        case 1:
            # Simple Z shading
            z3dpy.TkDrawTriF(tri, zp.TriGetNormal(tri)[2], canvas)
        case 2:
            # Baked lighting
            z3dpy.TkDrawTriFLB(tri, canvas)