1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- namespace SicknessReduction.Visual.DoF
- {
- public class DynamicDoF : MonoBehaviour
- {
- public Camera playerCamera;
- public VolumeProfile postProcessProfile;
-
- private Transform cameraTransform;
- private DepthOfField doF;
- private bool doFAvailable;
- private void Start()
- {
- cameraTransform = playerCamera.transform;
- doF = (DepthOfField) postProcessProfile.components.FirstOrDefault(c => c is DepthOfField);
- doFAvailable = doF != null;
- if (doFAvailable)
- {
- // ReSharper disable once PossibleNullReferenceException
- doF.mode.value = DepthOfFieldMode.Bokeh;
- }
- else
- {
- Debug.LogWarning("No DepthOfField found in PostProcessing Profile!");
- }
-
- }
- private void Update()
- {
- if(!doFAvailable) return;
- var focalDistance = CastRay();
- if (focalDistance < 0)
- {
- doF.active = false;
- return;
- }
- doF.active = true;
- doF.focusDistance.value = focalDistance;
- doF.focalLength.value = focalDistance * 10;
- }
- private float CastRay()
- {
- var position = cameraTransform.position;
- var forward = cameraTransform.forward;
- var start = position + forward * playerCamera.nearClipPlane;
- var end = position + forward * playerCamera.farClipPlane;
- if (Physics.Linecast(start, end, out var hit, Physics.DefaultRaycastLayers))
- {
- Debug.DrawLine(start, end, Color.green);
- Debug.Log("DoF - Hit, Distance = "+hit.distance);
- return hit.distance;
- }
- Debug.DrawRay(position, position + forward * playerCamera.farClipPlane, Color.red);
- Debug.Log("DoF - No hit");
- return -1f;
- }
- }
- }
|