day/ night cycle

Yeah with SpotLight it works here too. I just replaced DirectionalLight with SpotLight, so you had to tweak the parameters. But the general shadow effect is there.

What methods do you use to tweak the spotlight parameters? Can’t find much in the manual. mediafire.com/?okzzzmmdo2z

Well this is kinda annoying, mediafire removed the download and I didn’t have the latest version in my hd. Let this be a lesson for me to do backups.

So I kinda started over (not really).
I added a moving avatar and camera, but the code quickly became ‘bloated’.

Finally all the sky is camera relative and is rendered behind everything else.
Sun movement is a simple interval.

But when I added the directional light, now it didn’t work for me as well… this is kinda weird, I’ll try to find out whats wrong.

If someone is still interested, here is the camera relative day-night sky code: youshare.com/Guest/e90e99a1dd691b7e.zip.html
But there is no terrain, I’m trying to fix the lightning now. Default panda mouse controls.

Is this code still available somewhere? I am doing a day night cycle too and am having some difficulty with the shadows.

Thanks,
Daniel.

Hm, maybe I have it, I’ll have a look. It will probably be outdated, but…

I removed the shadows because everyone had problems with the Directional Light setup.

The latest code was something like this:

from panda3d.core import *
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.actor import Actor
from direct.interval.IntervalGlobal import *


class World(DirectObject):
	def __init__(self):
		
		# Directiona lLight
		self.dlight = render.attachNewNode(DirectionalLight("Spot"))
		self.dlight.setPos(0,0,150)
		self.dlight.lookAt(0,0,0)
		render.setLight(self.dlight)
		
		self.dlight.node().setShadowCaster(True, 4096, 4096)
		self.dlight.node().showFrustum()
		self.dlight.node().getLens().setNearFar(40,400)
		self.dlight.node().getLens().setFilmSize(400, 400)
		
		self.lightInterval = LerpHprInterval(self.dlight, 24, Vec3(0,360,0) , startHpr= Vec3(0,0,0))
		self.lightInterval.loop()
		self.dlight.setHpr(0,-120,0)
		
		# Ambient Light
		self.alight = render.attachNewNode(AmbientLight("Ambient"))
		self.alight.node().setColor(Vec4(0.4, 0.4, 0.4, 1))
		render.setLight(self.alight)
		
		# Important! Enable the shader generator.
		render.setShaderAuto()
		
		
		# Load the scene.
		self.environ =loader.loadModel('environment')
		self.environ.setScale(0.4)
		self.environ.setPos(0,50,0)
		self.environ.reparentTo(render)
		self.environ.setDepthOffset(1) # fixes self shadowing?
		
		# animated model
		self.pandaModel=Actor.Actor('panda-model',{'walk':'panda-walk4'})
		self.pandaModel.reparentTo(render)
		self.pandaModel.setPos(0,0,0)
		self.pandaModel.setScale(0.01)
		self.pandaModel.setDepthOffset(1) # fixes self shadowing?
		
		self.pandaWalk = self.pandaModel.actorInterval('walk',playRate=1.8)
		self.pandaWalk.loop()
		
		self.pandaInterval = LerpPosInterval(self.pandaModel, 4, Vec3(10,0,0) , startPos= Vec3(0,0,0))
		self.pandaInterval.loop()
		
		taskMgr.add( self.lightPositionTask, "lightPositionTask")
		
	# for a huge world the light should follow the player (for shadowing purposes, position for dlight doesn't really matter, just the rotation)
	def lightPositionTask(self, task):
		
		self.dlight.setX(self.pandaModel.getX())
		self.dlight.setY(self.pandaModel.getY())
		return task.cont
  
World()
run()

As you can see there are problems, the biggest is, if you comment the line

self.lightInterval.loop()

.Notice the shadow ‘shaking’?
I was going to ask about this in the forums.

Your code is very similar to mine except I was using a spotlight. This video shows the problem I am having with the shadows:

http://www.youtube.com/watch?v=tJjDfbBgBQM (looks best in (480p)

My problem could just be the type of light, lenseFov, NearFar, FilmSize, etc so here is that info:

slight = Spotlight('slight')
slight.setColor(VBase4(1, 1, 1, 1))
slight.setShadowCaster(True, 1024, 1024)
lens = PerspectiveLens()
slight.setLens(lens)
slight.getLens().setFov(40)
slight.getLens().setNearFar(10,500)

This light is about 500 units above the ground and rotates around (0,0,0) always looking at (0,0,0)

I appreciate would appreciate any advice or tips on resolving this behavior.

  • Daniel.

so… the light is actually rotating around the terrain?
I don’t think thats such a good idea. I rotate the directional light around itself - position for directional light doesn’t matter. This is just a wild guess but maybe that happens when the light is BELOW the terrain?

And if it is 500 units far from its center, shouldn’t you make it’s Far value a bit higher than 500?

When the light goes below the terrain it makes the scene completely black (except for the ambient light). The crazy lines appear whenever the sun is above the horizon and as it is moving across the sky. I have tried using both the directional light method you used and the spotlight. The directional light gave me the same effect as when I had shadows turned off while using the spot light:(

I tried adjusting my far value but it did not make any changes.

Thanks,
Daniel.

You didn’t say if my code gives the same results

Ok, your code did not give me any shadows at first. So I made a few modifications to use a Spotlight and my sun rotation method. Here is your code which I butchered to get shadows working on my PC.

from panda3d.core import * 
import direct.directbase.DirectStart 
from direct.showbase.DirectObject import DirectObject 
from direct.actor import Actor 
from direct.interval.IntervalGlobal import * 


class World(DirectObject): 
   def __init__(self): 
       
      # node for the sun to rotate around and look at.
      self.centerOfWorld = NodePath("centerOfTheWorld")
      self.centerOfWorld.reparentTo(render)
      self.centerOfWorld.setPos(0,0,-10)
       
      # make the center of the world spin
      worldRotationInterval = self.centerOfWorld.hprInterval(24, Point3(0,-180,0), startHpr=Point3(0,180,0))
      spinWorld = Sequence(worldRotationInterval, name="spinWorld")
      spinWorld.loop()
      
      # Directiona lLight 
      self.dlight = render.attachNewNode(Spotlight("Spot")) 
      self.dlight.setPos(0,0,800) 
      self.dlight.lookAt(self.centerOfWorld) 
      render.setLight(self.dlight) 
      self.dlight.reparentTo(self.centerOfWorld)
       
      self.dlight.node().setShadowCaster(True, 2048, 2048) 
      self.dlight.node().showFrustum() 
      self.dlight.node().getLens().setNearFar(80,800) 
      self.dlight.node().getLens().setFilmSize(800, 800) 
       
      # Ambient Light 
      self.alight = render.attachNewNode(AmbientLight("Ambient")) 
      self.alight.node().setColor(Vec4(0.4, 0.4, 0.4, 1)) 
      render.setLight(self.alight) 
       
      # Important! Enable the shader generator. 
      render.setShaderAuto() 
       
       
      # Load the scene. 
      self.environ =loader.loadModel('environment') 
      self.environ.setScale(0.4) 
      self.environ.setPos(0,50,0) 
      self.environ.reparentTo(render) 
      #self.environ.setDepthOffset(1) # fixes self shadowing? 
       
      # animated model 
      self.pandaModel=Actor.Actor('panda-model',{'walk':'panda-walk4'}) 
      self.pandaModel.reparentTo(render) 
      self.pandaModel.setPos(0,0,0) 
      self.pandaModel.setScale(0.01) 
      self.pandaModel.setDepthOffset(1) # fixes self shadowing? 
       
      self.pandaWalk = self.pandaModel.actorInterval('walk',playRate=1.8) 
      self.pandaWalk.loop() 
       
      self.pandaInterval = LerpPosInterval(self.pandaModel, 4, Vec3(10,0,0) , startPos= Vec3(0,0,0)) 
      self.pandaInterval.loop() 

World() 
run()

When this code worked as I expected I was thinking there was something wrong with the egg I used in my program. Turns out the issue was: (drum roll)

… setTwoSided(True)

Want to see my error? Add this line:

      self.environ.setTwoSided(True)

I am sorry for the wild goose chase and appreciate all of your help!

  • Daniel.

PS: Incase anyone searches for this, “setShadowCaster does work well with setTwoSided(True)”

About Directional Lights: I triple checked again, it works, so maybe its time I reported this as a Panda bug.

BTW I noticed that with my rotating light method, when the light goes below the ground and is pointing at the underside of the terrain my FPS goes from 60 (vSync) to 30. This never happened with setTwoSided but shadows didnt work with that option.

Anyways, just wanted let you know that a spot light pointing at the back side of a mesh causes some strange FPS tax.

It really just means your fps went from just over 60 fps to just under 60 fps. (With vsync enabled, anything under 60 fps must drop all the way down to 30 fps.)

As to what, specifically, caused the frame rate to drop below 60 fps, it could be anything. If I had to guess, I’d say it’s probably sending more geometry to the shadow pass or something like that.

David

drwr:
That is strange because I have seen the FPS anywhere between 60 and 5. This is the first instance where the FPS is cut in half instantly, and its as soon as the sun (Spotlight) drops below the horizon.

I will probably just turn off the sun once its out of sight to fix my issue :slight_smile:

With video sync on, the fps can only be an integer harmonic of 60 fps. This means it may be 60/1 = 60 fps, 60/2 = 30 fps, 60/3 = 20 fps, 60/4 = 15 fps, 60/5 = 12 fps, 60/6 = 10 fps, and so on down. It may only be exactly one of these values at any given time (though often you may see the frame rate meter show a slightly different value, if your frame rate is changing, because the meter shows the average over several frames).

David