FocusPuller
A class in Root.Township, Alta.ScreenshotCamera.
Depth of field control on the hand camera with three modes (perfect, auto and manual). Auto focus reads depth at weighted focus points with a compute shader; manual focus turns a dial as you twist your hand. Runs for the camera's owner only.
- Listed in Customization and screenshots
- Inherits or implements
MonoBehaviour - Nested types
FocusMode,FocusPoint
Constructors
| Member | What it is |
|---|---|
FocusPuller() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
30 private and internal fields
| Member | What it is |
|---|---|
private static readonly int _CameraDepthTexture | Shader property ID for the global camera depth texture. |
private static readonly int _DepthTexture | Shader property ID of the depth texture input on the compute shader. |
private static readonly int _Height | Shader property ID for the render texture height. |
private static readonly int _Width | Shader property ID for the render texture width. |
private AnimationCurve autoFocusReadjustmentCurve | Easing curve used when auto focus moves to a new distance. |
private Coroutine autoReadjustment | The running AutoReadjustment coroutine, if any. |
private HandCamera baseCamera | The hand camera this focus puller is on. |
private ComputeShader computeShader | Compute shader whose Calculator kernel samples depth at the focus points. |
private DepthOfField depthOfField | Depth of field effect found on the volume's profile. |
private float focalLength | Depth range around the focus distance where a point counts as in focus. |
private float focusCorrection | Distance added to the computed auto focus distance. |
private FocusPuller.FocusPoint[] focusPoints | Points on the viewfinder sampled for auto focus, each with a weight. |
private float inFocusAlpha | Alpha of focus point markers that are in focus. |
private Material inFocusMaterial | Material for focus points that are in focus. |
private Pickup interactingPickup | Pickup the player grabs to adjust focus by hand. |
private int kernel | Index of the compute shader kernel. |
private float maxDistance | Farthest focus distance allowed. |
private float minDistance | Nearest focus distance allowed. |
private float outtaFocusAlpha | Alpha of focus point markers that are out of focus. |
private Material outtaFocusMaterial | Material for focus points that are out of focus. |
private Transform pivot | Dial transform turned during manual focus. |
private float readjustmentDuration | Seconds the auto focus takes to ease to a new distance. |
private ComputeBuffer readPositionsBuffer | Compute buffer of the focus points' normalised screen positions. |
private float[] results | Depth readings per focus point from the last auto focus. |
private ComputeBuffer resultsBuffer | Compute buffer the shader writes depth readings to. |
private float sensitivity | How much focus distance changes per degree of hand twist in manual mode. |
private int totalFocusWeight | Sum of the focus points' weights. |
private Vector2 viewSize | Size of the viewfinder area, used to normalise focus point positions. |
private PostProcessVolume volume | Post process volume holding the depth of field effect. |
private bool willCalculate | Set when auto focus should run after the next camera render. |
Properties
| Member | What it is |
|---|---|
FocusPuller.FocusMode CurrentFocus { get; private set; } | Current focus mode. Changed only by ToggleActive. |
Events
| Member | What it is |
|---|---|
event Action<FocusPuller.FocusMode> OnFocusModeChanged | Raised with the new mode after ToggleActive. |
Methods
| Member | What it is |
|---|---|
void ToggleActive() | Cycles perfect, auto and manual focus. Turns depth of field and forced high res on for auto and manual, and shows points in auto. |
12 private and internal methods
| Member | What it is |
|---|---|
private IEnumerator AutoReadjustment(float targetDistance) (iterator) | Coroutine that eases the depth of field focus distance to a target over readjustmentDuration. |
private void Awake() | Hooks the pickup's interaction filter to the camera and, off headless, waits for ownership and hides focus points. |
private void CalculateAutoFocus() | Reads depth at each focus point, drops outliers, averages the rest and starts easing focus to that distance. Colours points in or out of focus. |
private void CheckAutoFocus() | On photo start in auto mode, flags auto focus to run after the next render. |
private void CheckOwner(bool isOwned) | When owned, hooks render and photo events, makes the in and out of focus materials, finds the depth of field effect and sets up the compute buffers. |
private IEnumerator DistanceCheck(Interactor interactor) (iterator) | Coroutine for manual mode: while the pickup is held, twisting the hand turns the pivot and changes the focus distance. |
private void FinishedRender(HandCamera renderingCamera, HandCameraEvent cameraEvent) | After the camera renders, runs a pending auto focus, and after a photo in auto mode hides the focus points until the screen is free. |
private void OnDestroy() | For the local owner, unhooks render events, cleans the materials and frees the compute buffers. |
private void OnDrawGizmosSelected() | Editor gizmo for the view size and forward direction. |
private void PrepareToRender(HandCamera renderingCamera, HandCameraEvent cameraEvent) | Pre-render hook. Does nothing useful in this build. |
private IEnumerator ReturnPoints() (iterator) | Coroutine that shows the auto focus points again once the camera screen is free. |
private void StartCheck(Interactable interactable, Interactor interactor) | When the focus pickup is grabbed in manual mode, starts DistanceCheck. |
Used in
Members of other types that take, return or hold a FocusPuller: