I discovered Panda3d recently and it’s a great way to dive in 3D programming.
I need to do some simple shadow projection for a school project about initiation in astronomy. For this I tried a point light to simulate the sunlight but as it didn’t cast any shadows i then opted for a Spotlight (is there a way to cast shadows from a point light ?)
While it casts shadows now I don’t understand why it seems the shadows appear misaligned as if the spot was shifted from the showed frustrum ?!?
see this picture for an illustration of my problem:
my code is at
I hope it is some beginner error and not a strange bug as I need shadows to behave a little more realistically for simulating eclipses ;p
Thanks in advance for any thoughts !
this shift seems related to the FoV of the spot lense as it diminishes if I reduce the fov.
unfortunately I need a large FoV to send light on both the earth and the moon…
Maybe you already know this:
In Panda3D (and most realtime 3D-engines) there is no physically exact lighting/shadowing but tricks are used instead to make it look good.
In Panda3D shadowing is done by rendering the scene from the light’s view (lights are actually cameras) and everything the camera sees is lit.
There are several options what you could do to optimize the scene:
You should set the near and far clip planes of the light so that only the objects that you want to cast/recieve shadows fit in tight. (setNearFar)
Maybe a directional light would fit this case better as the distance sun-earth is really big compared to earth-radius. (there you would need to setFilmSize)
You can setShadowCaster to a very high value (best is power of two like 2048).
showFrustum() on your light is very helpful to see where your light shines.
Thanks for the answer, indeed I noticed it was about tricks and i reckon a directionnal light would be better but I can’t manage to set it to cast shadows. moreover I have a VERY strange behavior with such a light as the lightning moves with my camera !!
I really don’t get it as the ligth is defined as follow and as I update its rotation with a task repeating self.light.lookAt(self.earth)
#the light on the earth system
self.light = render.attachNewNode(DirectionalLight("SunLight"))
self.light.node().setShadowCaster(True, 2048, 2048)
# a mask to define objects unaffected by light
Ok so I found out that in order to cast shadows with directionnal light I must add a pointless spotlight. I just added one pointing up and the directionnal light casts shadows now but too many of them :
the moon is now blinking ?!? as if it received shadows from invisible stripes blocking light.
The code is at dl.free.fr/pOY1Xg9Gs if your motivated enough to help me out.
I feel a little lost
It is difficult to get shadowing working correctly with a directional light. It is therefore recommended to use a spotlight instead. You do not need a spotlight for the directional light to work, though. Adding a spot light won’t make the directional light work better, instead, you’ll just be seeing the spot light’s shadows too.
In order to make a directional light work, you need to use showFrustum() to show the frustum, find the frustum, and then tweak the near-far clip and the film size until the frustum completely envelops your caster and surface to cast upon. You shouldn’t attempt to do it blindly, that will just cause you to pull your hair out .
Thanks for answering and I’m really enjoying the discovery of real-time 3D programming with Panda !
I know spotlights are recommended but I couldn’t figure out how to cast correctly aligned shadows with a spotlight (see the first post for an illustration).
About the curious blinking shadow artifact I managed to make it disappear by setting the filmsize and the near-far clip of the directional lamp as small as possible. This seems to work ! Maybe I could add a comment in the documentation to inform beginners of these potential artifacts and about the importance of setting the parameters to a close fit ?
Anyway I’m still curious about the misalignment happening with the spotlight…if you have a suggestion…
And rdb, I assure you that the simple fact of adding a spotlight makes my directional lamp stop following the camera and start casting shadows. And this is reversible !
So it might be a little bug…