I am working on a scene where the user may toggle between night and day and I have several environment lights that I would like to toggle on and off when the user clicks a menu option. I am hoping to use light maps. Currently, I can only set a lightmap in the editor. When the day/night toggles outside the editor, the environment lights do not turn on/off by disabling the light.
What would be the best way to go about this? I have tried setting up a LightMapManager and have tried to CalculateLighting() but nothing is working yet.
Any help would be greatly appreciated.
Scott
Hi Robert,
Is the game targeting Windows or Xbox / WP7? If the game is targeting Windows only you can use the CalculateLighting() method to relight the scene when needed. However this method is not available on Xbox / WP7, though it may be possible to fake a relight by manipulating the scene object UniqueIds.
Let me know if this helps!
Follow me on Twitter – development and personal tweetsAwesome XNA Videos – Lighting, Rendering, and game videos
Thanks for writing back so quickly.
The program is designed to run using Windows only so it seems the CalculateLight() method could be appropriate.
I will do my best to give you a solid desciption of the problem. Essentially there is a parked vehicle. It will remain stationary as well as the environment around it. There is a toggle allowing the user to switch from night to day. When night, I am trying to have the headlights on using lightmaps. When day, the headlights should be off and the light map recalculated. I have set the build properties of the model that will have light cast upon it to "Generate light map coordinates" in the editor. I have then set up two spotlights as car headlights (also in the editor). I have checked both the "Receive updates" and "light mapped" boxes for both spotlights.
While in the editor, if I recalculate lights while night, I get the light map I am looking for. The same is true for day. However, I need to allow the user to be able to turn the headlights on and off while running the program. In the code, I have the spotlights disabled during day and enabled at night. After each enable/disable, I then call CalculateLighting().
When the program runs and the user toggles between night and day, the headlights are forever on. The toggle has no effect on manipulating the light map. If I change the lights to dynamic, the toggle works fine.
In the program, there will be a lot of lights, so the more that I can lightmap the better. Any help is greatly appreciated.
Take care,
Hi Scott,
That setup should work correctly - as a test here's what I did:
Then I added this code to Game.Update():
KeyboardState kb = Keyboard.GetState(); if (kb.IsKeyDown(Keys.R)) { ILight light; if (sceneInterface.LightManager.Find("Sun", out light)) light.Enabled = !light.Enabled; if (sceneInterface.LightManager.Find("Spot", out light)) light.Enabled = !light.Enabled; sceneInterface.LightMapManager.CalculateLighting(); }
When you press R while the game is running the lighting will toggle from the Sun to the spotlight.
Thank you so much. My code was almost identical to yours but I was instead passing the sceneInterface.LightMapManager to the class where the lights were being toggled. Somehow I was losing the association with the sceneInterface. As soon as I set the sceneInterface to a global variable rather than pass it, it worked instantly. Ugh!! I appreciate you taking the time to help me. I've been banging my head for awhile now. By looking at your code, I realized instantly what was probably going wrong.
Hello, I am also working on the the day night cycle , and am using CalculateLighting(), when i load the model from the editor i am able to recalculate the lights, and when i re start the game i get the calculated light map without having to calculating them again,
but in case if i load the model through code, i need light to be recalculated every time the the game starts, plus it generated new light maps file every time, so is there a way i can reuse lightmaps next time when i run the game without recalculating (bcoz recalculating the light maps again every occupies the more and more memory every time).
Hi dax,
Make sure to assign code generated objects a completely unique id in the UniqueId property, and that the object receives the same unique id each time the level / game is loaded. The UniqueId is used by the light mapping system to link objects with their light maps.