I’ve written some code to calculate a PVS based for objects in a scene (PC not IPhone). It works well but is very slow to calculate. That’s OK though because only static objects are in the PVS so it only needs calculating after a change in the scene and then stored.
My first problem is that I don’t know the best way to store the data I’ve calculated. I could serialize it and store it out manually but is there a better way in Unity to do this so it gets saved in the scene data?
Also I’d like to set things up so the PVS automatically gets recalculated when new objects are added to the scene or position of existing objects are changed. I reckon I can do this by iterating through all the objects in the scene and calculating a checksum based on their position. When the game is run I should just need to re-calculate the checksum, compare it to the old checksum and if it’s different rebuild the PVS. Again I don’t know how to create a value which is preserved when the game is quit and Unity closed down.
Yes PVS is Potential Visibility Sets. As the Wiki page says it’s a system by which all the potentially visible polygons in a region, entire models in my case, are calculated off line then at run time the code finds the list of models which are associated with that region, renders them and ignores all the other models in the scene. It’s a very useful optimization when you have a large open environment with a hilly terrain and a long view distance.
After some messing about my algorithm seems to be working fine so that’s not my problem. My problem is that I want to pre-compute the data and save it with the rest of the scene data so that it doesn’t have to be computed each time the game is run. And although I do know how to save and load data it seems a very clumsy way of doing it. To summarise what I want to do is:
Run Game
Check whether scene has changed since last time PVS was built (by comparing the old checksum with the new)
If not play game with existing PVS
If yes then rebuild PVS and save it
I’ve got a few more tweaks to make on the code yet but once I get it working I’m happy to share it. There are a few places where I’m not sure I’m doing things optimally anyway and it would be very good to get some feedback.