Aloha everyone, I have been working with forum user @iPLEOMAX to create a system of run time asset management and I would like to share the code for discussion.
from bge import logic
scene = logic.getCurrentScene() #Gets current scene
cam = scene.active_camera #Gets active camera (in case the defined camera does not exist)
cont = logic.getCurrentController() #Get the current controller
own = cont.owner #Get the object running this controller
SpawnInstance = 'Instance' #Name for our dictionary entry where the new object will be stored
SpawnObject = 'SpawnObject' #Game property of the empty which holds the name of object prefab
SpawnCamera = 'SpawnCamera' #Game property of the empty which holds the name of camera whose frustum is to be used
if not SpawnInstance in own.attrDict: #Prepare the dictionary entry or else we'd get 'Not found' errors.
own.attrDict[SpawnInstance] = None
if SpawnObject in own: #Check if this empty has a 'SpawnObject' Property
if SpawnCamera in own: #Check if this empty has a 'SpawnCamera' Property
if own[SpawnCamera] != 'Active': #Ignore if it says 'Active'
cam = scene.objects[own[SpawnCamera]] #Use the camera as defined in the property
if cam.pointInsideFrustum(own.worldPosition): #Check if empty is within the camera view
if not own.attrDict[SpawnInstance]: #If the object wasn't created
own.attrDict[SpawnInstance] = scene.addObject(own[SpawnObject], own.name, 0) #Create it
own.attrDict[SpawnInstance].setParent(own, 0, 0) #Set parent
else: #Not in view
if own.attrDict[SpawnInstance]: #If created
own.attrDict[SpawnInstance].endObject() #Remove it
own.attrDict[SpawnInstance] = None #Nullify dictionary entry
This code is intended to use an empties world location and a camera frustum check to determine if an object should exist. This draft serves as the proof of concept and has evolved significantly over the last few weeks. We have explored several options and have had various successes. The most promising so far uses batch processing to group empties, see demo. We are looking for the most efficient method to accomplish this concept, including modifying the source. Can anyone clarify a couple points?;
-
Does the camera culling code consider empties? If it only considers mesh objects, what can you suggest? I assume it would be even faster if we made use of the native code to make spawn/despawn checks.
-
We are also looking into the creation of a KD tree to manage the spawn positions, does anyone have any experience or tips they can share to maximize performance?
The proof of concept (820KB) uses empties to simulate 22,716 objects and 15,901,200 tri’s. The batch spawner (60MB) uses empties to simulate 30,000 objects and 21,000,000 tri’s.