LOD for big entity groups

Blitz3D Forums/Blitz3D Programming/LOD for big entity groups

lauri(Posted 2011) [#1]
I'm trying to create a forest in my game. I'm trying to use entities, which will change to sprites when distance to player is long enough.

	For pa.PLANT = Each PLANT
		ppx=EntityX(pa\tree)
		ppy=EntityY(pa\tree)
		ppz=EntityZ(pa\tree)
		If EntityDistance(pa\tree,player)<100
			FreeEntity pa\tree
			pa\tree=CopyEntity(tree1)
		Else
			FreeEntity pa\tree
			pa\tree=CopyEntity(tree1spr)
		EndIf
		PositionEntity pa\tree,ppx,ppy,ppz
	Next


What's wrong with this? Trees doesn't change to sprites and move to 0,0,0.


Yasha(Posted 2011) [#2]
Hmm... perhaps a parenting issue? You're creating the copy with no parent.

Anyway... creating copies and freeing old entities on every frame is going to kill your performance far harder than not bothering with LOD at all. A more efficient solution would be to create all of the trees and tree sprites when the scene is loaded, and use HideEntity/ShowEntity rather than copy and free. That way you also don't have to bother positioning anything since they're already in place (this is the standard way to do LOD for both static and animated objects), so the positioning issue will never come up.


Warner(Posted 2011) [#3]
Maybe instead of reading EntityX/Y/Z, store the x/y/z coords into the type itself.
Also, instead of freeing entities each frame, it could be better to only free them if they should change from hipoly to lowpoly or vice versa:
	For pa.PLANT = Each PLANT
                test = EntityDistance(pa\tree,player)<100               
		If test <> pa\oldtest  ;<-----only respond if state changed
                  pa\oldtest = test    ;<-----store previous state
                  if test
			FreeEntity pa\tree
			pa\tree=CopyEntity(tree1)
   		  Else
			FreeEntity pa\tree
			pa\tree=CopyEntity(tree1spr)
		  EndIf
                EndIf
		PositionEntity pa\tree,pa\ppx,pa\ppy,pa\ppz ;<---store x/y/z into type
	Next

Then again, using ShowEntity/HideEntity could be faster, I don't know. You could test this.
And, on some computers, sprites do not work anymore. When a sprite is used, RenderWorld gives a MAV. Use a manually created quad instead.


lauri(Posted 2011) [#4]

Now I'm using hide/showentity and manually created quads, and game runs smoothly. But loading takes still over 30s with ~4000 trees, what is far too much because I'm adding there lots of other things like rocks, houses, cars, zombies, and more trees. How I can decrease loading times but still keep entities?


Warner(Posted 2011) [#5]
You are using CopyEntity, right? Perhaps it is not so much the loading, but placing the trees on the terrain that causes the long delay.


lauri(Posted 2011) [#6]
No, it is CopyEntity. I tried to replace CopyEntity with MakeMesh and it tooks only few seconds.