Lights 0.4 - ZackWilde27/Z3dPy GitHub Wiki
z3dpy.Light( iType, vPosition, FStrength, fRadius, *vColour )
myLight = z3dpy.Light( z3dpy.LIGHT_POINT, [12.0, 45.0, 23.52], 1.0, 50.0, (255, 0, 0) )
# When defining a sun light, vPosition is vRotation
mySunLight = z3dpy.Light( z3dpy.LIGHT_SUN, [45, 10, 0], 1.0, 50.0, (255, 0, 0) )
Properties
-
type
-
positionWhen it's a LIGHT_POINT, this is the position, when it's a LIGHT_SUN, this is the direction
-
strength
-
radius
-
colour
All light types are now the same light class with different type numbers.
Material/Shaders
The easiest method would be to set the mesh's material to MATERIAL_DYNAMIC, and add the lights to the z3dpy.lights list.
z3dpy.lights.append(myLight)
myMesh.material = z3dpy.MATERIAL_DYNAMIC
# A peek at MATERIAL_DYNAMIC:
MATERIAL_DYNAMIC = Material(world = SHADER_DYNAMIC)
# A peek at SHADER_DYNAMIC:
def SHADER_DYNAMIC(tri):
return VectorMul(tri[6], CheapLighting(tri))
To do these lighting calculations in your own shader, use CheapLighting() or ExpensiveLighting()
# There are two functions to get the lighting colour of a tri. CheapLighting(tri, *lights), and ExpensiveLighting(tri, *lights, *thingsThatWillCastShadows)
def MyShader(tri):
# These functions return a normalized colour and do not factor in the colour of the triangle
# you'll have to VectorMul() with the input colour to get the final colour.
return z3dpy.VectorMul(z3dpy.TriGetColour(tri), z3dpy.CheapLighting(tri))
# or
# ExpensiveLighting will shoot a ray towards the light source and check for collisions to create shadows.
# It's flat-lit per-triangle though so the results will depend on the density of triangles.
return z3dpy.VectorMul(z3dpy.TriGetColour(tri), z3dpy.ExpensiveLighting(tri))
worldColour
worldColour is the ambient lighting, used to give colour to the dark areas
# It's normalized, so white is (1.0, 1.0, 1.0)
z3dpy.worldColour = (0.1, 0.2, 0.3)
worldColour = (0.0, 0.0, 0.0)
worldColour = (0.15, 0.15, 0.2)
The brighter the worldColour is, the more detail will be lost (note the shadows on the bird are much more detailed in the previous images)
Baked Lighting
If an object is not going to move, it's lighting can be saved at the start and re-used cheaply. To do this call BakeLighting() after defining the lights and things.
myThing = z3dpy.Thing([myMesh], [0, 0, 0])
myLight = z3dpy.Light(z3dpy.LIGHT_POINT, [0, -5, 0], 1.0, 50.0, (255, 0, 0))
# BakeLighting(things, *bExpensive, *lights, *shadowCastersIfExpensive)
z3dpy.BakeLighting([myThing], False, [myLight])
Then the baked shade will show up in MATERIAL_STATIC
myMesh.material = z3dpy.MATERIAL_STATIC
# A peek at MATERIAL_STATIC:
MATERIAL_STATIC = Material(view = SHADER_STATIC)
# A peek at SHADER_STATIC:
def SHADER_STATIC(tri):
return VectorMul(tri[6], tri[5])
# Then, during the draw loop...
for tri in z3dpy.RenderThings([myThing]):
colour = z3dpy.TriGetColour(tri)