Auto Focus
Automatically adjust the Leia Camera's depth and focus based on scene content.
Sample Scene: Assets/LeiaLoft/Examples/Autofocus/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.

LeiaDepthFocus

The LeiaDepthFocus component samples the render texture of a depth camera to determine the best convergence distance and baseline scaling for the LeiaCamera.
Parameters:
    Depth Scale - a scalar value used to increase or decrease the depth output by the auto depth algorithm.
    Min Baseline - The minimum value that the LeiaCamera's baseline scaling can be set to as a result of auto focus.
    Max Baseline - The maximum value that the LeiaCamera's baseline scaling can be set to as a result of auto focus.
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 LeiaRaycastAutoFocus 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.
Parameters:
    Layers - Determines which physics layers raycasts should be performed against. You can use this to select which object layers should be included and excluded from the auto focus algorithm.
    Focus Speed - Speed at which the convergence distance changes from its current value
    Focus Offset - Convergence plane offset distance from the value determined by the auto focus algorithm.
    Max Focus Distance - The maximum value the auto focus algorithm is allowed to set the convergence distance to.
    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.
    Depth Scale - A scalar value used to increase or decrease the depth output by the auto depth algorithm.
    Min Baseline - The minimum value that the LeiaCamera's baseline scaling can be set to as a result of auto focus.
    Max Baseline - The maximum value that the LeiaCamera's baseline scaling can be set to as a result of auto focus.
    Show Debug Raycasts - Show debug rays in the scene editor
    Convergence Jitter Filter - prevent the convergence distance from jittering when objects in the scene are moving by setting a jitter filter value. The higher the value, the more the convergence target will have to change before the convergence distance is updated.
Use raycasting against mesh colliders in the scene to determine closest, average, and furthest points from the camera.
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.
Parameters:
    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.
    Samples - How many mesh vertices to take as sample points for determining baseline and convergence. Larger sample count will be more costly performance-wise.
    Min Baseline - The minimum value that the Leia Camera's baseline scaling can be set to as a result of auto focus.
    Max Baseline - The maximum value that the Leia Camera's baseline scaling can be set to as a result of auto focus.
    Convergence Jitter Filter - Prevent convergence distance from jittering by applying a minimum amount which the average vertex distance must change by before the target convergence distance is updated.
    Focus Speed - Speed at which the convergence distance changes from its current value.
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.
Last modified 1mo ago