Materials - ZackWilde27/Z3dPy GitHub Wiki

z3dpy.Material( *local, *world, *view )

myMaterial = z3dpy.Material(world = z3dpy.SHADER_DYNAMIC)

Properties

  • localThe shader performed before the transform stage

  • worldThe shader performed after the transform stage

  • viewThe shader performed after the view stage

Materials determine how the triangles of a Mesh are coloured, so the colour can be passed un-altered into the draw function, and the material can be swapped out however.

myMesh.material = z3dpy.MATERIAL_SIMPLE

def ThatFunction():
    myMesh.material = z3dpy.MATERIAL_DYNAMIC

# Then during the draw loop...
    for tri in z3dpy.RenderMeshList([myMesh]):
        # The colour is pre-set based on the material of the mesh it came from
        pygame.draw.polygon(surface, z3dpy.TriGetColour(tri), z3dpy.FormatTri(tri, False))

Built-in Options:

MATERIAL_UNLIT

The colour of the triangle is untouched.

image

MATERIAL_SIMPLE

The most simple form of shading, isolating one of the triangle's normal's channels.

image

MATERIAL_DYNAMIC or MATERIAL_STATIC

MATERIAL_DYNAMIC does lighting calculations per-frame, MATERIAL_STATIC re-uses the pre-baked shade.

image

Custom Materials

A shader is a function that takes a tri and returns a colour for it

For example, a shader that returns the normal of a triangle as a colour.

def MyShader(tri):
    colour = z3dpy.VectorMulF(z3dpy.GetNormal(tri), -255)
    return z3dpy.VectorMaxF(colour, 0)

Putting this shader in either local, world, or view will give different results.

# You can either pick a stage or make it multi-stage
myMaterial = z3dpy.Material(world = MyShader)

https://github.com/ZackWilde27/Z3dPy/assets/115175938/48ba9820-aefe-4653-aefc-8abb1358baa7

I've placed the camera to look down the X axis, so that the difference between world and view is clear.