|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(Camera))]
|
|
|
|
|
public class MotionBlur : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("Blur Settings")]
|
|
|
|
|
[Range(1, 5)] public int FastFilter = 4;
|
|
|
|
|
public float blurSmoothing = 2f;
|
|
|
|
|
|
|
|
|
|
[Header("Blur Strength Ranges")]
|
|
|
|
|
public float minBlurNormal = 0.05f;
|
|
|
|
|
public float maxBlurNormal = 0.15f;
|
|
|
|
|
|
|
|
|
|
public float minBlurNOS = 0.1f;
|
|
|
|
|
public float maxBlurNOS = 0.3f;
|
|
|
|
|
|
|
|
|
|
[Header("References")]
|
|
|
|
|
public Material motionBlurMaterial;
|
|
|
|
|
public Texture2D mask;
|
|
|
|
|
public VehicleController vehicle;
|
|
|
|
|
|
|
|
|
|
private Camera cam;
|
|
|
|
|
|
|
|
|
|
private Matrix4x4 previousViewProjection;
|
|
|
|
|
private Matrix4x4 viewProj;
|
|
|
|
|
private Matrix4x4 currentToPreviousViewProjectionMatrix;
|
|
|
|
|
|
|
|
|
|
private float smoothedSpeed = 0f;
|
|
|
|
|
|
|
|
|
|
private static readonly int currentPrevString = Shader.PropertyToID("_CurrentToPreviousViewProjectionMatrix");
|
|
|
|
|
private static readonly int distanceString = Shader.PropertyToID("_Distance");
|
|
|
|
|
private static readonly int blurStrengthString = Shader.PropertyToID("_BlurStrength");
|
|
|
|
|
private static readonly int blurTexString = Shader.PropertyToID("_BlurTex");
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
cam = GetComponent<Camera>();
|
|
|
|
|
previousViewProjection = cam.projectionMatrix * cam.worldToCameraMatrix;
|
|
|
|
|
Shader.SetGlobalTexture("_MaskTex", mask != null ? mask : Texture2D.whiteTexture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
if (vehicle != null)
|
|
|
|
|
{
|
|
|
|
|
float targetSpeed = Mathf.Clamp01(vehicle.CurrentSpeed / vehicle.MaximumSpeed);
|
|
|
|
|
smoothedSpeed = Mathf.Lerp(smoothedSpeed, targetSpeed, Time.deltaTime * blurSmoothing);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
smoothedSpeed = Mathf.Lerp(smoothedSpeed, 0f, Time.deltaTime * blurSmoothing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRenderImage(RenderTexture src, RenderTexture dest)
|
|
|
|
|
{
|
|
|
|
|
viewProj = cam.projectionMatrix * cam.worldToCameraMatrix;
|
|
|
|
|
currentToPreviousViewProjectionMatrix = previousViewProjection * viewProj.inverse;
|
|
|
|
|
|
|
|
|
|
motionBlurMaterial.SetMatrix(currentPrevString, currentToPreviousViewProjectionMatrix);
|
|
|
|
|
motionBlurMaterial.SetFloat(distanceString, 1f); // Required
|
|
|
|
|
|
|
|
|
|
// Choose blur range based on NOS state
|
|
|
|
|
float min = vehicle != null && vehicle.nosActive ? minBlurNOS : minBlurNormal;
|
|
|
|
|
float max = vehicle != null && vehicle.nosActive ? maxBlurNOS : maxBlurNormal;
|
|
|
|
|
|
|
|
|
|
float blurStrength = Mathf.Lerp(min, max, smoothedSpeed);
|
|
|
|
|
motionBlurMaterial.SetFloat(blurStrengthString, blurStrength);
|
|
|
|
|
|
|
|
|
|
previousViewProjection = viewProj;
|
|
|
|
|
|
|
|
|
|
RenderTexture rt = RenderTexture.GetTemporary(
|
|
|
|
|
Screen.width / FastFilter,
|
|
|
|
|
Screen.height / FastFilter,
|
|
|
|
|
0,
|
|
|
|
|
src.format
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Graphics.Blit(src, rt, motionBlurMaterial, 0);
|
|
|
|
|
motionBlurMaterial.SetTexture(blurTexString, rt);
|
|
|
|
|
RenderTexture.ReleaseTemporary(rt);
|
|
|
|
|
|
|
|
|
|
Graphics.Blit(src, dest, motionBlurMaterial, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|