Links

Auto Focus

Automatically adjust the Leia Camera's depth and focus based on scene content.
Sample Scene: Assets/LeiaLoft/Modules/Autofocus/Examples/AutoFocusMethods
Getting the right convergence (focus distance) and baseline scaling (depth) for the LeiaCamera component can be tricky, especially for scenes where the content is constantly changing. To assist developers, we have created script modules which can be attached to the same game object as a LeiaCamera component and automatically set the baseline and convergence based on the visible content in the scene.
There are currently three different possible approaches, each with it's own pros and cons: a depth map based approach, a raycasting approach, and a mesh-vertices based approach.
All three auto focus scripts have these common parameters:
  • Baseline Range - The minimum and maximum values that the LeiaCamera's baseline scaling can be set to as a result of auto focus.
  • Convergence Range - The minimum and maximum values that the LeiaCamera's convergence distance can be set to as a result of auto focus.
  • Depth Scale - a scalar value used to increase or decrease the depth output by the auto depth algorithm.
  • Convergence Change Threshold - prevent the convergence distance from jittering when objects in the scene are moving by setting a threshold the convergence target will have to change by before the actual convergence distance is updated. Note the convergence distance will still smoothly adjust to the new target value, not jump instantly.
  • Baseline Change Threshold - prevent the baseline scaling from jittering by setting a threshold the baseline target will have to change by before the actual baseline scaling is updated. Note the baseline scaling will still smoothly adjust to the new target value, not jump instantly.
  • Convergence Focus Speed - Speed at which the convergence distance moves from its current value to the target value.
  • Baseline Focus Speed - Speed at which the baseline scaling moves from its current value to the target value.
  • Focus Offset - Offset the LeiaCamera convergence distance from its computed optimal value by this amount. This can be useful to bring objects more to the foreground to make them pop out more, or push them more into the background so that (for example) UI can be drawn over them.

LeiaDepthFocus

The LeiaDepthFocus component samples the render texture of a depth camera to determine the best convergence distance and baseline scaling for the LeiaCamera.
Pros:
  • Fast, accurate, per-frame baseline and convergence calculation
  • Easy to Use
Cons:
  • Won't work in scenes where scene objects use shaders that don't write to the depth buffer.
  • It won't work on platforms that don't support compute shaders. Luckily Lumepad doesn't have this issue.
Known Issues: In Unity 2017 and 2018 editor versions, LeiaDepthFocus may not work in the editor when the build target is set to Android, but should still work in Android builds.
If auto focus needs to be tested in editor, the build target can be set to Windows to work around this issue, or the project could be upgraded to use a 2019 Unity version or newer.

LeiaRaycastFocus

The LeiaRaycastFocus component uses raycasting to determine the distance to visible objects in the scene, and then uses that information to determine a suitable convergence distance and baseline scaling for the LeiaCamera.
Unique Parameters:
  • Max Raycast Distance - The maximum distance the auto focus algorithm is allowed to raycast to collect sample points. Note that if this is smaller than the convergence range max value, then it will take precedence over the convergence range max value when determining the max convergence distance.
  • Samples - How many raycast samples to take to use in calculations determining the convergence and focus distance. More samples will yield more accurate results but be more expensive performance-wise.
  • Show Debug Raycasts - Show debug rays in the scene editor.
Pros: Fast, Accurate, Easy to Use
Cons: Requires colliders to be setup properly in the scene for auto focus to work properly. Also physics simulation can be more expensive on performance, depending on how many samples you take.

LeiaTargetFocus

The LeiaTargetFocus component takes a target GameObject as a parameter and will keep that object in focus based on it's mesh vertices.
Unique Parameters:
  • Samples - How many mesh vertices to take as sample points for determining baseline and convergence. Larger sample count will be more costly performance-wise.
  • Target - The target game object to focus on. Child objects of the target game object will also be taken into account in the auto focus algorithm.
Pros:
  • Makes it easy to always keep a desired target game object in focus.
  • Doesn't require shaders which write to the depth buffer
Cons:
  • Tradeoff between accuracy and performance depending on how many samples are taken. More samples yields better accuracy but reduced performance. Less samples yields less accuracy but better performance. This tradeoff could possibly be reduced in the future by using compute shaders to do the calculations on the GPU instead of the CPU, similar to LeiaDepthFocus.