Skip to main content

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.

Constructors​

MemberWhat it is
FocusPuller()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

30 private and internal fields
MemberWhat it is
private static readonly int _CameraDepthTextureShader property ID for the global camera depth texture.
private static readonly int _DepthTextureShader property ID of the depth texture input on the compute shader.
private static readonly int _HeightShader property ID for the render texture height.
private static readonly int _WidthShader property ID for the render texture width.
private AnimationCurve autoFocusReadjustmentCurveEasing curve used when auto focus moves to a new distance.
private Coroutine autoReadjustmentThe running AutoReadjustment coroutine, if any.
private HandCamera baseCameraThe hand camera this focus puller is on.
private ComputeShader computeShaderCompute shader whose Calculator kernel samples depth at the focus points.
private DepthOfField depthOfFieldDepth of field effect found on the volume's profile.
private float focalLengthDepth range around the focus distance where a point counts as in focus.
private float focusCorrectionDistance added to the computed auto focus distance.
private FocusPuller.FocusPoint[] focusPointsPoints on the viewfinder sampled for auto focus, each with a weight.
private float inFocusAlphaAlpha of focus point markers that are in focus.
private Material inFocusMaterialMaterial for focus points that are in focus.
private Pickup interactingPickupPickup the player grabs to adjust focus by hand.
private int kernelIndex of the compute shader kernel.
private float maxDistanceFarthest focus distance allowed.
private float minDistanceNearest focus distance allowed.
private float outtaFocusAlphaAlpha of focus point markers that are out of focus.
private Material outtaFocusMaterialMaterial for focus points that are out of focus.
private Transform pivotDial transform turned during manual focus.
private float readjustmentDurationSeconds the auto focus takes to ease to a new distance.
private ComputeBuffer readPositionsBufferCompute buffer of the focus points' normalised screen positions.
private float[] resultsDepth readings per focus point from the last auto focus.
private ComputeBuffer resultsBufferCompute buffer the shader writes depth readings to.
private float sensitivityHow much focus distance changes per degree of hand twist in manual mode.
private int totalFocusWeightSum of the focus points' weights.
private Vector2 viewSizeSize of the viewfinder area, used to normalise focus point positions.
private PostProcessVolume volumePost process volume holding the depth of field effect.
private bool willCalculateSet when auto focus should run after the next camera render.

Properties​

MemberWhat it is
FocusPuller.FocusMode CurrentFocus { get; private set; }Current focus mode. Changed only by ToggleActive.

Events​

MemberWhat it is
event Action<FocusPuller.FocusMode> OnFocusModeChangedRaised with the new mode after ToggleActive.

Methods​

MemberWhat 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
MemberWhat 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: