Dynamic LODs using the Handler API – Blender Handler API #1
When working on a scene with a lot of assets, it’s a good idea to use proportionately lower levels of subdivision as assets get further away from the camera. But how do you do this efficiently when you are dealing with a large number of assets, say 50-100 or more?
The Handler API allows you to write simple handlers that get triggered every time a certain condition is met. In this case, we can think of the camera’s movement as a trigger. Camera movements, among other things, are captured in the depsgraph_update_pre
and depsgraph_update_post
handlers which get triggered before and after any change to any objects in the scene respectively.
Using this, we can write a simple handler function that gets called every time the camera moves. Roughly, the handler would work something like:
For every object in the scene, calculate the distance between the object and the camera. Determine the appropriate subdivision level based on predefined distance thresholds. Assign the calculated subdivision level to the object.
Here’s a quick code snippet illustrating the handler 👇🏻
Instead of using the depsgraph handler, you can also use the frame_change_pre
or frame_change_post
handlers to recalculate subdivisions every time there is a frame change, which could be useful when doing animations.
Similarly, this can be extended to assign appropriate texture resolutions based on camera distance/screen space as well.
Subscribe to my newsletter
Join other Technical Artists, Developers and CG enthusiasts who receive my latest posts on Blender, Python, scripting, computer graphics and more directly to their inbox.