Bit of a request here, but i’ve been google’ing like crazy and found nothing useful, im looking for a LOD script that will allow me to use as many levels as I see fit so 2 or more.
something like lod_1[insertname] lod_2[insertname] lod_3[insertname] so on and so forth.
also need to be able to link/append.
As for the first problem, my LOD script in my BGHelper module can help. Maybe it’s a little overkill if you just need that one script, but it’s pretty simple to set-up. If you want, I suppose I could block the LOD part out into a single script to give here.
For the second problem, do you mean link / append dynamically, through the LibLoad() function (appending or linking in-game from other blend files)?
yeah If your willing to write up/block out part of a script that would be awesome or a link to and already existing one, whatever you willing to do.
as for the link/append I just want to make sure im able to grab the main object from the external .blend I know nothing about scripting I’m just looking to do some environment test using blender.
This should work:
from bge import logic
def LOD(detaillist, object = None, camera = None, replaceonce = 1):
"""
A more complex and flexible LOD system.
detaillist = list of detail meshes and their distances, with the first part of each entry
being the distance value, and the second being the mesh name.
For example, [ [0, 'lowpoly'], [10, 'medpoly'], [20, 'highpoly'] ]
will replace the mesh like this:
If the distance to the camera is greater than 20, then it will use the 'highpoly' mesh
Else, if the distance to the camera is greater than 10, then it will use the 'medpoly' mesh
Else, if the distance to the camera is greater than 0, then it will use the 'lowpoly' mesh
The distance can't go below 0, so use that for the lowest poly mesh for your game object
object = object to perform LOD calculation from (and mesh replacement on)
camera = the object to calculate distance to; doesn't have to be a camera at all - it can even be a point in space.
replaceonce = Replacing the object mesh takes Rasterizer time - doing this every frame is bad on it. Setting this to 1
ensures that replacing the mesh only is done when necessary (if the object's first mesh is not equal to the mesh to
replace).
There is a known bug that there can be a noticeable stutter when replacing particularly high-poly meshes. This is caused
by the high poly mesh not being stored in RAM if it is in a hidden layer. The solution is to place the mesh somewhere in an
active scene (the solution was only tested with the high-poly mesh in the current scene, though other scenes (background
or overlay) might work, as well).
"""
if object == None:
obj = logic.getCurrentController().owner
else:
obj = object
if camera == None:
cam = logic.getCurrentScene().active_camera
else:
cam = camera
mesh = [-1, None]
for entry in detaillist: # Loop through the detail list and find the entry that's closest to the camera
dist = obj.getDistanceTo(cam)
if dist > entry[0]: # The distance to the camera is greater than the minimum distance for the detail mesh, so consider it
if mesh[0] == -1: # There is no previous mesh to use, so go with this one
mesh = entry
else: # There is a previous mesh to use, so compare the two (the current one in entry, and the previous one in mesh)
if dist - entry[0] < dist - mesh[0]: # Compare the current mesh entry's distance value
mesh = entry # with the previous mesh's minimum distance to find the one that's closest.
# e.g. High poly mesh at 0, low poly at 50, camera at 100
# 100 - 0 = 100, 100 - 50 = 50, low poly is closest, so go with that mesh
if replaceonce:
if str(obj.meshes[0]) != mesh[1]: # Only replace the mesh if the object's current mesh isn't equal to the one to use (if it's equal, then the mesh was replaced earlier)
obj.replaceMesh(mesh[1])
else:
obj.replaceMesh(mesh[1])
LOD([ [0, 'LowPolyMeshName'], [10, 'MedPolyMeshName'], ...])
Run that script connected to an Always sensor with 0 pulse setting in the object that you want to perform LOD on. If you want to do it on a group of objects, there’s a better way.
thanks man I appreciate the work, but im having a little trouble understanding this I made an always sensor with a true pluse at 0 connected that to the script right? changed the “LOD([ [0, ‘LowPolyMeshName’], [10, ‘MedPolyMeshName’], …])” at the bottom of the code if fit my objects names, is there anything else.
Im not sure if it’s possible but i’ll ask anyway could you design the code so that the main script it on an Empty in the scene, and the any default mesh has ‘string’ property called “LODsys” or something so it just looks for object that has the LODsys property with a string being from low to high like this “10[‘Mesh_Low’], [‘Mesh_Med’], [‘Mesh_High’]” the “10” being the Blender units(or something) form low to high and have them automatically spaced.
sorry if that sounds stupid, my knowledge of coding in general is very limited as I am just a lowly artist lol.
thank you for you time so far btw
Change the “LowPolyMeshName”, “MedPolyMeshName”, etc. to be the names of the meshes to switch your object’s mesh out with, not the objects themselves. The meshes are in the Edit tab of the Properties window section.
As for the empty, sure, I could do that. It’ll be awhile, but I should be able to get it done.
EDIT: It didn’t take that long… Anywho, here’s the blend. You can see how it works, but just appending the LODHandler object and the LOD script (which I so conveniently named, “Text” facepalm) to your scene should work. LOD objects will need two properties - a ‘loddist’ property, and a ‘lodmesh’ property. The distance property is the distance steps at which the LOD takes effect, and the mesh is a string in which you enter the names of the meshes IN QUOTES, like this:
‘HighDetailMesh’, ‘MediumDetailMesh’, ‘LowDetailMesh’
It goes from close to far.
QuickieLOD.blend (409 KB)
Awesome man, I made a test scene from scratch with your code added to an empty named the script “LODSystem” this is really cool, and very simple to set up you should put this on your site and maybe make a thread on the resources page.
Thank you for all you work and help this is perfect :yes: