Shadow Module

Filed under: Scripts 
Tags: ,

Use the shadow module to create dynamically projected shadows within Vizard. This post describes how to use the module and includes a short video and sample script that shows an avatar walking around with a shadow.

The shadow module works well for creating shadows as long as the shadow casting objects are contained in a small area. With the default resolution you'll get sharp shadows in a space that is 5 meters by 5 meters. If you would like to use this with larger areas you have a few options. By leaving the resolution as it is, you can increase the area covered with some loss in the smoothness of your shadows. By increasing the resolution along with the area you will be able to have sharper shadows in these larger areas, however more texture memory will be used.

To have a shadow follow a single moving object, for example a walking avatar over large distances, you can have the shadow projector follow the avatar's position. That way you can maintain a high resolution shadow for your avatar anywhere in your environment without the higher cost in texture memory.

Download the shadow module and an example of how to use it.

The following code is the same as the example included with the shadow module above with the addition of a projector following a walking avatar.

import viz
viz.go()

#add sky
env = viz.add(viz.ENVIRONMENT_MAP,'sky.jpg')
sky = viz.add('skydome.dlc')
sky.texture(env)

#Add ground model
ground = viz.add('tut_ground.wrl')

#add avatar
avatar = viz.add('vcc_female.cfg', pos = [-3,0,5], euler = 180)

#create walking sequence and add to avatar
walk1 = vizact.walkTo([3,0,5])
walk2 = vizact.walkTo([3,0,2.5])
walk3 = vizact.walkTo([-3,0,2.5])
walk4 = vizact.walkTo([-3,0,5])
walk_sequence = vizact.sequence([walk1,walk2,walk3,walk4], viz.FOREVER)

avatar.addAction(walk_sequence)

#move view back a bit
viz.MainView.move([0,0,-6])

#Shadow resolution (power of two)
#Higher values mean sharper shadows, but take more texture memory
SHADOW_RES = 256

#Postion of shadow projector
SHADOW_POS = [-3,10,5]

#Controls size of orthographic shadow projector
#Large values mean larger area is covered, but resolution will be diluted
SHADOW_AREA = [5,5]

#Create shadow projector
import Shadow
shadow = Shadow.ShadowProjector(size=SHADOW_RES,pos=SHADOW_POS,area=SHADOW_AREA)

#add avatar as shadow caster
shadow.addCaster(avatar)

#Add ground as shadow receiver
shadow.addReceiver(ground)

#keep shadow projector above avatar
def setShadowPos():
   
    pos = avatar.getPosition()
    shadow.setPosition([pos[0],10,pos[2]])
   
vizact.ontimer(0, setShadowPos)