lod search best solution

Hello, i want to create big map with many object.
but I do not want to load all map it is not efficient, I want to set up an algorithm of lod.

in the final version my map will be stored in a sqlite database, i think is best choice for have performance and portable solution.

for my example code, database is just python list with 4 object.

from math import sqrt
from panda3d.ode import OdeWorld, OdeSimpleSpace, OdeJointGroup
from panda3d.ode import OdeBody, OdeMass, OdeBoxGeom, OdePlaneGeom
from panda3d.core import BitMask32, CardMaker, Vec4, Quat
from random import randint, random
import threading
import time

from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):
 
    def __init__(self):
        ShowBase.__init__(self)
        
        self.lod_distance=100

        # Load the environment model.
        self.scene = self.loader.loadModel("models/environment")
        # Reparent the model to render.
        self.scene.reparentTo(self.render)
        # Apply scale and position transforms on the model.
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(80, 300, -20)

        self.database = [["robot.egg.pz",10,100,0],["robot.egg.pz",-10,50,0],["robot.egg.pz",0,-100,10],["robot.egg.pz",10,-130,10]]
        
        self.maps_load=[]
        self.map_obj_is_load=[]
        
        threads = threading.Thread(target=self.get_lod, args=())
        threads.start()
        
        threads2 = threading.Thread(target=self.render_lod, args=())
        threads2.start()

    def get_lod(self):
        while True:
            time.sleep(0.5)
            x=base.camera.getX()
            y=base.camera.getY()
            z=base.camera.getZ()
            
            
            for element in self.database:
                if (-self.lod_distance <= x-element[1] <=self.lod_distance and
                -self.lod_distance <= y-element[2] <=self.lod_distance and
                -self.lod_distance <= z-element[3] <=self.lod_distance):
                    if element not in self.maps_load:
                        print("create")
                        self.maps_load.append(element)
                        self.map_obj_is_load.append(0)
            
    def render_lod(self):
        while True:
            time.sleep(0.5)
            
            x=base.camera.getX()
            y=base.camera.getY()
            z=base.camera.getZ()
            
            for i in range(0,len(self.map_obj_is_load)):
            
                try:
                    if self.map_obj_is_load[i]==0:
                        m = loader.loadModel(self.maps_load[i][0])
                        m.reparentTo(self.render)
                        m.setPos(self.maps_load[i][1],self.maps_load[i][2],self.maps_load[i][3])
                        self.map_obj_is_load[i]=m
                        
                    else:
                        element=self.map_obj_is_load[i]
                        if (-self.lod_distance <= x-element.getX() <=self.lod_distance and
                        -self.lod_distance <= y-element.getY() <=self.lod_distance and
                        -self.lod_distance <= z-element.getZ() <=self.lod_distance):
                            None
                        else:
                            element.detachNode()
                            print("remove")
                            del self.map_obj_is_load[i]
                            del self.maps_load[i]
                except:
                    pass
 
if __name__ == '__main__':
    app = MyApp()
    app.run()
  1. I don’t think my code is performing well, i search best solution that while true loop. May be with multiprocessing solution ?
  2. i have question for panda3D, how can i load model (in threading ?) without lagging my game ?

Hi,

  1. Could you a tell a bit more about what your trying to do? From your code it seems like you want a lot of robots but from your question it seems it’s about rendering a large terrain? For these two examples I would use a different approach. It also seems that what you are trying to do is culling the objects, meaning they are not rendered when they are out of view. I think Panda automatically takes care of this if not explicitely disabled on nodes.

  2. I have actually never used it myself but have you tried loading models as described here under model loading?
    https://www.panda3d.org/manual/index.php/Threading#Model_loading

If you’re feeling particularly adventurous and are OK with using development builds of Panda with Python 3.6 and later, coroutines might be a good fit for helping you make your code asynchronous:

panda3d.org/blog/november-2 … nt-update/

already, thank you for your answers
rdb, I’m using the latest version under python35, panda1.10 devel

Stijn, I use the robot model as an example, in reality I would like to load a large 3D environment, like 1000km^2 map size.
I want to load very large map with grass and threes.

But it is impossible to fully load a 1000km^2 map. I would like to load only objects that are near me : the terrain, Grass, trees, rocks and… robots ! :smiley:

for terrain i think that i use GeoMipTerrain, it best solution for very large terrain.