Sample: using directional lights+shadows effectively


Using directional lights with shadowing can often be a stumbling block for Panda users. Fitting the frustum around the scene properly can be tricky. So here is a little sample program that shows how to use directional lights effectively.

It includes a function that will tightly fit the frustum around the scene, for static, smaller scenes (press 1 to use it automatically). This is great for smaller scenes or where the light is static and there is no time-of-day, because it is slow to compute this and shouldn’t be used at runtime for more complex scenes. It’s delightfully simple, though:

    bmin, bmax = scene.get_tight_bounds(sun_path)
    lens = sun.get_lens()
    lens.set_film_offset((bmin.xz + bmax.xz) * 0.5)
    lens.set_film_size(bmax.xz - bmin.xz)
    lens.set_near_far(bmin.y, bmax.y)

It also includes a function (press 2) that is meant to dynamically adjust the frustum around both the scene and the area that is currently in view. It is not as tightly fitting as the first method; a much better implementation is warranted, but this may be a good starting point. It will try to make sure that areas you are not looking at are not shadowed, and that the shadow camera is even completely disabled if you are looking away from the scene.

It uses a GLSL shader that uses PBR lighting models to apply the shadows and lighting, though the shader is optional.


 tab: toggle buffer viewer
 f12: save screenshot
 o: enable OOBE mode
 1: enable static adjustment mode
 2: enable dynamic adjustment mode
 3: disable automatic adjustment (1.9 MB)

New Panda, New samples?